SHARE
TWEET

Code for using a Sonoff S20 for indication on the LEDs

hanpa Jul 31st, 2017 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2. #include <ESP8266WiFi.h>
  3. #include <ESP8266mDNS.h>
  4. #include "Adafruit_MQTT.h"
  5. #include "Adafruit_MQTT_Client.h"
  6. #include <ArduinoOTA.h>
  7. #include "/Users/hanpa/Dropbox/Hem/Arduino/common/credentials.h"
  8.  
  9. const char* version   = "S20Indicator 1.0";
  10. const char* date_name = "2017-07-30 Hans Palm";
  11.  
  12. // Hans Palm 2017
  13. // http://www.hobbyelektronik.nu/
  14. //
  15. // This code uses the green LED and blue LED on a Sonoff S20 for indication purposes
  16. // Modes can be controlled using MQTT. Note that the blue LED is connected to the same GPIO as the relay
  17. //
  18. // Currently implemented modes:
  19. // 0 - no light
  20. // 1 - fast blinking
  21. // 2 - slow blinking
  22. //
  23. // E.g:
  24. //   mosquitto_pub -h localhost -t "S20Indicator1/greenBlinkMode" -m "1" #fast blink green
  25. //   mosquitto_pub -h localhost -t "S20Indicator1/greenBlinkMode" -m "0" #green off
  26. //
  27. // Credentials are assumed to be stated in a #include(d) .h file or stated directly below instead
  28. // of constant names for ssid, password, mqtt_server and mqtt_port
  29. //
  30. // OTA support included
  31.  
  32. /************************* WiFi Access Point *********************************/
  33.  
  34. #define WLAN_SSID       ssid
  35. #define WLAN_PASS       password
  36.  
  37. /***************************** MQTT settings *********************************/
  38. #define SERVER      mqtt_server
  39. #define SERVERPORT  mqtt_port              // use 8883 for SSL
  40. #define USERNAME    ""                     // Not used, from Adafruit.io Setup
  41. #define KEY         ""                     // Not used, from Adafruit.io Setup
  42.  
  43. /************************* OTA *********************************/
  44. #define OTA_HOST_NAME   "S20Indicator1"        // Ej _ i hostname!
  45. const char* host = OTA_HOST_NAME;
  46.  
  47. // The follwing parameters are only required for fixed IP, otherwise uncomment, also uncomment WiFi.config(ip, gateway, subnet)
  48. //IPAddress ip(192, 168, 0, TBD);
  49. IPAddress gateway(192, 168, 0, 1);
  50. IPAddress subnet(255, 255, 255, 0);
  51.  
  52. // Types and variables for S20 indication
  53. int8_t bluePin  = 12; // GPIO12, Also controls the relay
  54. int8_t greenPin = 13; // GPIO13
  55.  
  56. enum blinkPattern : uint32_t {
  57.   OFF,             // 0
  58.   fastBlinking,    // 1
  59.   slowBlinking     // 2
  60. };
  61.  
  62. blinkPattern greenBlinkMode = OFF;
  63. blinkPattern blueBlinkMode  = OFF;
  64.  
  65. // Create an ESP8266 WiFiClient class to connect to the MQTT server.
  66. WiFiClient client;
  67. // or... use WiFiFlientSecure for SSL
  68. //WiFiClientSecure client;
  69.  
  70. // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
  71. Adafruit_MQTT_Client mqtt(&client, SERVER, SERVERPORT, USERNAME, KEY);
  72.  
  73. /****************************** Feeds ***************************************/
  74.  
  75. // Setup for subscribed feeds.
  76. Adafruit_MQTT_Subscribe greenblinkmodevalue = Adafruit_MQTT_Subscribe(&mqtt, USERNAME "S20Indicator1/greenBlinkMode");
  77. Adafruit_MQTT_Subscribe blueblinkmodevalue = Adafruit_MQTT_Subscribe(&mqtt, USERNAME "S20Indicator1/blueBlinkMode");
  78.  
  79. void MQTT_connect();
  80.  
  81.  
  82. void greenblinkmodevaluecallback(uint32_t x) {
  83.   //Serial.print("The ordered led1 colour value is: ");
  84.   //Serial.println(x);
  85.   //led1Colour = x;
  86.   greenBlinkMode = blinkPattern (x);
  87. }
  88. void blueblinkmodevaluecallback(uint32_t x) {
  89.   //Serial.print("The ordered led1 colour value is: ");
  90.   //Serial.println(x);
  91.   //led1Colour = x;
  92.   blueBlinkMode = blinkPattern (x);
  93. }
  94.  
  95. void OTA_setup() {
  96.   ArduinoOTA.setHostname(host);
  97.  
  98.   ArduinoOTA.onStart([]() {
  99.     String type;
  100. //    if (ArduinoOTA.getCommand() == U_FLASH)
  101.       type = "sketch";
  102. //    else // U_SPIFFS
  103. //      type = "filesystem";
  104.  
  105.     // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
  106.     Serial.println("Start updating " + type);
  107.   });
  108.   ArduinoOTA.onEnd([]() {
  109.     Serial.println("\nEnd");
  110.   });
  111.   ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  112.     Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  113.   });
  114.   ArduinoOTA.onError([](ota_error_t error) {
  115.     Serial.printf("Error[%u]: ", error);
  116.     if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
  117.     else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
  118.     else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
  119.     else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
  120.     else if (error == OTA_END_ERROR) Serial.println("End Failed");
  121.   });
  122.   ArduinoOTA.begin();
  123. }
  124.  
  125. void setGreenLED(uint8_t value) {
  126.   digitalWrite(greenPin, !value);
  127. }
  128.  
  129. void setBlueLEDandRelay(uint8_t value) {
  130.   digitalWrite(bluePin, value);
  131. }
  132.  
  133. void setup() {
  134.   Serial.begin(115200);
  135.   delay(10);
  136.  
  137.   // Set up S20 pins
  138.   pinMode(bluePin, OUTPUT);
  139.   pinMode(greenPin, OUTPUT);
  140.  
  141.   setBlueLEDandRelay(1);
  142.   setGreenLED(1);
  143.  
  144.   // Connect to WiFi access point.
  145.   Serial.println(); Serial.println();
  146.   Serial.print("Connecting to ");
  147.   Serial.println(WLAN_SSID);
  148.  
  149.   //WiFi.config(ip, gateway, subnet); // Only required for fixed IP
  150.   WiFi.begin(WLAN_SSID, WLAN_PASS);
  151.   while (WiFi.status() != WL_CONNECTED) {
  152.     delay(500);
  153.     Serial.print(".");
  154.   }
  155.   Serial.println();
  156.  
  157.   Serial.println("WiFi connected");
  158.   Serial.println("IP address: "); Serial.println(WiFi.localIP());
  159.  
  160.   OTA_setup();
  161.  
  162.   // Setup MQTT subscriptions
  163.   greenblinkmodevalue.setCallback(greenblinkmodevaluecallback);
  164.   mqtt.subscribe(&greenblinkmodevalue);
  165.   blueblinkmodevalue.setCallback(blueblinkmodevaluecallback);
  166.   mqtt.subscribe(&blueblinkmodevalue);
  167.  
  168.   setBlueLEDandRelay(0);
  169.   setGreenLED(0);
  170. }
  171.  
  172. void loop() {
  173.   ArduinoOTA.handle();
  174.  
  175.   // Ensure the connection to the MQTT server is alive (this will make the first
  176.   // connection and automatically reconnect when disconnected).  See the MQTT_connect
  177.   // function definition further below.
  178.   MQTT_connect();
  179.  
  180.   // Process mqtt data
  181.   mqtt.processPackets(10);
  182.  
  183.   // ping the server to keep the mqtt connection alive
  184.   // NOT required if you are publishing once every KEEPALIVE seconds
  185.   /*
  186.   if(! mqtt.ping()) {
  187.   mqtt.disconnect();
  188. }
  189. */
  190.  
  191. switch (greenBlinkMode) {
  192.   case OFF:
  193.   setGreenLED(0);
  194.   break;
  195.   case fastBlinking:
  196.   setGreenLED( (millis()/100) % 2);
  197.   break;
  198.   case slowBlinking:
  199.   setGreenLED( (millis()/1000) % 2);
  200.   break;
  201. }
  202.  
  203. switch (blueBlinkMode) {
  204.   case OFF:
  205.   setBlueLEDandRelay(0);
  206.   break;
  207.   case fastBlinking:
  208.   setBlueLEDandRelay( (millis()/100) % 2);
  209.   break;
  210.   case slowBlinking:
  211.   setBlueLEDandRelay( (millis()/1000) % 2);
  212.   break;
  213. }
  214.  
  215. }
  216.  
  217. // Function to connect and reconnect as necessary to the MQTT server.
  218. // Should be called in the loop function and it will take care if connecting.
  219. void MQTT_connect() {
  220.   int8_t ret;
  221.  
  222.   // Stop if already connected.
  223.   if (mqtt.connected()) {
  224.     return;
  225.   }
  226.  
  227.   Serial.print("Connecting to MQTT... ");
  228.  
  229.   while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
  230.     Serial.println(mqtt.connectErrorString(ret));
  231.     Serial.println("Retrying MQTT connection in 5 seconds...");
  232.     mqtt.disconnect();
  233.     delay(5000);  // wait 5 seconds
  234.   }
  235.   Serial.println("MQTT Connected!");
  236. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top