#include <WiFi.h>
#include <ThingSpeak.h> // always include thingspeak header file after other header files and custom macros
char ssid[] = "Wokwi-GUEST"; // your network SSID (name)
char pass[] = ""; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;
const char* server = "api.thingspeak.com";
// Counting channel details
unsigned long counterChannelNumber = 2691707;
const char * myCounterReadAPIKey = "3H9VS434QR716KKV";
void setup() {
Serial.begin(115200); //Initialize serial
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(5000);
}
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop() {
int statusCode = 0;
float temp = ThingSpeak.readFloatField(counterChannelNumber, 1);
float hum = ThingSpeak.readFloatField(counterChannelNumber, 2);
// Check the status of the read operation to see if it was successful
statusCode = ThingSpeak.getLastReadStatus();
if (statusCode == 200) {
Serial.print("Temp: " + String(temp, 2) + "°C ");
Serial.println("Humidity: " + String(hum, 1) + "%");;
}
else {
Serial.println("Problem reading channel. HTTP error code " + String(statusCode));
}
delay(15000); // No need to read the counter too often.
}