Need help in coding a soft AP IP and how to transfer esp8266 circuit connection on breadboard to the motor shield

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

if you have problems with some logic statement write a short program to test it
e.g. try reading a value from the keyboard and testiung it

void setup() {
  Serial.begin(115200);
}

void loop() {
  if (Serial.available()) {
    int distance = Serial.parseInt();
    static int oldDistance = -999;
    if (distance != oldDistance) {  // if no change ignore
      oldDistance = distance;
      if (distance >= 10 && distance <= 200)  // check range
        Serial.println("distance in range");
      else Serial.println("distance not in range");
    }
    while (Serial.available()) Serial.read();  // flush input buffers
  }
}

1 Like

oh do i just need to replace this with the code at the Client ESP? Thank you! I will try it now

Please help me
Both sensor operating voltage is 5V, the trigger pin, echo pin and ground pin of the hc-sr04 ultrasonic sensor and the output pin, ground pin of the hc-sr501 can connect to the ESP8266, so how can I connect the remaining 2 VCC pin of the two sensor to one nodemcu esp8266?

The 3.3V from an ESP pin may be enough to trigger the ultrasonic sensor. Only way to know for sure is to test it.

For the 5V output signals from the sensors connecting to the 3.3V inputs on the ESP, reduce them down to 3.3V with voltage dividers, I suggest 20K + 10K.

2 Likes

Ooh do you know how to connect the two sensors VCC pin to the ESP8266 pin?

Yes. Don't you? Everything you need is right there in front of you...

but I can't connect the Vcc pin of each sensor which is to direct to Vin pin of the ESP8266 as it makes the esp8266 can't receive the coding

That should not happen.

Post a schematic diagram showing how everything is connected when you experience that problem. Some bright, clear photos also.


something like this?

@gmikuo ,

Your two or more topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

1 Like

I can't see a problem in your photo. There should be no problem uploading code like this.

Which sensor is preventing upload? Or is the problem only when both are connected like this?

Oh nvm now it works kinda? previously I may have accidently touch some of the wire to each other making it can't receive the uploaded code

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.