/**
ESP32 + DHT22 Example for Wokwi
https://wokwi.com/arduino/projects/322410731508073042
*/
#include <WiFi.h>
#include "ThingSpeak.h"
#include <iostream>
const int pirPin = 18;
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const int myChannelNumber = 2261728;
const char* myApiKey = "VLSESE83AF8HSDXV";
const char* server = "api.thingspeak.com";
int pirState = LOW;
int message = 0;
WiFiClient client;
void setup() {
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!");
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
Serial.println("Tran Hai Long B18DCAT149!");
}
void loop() {
ThingSpeak.setField(2,message);
int currentPir = digitalRead(pirPin);
if (currentPir == HIGH) {
if(pirState == LOW) {
message = 1;
}
pirState = HIGH;
}
else {
if (pirState == HIGH) {
message = -1;
pirState = LOW;
}
}
if (message != 0) {
int x = ThingSpeak.writeFields(myChannelNumber, myApiKey);
delay(1000);
if(x == 200){
Serial.println("Data pushed successful!");
}
else {
Serial.println("Push error" + String(x));
}
Serial.println("---");
}
delay(1000);
message = 0;
}