Advertisement
merkelck

esp8266-01 code for sonoff sensors

Jul 5th, 2020
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. /*********
  2. updated 8/12/9 kentm
  3. Complete project details at https://randomnerdtutorials.com
  4. adapted from multisensor NodeRed project. Each sketch addresses
  5. one modified sonoff basic device that must match "sonoffxxX"
  6. Search sonoffxxX and replace with current number (four)
  7. The "S" in the name indicates this a sensor
  8. modified for RCWL-0516 sensor and ESP826601
  9. Adjust total "ON" time in final loop statement in millis
  10. *********/
  11.  
  12. // Load libraries
  13. #include <ESP8266WiFi.h>
  14. #include <PubSubClient.h>
  15.  
  16.  
  17. // Replace with your network credentials
  18. const char* ssid = "xxxxxxx";
  19. const char* password = "xxxxxxxx";
  20.  
  21. // Change the variable to your Raspberry Pi IP address,
  22. // so it connects to your MQTT broker
  23. const char* mqtt_server = "xxx.xxx.x.x";
  24.  
  25.  
  26. // Initializes the espClient. You should change the
  27. // espClient name if you have multiple ESPs running
  28. // in your home automation system
  29. WiFiClient espClient;
  30. PubSubClient client(espClient);
  31.  
  32.  
  33. const int motionSensor = 2;
  34.  
  35. // Timers - Auxiliary variables
  36. unsigned long now = millis();
  37. unsigned long lastMeasure = 0;
  38. boolean startTimer = false;
  39. unsigned long currentTime = millis();
  40. unsigned long previousTime = 0;
  41.  
  42. // Don't change the function below.
  43. // This function connects your ESP8266 to your router
  44. void setup_wifi() {
  45. delay(10);
  46. // We start by connecting to a WiFi network
  47. Serial.println();
  48. Serial.print("Connecting to ");
  49. Serial.println(ssid);
  50. WiFi.begin(ssid, password);
  51. while (WiFi.status() != WL_CONNECTED) {
  52. delay(500);
  53. Serial.print(".");
  54. }
  55. Serial.println("");
  56. Serial.print("WiFi connected - ESP IP address: ");
  57. Serial.println(WiFi.localIP());
  58. }
  59.  
  60. // This functions is executed when some device publishes
  61. // a message to a topic that your ESP8266 is subscribed to
  62. // Change the function below to add logic to your program,
  63. // so when a device publishes a message to a topic that
  64. // your ESP8266 is subscribed you can actually do something
  65.  
  66. void callback(String topic, byte* message, unsigned int length) {
  67. Serial.print("Message arrived on topic: ");
  68. Serial.print(topic);
  69. Serial.print(". Message: ");
  70. String messageTemp;
  71.  
  72. for (int i = 0; i < length; i++) {
  73. Serial.print((char)message[i]);
  74. messageTemp += (char)message[i];
  75. }
  76. Serial.println();
  77.  
  78. Serial.println();
  79. }
  80.  
  81. // This functions reconnects your ESP8266 to your MQTT broker
  82. // Change the function below if you want to subscribe to
  83. // more topics with your ESP8266
  84. void reconnect() {
  85. // Loop until we're reconnected
  86. while (!client.connected()) {
  87. Serial.print("Attempting MQTT connection...");
  88. // Create a random client ID
  89. String clientId = "ESP8266Client-";
  90. clientId += String(random(0xffff), HEX);
  91. // Attempt to connect
  92. if (client.connect(clientId.c_str())) {
  93. Serial.println("connected");
  94. // Subscribe or resubscribe to a topic
  95. // You can subscribe to more topics (to control more outputs)
  96. client.subscribe("esp8266/output");
  97. } else {
  98. Serial.print("failed, rc=");
  99. Serial.print(client.state());
  100. Serial.println(" try again in 5 seconds");
  101. // Wait 5 seconds before retrying
  102. delay(5000);
  103. }
  104. }
  105. }
  106.  
  107. // Checks if motion was detected and the sensors are armed.
  108. // Then, starts a timer.
  109. void detectsMovement() {
  110. Serial.println("MOTION DETECTED!");
  111. client.publish("esp8266/motion", "MOTION DETECTED-xxX!");
  112. client.publish("home/office/sonoff-xxX","on");
  113. previousTime = millis();
  114. startTimer = true;
  115. }
  116.  
  117. void setup() {
  118.  
  119.  
  120. // Serial port for debugging purposes
  121. Serial.begin(115200);
  122.  
  123. // RDR Motion Sensor mode, then set interrupt function and RISING mode
  124. pinMode(motionSensor, INPUT);
  125. attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);
  126.  
  127.  
  128. setup_wifi();
  129. client.setServer(mqtt_server, 1883);
  130. client.setCallback(callback);
  131. }
  132.  
  133. void loop() {
  134. if (!client.connected()) {
  135. reconnect();
  136. }
  137. client.loop();
  138.  
  139. // Timer variable with current time
  140. now = millis();
  141.  
  142. // After 150 seconds have passed since motion was detected,
  143. // publishes a "No motion" message
  144. if ((now - previousTime > 150000)&& startTimer) {
  145. client.publish("esp8266/motion", "NO MOTION-xxX");
  146. client.publish("home/office/sonoffxxX","off");
  147. Serial.println("Motion stopped");
  148. startTimer = false;
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement