#include <WiFi.h>
#include <WiFiClient.h>
#include <HTTPClient.h>
// Wi-Fi credentials
const char* ssid = "Wokwi-Guest";
const char* pass = ""; // No password for Wokwi-Guest
// ThingSpeak / TalkBack Settings
const char* server = "http://api.thingspeak.com";
String myWriteAPIKey = "ET8BDHPGNFSV87O3"; // TalkBack API Key
String myTalkBackKey = "ET8BDHPGNFSV87O3"; // Same as API Key for TalkBack
String myTalkBackID = "54638"; // Your TalkBack ID
// Pins
const int LED1_PIN = 2;
const int LED2_PIN = 4;
// Data values
int number1 = 0;
int number2 = 0;
int number3 = 0;
int number4 = 0;
// Create WiFi client
WiFiClient client;
void setup() {
Serial.begin(115200);
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
connectWiFi();
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
connectWiFi();
}
// Create the POST message
String postMessage =
"field1=" + String(number1) +
"&field2=" + String(number2) +
"&field3=" + String(number3) +
"&field4=" + String(number4) +
"&api_key=" + myWriteAPIKey +
"&talkback_key=" + myTalkBackKey;
// Prepare for any new command
String newCommand;
// Send POST request and get any command
int httpResult = httpPOST(postMessage, newCommand);
client.stop(); // Close connection
if (httpResult == 200) {
Serial.println("Checking TalkBack queue...");
if (newCommand.length() > 0) {
Serial.print(" Latest command: ");
Serial.println(newCommand);
handleCommand(newCommand);
} else {
Serial.println(" No new command.");
}
} else {
Serial.println("HTTP error code: " + String(httpResult));
}
// Update values for next POST
number1 = (number1 + 1) % 100;
number2 = random(0, 100);
number3 = random(0, 100);
number4 = random(0, 100);
delay(20000); // 20 seconds between updates
}
void connectWiFi() {
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA); // <--- THIS is the line I suggested
WiFi.begin(ssid, pass);
int retries = 0;
while (WiFi.status() != WL_CONNECTED && retries < 10) {
delay(1000);
Serial.print(".");
retries++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("\nWi-Fi connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
} else {
Serial.println("\nFailed to connect to Wi-Fi. Restarting...");
ESP.restart();
}
}
void handleCommand(String cmd) {
if (cmd == "LED1_ON") {
digitalWrite(LED1_PIN, HIGH);
} else if (cmd == "LED1_OFF") {
digitalWrite(LED1_PIN, LOW);
} else if (cmd == "LED2_ON") {
digitalWrite(LED2_PIN, HIGH);
} else if (cmd == "LED2_OFF") {
digitalWrite(LED2_PIN, LOW);
} else {
Serial.println("Unknown command received.");
}
}
// Simple POST request function
int httpPOST(String postData, String &command) {
HTTPClient http;
int httpCode = -1;
String endpoint = String(server) + "/talkbacks/" + myTalkBackID + "/commands/execute";
if (http.begin(client, endpoint)) {
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
httpCode = http.POST(postData);
if (httpCode == 200) {
command = http.getString();
} else {
Serial.print("POST failed, error: ");
Serial.println(http.errorToString(httpCode));
}
http.end();
}
return httpCode;
}