#include <SPI.h> // Required for shield communication
#include <WiFi.h>
// Wi-Fi network configuration
char ssid[] = "Wokwi-GUEST"; // Your network SSID (name)
char pass[] = ""; // Your network password (fill this in)
int LED_PIN = 13; // GPIO pin connected to an LED
WiFiClient client; // Create a Wi-Fi client
unsigned long myTalkBackID = 51190;
const char * myTalkBackKey = "JUVABF9FB6B7XO84";
void setup() {
Serial.begin(115200); // Initialize serial communication for debugging
pinMode(LED_PIN, OUTPUT); // Set the 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(String(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 = String("/talkbacks/") + String(myTalkBackID) + String("/commands/execute");
// Create the message body for the POST request out of the values
String postMessage = String("api_key=") + String(myTalkBackKey); // Construct the POST data
// Make a string for any commands in the queue
String newCommand = String();
// 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("led on") == -1 && newCommand.indexOf("led off") == -1) {
Serial.println("No command or syntax error!");
} else {
if (newCommand.indexOf("led on") != -1) {
Serial.println("New command: Turn LED on");
digitalWrite(LED_PIN, HIGH); // Turn on the LED
} else if (newCommand.indexOf("led off") != -1) {
Serial.println("New command: Turn LED off");
digitalWrite(LED_PIN, LOW); // Turn off the LED
}
}
} 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 = String("POST ") + uri + String(" HTTP/1.1\r\n") +
String("Host: api.thingspeak.com\r\n") +
String("Content-Type: application/x-www-form-urlencoded\r\n") +
String("Connection: close\r\n") +
String("Content-Length: ") + String(postMessage.length()) +
String("\r\n\r\n");
client.print(Headers); // Send HTTP headers
client.print(postMessage); // Send POST data
long startWaitForResponseAt = millis();
while (client.available() == 0 && millis() - startWaitForResponseAt < 5000) {
delay(100); // Wait for a response with a timeout of 5 seconds
}
if (client.available() == 0) {
return -304; // Didn't get server response in time
}
if (!client.find(const_cast<char *>("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(const_cast<char *>("\n\r\n"))) {
return -303; // Couldn't parse response
}
String tempString = String(client.readString()); // Read the response
response = tempString;
// Serial.println("response: " + response);
return status; // Return HTTP status code
}