#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <NTPClient.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <HTTPClient.h>
// Define pin connections
#define LDR_PIN 5
#define SWITCH_PIN 4
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// NTP Client setup
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 0, 60000);
// Line Notify
const char* LINE_TOKEN = "ij3BTrbSEN804X54cvkb4h2FdneatPFXkENlWBrXlRp";
// Function to send a LINE notification
void sendLineNotification(String message) {
HTTPClient http;
http.begin("https://notify-api.line.me/api/notify");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.addHeader("Authorization", "Bearer " + String(LINE_TOKEN));
int httpResponseCode = http.POST("message=" + message);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(response);
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end();
}
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize OLED display
if(!display.begin(SSD1306_PAGEADDR, OLED_RESET)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(2000);
display.clearDisplay();
// Initialize LDR and switch pin
pinMode(LDR_PIN, INPUT);
pinMode(SWITCH_PIN, INPUT);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize time client
timeClient.begin();
}
void loop() {
// Update time client
timeClient.update();
String formattedDate = timeClient.getFormattedTime();
// Read LDR value
int ldrValue = analogRead(LDR_PIN);
float voltage = ldrValue * (5.0 / 1023.0);
float lux = voltage * 200; // Adjust this based on your LDR calibration
// Read switch status
int switchState = digitalRead(SWITCH_PIN);
// Display data on OLED
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print("Date/Time: ");
display.println(formattedDate);
display.print("Switch: ");
display.println(switchState == LOW ? "OFF" : "ON");
display.print("LUX: ");
display.println(lux);
display.display();
// Send LINE notifications based on conditions
if (switchState == LOW) {
sendLineNotification("Switch is OFF");
}
if (lux > 90000) {
sendLineNotification("LUX value exceeds 90000");
}
// Delay before next loop
delay(1000);
}