#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// OLED Configuration
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_ADDR 0x3C // Correct I2C address for the OLED
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// PIR Sensor Configuration
#define PIR_PIN 12 // PIR sensor connected to GPIO12
bool motionDetected = false; // To track motion state
// WiFi Configuration
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ThingSpeak Configuration
const char* server = "https://api.thingspeak.com/update?api_key=JG772FC2J98MQZXK&field1=0";
const char* apiKey = "JG772FC2J98MQZXK";
// User credentials
const String validUsername = "admin";
const String validPassword = "1234";
// Login flag and session timeout
bool isLoggedIn = false;
unsigned long lastActivityTime = 0; // Last activity time (millis)
const unsigned long sessionTimeout = 300000; // Timeout duration: 5 minutes
// Mock temperature and humidity values
float temperature = 25.0; // Set a constant value or simulate changes
float humidity = 50.0; // Set a constant value or simulate changes
void setup() {
Serial.begin(115200);
// Initialize PIR sensor pin
pinMode(PIR_PIN, INPUT);
// Initialize OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) { // Correct initialization
Serial.println("OLED initialization failed!");
while (true);
}
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
// Display welcome message
display.setTextSize(1);
display.setCursor(0, 0);
display.println("Welcome! Please log in:");
display.display();
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi!");
}
void loop() {
if (!isLoggedIn) {
// Check for login input via Serial Monitor
if (Serial.available()) {
String input = Serial.readStringUntil('\n');
handleLogin(input);
}
} else {
// Check for session expiry
if (millis() - lastActivityTime > sessionTimeout) {
isLoggedIn = false;
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.println("Session expired.");
display.println("Please log in again.");
display.display();
Serial.println("Session expired. Logged out.");
return;
}
// Read PIR sensor to detect motion
motionDetected = digitalRead(PIR_PIN);
// Display temperature and motion detection on the OLED
displaySensorData();
// Send data to ThingSpeak
sendDataToThingSpeak();
delay(2000); // Refresh every 2 seconds
}
}
// Handle login
void handleLogin(String input) {
if (input.startsWith("POST /login")) {
// Parse username and password
int usernameIndex = input.indexOf("username=");
int passwordIndex = input.indexOf("password=");
String username = input.substring(usernameIndex + 9, input.indexOf('&', usernameIndex));
String password = input.substring(passwordIndex + 9);
if (username == validUsername && password == validPassword) {
isLoggedIn = true;
lastActivityTime = millis(); // Reset activity timer
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.println("Login successful!");
display.println("Fetching data...");
display.display();
Serial.println("Login successful!");
} else {
Serial.println("Invalid credentials! Try again.");
}
}
}
// Display temperature, humidity, and motion status
void displaySensorData() {
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.println("Battery Temperature & Humidity:");
display.setTextSize(2);
display.setCursor(0, 16);
display.print(temperature);
display.print(" C");
display.setCursor(0, 40);
display.print(humidity);
display.print(" %");
// Display PIR motion detection result
display.setTextSize(1);
display.setCursor(0, 56);
if (motionDetected) {
display.println("Solar Detected!");
} else {
display.println("No Solar.");
}
display.display();
// Update the activity timer
lastActivityTime = millis();
}
// Send data to ThingSpeak
void sendDataToThingSpeak() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String(server) + "?api_key=" + apiKey
+ "&field1=" + String(temperature)
+ "&field2=" + String(humidity)
+ "&field3=" + String(motionDetected ? 1 : 0);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.println("Data sent to ThingSpeak: " + url);
} else {
Serial.println("Error sending data to ThingSpeak.");
}
http.end();
} else {
Serial.println("WiFi not connected!");
}
}