techposts

DIY Smart Battery Powered Motion Sensor

Aug 31st, 2024
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.91 KB | Source Code | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. // Replace with your Wi-Fi credentials
  5. const char* ssid = "WiFi-SSID";
  6. const char* password = "WI-FI_PASSWORD";
  7.  
  8. // MQTT Server details
  9. const char* mqtt_server = "192.168.0.xxx";
  10. const char* mqtt_user = "USERNAME";
  11. const char* mqtt_password = "PASSWORD";
  12. const int mqtt_port = 1883;
  13.  
  14. // PIR Sensor
  15. int LED = LED_BUILTIN;          // Choose the pin for the LED
  16. int pirPin = 3;                 // Using the RX Pin as input from the PIR
  17. int val = 0;                    // Variable for reading the pin status
  18.  
  19. WiFiClient espClient;
  20. PubSubClient client(espClient);
  21.  
  22. void callback(char* topic, byte* payload, unsigned int length) {
  23.   Serial.print("Message arrived [");
  24.   Serial.print(topic);
  25.   Serial.print("] ");
  26.   for (unsigned int i = 0; i < length; i++) {
  27.     Serial.print((char)payload[i]);
  28.   }
  29.   Serial.println();
  30. }
  31.  
  32. void blink(int times, int delayTime) {
  33.   for (int j = 1; j <= times; j++) {
  34.     digitalWrite(LED, LOW);
  35.     delay(delayTime);
  36.     digitalWrite(LED, HIGH);
  37.     delay(delayTime);
  38.   }
  39. }
  40.  
  41. void connectToMQTT() {
  42.   while (!client.connected()) {
  43.     Serial.println("Connecting to MQTT server...");
  44.     if (client.connect("espClient", mqtt_user, mqtt_password)) {
  45.       Serial.println("Connected to MQTT server");
  46.       client.setCallback(callback);
  47.       client.subscribe("inTopic");
  48.     } else {
  49.       Serial.print("Failed to connect, rc=");
  50.       Serial.print(client.state());
  51.       Serial.println(" try again in 5 seconds");
  52.       delay(5000);
  53.     }
  54.   }
  55. }
  56.  
  57. void setup() {
  58.   pinMode(LED, OUTPUT);
  59.   digitalWrite(LED, HIGH);
  60.   pinMode(pirPin, INPUT);
  61.   Serial.begin(115200);
  62.   delay(10);
  63.  
  64.   WiFi.begin(ssid, password);
  65.   Serial.print("Connecting to WiFi");
  66.   while (WiFi.status() != WL_CONNECTED) {
  67.     delay(1000);
  68.     Serial.print(".");
  69.   }
  70.   Serial.println("\nConnected to WiFi");
  71.  
  72.   client.setServer(mqtt_server, mqtt_port);
  73.  
  74.   blink(3, 100);
  75.  
  76.   connectToMQTT();
  77. }
  78.  
  79. void loop() {
  80.   if (!client.connected()) {
  81.     connectToMQTT();
  82.   }
  83.   client.loop();
  84.  
  85.   val = digitalRead(pirPin);
  86.   Serial.print("PIR Sensor Value: ");
  87.   Serial.println(val); // Debug print for PIR sensor value
  88.  
  89.   if (val == HIGH) {
  90.     digitalWrite(LED, LOW);
  91.     Serial.println("Motion Detected!");
  92.     if (client.publish("MainEntrancePir/sensor", "Motion Detected!")) {
  93.       Serial.println("Motion Detected message sent");
  94.     } else {
  95.       Serial.println("Failed to send Motion Detected message");
  96.     }
  97.     delay(10000); // short delay before checking the PIR again
  98.   } else {
  99.     digitalWrite(LED, HIGH);
  100.     Serial.println("No Motion Detected.");
  101.     if (client.publish("MainEntrancePir/sensor", "No Motion Detected.")) {
  102.       Serial.println("No Motion Detected message sent");
  103.     } else {
  104.       Serial.println("Failed to send No Motion Detected message");
  105.     }
  106.     delay(2000);
  107.     blink(1, 200);
  108.     ESP.deepSleep(0);
  109.   }
  110. }
  111.  
Tags: smart Home
Advertisement
Add Comment
Please, Sign In to add comment