/**
ESP32 + DHT22 Example for Wokwi
https://wokwi.com/arduino/projects/322410731508073042
*/
#include <WiFi.h>
#include "DHTesp.h"
#include "ThingSpeak.h"
int val=0;
const int PIR_PIN = 14;
const int LED_PIN = 15;
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const int myChannelNumber = 2271237;
const char* myApiKey = "GW99JU2I41YR2QFM";
const char* server = "api.thingspeak.com";
DHTesp dhtSensor;
WiFiClient client;
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
pinMode(PIR_PIN, INPUT_PULLUP);
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() {
if(digitalRead(PIR_PIN)==HIGH){
ThingSpeak.setField(1,"ON");
digitalWrite(LED_PIN, HIGH);
}
else {
ThingSpeak.setField(1,"OFF");
digitalWrite(LED_PIN, LOW);
}
int x = ThingSpeak.writeFields(myChannelNumber,myApiKey);
if(x == 200){
Serial.println("Data pushed successfull");
}else{
Serial.println("Push error" + String(x));
}
Serial.println("---");
delay(10000);
}