/**
ESP32 + DHT22 Example for Wokwi
https://wokwi.com/arduino/projects/322410731508073042
*/
#include <WiFi.h>
#include "DHTesp.h"
#include "ThingSpeak.h"
const int DHT_PIN = 15;
const int LED_PIN = 13;
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const char* server = "api.thingspeak.com";
unsigned long counterChannelNumber = 2361403; // Channel ID
const char * myCounterReadAPIKey = "FJM6G0590B3QN48R"; // Read API Key
const int FieldNumber1 = 1; // The field you wish to read
const int FieldNumber2 = 2; // The field you wish to read
DHTesp dhtSensor;
WiFiClient client;
void setup() {
Serial.begin(115200);
//dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
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() {
if (WiFi.status() != WL_CONNECTED)
{
Serial.print("Connecting to ");
Serial.println(" ....");
while (WiFi.status() != WL_CONNECTED)
{
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
delay(5000);
}
Serial.println("Connected to Wi-Fi Succesfully.");
}
//--------- End of Network connection--------//
//---------------- Channel 1 ----------------//
long temp = ThingSpeak.readLongField(counterChannelNumber, FieldNumber1, myCounterReadAPIKey);
int statusCode = 0;
statusCode = ThingSpeak.getLastReadStatus();
if (statusCode == 200)
{
Serial.print("Temperature: ");
Serial.println(temp);
}
else
{
Serial.println("Unable to read channel / No internet connection");
}
delay(100);
//-------------- End of Channel 1 -------------//
//---------------- Channel 2 ----------------//
long humidity = ThingSpeak.readLongField(counterChannelNumber, FieldNumber2, myCounterReadAPIKey);
statusCode = ThingSpeak.getLastReadStatus();
if (statusCode == 200)
{
Serial.print("Humidity: ");
Serial.println(humidity);
}
else
{
Serial.println("Unable to read channel / No internet connection");
}
delay(15000);
//-------------- End of Channel 2 -------------//
}