How to make HTTP GET by PIR only once

The Problem

I'm Using ESP32 and PIR sensor for HTTP GET. I can successfully HTTP GET but i want to GET only once for the first time. So i int a 'requested = false;' at the loop and 'requested = true;' at the end of the loop's if. But this code still bring not the first time. How can i do?
(sorry for short english;;; it's my best)

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

const char* ssid = "ZEROWEB";
const char* password = "q1234!@#$";

const int pir = 27;

String serverName = "https://api.app.nipa.zeroweb.cloud/pir/";

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

void setup() {
Serial.begin(115200);
pinMode(pir, INPUT);
// int requested = false;
// λ‚˜μ€‘μ— 계속 μΈμ‹λ˜λŠ” μƒνƒœμ—μ„œ ν•œλ²ˆλ” GETν•˜λŠ” κ±Έ λ°©μ§€ν•˜κΈ° μœ„ν•¨
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);+
int 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(1200);
}

my first time writing.....sry;;;;;

Please follow the advice given in the link below when posting code , use code tags and post the code here to make it easier to read and copy for examination

void loop()
{
  int pir_stat = digitalRead(pir);
  int requested = false;
  if (pir_stat == HIGH && !requested )
  {
    if ((millis() - lastTime) > timerDelay)
    {
      if (WiFi.status() == WL_CONNECTED)
      {

requested is set to false at the start of loop() (by the way it should be a boolean, not an int), then you have code that depends on it being true in order to run. Is that what you meant to do ?

1 Like


um i think so, and i tried with boolean but it still continues to http get...

i want this code to 1)connected to wifi β†’ 2) motion detects(http get only once for the first time on serial) β†’ 3) motion undetected β†’ 4) motion detects(http get only once)

at the image and the code, this thing continues to http get about 7seconds delay rather it is the first time or not

Make the test of the value of requested more explicit using == and a value
Immediately after you set it to false what do you expect its value to be ?

Even if that part of the code worked then why are you setting requested to false each time through loop() if you only want the code to run once ?

If you want to use a local variable, which would be good coding practice, then declare it as static to avoid it being reset each time through loop()

1 Like

i want only serial print when movement detected just once! But i cant handle it more from here... i was thinking that if i use requested=false and requested-=true at the end of the if can only sign up once in the screen... can you help to fix the code...??? (i changed it to boolean by the way and it plays the same..)

I don't have time to look in detail but try this

void loop()
{  
  static boolean requested = false;
  int pir_stat = digitalRead(pir);
  if (pir_stat == HIGH && requested == false )
  {
    if ((millis() - lastTime) > timerDelay)
    {
      if (WiFi.status() == WL_CONNECTED)
      {
        HTTPClient http;

        //then after doing what you want
        requested = true;
1 Like

sry;;; it doesn't work.....γ… γ… 

have another clue...???

Post your full sketch as it is now, using code tags when you do

1 Like