You also need to install the ArduinoJSON library.
You can install this library in the Arduino IDE Library Manager.
Just go to
Sketch > Include Library > Manage Libraries
and search for the library name as follows:
code
نسخ
اقتباس
عرض
تنزيل
#include "WiFi.h" // ESP32 WiFi include
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "aaa";
const char* password = "aaa";
WiFiClient client;
HTTPClient httpClient;
const char *url = "http://192.168.43.154/path-to-folder/send_receive_data.php";
//===============================(ConnectToWiFi)
void ConnectToWiFi()
{
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to "); Serial.println(ssid);
uint8_t i = 0;
while (WiFi.status() != WL_CONNECTED)
{
Serial.print('.');
delay(500);
if ((++i % 16) == 0)
{
Serial.println(F(" still trying to connect"));
}
}
Serial.print(F("Connected. My IP address is: "));
Serial.println(WiFi.localIP());
}
//===============================(/ConnectToWiFi)
void setup()
{
Serial.begin(115200);
ConnectToWiFi();
Serial.print("localIP: ");
Serial.println(WiFi.localIP());
delay(1000);
}
void loop()
{
//-------------------------------(wifi_status)
switch (WiFi.status())
{
case WL_NO_SSID_AVAIL:
Serial.println("Configured SSID cannot be reached");
break;
case WL_CONNECTED:
Serial.println("Connection successfully established");
Serial.println(WiFi.localIP());
break;
case WL_CONNECT_FAILED:
Serial.println("Connection failed");
break;
}
Serial.printf("Connection status: %dn", WiFi.status());
Serial.print("RRSI: ");
Serial.println(WiFi.RSSI());
//-------------------------------(/wifi_status)
//-----------------------------(send&fetch data)
String data = "temperture_val=" +String(22) + "&humidity_val=" + String(99)+"";
httpClient.begin(client, url);
httpClient.addHeader("Content-Type", "application/x-www-form-urlencoded");
httpClient.POST(data);
String string_json_fetched = httpClient.getString();
httpClient.end();
Serial.println(string_json_fetched);
StaticJsonDocument<500> doc;
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, string_json_fetched);
// Test if parsing succeeds.
if (error)
{
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
//-----------------------------(/send&fetch data)
delay(3000);
}