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 = "*******";
const char* password = "******";
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();
}