// Check live data on https://thingspeak.mathworks.com/channels/2691707
#include <WiFi.h> //Library for WiFi Functionality
#include "ThingSpeak.h" // Library for Thingspeak Specific Write read Functions
#include <DHT.h> // Library for DHT Sensors
#define DHTPIN 21 // Pin where the DHT22 is connected
#define DHTTYPE DHT22 // Type of DHT sensor
DHT dht(DHTPIN, DHTTYPE); // Define DHT Library object as dht
const char* WIFI_NAME = "Wokwi-GUEST"; // Default Wokwi SSID - Dont Change
const char* WIFI_PASSWORD = ""; // Default Wokwi PASSWORD (Open Network) - Dont Change
const int myChannelNumber = 2691707; // My channel Number
const char* myApiKey = "3H9VS434QR716KKV"; // Channel Write API Key
const char* server = "api.thingspeak.com"; // Server/host
WiFiClient client; // WiFi CLient object as client variable
void setup() {
Serial.begin(115200); // Serial Monitor
dht.begin(); // Init DHT Sensor
WiFi.begin(WIFI_NAME, WIFI_PASSWORD); //Connect to WiFi with given SSID and Pass
delay(2000); // wait for wifi to connect
while (WiFi.status() != WL_CONNECTED) { // until wifi connected
delay(1000);
Serial.println("Wifi not connected");
}
Serial.println("Wifi connected !");
WiFi.mode(WIFI_STA); // set wifi to be act in station mode
ThingSpeak.begin(client); // setup thingspeak with client
}
void loop() {
float temperature = dht.readTemperature(); // Read temperature in Celsius
float humidity = dht.readHumidity(); // Read humidity
Serial.print("Temp: " + String(temperature, 2) + "°C ");
Serial.println("Humidity: " + String(humidity, 1) + "%");
ThingSpeak.setField(1, temperature); //add data in field 1
ThingSpeak.setField(2, humidity); //add data in field 2
int x = ThingSpeak.writeFields(myChannelNumber, myApiKey); //Write to Thingspeak and save response in x
if (x == 200) {
Serial.println("Data pushed successfull");
} else {
Serial.println("Push error" + String(x));
}
Serial.println("---");
delay(1000); //Thingspeak Free account limit is 15sec. dont send before this time.
}