#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "HUAWEI_81C3"; // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = "AN2M92FFY6G"; // The password of the Wi-Fi network
const char* serverName = "http://192.168.4.105:5000/sensordata"; // The URL of the server to send data to
void setup() {
Serial.begin(115200);
delay(10);
Serial.println('\n');
WiFi.begin(ssid, password); // Connect to Wi-Fi network
while (WiFi.status() != WL_CONNECTED) { // Wait until connected to Wi-Fi
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("Connected to Wi-Fi");
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Print the IP address of the ESP32/ESP8266
// Send HTTP POST request
if (WiFi.status() == WL_CONNECTED) { // Check if connected to Wi-Fi
HTTPClient http;
http.begin(serverName); // Specify the URL
http.addHeader("Content-Type", "application/json"); // Specify content-type header
// Create JSON data
String jsonData = "{\"temperature\":25.5,\"humidity\":60}";
int httpResponseCode = http.POST(jsonData); // Send the POST request
if (httpResponseCode > 0) { // Check the response code
String response = http.getString(); // Get the response
Serial.println("Response:");
Serial.println(response);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end(); // Free resources
}
}
void loop() {
// Nothing to do here
}