Issue with inserting multiple sensor data into database

Hi everyone,
In a nutshell, I trying to insert data from multiple sensors into a database using php rest api with http requests. I want to do it with all sensors at once as well as one sensor at a time. I managed to log data for all the sensors at once but it is not working when doing it with only one sensor. The problem is that the I cannot set the variable storing the sensor to 0 when a different sensors is activated.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>


#define LM35_Sensor A0
#define LDR_Sensor D2
#define LED D3


const char* ssid     = "AndroidAP";
const char* password = "qfyx7094";

String get_status_url = "http://192.168.1.79/folder2/getStatus.php";
String add_data_url = "http://192.168.1.79/folder2/insertData.php";

WiFiClient client;
HTTPClient http;

float celsius = 0;
int light_Status = 0;

const unsigned long eventInterval_1 = 5000;
unsigned long previousTime_1 = 0;

const unsigned long eventInterval_2 = 5000;
unsigned long previousTime_2 = 0;

unsigned long currentTime;

int device_status1 = 0;
int device_status2 = 0;


void setup() {
  pinMode (LDR_Sensor, INPUT);
  pinMode (LED, OUTPUT);
  
  Serial.begin(115200);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  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 loop() {
  currentTime = millis();
  getStatus1();
  getStatus2();


}

void getStatus1() {

  String url1 = get_status_url + "?did=1";
  if (currentTime - previousTime_1 >= eventInterval_1) {
    http.begin(client, url1);
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
    int httpCode1 = http.GET();

    if (httpCode1 > 0) { //Check the returning code

      String payload = http.getString();   //Get the request response payload
      device_status1 = payload.toInt();
    }

    http.end();   //Close connection

    if (device_status1 == 1)
    {
      getTemp();
      postData();
    }
    else if (device_status1 == 0)
    {

      Serial.println("Device 1 OFF");
    }

    previousTime_1 = currentTime;

  }
}

void getStatus2() {

  String url2 = get_status_url + "?did=2";
  if (currentTime - previousTime_2 >= eventInterval_2) {
    http.begin(client, url2);
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
    int httpCode2 = http.GET();

    if (httpCode2 > 0) { //Check the returning code

      String payload = http.getString();   //Get the request response payload
      device_status2 = payload.toInt();
    }

    http.end();   //Close connection

    if (device_status2 == 1)
    {
      getLDR();
      postData();
      
     
    }
    else if (device_status2 == 0)
    {
      digitalWrite(LED, LOW);
      Serial.println("Device 2 OFF");
    }

    previousTime_2 = currentTime;

  }
}


//Sensor 1 
void getTemp() {

  int analogValue = analogRead(LM35_Sensor);
  float millivolts = (analogValue / 1024.0) * 3300; //3300 is the voltage provided by NodeMCU
  celsius = millivolts / 10;

  // check temp value
  if (celsius >= 32) { // if temperature t from lm35 is great than 32
    Serial.println("the Fan is on "); // print the fan is on
    Serial.print(F("%  Temperature in Celsius: ")); // and print temp values
    Serial.print(celsius);
    Serial.println("");
  }
  else if (celsius < 32) {
    Serial.println("the Fan is off ");
    Serial.print(F("%  Temperature in Celsius: "));
    Serial.print(celsius);
    Serial.println("");
  }


}
//sensor 2
void getLDR() {

  light_Status = digitalRead (LDR_Sensor);
  Serial.print("LDR value:");
  Serial.println(light_Status);

switch (light_Status){
  
  case HIGH:
  digitalWrite(LED, HIGH); 
  Serial.println("LED is ON");
  break;
  
  case LOW:
  digitalWrite(LED, LOW); 
  Serial.println("LED OFF");
  break;

  default: 

  digitalWrite(LED, LOW); 
  Serial.println("LED is OFF");
  
  }
}

// converting the values into string then sending it to the server
void postData(){
  String temp, ldr, postData;
  
  temp = String(celsius);
  ldr = String(light_Status);

  postData = "temp=" + temp + "&led=" + ldr;

  http.begin(client, add_data_url);
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");

  // Send HTTP POST request
  int httpCode = http.POST(postData);

  if (httpCode > 0) {
    String payload = http.getString();
    Serial.println(payload);    //Print request response payload
  }
  else {
    Serial.print("Error code: ");
    Serial.println(httpCode);
  }
  // Free resources
  http.end();
   
  }
  

Welcome to the forum

Which variable is causing the problem ?

Hello UKHeliBob, Sorry for my English, not a native speaker but I will try my best to explain the situation to you.

float celsius = 0;
int light_Status = 0;

So both of these global variable are the issue here. Lets say (device_status1 == 1) the functions void getTemp() and void postData() are triggered rightfully. The variable celsius is passed through the void getTemp() and sent to void postData() with the correct value. But now, if I make (device_status1 == 0) and (device_status2 == 1) instead of making the variable celsius to 0 it post the last value it recorded.

You only set celsius to zero when you declare it so no wonder it retains the value it last had when a different sensor is activated. If you want its value to be zero when a different sensor is activated then do it at that time

Yes, I was wondering the same but how to do it that's my question. So far I know that this needs to be done in the postData() function. So I was thinking of using an if statement like device_status1 == 0 set getLDR() to 0 or something like this. Am I on the right track or should I use something else?

You test the device statuses several times in the code, so if you are not measuring the temperature just set celsius to zero

So where do I set it to 0 in the getLDR() function or the postData() function?

The best way to do it would be to test which device you are using in a single place in the code. Once you know which device you are using then set the variable that you will not be using to zero or whatever you want

Hi UKHeliBob, sorry for the late reply, but based on your suggestion I managed to write the necessary lines of codes for my project. Thanks