#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String talkbacks_id = "52316";
String command_id = "41530336";
String talkbacks_api = "DT3WN6FNS6GKQPHA";
String command_string="";
void GETcommand()
{
HTTPClient http;
Serial.print("[HTTP] begin...\n");
//"http://api.thingspeak.com/talkbacks/35233/commands/16599915.json?api_key=0E1160BEX9CSNU6V"
String url = "http://api.thingspeak.com/talkbacks/";
url += talkbacks_id;
url += "/commands/";
url += command_id;
url += ".json?api_key=";
url += talkbacks_api;
http.begin(url); //HTTP
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println(payload);
int index_start = payload.indexOf("command_string\":\"") + 17;
int index_stop = payload.indexOf("\",\"position");
command_string = payload.substring(index_start, index_stop);
Serial.println(command_string);
}
}
else
{
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
delay(1000);
}
void connectWifi (const char* ssid, const char* password)
{
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to WiFi");
}
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
connectWifi(ssid, password);
pinMode(2, OUTPUT);
}
void loop() {
GETcommand();
if(command_string == "LEDOFF")
{
digitalWrite(2, LOW);//turn on the LED
}
else if(command_string == "LEDON")
{
digitalWrite(2, HIGH);// turn on the LED
}
delay(1000);
}