#include <SPI.h> // Required for shield communication
#include <WiFi.h>
char ssid[] = "Wokwi-GUEST"; // your network SSID (name)
char pass[] = ""; // your network password
int LED_PIN = 13;
WiFiClient client;
unsigned long myTalkBackID = 49817;
const char * myTalkBackKey = "WWXPSAFJIHHO9P8A";
void setup() {
Serial.begin(115200); // Initialize serial
pinMode(LED_PIN,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));
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, pass);
Serial.print(".");
delay(2000);
}
Serial.println("\nConnected.");
}
// Create the TalkBack URI
String tbURI = String("/talkbacks/") + String(myTalkBackID) + String("/commands/execute");
// Create the message body for the POST out of the values
String postMessage = String("api_key=") + String(myTalkBackKey);
// Make a string for any commands in the queue
String newCommand = String();
// Make the POST to ThingSpeak
int x = httpPOST(tbURI, postMessage, newCommand);
client.stop();
// 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);
}
else if(newCommand.indexOf("led off") != -1){
Serial.println(" New command: Turn led off");
digitalWrite(LED_PIN, LOW);
}
}
}
else{
Serial.println("Problem checking queue. HTTP error code " + String(x));
}
delay(5000); // Wait 5 seconds to check 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);
if(!connectSuccess){
return -301;
}
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);
client.print(postMessage);
long startWaitForResponseAt = millis();
while(client.available() == 0 && millis() - startWaitForResponseAt < 5000){
delay(100);
}
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();
if(status != 200){
return status;
}
if(!client.find(const_cast<char *>("\n\r\n"))){
return -303;
}
String tempString = String(client.readString());
response = tempString;
return status;
}