Advertisement
baldengineer

MQTT PIR Example

Apr 27th, 2017
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.98 KB | None | 0 0
  1. // James Lewis
  2. // PIR with MQTT Example, using millis()
  3. // www.baldengineer.com for more information
  4.  
  5. #include <ESP8266WiFi.h>
  6. #include <PubSubClient.h>
  7.  
  8. #define MOTION 1
  9. #define STILL 0
  10.  
  11. // Update these with values suitable for your network.
  12. // oOoooooo Secrets!!!!!
  13. const char* ssid = "";
  14. const char* password = "";
  15. const char* mqtt_server = "rasppib";
  16.  
  17. WiFiClient espClient;
  18. PubSubClient client(espClient);
  19. unsigned long lastMsg = 0;
  20. char msg[50];
  21. int value = 0;
  22.  
  23. int redLED = 0;
  24. int blueLED = 2;
  25.  
  26. int pirPin = 12;
  27.  
  28. void setup_wifi() {
  29.  
  30.   delay(10);
  31.   // We start by connecting to a WiFi network
  32.   Serial.println();
  33.   Serial.print("Connecting to ");
  34.   Serial.println(ssid);
  35.  
  36.   WiFi.begin(ssid, password);
  37.  
  38.   while (WiFi.status() != WL_CONNECTED) {
  39.     delay(500);
  40.     Serial.print(".");
  41.   }
  42.  
  43.   Serial.println("");
  44.   Serial.println("WiFi connected");
  45.   Serial.println("IP address: ");
  46.   Serial.println(WiFi.localIP());
  47. }
  48.  
  49. void callback(char* topic, byte* payload, unsigned int length) {
  50.   Serial.print("Message arrived [");
  51.   Serial.print(topic);
  52.   Serial.print("] ");
  53.   for (int i = 0; i < length; i++) {
  54.     Serial.print((char)payload[i]);
  55.   }
  56.   Serial.println();
  57. }
  58.  
  59. void reconnect() {
  60.   // Loop until we're reconnected
  61.   while (!client.connected()) {
  62.     Serial.print("Attempting MQTT connection...");
  63.     // Attempt to connect
  64.     if (client.connect("ESP8266Client")) {
  65.       Serial.println("connected");
  66.       // Once connected, publish an announcement...
  67.       client.publish("console", "PIR 1 Reporting for duty. huhuh duty.");
  68.       digitalWrite(redLED, HIGH);
  69.       // ... and resubscribe
  70.       // client.subscribe("inTopic");
  71.     } else {
  72.       digitalWrite(redLED, LOW);
  73.       Serial.print("failed, rc=");
  74.       Serial.print(client.state());
  75.       Serial.println(" try again in 5 seconds");
  76.       // Wait 5 seconds before retrying
  77.       delay(5000);
  78.     }
  79.   }
  80. }
  81.  
  82. bool pirLastStatus = LOW;
  83.  
  84. void setup() {
  85.   pinMode(redLED, OUTPUT);
  86.   pinMode(blueLED, OUTPUT);
  87.   pinMode(pirPin, INPUT);
  88.   digitalWrite(redLED, LOW);
  89.   digitalWrite(blueLED, LOW);
  90.   Serial.begin(115200);
  91.   setup_wifi();
  92.   client.setServer(mqtt_server, 1883);
  93.   client.setCallback(callback);
  94. }
  95.  
  96.  
  97. void loop() {
  98.   if (!client.connected()) {
  99.     reconnect();
  100.   }
  101.   client.loop();
  102.  
  103.   int pirStatus = digitalRead(pirPin);
  104.   digitalWrite(blueLED, !pirStatus);
  105.  
  106.   unsigned long now = millis();
  107.  
  108.   if (pirStatus != pirLastStatus) {
  109.     // a change occured, interrupt
  110.     if (pirStatus == MOTION)
  111.       sprintf(msg, "MOTION");
  112.     else
  113.       sprintf(msg, "STILL");
  114.  
  115.     Serial.print("Publish message: ");
  116.     Serial.println(msg);
  117.     client.publish("pir1Status", msg);
  118.  
  119.     pirLastStatus = pirStatus;
  120.     lastMsg = now;
  121.   }
  122.  
  123.   if (now - lastMsg > 5000) {
  124.     lastMsg = now;
  125.     //snprintf (msg, 75, "", value,);
  126.     sprintf(msg, "IDLE");
  127.  
  128.     Serial.print("Publish message: ");
  129.     Serial.println(msg);
  130.     client.publish("pir1Status", msg);
  131.   }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement