#include <WiFi.h>
#include "ThingSpeak.h"
const int MOTION =2 ;
const int LED_PIN = 12;
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const int myChannelNumber = 2271286;
const char* myApiKey = "HQMDE9VNUPRD4PUY";
const char* server = "api.thingspeak.com";
const char * myTalkBackKey = "I2PAJWRYA2O204A7";
unsigned long myTalkBackID = 50045;
int motionReal;
WiFiClient client;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(MOTION, INPUT);
pinMode(LED_PIN, OUTPUT);
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED){
delay(1000);
Serial.println("Wifi not connected");
}
Serial.println("Wifi connected !");
Serial.println("Local IP: " + String(WiFi.localIP()));
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
}
void loop() {
motionReal = digitalRead(MOTION);
ThingSpeak.setField(1,motionReal);
int x = ThingSpeak.writeFields(myChannelNumber,myApiKey);
if(motionReal == LOW)
{
digitalWrite(LED_PIN, HIGH);
}
if(x == 200){
Serial.println("Data pushed successfull");
}else{
Serial.println("Push error" + String(x));
}
Serial.println("---");
String tbURI = String("/talkbacks/") + String(myTalkBackID) + String("/commands/execute");
String postMessage = String("api_key=") + String(myTalkBackKey);
String newCommand = String();
// Make the POST to ThingSpeak
int y = httpPOST(tbURI, postMessage, newCommand);
client.stop();
// Check the result
if(y == 200){
Serial.println("checking queue...");
// check for a command returned from TalkBack
if(newCommand.indexOf("motion on") == -1 && newCommand.indexOf("motion off") == -1){
Serial.println(" No command or syntax error!");
} else{
if(newCommand.indexOf("motion on") != -1){
Serial.println(" New command: Turn motion on");
digitalWrite(MOTION, HIGH);
} else if(newCommand.indexOf("motion off") != -1){
Serial.println(" New command: Turn motion off");
digitalWrite(MOTION, LOW);
}
}
} else{
Serial.println("Problem checking queue. HTTP error code " + String(x));
}
delay(1000); // this speeds up the simulation
}
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;
}