#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* serverAddress = "http://127.0.0.1:1880"; // Change this to your Node-RED server's IP address
const char* endpoint = "/temp-sensor"; // Change this to your Node-RED endpoint
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
// Send data to Node-RED server
sendDataToNodeRed(5);
delay(5000);
}
void sendDataToNodeRed(int value) {
HTTPClient http;
// Construct URL
String url = String(serverAddress);
url.concat(endpoint);
// Construct data to send
String data = "value=";
data.concat(String(value));
// Start HTTP connection
http.begin(url);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Send POST request with data
int httpResponseCode = http.POST(data);
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// End HTTP connection
http.end();
}