Using PIR and ESP32 for HTTP GET

Hello! I'm Testing HTTP GET using PIR sensor and ESP32 on BreadBoard.

Problem
I have successfully monitored HTTP GET, but the problem started when i used PIR sensor for HTTP GET. I wanted to GET when movement is detected, and serial print 'movement undetected' when there is no movement. Here, I also want to HTTP GET once only for the first time that movement detected.

Another thing is, when PIR sensor is HIGH(on/1) i wonder why it continues HTTP GET about 6~7seconds delay...I want only once!!
(I made a 'boolean requested=false' at the loop code and 'requested = true' at the bottom, so when it starts to loop, when motion is detected it will HTTP GET sucessfully and at the end of the loop 'requested' will change into true so it will not HTTP GET again...but it didn't...help please)

Read my code please, and give some ideas please...i'm so curious....!!

#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = "wifiname";
const char* password = "password";
String serverName = "correct servername written";

const int pir = 27;

unsigned long lastTime = 0;
unsigned long timerDelay = 5000;



void setup() {
  Serial.begin(115200);
  pinMode(pir, INPUT);
  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
  Serial.println("it will take 5 seconds...");
}



void loop() {
  int pir_stat = digitalRead(pir);
  static boolean requested = false;

  if (pir_stat == HIGH && !requested ) {     
    if ((millis() - lastTime) > timerDelay) {
      if (WiFi.status() == WL_CONNECTED) {

        HTTPClient http;

        String serverPath = serverName + "?temperature=24.37";
        http.begin(serverPath.c_str());
        int httpResponseCode = http.GET();

        if (httpResponseCode > 0) {
          Serial.println("motion detected");
          Serial.print("HTTP Response code: ");
          Serial.println(httpResponseCode);
          String payload = http.getString();
          Serial.println(payload);
        }
        else {
          Serial.print("Error code: ");
          Serial.println(httpResponseCode);
        }
        http.end();
      }
      else {
        Serial.println("WiFi Disconnected");
      }
      lastTime = millis();
    }
    requested = true;
  }
  else {
    Serial.println("motion undetected_NO PIR API REQUESTED");
  }
  delay(1000);
}

(Sorry for short english.....my second time writing this article...)

Your going to have to do a bit of research but please go back and edit your post to put the code in code tags.

1 Like

I did..!!! can you help me please...not just research... i cant read english that much well...so i want some step by step explanation...please :disappointed_relieved:

You only want to detect one time movement and then send a signal and then do not detect any more movement after detecting movement for the first time, correct?

nono detect for the first time, and wait until for the next detect not just only once...

How long to wait before detecting the next movement?

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