#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define WIFI_CHANNEL 6
#include <WiFi.h>
#include "secrets.h"
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros
#include "DHTesp.h"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);
#define SECRET_CH_ID 3195905 // replace 0000000 with your channel number
#define SECRET_WRITE_APIKEY "VQIDLE7O9AO7A55T" // replace XYZ with your channel write API Key
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
const int DHT_PIN = 19;
// Initialize our values
int number1 ;
int number2;
DHTesp dhtSensor;
void setup() {
Serial.begin(115200); //Initialize serial
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
Serial.print("Connecting to WiFi ");
Serial.print(WIFI_SSID);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
dhtSensor.setup(DHT_PIN, DHTesp::DHT11);
display.begin(SSD1306_SWITCHCAPVCC, 0x3c); // Address 0x3C for 128x32
display.display();
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop() {
// Connect or reconnect to WiFi
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,30);
display.print("Temp: " + String(data.temperature, 2) + "C");
display.setCursor(0,40);
display.print("Humidity: " + String(data.humidity, 1) + "%");
display.display();
// set the fields with the values
number1=String(data.temperature, 2).toInt();
number2=String(data.humidity, 1).toInt();
ThingSpeak.setField(1, number1);
ThingSpeak.setField(2, number2);
// write to the ThingSpeak channel
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
// change the values
delay(20000); // Wait 20 seconds to update the channel again
}