Hello everyone,
I’m doing a project where I use a ESP32 wifi module and a ldr sensor. This will allow me to capture the values recorded in real time in a BD in a CPanel host.
Everything is OK but when I compile the code, the sensor only send values '0' i see the code and I don't watch the issue, i tried make variables to store sensor values, this without success .
My code is
#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#define LD 4 // Number Pin of LDR
const char* ssid = "xxxxx";
const char* password = "xxxx";
const char* serverName = "http://xxxx.xxxx/post-esp32-data.php";
String apiKeyValue = "xxxxxx";
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);
}
Yes, i tried this code line for testing purposes without the LDR sensor and its' successfully: String httpRequestData = "api_key=xxxxxx&sensor=LDR&location=Office&value1=24.75";
Hi
Answering your question, the real values of LDR-val are 100-200 (I know this because I have tested my sensor in another program where it shows its values without connection to MySQL)
This is what it shows me on the serial monitor
I found the issue! is the code line 'WiFi.begin(ssid, password)'
When i write this code the sensor start send values 0000, and when i erase this code line, the sensor send the real values, but i don't understand how i can fix it