Using PIR and ESP32 for HTTP GET

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

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' 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() {
  pinMode(pir, INPUT);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
  Serial.print("Connected to WiFi network with IP Address: ");
  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";
        int httpResponseCode = http.GET();

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

(Sorry for short 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 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.