#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <WiFi.h>
#include "DHTesp.h"
#include "ThingSpeak.h"
#include <SPI.h>
#include <SD.h>
// OLED display dimensions (128x64)
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// DHT22 sensor setup
const int DHT_PIN = 15; // DHT22 sensor GPIO Pin
const char* WIFI_NAME = "Wokwi-GUEST"; // WiFi SSID
const char* WIFI_PASSWORD = ""; // WiFi Password
const int myChannelNumber = 2792964; // ThingSpeak channel number
const char* myApiKey = "MS6RDDH6HXAPANDI"; // ThingSpeak API key
const char* server = "api.thingspeak.com"; // ThingSpeak server address
// SD card chip select pin
#define SD_CS_PIN 5 // Chip Select Pin for SD card
// Create an instance of the DHTesp library
DHTesp dhtSensor;
// Create a WiFi client object
WiFiClient client;
void setup() {
// Initialize the serial communication at a baud rate of 115200
Serial.begin(115200);
// Initialize the DHT22 sensor
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
// Initialize the OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address is often 0x3C
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.display(); // Clear the display buffer
delay(2000); // Pause for 2 seconds
display.clearDisplay(); // Clear the display
// Connect to WiFi
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); // Set the WiFi mode to station mode
ThingSpeak.begin(client); // Initialize the ThingSpeak library
// Initialize SD card
if (!SD.begin(SD_CS_PIN)) {
Serial.println("SD card initialization failed!");
return;
}
Serial.println("SD card initialized.");
}
void loop() {
// Read temperature and humidity from the DHT22 sensor
TempAndHumidity data = dhtSensor.getTempAndHumidity();
// Set the value of field 1 in the ThingSpeak channel to the temperature
ThingSpeak.setField(1, data.temperature);
// Set the value of field 2 in the ThingSpeak channel to the humidity
ThingSpeak.setField(2, data.humidity);
// Write the data to the ThingSpeak channel
int status = ThingSpeak.writeFields(myChannelNumber, myApiKey);
// Print the temperature and humidity values
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
// Write the data to SD card
File dataFile = SD.open("/sensor_data.txt", FILE_APPEND); // Open the file in append mode
if (dataFile) {
// Write the temperature and humidity data to the file
dataFile.print("Temp: ");
dataFile.print(data.temperature);
dataFile.print("°C, Humidity: ");
dataFile.print(data.humidity);
dataFile.println("%");
dataFile.close(); // Close the file
Serial.println("Data written to SD card.");
} else {
Serial.println("Failed to open file for writing.");
}
// Check the status of the ThingSpeak update
if (status == 200) {
Serial.println("Data pushed to ThingSpeak successfully.");
} else {
Serial.println("Push error: " + String(status));
}
// Display the temperature and humidity on the OLED
display.clearDisplay(); // Clear the previous display
display.setTextSize(1); // Text size
display.setTextColor(SSD1306_WHITE); // Text color
display.setCursor(0, 0); // Position the cursor at top-left corner
display.print("Temp: ");
display.print(data.temperature, 2);
display.print(" C");
display.setCursor(0, 20); // Position the cursor for humidity
display.print("Humidity: ");
display.print(data.humidity, 1);
display.print(" %");
display.display(); // Update the display with the new data
Serial.println("---"); // Print a separator
delay(10000); // Delay for 10 seconds
}