#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <ThingSpeak.h>
// WiFi settings
const char* ssid = "Wokwi-GUEST"; // your network SSID (name)
const char* password = ""; // your network password
// ThingSpeak settings
unsigned long myChannelNumber = 2552556; // Replace with your channel number
const char* myWriteAPIKey = "I2GWMDGC2QIDT4QR"; // Replace with your Write API Key
// Define the pins for the IR sensor, buzzer, button, and LED
int IRsensor_pin = 12;
int buzzer_pin = 14; // GPIO pin for the buzzer
int button_pin = 17; // GPIO pin for the button
// Address for the I2C LCD
const int lcdAddr = 0x27; // Change this address if your module has a different address
// Number of columns and rows in the LCD
const int lcdColumns = 16;
const int lcdRows = 2;
// Initialize the LCD
LiquidCrystal_I2C lcd(lcdAddr, lcdColumns, lcdRows);
// Initialize WiFi client
WiFiClient client;
int motion_detected;
void setup() {
Serial.begin(115200);
// Initialize the pins
pinMode(IRsensor_pin, INPUT);
pinMode(buzzer_pin, OUTPUT);
pinMode(button_pin, INPUT_PULLUP); // Enable internal pull-up resistor
// Initialize the I2C communication
Wire.begin();
// Initialize the LCD
lcd.init();
lcd.backlight(); // Turn on the backlight
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
// Initialize ThingSpeak
ThingSpeak.begin(client);
}
void loop() {
// Read the flame sensor
if (digitalRead(IRsensor_pin) == 1) { // Flame detected
digitalWrite(buzzer_pin, HIGH); // Activate the buzzer
delay(2000); // Keep the buzzer on for 2 seconds
motion_detected = 1;
// Display "Flame detected" on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Motion detected");
} else { // No flame detected
digitalWrite(buzzer_pin, LOW); // Turn off the buzzer
motion_detected = 0;
// Clear the LCD
lcd.clear();
}
// Alternate sent data **Remove for actual
if (motion_detected == 0) {
motion_detected == 1;
} else {
motion_detected == 0;
}
// Read the button state
if (digitalRead(button_pin) == HIGH) { // Button pressed
if (digitalRead(buzzer_pin) == HIGH) {
digitalWrite(buzzer_pin, LOW); // Turn on the LED
}
}
// Send data to ThingSpeak
ThingSpeak.setField(1, motion_detected); // Update the field with flame sensor data
Serial.print("Motion Detection: ");
Serial.println(motion_detected);
// Write to ThingSpeak
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (x == 200) {
Serial.println("Channel update successful.");
} else {
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
delay(5000);
}