I need help with how to transfer the connection on the breadboard for the esp8266 with Hc-sr04 ultrasonic sensor and hc-sr501 pir motion sensor to the motor shield and to review and help modify (if possible)
And then, for the Client esp8266 coding,
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#define TRIGGER_PIN D5 // Pin for sensor trigger
#define ECHO_PIN D6 // Pin for sensor echo
#define MOTION_PIN D3 // Pin for motion sensor
#define MAX_DISTANCE 200 // Maximum range of ultrasonic sensor in centimeters
#define MAX_MOTION_DISTANCE 200 // Maximum range of PIR motion sensor in centimeters
#define MIN_DISTANCE 10 // Minimum distance for sending data packet
#define RECEIVER_IP IPAddress(192, 168, 4, 1) // IP address of the receiver
#define PORT 1234 // UDP port number
WiFiUDP udp;
int prevDistance = 10; // Variable to store the previous distance reading
void setup() {
Serial.begin(115200);
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(MOTION_PIN, INPUT);
Serial.println();
Serial.println("Connecting to Access Point...");
WiFi.begin("mukhriz", "b4e7f435c0"); // Replace with your Access Point SSID and password
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
// Measure distance with the ultrasonic sensor
int distance = measureDistance();
// Check if motion is detected
bool motionDetected = digitalRead(MOTION_PIN) == HIGH;
// Check if either sensor's maximum range is disturbed
if ((distance < MAX_DISTANCE && distance != prevDistance) || (motionDetected && distance > MIN_DISTANCE && distance < MAX_MOTION_DISTANCE)) {
// Send trigger to the receiver
udp.beginPacket(RECEIVER_IP, PORT);
udp.write("trigger");
udp.endPacket();
Serial.println("Trigger sent");
delay(1000); // Wait to prevent multiple triggers
} else {
Serial.println("No change in sensor readings or distance below minimum threshold");
}
prevDistance = distance; // Update the previous distance reading
}
// Function to measure distance with the ultrasonic sensor
int measureDistance() {
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
return pulseIn(ECHO_PIN, HIGH) / 58; // Distance in centimeters
}
And for the Access point esp8266 coding:
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#define LED_PIN D1 // Pin for LED
#define PORT 1234 // UDP port number
WiFiUDP udp;
bool ledState = false;
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
Serial.println();
Serial.println("Starting Access Point...");
WiFi.mode(WIFI_AP);
WiFi.softAP("mukhriz", "b4e7f435c0");
Serial.println("Access Point started");
Serial.println("IP address: ");
Serial.println(WiFi.softAPIP());
Serial.println("Starting UDP");
udp.begin(PORT);
Serial.print("Local port: ");
Serial.println(udp.localPort());
}
void loop() {
int packetSize = udp.parsePacket();
if (packetSize) {
Serial.print("Received packet of size ");
Serial.println(packetSize);
char incomingPacket[255];
udp.read(incomingPacket, 255);
Serial.println("Contents:");
Serial.println(incomingPacket);
// Check if received data indicates a trigger
if (strcmp(incomingPacket, "trigger") == 0) {
// Toggle LED state
ledState = !ledState;
digitalWrite(LED_PIN, ledState ? HIGH : LOW);
Serial.println(ledState ? "LED turned ON" : "LED turned OFF");
}
}
}
So for this project I'm trying to make when the client esp8266 detect the change is distance of the sensors within below 200cm and above 10cm, it will send a data packet to the access point esp8266 and after the access point get that data packet, it will trigger the led at the access point to turn ON.
I need help on how to code the maximum range of distance that both sensors can detect to only 200cm and minimum of 10cm and if the value detected is above 200cm and below 10cm, the data packet will not send the trigger to the other esp8266 which acts as an access point . Use code tags to format code for the forum