#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "RTClib.h"
#include <WiFi.h>
#include "ThingSpeak.h"
const char* WIFI_NAME = "Wokwi-GUEST"; // WiFi SSID
const char* WIFI_PASSWORD = ""; // WiFI Password
const int myChannelNumber = 2543105; // ThingSpeak channel number
const char* myApiKey = "UZDJCWHRUSBBLOJ5"; // ThingSpeak API key
const char* server = "api.thingspeak.com"; // ThingSpeak server address
RTC_DS1307 rtc;
#define tempSensor 33
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
WiFiClient client;
const float BETA = 3950;
float Temperature = 0.0;
String Date;
String Time;
void setup() {
Serial.begin(115200);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
WiFi.begin(WIFI_NAME, WIFI_PASSWORD); // Connect to the WiFi network
while (WiFi.status() != WL_CONNECTED){
delay(1000);
Serial.println("Wifi not connected"); // Print a message if WiFi is not connected
}
Serial.println("Wifi connected !"); // Print a message if WiFi is connected
Serial.println("Local IP: " + String(WiFi.localIP())); // Print the local IP address
WiFi.mode(WIFI_STA); // Set the WiFi mode to station mode
ThingSpeak.begin(client); // Initialize the ThingSpeak library
}
void loop() {
Temperature = temperature();
dateTime();
displayTT();
ThingSpeak.setField(1,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);
delay(1000);
display.clearDisplay();
}
float temperature() {
int analogValue = analogRead(tempSensor);
float celsius = 1 / (log(1 / (4096. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
Serial.print("Temperature: ");
Serial.print(celsius);
Serial.println(" ℃");
return celsius;
}
void displayTT() {
display.setCursor(0, 0);
display.drawRect(0, 0, 128, 64, SSD1306_WHITE);
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(5, 15);
display.print("Date: ");
display.println(Date);
display.setCursor(5, 25);
display.print("Time: ");
display.println(Time);
display.setCursor(5, 35);
display.print("Temperature: ");
display.print(Temperature);
display.println(" C");
display.display();
}
void dateTime() {
DateTime now = rtc.now();
String y = String(now.year());
String m = String(now.month());
String d = String(now.day());
String h = String(now.hour());
String mi = String(now.minute());
String s = String(now.second());
Date = y + "/" + m + "/" + d;
Time = h + ":" + mi + ":" + s;
}