#include <WiFi.h>
#include "DHTesp.h"
#include "ThingSpeak.h"
#include <SPI.h> // Required for shield communication
char ssid[] = "Wokwi-GUEST"; // your network SSID (name)
char pass[] = ""; // your network password
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const int myChannelNumber = 2271310;
const char* myApiKey = "6H5WOIYWGSDLEDI1";
const char* server = "api.thingspeak.com";
WiFiClient client;
unsigned long myTalkBackID = 50031;
const char * myTalkBackKey = "DUU5NRJZA3R1PFA1";
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
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() {
val = digitalRead(inputPin);
ThingSpeak.setField(3,val);
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 k = httpPOST(tbURI, postMessage, newCommand);
client.stop();
// Check the result
if(k == 200){
Serial.println("checking queue...");
// check for a command returned from TalkBack
if(newCommand.indexOf("on") == -1 && newCommand.indexOf("off") == -1){
Serial.println(" No command or syntax error!");
}
else{
if(newCommand.indexOf("on") != -1){
int x ;
if (val == HIGH) {
if (pirState == LOW) {
x = ThingSpeak.writeFields(myChannelNumber,myApiKey);
Serial.println("Motion detected!");
pirState = HIGH;
}
} else {
if (pirState == HIGH) {
x = ThingSpeak.writeFields(myChannelNumber,myApiKey);
Serial.println("Motion ended!");
pirState = LOW;
}
}
if(x == 200){
Serial.println("Data pushed successfull");
}else{
Serial.println("Push error" + String(x));
}
Serial.println("---");
// Create the TalkBack URI
}
else if(newCommand.indexOf("off") != -1){
Serial.println(" New command: Turn off");
}
}
}
else{
Serial.println("Problem checking queue. HTTP error code " + String(k));
}
delay(1000); // 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;
delay(1000);
}