My sensor send values 0 to my database in MySQL ESP32
Posted: Mon Aug 09, 2021 8:56 pm
Hello everyone
I am a beginner with ESP32 and I am developing a example where I show simple data to a web page hosted in CPanel, within CPanel I made two .php pages, one where the POST method will be executed and another where the data will be displayed .
Everything is OK, But the problem is when I upload the code, the sent values are same as 0, I don't look the problem in the code, as I sent the data of LDR sensor with the variable LDR_val, this variable calls the function 'analogRead' of the PIN where the LDR sensor is connected.
My code is:
Thanks
I am a beginner with ESP32 and I am developing a example where I show simple data to a web page hosted in CPanel, within CPanel I made two .php pages, one where the POST method will be executed and another where the data will be displayed .
Everything is OK, But the problem is when I upload the code, the sent values are same as 0, I don't look the problem in the code, as I sent the data of LDR sensor with the variable LDR_val, this variable calls the function 'analogRead' of the PIN where the LDR sensor is connected.
My code is:
Code: Select all
#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#define LD 4 // Number Pin of LDR
const char* ssid = "[ssid]";
const char* password = "[password]";
const char* serverName = "http://[server]/post-esp32-data.php";
String apiKeyValue = "[api_key]";
String sensorName = "LDR";
String sensorLocation = "Office";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
//WiFi.setSleep (false);
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());
}
void loop() {
int LDR_val = analogRead(LD);
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
WiFiClient client;
HTTPClient http;
//Domain name with URL path or IP address with path
http.begin(client, serverName);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Prepare HTTP POST request data
String httpRequestData = "api_key=" + apiKeyValue + "&sensor=" + sensorName
+ "&location=" + sensorLocation + "&value1=" + LDR_val + "";
Serial.print("httpRequestData: ");
Serial.println(httpRequestData);
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
//Send an HTTP POST request every 30 seconds
delay(30000);
}