My sensor send values 0 to my database in MySQL ESP32

hanshasn12345678
Posts: 3
Joined: Wed Aug 04, 2021 10:24 pm

My sensor send values 0 to my database in MySQL ESP32

Postby hanshasn12345678 » 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:

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

ESP_Sprite
Posts: 9727
Joined: Thu Nov 26, 2015 4:08 am

Re: My sensor send values 0 to my database in MySQL ESP32

Postby ESP_Sprite » Tue Aug 10, 2021 1:22 am

Mod note: I've taken the liberty to censor the server name, API key and your SSID and password from the code. It's not a good idea to leak all those to the public Internet; especially the URL and API key could allow people to mess up your server.

Who is online

Users browsing this forum: No registered users and 60 guests