Skip to content

Instantly share code, notes, and snippets.

@ElvisAns
Last active October 13, 2024 05:48
Show Gist options
  • Save ElvisAns/a966d01826dcd3344828a6e3ef0d8c7c to your computer and use it in GitHub Desktop.
Save ElvisAns/a966d01826dcd3344828a6e3ef0d8c7c to your computer and use it in GitHub Desktop.
Smart light
#include <SoftwareSerial.h>
// Pin configuration
const int ldrPin = A0; // Pin for the LDR detecting flickering
const int dayNightLdrPin = A1; // Pin for the LDR detecting day/night
const int vibrationPin = 2; // Pin for the vibration sensor
const int currentSensorPin = A2; // Pin for the current sensor
const int relayPin = 3; // Pin for the relay to control the lamp
// Sensor variables
float thresholdCurrent = 0.1; // Current threshold (in Amperes)
int ldrThreshold = 700; // Threshold for flickering (adjust as needed)
int dayNightThreshold = 300; // LDR value threshold to determine day/night
int flickerCount = 0; // Counter for LDR changes
const int flickerThreshold = 10; // Number of changes to detect flickering
unsigned long lastFlickerCheck = 0;
const unsigned long flickerCheckInterval = 1000; // Check interval in milliseconds
// Alert flags
volatile bool vibrationDetected = false;
volatile bool currentProblemDetected = false;
volatile bool flickeringDetected = false;
bool isNight = false;
// GSM and GPS module configuration
SoftwareSerial sim800l(7, 8); // RX, TX for GSM module
SoftwareSerial gps(4, 3); // RX, TX for GPS module
void setup() {
Serial.begin(9600);
sim800l.begin(9600);
gps.begin(9600);
Serial.println("Initializing...");
delay(1000);
Serial.println("Checking...");
delay(1000);
sim800l.println("AT"); //Once the handshake test is successful, it will back to OK
updateSerial();
pinMode(vibrationPin, INPUT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Ensure the relay is off initially
attachInterrupt(digitalPinToInterrupt(vibrationPin), detectVibration, FALLING); // when vibration goes from high to low then there is a vibration
}
void loop() {
// Read the ambient light level
int dayNightValue = analogRead(dayNightLdrPin);
isNight = dayNightValue < dayNightThreshold;
// Only run detection logic if it's night
if (isNight) {
// Read current
float current = readCurrent();
if (current < thresholdCurrent) {
if (!currentProblemDetected) {
currentProblemDetected = true;
Serial.println("Problem detected: Low current");
}
} else {
currentProblemDetected = false;
}
// Read LDR for flickering detection
int ldrValue = analogRead(ldrPin);
if (ldrValue > ldrThreshold) {
flickerCount++;
}
// Check flickering every second
if (millis() - lastFlickerCheck >= flickerCheckInterval) {
lastFlickerCheck = millis();
if (flickerCount >= flickerThreshold) {
flickeringDetected = true;
Serial.println("Flickering detected");
sendSMS("Flickering detected");
}
flickerCount = 0; // Reset the counter
}
// Read GPS and send coordinates if a problem is detected
if (Serial.available()) {
String gpsData = readGPS();
if (gpsData.length() > 0) {
if (vibrationDetected || currentProblemDetected || flickeringDetected) {
sendSMS("GPS Coordinates: " + gpsData);
}
}
}
// Send SMS based on flags
if (currentProblemDetected) {
sendSMS("Problem detected: Low current");
}
if (flickeringDetected) {
flickeringDetected = false; // Reset after sending SMS
}
if (vibrationDetected) {
sendSMS("Vibration detected!");
vibrationDetected = false; // Reset after sending SMS
}
} else {
// Turn off the relay if it's day
digitalWrite(relayPin, LOW);
}
delay(1000); // Wait 1 second before the next read
}
// Function to read current
float readCurrent() {
int sensorValue = analogRead(currentSensorPin);
float voltage = sensorValue * (5.0 / 1023.0);
float current = (voltage - 2.5) / 0.185; // ACS712 sensitivity 185mV/A
return current;
}
// Function to detect vibrations
void detectVibration() {
vibrationDetected = true;
}
// Function to send an SMS
void sendSMS(String message) {
sim800l.println("AT+CMGF=1"); // Set SMS mode to text
delay(1000);
sim800l.println("AT+CMGS=\"+1234567890\""); // Replace with your phone number
delay(1000);
sim800l.println(message); // SMS content
delay(1000);
sim800l.write(26); // End of SMS character
updateSerial();
delay(1000);
Serial.println("SMS sent: " + message);
}
// Function to read GPS data
String readGPS() {
String gpsData = "";
while (gps.available()) {
char c = gps.read();
gpsData += c;
if (c == '\n') {
break;
}
}
return gpsData;
}
void updateSerial() {
delay(500);
while (Serial.available()) {
sim800l.write(Serial.read()); //Forward what Serial received to Software Serial Port
}
while (sim800l.available()) {
Serial.write(sim800l.read()); //Forward what Software Serial received to Serial Port
}
}
@ElvisAns
Copy link
Author

Schematic_Smart-Light_2024-08-23 (1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment