#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String payload;
const String url = "https://api.thingspeak.com/update?";
String mykey = "MLV9DOGOAT8JY1PY";
HTTPClient http;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("OK! IP=");
Serial.println(WiFi.localIP());
Serial.print("Fetching " + url + "... ");
http.begin(url);
}
void loop() {
int x = random(30);
int y = random(100);
http.addHeader("Content_Type","application/x-wwwform-urlencoded");
String httprequestpost = "api_key="+ mykey +"&field1="+String(x)+"&field2="+String(y);
int httpResponseCode = http.POST(httprequestpost);
if (httpResponseCode > 0) {
Serial.print("HTTP ");
Serial.println(httpResponseCode);
// Disconnect
http.end();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
Serial.println(":-(");
}
delay(2000);
}