// Student Name: Muhammad Afeeq Zhikri Bin Zamzuri
// Student ID: 52224122134
#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
// OLED display object connected to I2C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ThingSpeak API information
const char* server = "https://api.thingspeak.com";
const char* apiKey = "CDJEFTTGRAFUMV67"; // Replace with your ThingSpeak Write API Key
void setup() {
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.setTextColor(WHITE);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nWiFi connected!");
}
void sendDataToThingSpeak() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String(server) + "/update?api_key=" + apiKey;
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
Serial.println("Data sent to ThingSpeak successfully!");
} else {
Serial.print("Error sending data: ");
Serial.println(http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.println("WiFi not connected! Unable to send data.");
}
}
void loop() {
static unsigned long lastSendTime = 0;
if (millis() - lastSendTime >= 10000) { // Send data every 10 seconds
sendDataToThingSpeak();
lastSendTime = millis();
}
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.print("Sending data...");
display.display();
}