#include <WiFi.h>
#include <HTTPClient.h>
#include <SD.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const String apiKey = "32AH0NSPZHF20RYI"; // ThingSpeak API key
const int chipSelect = 5; // SD card chip select pin
int motion = 0; // Motion state (0 = no motion, 1 = motion detected)
int door = 1; // Door state (0 = closed, 1 = open)
#define SCREEN_WIDTH 128 // Screen width
#define SCREEN_HEIGHT 64 // Screen height
#define OLED_RESET -1 // No reset pin
#define SSD1306_I2C_ADDRESS 0x3C // I2C address of the OLED screen
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); // OLED display object
void setup() {
Serial.begin(115200); // Start serial communication
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP()); // Print the IP address
// Initialize OLED screen
if (!display.begin(SSD1306_SWITCHCAPVCC, SSD1306_I2C_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Stop execution if screen fails to initialize
}
display.clearDisplay();
display.setTextSize(1); // Set text size
display.setTextColor(SSD1306_WHITE); // Set text color to white
display.setCursor(0, 0); // Set cursor to top-left corner
display.println(F("Starting..."));
display.display();
// Initialize SD card
if (!SD.begin(chipSelect)) {
Serial.println("SD card initialization failed!");
while (1); // Stop execution if SD card initialization fails
}
Serial.println("SD card initialized");
// Create or open log file
File dataFile = SD.open("/log.csv", FILE_WRITE);
if (dataFile) {
dataFile.println("Motion,Door"); // Write header if file is new
dataFile.close();
Serial.println("log.csv file is ready.");
} else {
Serial.println("Error: Could not create log.csv file.");
}
}
void loop() {
// Write data to log.csv
File dataFile = SD.open("/log.csv", FILE_APPEND);
if (dataFile) {
String data = String(motion) + "," + String(door);
dataFile.println(data); // Write data to the file
dataFile.close(); // Close the file
Serial.println("Data written to log.csv: Motion: " + String(motion) + ", Door: " + String(door));
} else {
Serial.println("Error: Could not open log.csv file for writing.");
}
// Send data to ThingSpeak
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = "http://api.thingspeak.com/update?api_key=" + apiKey +
"&field1=" + String(motion) + "&field2=" + String(door);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString(); // Get the response from ThingSpeak
Serial.println("Response: " + response);
}
http.end(); // End HTTP connection
}
// Update OLED screen with motion and door status
display.clearDisplay();
display.setCursor(0, 0);
// Display motion status
if (motion == 0) {
display.println("No motion detected");
} else {
display.println("Motion detected");
}
// Display door status
if (door == 0) {
display.println("Door is closed");
} else {
display.println("Door is open");
}
display.display(); // Update the display
// Simulate changing motion and door states (remove these when using actual sensors)
motion = (motion + 1) % 2; // Toggle motion state
door = (door + 1) % 2; // Toggle door state
delay(15000); // Wait for 15 seconds before repeating
}