/**
ESP32 + DHT22 Example for Wokwi
https://wokwi.com/arduino/projects/322410731508073042
*/
#include <WiFi.h>
#include "ThingSpeak.h"
#include <LiquidCrystal_I2C.h>
#define NTP_SERVER
#define UTC_OFFSET 7
#define UTC_OFFSET_DST 0
#define MOTINE_SENSOR 4
const int LED_PIN = 13;
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const int myChannelNumber = 2271267;
const char* myApiKey = "7MB7MAHEK869T8RS";
const char* server = "api.thingspeak.com";
unsigned long myTalkBackID = 49982;
const char * myTalkBackKey = "PPC8IBULZX1GXI7J";
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
WiFiClient client;
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
pinMode(MOTINE_SENSOR, INPUT);
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() {
int pirState = digitalRead(MOTINE_SENSOR);
ThingSpeak.setField(1,pirState);
ThingSpeak.setField(2,HIGH);
if (pirState == HIGH) {
digitalWrite(LED_PIN, HIGH);
}else{
digitalWrite(LED_PIN, LOW);
}
int x = ThingSpeak.writeFields(myChannelNumber,myApiKey);
Serial.println("Alert: " + String("Alert on"));
Serial.println("Time: " + String("Time"));
if(x == 200){
Serial.println("Data pushed successfull");
}else{
Serial.println("Push error" + String(x));
}
Serial.println("---");
delay(10000);
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 xz = httpPOST(tbURI, postMessage, newCommand);
client.stop();
// Check the result
if(xz == 200){
Serial.println("checking queue...");
// check for a command returned from TalkBack
if(newCommand.indexOf("alert on") == -1 && newCommand.indexOf("alert off") == -1){
Serial.println(" No command or syntax error!");
}
else{
if(newCommand.indexOf("alert on") != -1){
Serial.println(" New command: Alert on");
pirState = HIGH;
}
else if(newCommand.indexOf("alert off") != -1){
Serial.println(" New command: Alert off");
pirState = 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;
}