Sensor send values 0 to my database in MySQL -ESP32

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);  
}

Thanks

Did you write any test code to only read the sensor (and then maybe send it to serial to check)?

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";

and I did other project where only read the values of sensor in the monitor serie and also is ok.

This only tests that the http request is ok by providing a fake LDR_val. What is the actual value of LDR-val? Do you know it is not 0?

What is shown in serial monitor from this?

    // Prepare HTTP POST request data
    String httpRequestData = "api_key=" + apiKeyValue + "&sensor=" + sensorName
                          + "&location=" + sensorLocation + "&value1=" + LDR_val + "";
    Serial.print("httpRequestData: ");
    Serial.println(httpRequestData);

(It probably doesn't matter, but why are you adding an empty string to the end of httpRequestData?)

LDR_val is an integer between 0 and 1023, but your test-value is a float. Does that make any difference on the server side?

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

The empty string to the end of httpRequestData actually I really downplayed it this detail :sweat_smile:

Actually in the server side the value of LDR_val is varchar(10) i tried change to int and float and the problem stays.

Other project, or other sketch? Were you testing the exact same hardware configuration, or were any changes made?

I think you should test it in this program as well. There are many things that can be different from the previous test.....

Just add a "Serial.println(LDR_val)" after analogRead( )

int LDR_val = analogRead(LD);
Serial.println(LDR_val);

Sorry, i meant other sketch, and i used the exact same hardware configuration and the sensor yes send the correct values

I add this code line and the sensor still send values '0'
It's unusual

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

I am not familiar with ESP32, but according to this article, reading ADC2 pins might fail if WiFi is used. Consider ADC1 GPIO instead.

ESP32 ADC – Read Analog Values with Arduino IDE

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.