Advertisement
Guest User

Untitled

a guest
Dec 16th, 2016
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. /*
  2. MQTT Binary Sensor - Motion (PIR) for Home-Assistant - NodeMCU (ESP8266)
  3. https://home-assistant.io/components/binary_sensor.mqtt/
  4. Libraries :
  5. - ESP8266 core for Arduino : https://github.com/esp8266/Arduino
  6. - PubSubClient : https://github.com/knolleary/pubsubclient
  7. Sources :
  8. - File > Examples > ES8266WiFi > WiFiClient
  9. - File > Examples > PubSubClient > mqtt_auth
  10. - File > Examples > PubSubClient > mqtt_esp8266
  11. - https://learn.adafruit.com/pir-passive-infrared-proximity-motion-sensor/using-a-pir
  12. Schematic :
  13. - https://github.com/mertenats/open-home-automation/blob/master/ha_mqtt_binary_sensor_pir/Schematic.png
  14. - PIR leg 1 - VCC
  15. - PIR leg 2 - D1/GPIO5
  16. - PIR leg 3 - GND
  17.  
  18. Configuration (HA) :
  19. binary_sensor:
  20. platform: mqtt
  21. state_topic: 'office/motion/status'
  22. name: 'Motion'
  23. sensor_class: motion
  24.  
  25. TODO :
  26. - Use the interrupts instead of constinously polling the sensor
  27. Samuel M. - v1.1 - 08.2016
  28. If you like this example, please add a star! Thank you!
  29. https://github.com/mertenats/open-home-automation
  30. */
  31.  
  32. #include <ESP8266WiFi.h>
  33. #include <PubSubClient.h>
  34.  
  35. #define MQTT_VERSION MQTT_VERSION_3_1_1
  36.  
  37. // Wifi: SSID and password
  38. const PROGMEM char* WIFI_SSID = "mtikdev";
  39. const PROGMEM char* WIFI_PASSWORD = "poiuytrewq";
  40.  
  41. // MQTT: ID, server IP, port, username and password
  42. const PROGMEM char* MQTT_CLIENT_ID = "test_motion";
  43. const PROGMEM char* MQTT_SERVER_IP = "192.168.88.88";
  44. const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
  45. const PROGMEM char* MQTT_USER = "pi";
  46. const PROGMEM char* MQTT_PASSWORD = "raspberry";
  47.  
  48. // MQTT: topic
  49. const PROGMEM char* MQTT_MOTION_STATUS_TOPIC = "test/motion/status";
  50.  
  51. // default payload
  52. const PROGMEM char* MOTION_ON = "ON";
  53. const PROGMEM char* MOTION_OFF = "OFF";
  54.  
  55. // PIR : D1/GPIO5
  56. const PROGMEM uint8_t PIR_PIN = 5;
  57. uint8_t m_pir_state = LOW; // no motion detected
  58. uint8_t m_pir_value = 0;
  59.  
  60. WiFiClient wifiClient;
  61. PubSubClient client(wifiClient);
  62.  
  63. // function called to publish the state of the pir sensor
  64. void publishPirSensorState() {
  65. if (m_pir_state) {
  66. client.publish(MQTT_MOTION_STATUS_TOPIC, MOTION_OFF, true);
  67. } else {
  68. client.publish(MQTT_MOTION_STATUS_TOPIC, MOTION_ON, true);
  69. }
  70. }
  71.  
  72. // function called when a MQTT message arrived
  73. void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
  74. }
  75.  
  76. void reconnect() {
  77. // Loop until we're reconnected
  78. while (!client.connected()) {
  79. Serial.print("INFO: Attempting MQTT connection...");
  80. // Attempt to connect
  81. if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
  82. Serial.println("INFO: connected");
  83. } else {
  84. Serial.print("ERROR: failed, rc=");
  85. Serial.print(client.state());
  86. Serial.println("DEBUG: try again in 5 seconds");
  87. // Wait 5 seconds before retrying
  88. delay(5000);
  89. }
  90. }
  91. }
  92.  
  93. void setup() {
  94. // init the serial
  95. Serial.begin(115200);
  96.  
  97. // init the WiFi connection
  98. Serial.println();
  99. Serial.println();
  100. Serial.print("INFO: Connecting to ");
  101. Serial.println(WIFI_SSID);
  102. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  103.  
  104. while (WiFi.status() != WL_CONNECTED) {
  105. delay(500);
  106. Serial.print(".");
  107. }
  108.  
  109. Serial.println("");
  110. Serial.println("INFO: WiFi connected");
  111. Serial.println("INFO: IP address: ");
  112. Serial.println(WiFi.localIP());
  113.  
  114. // init the MQTT connection
  115. client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
  116. client.setCallback(callback);
  117. }
  118.  
  119. void loop() {
  120. if (!client.connected()) {
  121. reconnect();
  122. }
  123. client.loop();
  124.  
  125. // read the PIR sensor
  126. m_pir_value = digitalRead(PIR_PIN);
  127. if (m_pir_value == HIGH) {
  128. if (m_pir_state == LOW) {
  129. // a motion is detected
  130. Serial.println("INFO: Motion detected");
  131. publishPirSensorState();
  132. m_pir_state = HIGH;
  133. }
  134. } else {
  135. if (m_pir_state == HIGH) {
  136. publishPirSensorState();
  137. Serial.println("INFO: Motion ended");
  138. m_pir_state = LOW;
  139. }
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement