merkelck

sonoff remote control

Mar 1st, 2019
91
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Code for remote device to control one sonoff basic switch at a time.
  2. The correct name must be entered in the publish and subscribe statements.
  3. The commented pin numbers were used for a ESP8266-01 board
  4. The current board is a Wemos D1 mini pro
  5. This code is largely the work of individuals on the Arduino forum.
  6. Without their help, it would not exist.
  7. Kentm
  8. */
  9.  
  10.  
  11. #include <ESP8266WiFi.h>
  12. #include <PubSubClient.h>
  13.  
  14. const char* ssid = ".....";
  15. const char* password = ".......";
  16. const char* mqttServer = "192.168.1....";
  17. const int mqttPort = 1883;
  18.  
  19. #define mqtt_publish "home/office/sonoff1"
  20. #define mqtt_subscribe "home/office/sonoff1"
  21. #define mqtt_sub1 "on"
  22. #define mqtt_sub2 "off"
  23.  
  24. #define LED 5 // D1(gpio5)
  25. #define BUTTON 4 //D2(gpio4)
  26.  
  27. bool lightsOn = false;
  28.  
  29. WiFiClient espClient;
  30. PubSubClient client(espClient);
  31.  
  32. void setup() {
  33.  
  34. Serial.begin(115200);
  35. pinMode(BUTTON, INPUT_PULLUP); // push button
  36. pinMode(LED, OUTPUT); // anything you want to control using a switch e.g. a Led
  37. delay(500);
  38.  
  39. Serial.print("Connecting to WiFi...");
  40. WiFi.begin(ssid, password);
  41. while (WiFi.status() != WL_CONNECTED)
  42. {
  43. delay(500);
  44. }
  45. Serial.print("Connected to ");
  46. Serial.println(ssid);
  47.  
  48. client.setServer(mqttServer, mqttPort);
  49. client.setCallback(callback);
  50.  
  51. Serial.print("Connecting to MQTT...");
  52. while (!client.connected())
  53. {
  54.  
  55. if (client.connect("ESP8266Client" ))
  56. {
  57. Serial.println("connected");
  58. }
  59. else
  60. {
  61. Serial.print("failed with state ");
  62. Serial.println(client.state());
  63. delay(2000);
  64.  
  65. }
  66. }
  67.  
  68. client.subscribe(mqtt_subscribe);
  69. client.publish(mqtt_publish, "Remote");
  70. }
  71. void loop ()
  72. {
  73. client.loop(); // MQTT houskeeping
  74.  
  75. if(digitalRead(BUTTON) == LOW) // Button pressed (LOW)
  76. {
  77. lightsOn = !lightsOn; // Toggle light state
  78.  
  79. if(lightsOn)
  80. {
  81. client.publish(mqtt_publish, mqtt_sub1);
  82. }
  83. else
  84. {
  85. client.publish(mqtt_publish, mqtt_sub2);
  86. }
  87.  
  88. delay(50); // Switch debounce
  89. while(digitalRead(BUTTON) == LOW) // Wait for button to be released
  90. {
  91. delay(10); // Short delay to yield else WDT reset will happen
  92. }
  93. }
  94. delay(100);
  95. }
  96.  
  97.  
  98. void callback(char* topic, byte * data, unsigned int length)// Callback for subscribed MQTT topics
  99. {
  100. Serial.print(topic);
  101. Serial.print(": ");
  102. for (int i = 0; i < length; i++)
  103. {
  104. Serial.print((char)data[i]);
  105. }
  106. Serial.println();
  107.  
  108.  
  109. if (strncmp(topic, mqtt_subscribe, strlen(mqtt_subscribe)) == 0) // Check it's the right topic
  110. {
  111. if (strncmp((char*)data, mqtt_sub1, strlen(mqtt_sub1)) == 0) // Check it's the right data
  112. {
  113. lightsOn = true;
  114. }
  115.  
  116. if (strncmp((char*)data, mqtt_sub2, strlen(mqtt_sub2)) == 0) // Check it's the right data
  117. {
  118. lightsOn = false;
  119. }
  120. }
  121. digitalWrite(LED, lightsOn);
  122.  
  123. }
RAW Paste Data