Esp32 Http Post Local Network

Hi to all friends... i have one problem and cant figure out what is wrong.
I have one server that i can send and store data to a MySQL Data base

i can send data with an arduino and a Sim800L device through GPRS in my domain ip (working fine)

also i can send and store data to my database with my web Browser if i post this address :
http://192.168.1.110:8000/testing01.php?airtemp=31&airhum=12&battery=1.05

where : 192.168.1.110 is my local ip where my server - data base is connected
: 8000 is the port for the Apache server
: testing01.php is the php file that collects and store the incoming data to the DataBase

I wanted to use a esp32 module that will be connected to the same wifi (local network) and reading some sensors and pass the data to the DataBase

i am using the code below :





#include <WiFi.h>
#include <HTTPClient.h>

// Replace with your network credentials
const char* ssid = "NameOfMyWifi";
const char* password = "MyPassword";

//Your Domain name with URL path or IP address with path
const char* serverName = "http://192.168.1.110:8000/testing01.php?";

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;

// Set timer to 10 seconds (5000)
unsigned long timerDelay = 10000;

//  Variables to send in my Database
int AirTempData = 90;
int AirHumData = 1;
float Battery_Value_voltage = 3.10;



void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  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());

  Serial.println("Timer set to 10 secondss (timerDelay variable), it will take 10 seconds before publishing the first reading.");

}

void loop() {
  //Send an HTTP POST request every seconds / minutes
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if (WiFi.status() == WL_CONNECTED) {
      WiFiClient client;
      HTTPClient http;

      // Your Domain name with URL path or IP address with path
      http.begin(client, serverName);
      
      // Data to send with HTTP POST
      String httpRequestData = "airtemp=" + String(AirTempData) + "&airhum=" + String(AirHumData) + "&battery=" + String(Battery_Value_voltage);

      // Send HTTP POST request
      int httpResponseCode = http.POST(httpRequestData);
      Serial.println(httpResponseCode);
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }


}

(i used just some variables and not actual data from sensors to make it simple at first)

So with this code i can connect my Esp32 to the wifi network and i am getting also a http Response : 200 (that is good as i know from my Sim800L adventure)

but when i open the data base nothing had stored

what may be wrong ?

Thanks in advance

When you use your browser like this, the browser uses GET, not POST, which is what your code is doing. The usage of GET and POST are different:

2 Likes

Hahahahahahaha :laughing: :laughing: :laughing:
So stupid of me, lost almost 2 hours of my life . That is a lesson , never work on a project directly after a long day at work :stuck_out_tongue:

Thanks for you help.
all working fine now

No problem.

How did you fix it? Did you change the Arduino code to use GET or the php code to expect POST?

I changed the Arduino code to use Get

i followed an example from : ESP32 HTTP GET and HTTP POST with Arduino IDE | Random Nerd Tutorials

and this is the code of this example

/*
  Rui Santos
  Complete project details at Complete project details at https://RandomNerdTutorials.com/esp32-http-get-post-arduino/

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*/

#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

//Your Domain name with URL path or IP address with path
String serverName = "http://192.168.1.106:1880/update-sensor";

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;

void setup() {
  Serial.begin(115200); 

  WiFi.begin(ssid, password);
  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());
 
  Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}

void loop() {
  //Send an HTTP POST request every 10 minutes
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
      HTTPClient http;

      String serverPath = serverName + "?temperature=24.37";
      
      // Your Domain name with URL path or IP address with path
      http.begin(serverPath.c_str());
      
      // If you need Node-RED/server authentication, insert user and password below
      //http.setAuthorization("REPLACE_WITH_SERVER_USERNAME", "REPLACE_WITH_SERVER_PASSWORD");
      
      // Send HTTP GET request
      int httpResponseCode = http.GET();
      
      if (httpResponseCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
        Serial.println(payload);
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
      }
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}

1 Like

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