#include <WiFi.h>
#include <ThingSpeak.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ThingSpeak API credentials
unsigned long channelID = 3062508; // ThingSpeak Channel ID
const char* apiKey = "Z2OZTWS7IAU08D91"; // ThingSpeak Write API Key
// Pin definitions
const int pirPin = 23; // PIR sensor connected to GPIO 23
const int redLedPin = 22; // Red LED for WiFi status
const int greenLedPin = 21; // Green LED for motion detection
const int oneWireBus = 0; // DS18B20 sensor data pin (GPIO 0)
// Setup OneWire and DallasTemperature instances
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
// LCD configuration
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust the I2C address if necessary
WiFiClient client; // WiFi client for ThingSpeak
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Initialize LCD
Wire.begin(18, 5); // SDA -> GPIO 18, SCL -> GPIO 5
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Connecting...");
// Initialize LEDs
pinMode(redLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
digitalWrite(redLedPin, LOW); // Red LED OFF initially
digitalWrite(greenLedPin, LOW); // Green LED OFF initially
// Initialize PIR sensor
pinMode(pirPin, INPUT);
// Initialize DS18B20 sensor
sensors.begin();
// Check if the DS18B20 sensor is found
uint8_t sensorAddress[8];
if (!sensors.getAddress(sensorAddress, 0)) {
Serial.println("DS18B20 not found!");
lcd.clear();
lcd.print("Temp Sensor Err");
while (1); // Halt execution if sensor not found
}
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(10);
Serial.println("Connecting to WiFi...");
lcd.setCursor(0, 1);
lcd.print("WiFi Connecting...");
digitalWrite(redLedPin, LOW); // Red LED OFF while connecting
}
Serial.println("Connected to WiFi!");
lcd.clear();
lcd.print("WiFi Connected");
digitalWrite(redLedPin, HIGH); // Red LED ON when connected
// Initialize ThingSpeak
ThingSpeak.begin(client);
delay(1000); // Wait for setup display
lcd.clear();
lcd.print("Setup Complete");
}
void loop() {
// Request temperature from DS18B20 sensor
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0); // Read the temperature from the first sensor
// Check if temperature reading is valid
if (temperature == -127.00) {
Serial.println("Failed to read temperature!");
temperature = 0.0; // Set to 0 if reading fails
}
// Display Temperature on LCD
lcd.setCursor(0, 0);
lcd.print("Temp: " + String(temperature) + "C");
// Read PIR sensor
int motionDetected = digitalRead(pirPin);
if (motionDetected) {
// Motion detected
Serial.println("Motion Detected!");
lcd.setCursor(0, 1);
lcd.print("Motion Detected!");
digitalWrite(greenLedPin, HIGH); // Turn on green LED
} else {
// No motion
lcd.setCursor(0, 1);
lcd.print("No Motion ");
digitalWrite(greenLedPin, LOW); // Turn off green LED
}
// Send data to ThingSpeak
ThingSpeak.setField(1, temperature); // Set temperature in Field 1
ThingSpeak.setField(2, motionDetected); // Set motion status in Field 2
int response = ThingSpeak.writeFields(channelID, apiKey); // Send data to ThingSpeak
if (response == 200) {
Serial.println("Data sent to ThingSpeak successfully!");
} else {
Serial.println("Error sending data to ThingSpeak: " + String(response));
}
delay(20000); // ThingSpeak allows updates every 15-20 seconds
}