#include <SPI.h>
#include <WiFi.h>
// Wi-Fi network configuration
const char *ssid = "Wokwi-GUEST"; // Your network SSID (name)
const char *pass = ""; // Your network password (fill this in)
const int LED_PIN_1 = 13; // GPIO pin connected to the first LED
const int LED_PIN_2 = 12; // GPIO pin connected to the second LED
WiFiClient client; // Create a Wi-Fi client
unsigned long myTalkBackID = 50127;
const char *myTalkBackKey = "8Y8LH1XQY0ZXIDHJ";
void setup() {
Serial.begin(115200); // Initialize serial communication for debugging
pinMode(LED_PIN_1, OUTPUT); // Set the first LED pin as an output
pinMode(LED_PIN_2, OUTPUT); // Set the second LED pin as an output
}
void loop() {
// Connect or reconnect to Wi-Fi
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid); // Print the network SSID being connected to
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass); // Start the Wi-Fi connection
Serial.print(".");
delay(2000); // Wait for 2 seconds before retrying
}
Serial.println("\nConnected.");
}
// Create the TalkBack URI
String tbURI = "/talkbacks/" + String(myTalkBackID) + "/commands/execute";
// Create the message body for the POST request out of the values
String postMessage = "api_key=" + String(myTalkBackKey); // Construct the POST data
// Make a string for any commands in the queue
String newCommand = "";
// Make the POST request to ThingSpeak
int x = httpPOST(tbURI, postMessage, newCommand); // Call the HTTP POST function
client.stop(); // Close the client connection
// Check the result
if (x == 200) {
Serial.println("Checking queue...");
// Check for a command returned from TalkBack
if (newCommand.indexOf("led1 on") != -1) {
Serial.println("New command: Turn LED 1 on");
digitalWrite(LED_PIN_1, HIGH); // Turn on the first LED
} else if (newCommand.indexOf("led1 off") != -1) {
Serial.println("New command: Turn LED 1 off");
digitalWrite(LED_PIN_1, LOW); // Turn off the first LED
} else if (newCommand.indexOf("led2 on") != -1) {
Serial.println("New command: Turn LED 2 on");
digitalWrite(LED_PIN_2, HIGH); // Turn on the second LED
} else if (newCommand.indexOf("led2 off") != -1) {
Serial.println("New command: Turn LED 2 off");
digitalWrite(LED_PIN_2, LOW); // Turn off the second LED
} else {
Serial.println("No command or syntax error!");
}
} else {
Serial.println("Problem checking queue. HTTP error code " + String(x));
}
delay(5000); // Wait 5 seconds before checking the queue again
}
// General function to POST to ThingSpeak
int httpPOST(String uri, String postMessage, String &response) {
bool connectSuccess = false;
connectSuccess = client.connect("api.thingspeak.com", 80); // Connect to ThingSpeak server
if (!connectSuccess) {
return -301; // Connection error
}
postMessage += "&headers=false";
String headers = "POST " + uri + " HTTP/1.1\r\n" +
"Host: api.thingspeak.com\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Connection: close\r\n" +
"Content-Length: " + String(postMessage.length()) + "\r\n\r\n";
client.print(headers); // Send HTTP headers
client.print(postMessage); // Send POST data
unsigned long startWaitForResponseAt = millis();
while (client.connected() && !client.available()) {
if (millis() - startWaitForResponseAt > 5000) {
client.stop();
return -304; // Didn't get server response in time
}
delay(100); // Wait for a response with a timeout of 5 seconds
}
if (!client.connected()) {
return -305; // Connection lost
}
if (!client.find("HTTP/1.1")) {
return -303; // Couldn't parse response (didn't find HTTP/1.1)
}
int status = client.parseInt(); // Parse HTTP response code
if (status != 200) {
return status; // Return HTTP status code
}
if (!client.find("\n\r\n")) {
return -303; // Couldn't parse response
}
response = client.readString(); // Read the response
return status; // Return HTTP status code
}