#include <WiFi.h>
#include <HTTPClient.h>
#include <Adafruit_SSD1306.h>
#define PIR_PIN 14
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
const char* ssid = "YOUR_SSID"; // Replace with your Wi-Fi SSID
const char* password = "YOUR_PASSWORD"; // Replace with your Wi-Fi password
const char* serverName = "http://<your-laptop-ip>:5000/upload"; // Replace <your-laptop-ip> with your laptop's IP address
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
pinMode(PIR_PIN, INPUT);
if(!display.begin(SSD1306_I2C_ADDRESS, OLED_RESET)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(2000);
display.clearDisplay();
}
void loop() {
int motionDetected = digitalRead(PIR_PIN);
if (motionDetected == HIGH) {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Motion Detected");
display.display();
captureAndSendImage();
delay(10000); // Wait for 10 seconds before checking again
}
}
void captureAndSendImage() {
// Simulate sending an image request (replace with actual image data in a real scenario)
HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "application/json");
String payload = "{\"request\": \"image\"}";
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
if (response.indexOf("recognized") > 0) {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Welcome Home!");
display.display();
} else {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Unknown Person");
display.display();
}
} else {
Serial.println("Error on sending POST: " + String(httpResponseCode));
}
http.end();
}