This is my coding:
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#define trigPin D7 // Pin for sensor trigger
#define echoPin D8 // Pin for sensor echo
#define motionPin D3 // Pin for motion sensor
#define RECEIVER_IP IPAddress(192, 168, 4, 1) // IP address of the receiver
#define PORT 1234 // UDP port number
#define SOUND_VELOCITY 0.034
#define CM_TO_INCH 0.393701
const char* ssid = "mukhriz"; // Enter your WiFi SSID
const char* password = "b4e7f435c0"; // Enter your WiFi password
const IPAddress serverIP(192, 168, 4, 1); // IP address of the access point
const int serverPort = 1234;
WiFiUDP udp;
// defines variables
long duration;
int distance;
float distanceCm;
float motionCm;
int measureDistance;
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motionPin, INPUT); // declare sensor as input
Serial.begin(115200);
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() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
distanceCm = duration * SOUND_VELOCITY/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance (cm): ");
Serial.println(distanceCm);
delay(1000);
// Check if distance is above 10cm and below 200cm
if (distanceCm > 10 && distanceCm < 200) {
sendTrigger();
delay(1000); // Delay to prevent multiple triggers
} else {
Serial.println("Distance detected by HC-SR04 is not within the desired range, no trigger sent.");
}
int motionState = digitalRead(motionPin);
// Prints the distance on the Serial Monitor
Serial.print("Motion (cm): ");
Serial.println(motionCm);
delay(1000);
val = digitalRead(motionPin); // read input value
if (val == motionCm > 10 && motionCm < 200) { // check if the input is HIGH
sendTrigger();
delay(1000);
} else
{Serial.println("Motion Distance detected by HC-SR501 is not within the desired range, no trigger sent.");
}
}
void connectToWiFi() {
Serial.println("Connecting to WiFi");
WiFi.begin(ssid, 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 sendTrigger() {
Serial.println("Sending trigger to access point");
// Send trigger data packet to the access point
udp.beginPacket(serverIP, serverPort);
udp.write("trigger");
udp.endPacket();
}
The other coding works fine but only for the PIR motion sensor coding can't read anything