Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <ESP8266WiFi.h>
- #include <ESP8266mDNS.h>
- #include "Adafruit_MQTT.h"
- #include "Adafruit_MQTT_Client.h"
- #include <ArduinoOTA.h>
- #include "/Users/hanpa/Dropbox/Hem/Arduino/common/credentials.h"
- const char* version = "S20Indicator 1.0";
- const char* date_name = "2017-07-30 Hans Palm";
- // Hans Palm 2017
- // http://www.hobbyelektronik.nu/
- //
- // This code uses the green LED and blue LED on a Sonoff S20 for indication purposes
- // Modes can be controlled using MQTT. Note that the blue LED is connected to the same GPIO as the relay
- //
- // Currently implemented modes:
- // 0 - no light
- // 1 - fast blinking
- // 2 - slow blinking
- //
- // E.g:
- // mosquitto_pub -h localhost -t "S20Indicator1/greenBlinkMode" -m "1" #fast blink green
- // mosquitto_pub -h localhost -t "S20Indicator1/greenBlinkMode" -m "0" #green off
- //
- // Credentials are assumed to be stated in a #include(d) .h file or stated directly below instead
- // of constant names for ssid, password, mqtt_server and mqtt_port
- //
- // OTA support included
- /************************* WiFi Access Point *********************************/
- #define WLAN_SSID ssid
- #define WLAN_PASS password
- /***************************** MQTT settings *********************************/
- #define SERVER mqtt_server
- #define SERVERPORT mqtt_port // use 8883 for SSL
- #define USERNAME "" // Not used, from Adafruit.io Setup
- #define KEY "" // Not used, from Adafruit.io Setup
- /************************* OTA *********************************/
- #define OTA_HOST_NAME "S20Indicator1" // Ej _ i hostname!
- const char* host = OTA_HOST_NAME;
- // The follwing parameters are only required for fixed IP, otherwise uncomment, also uncomment WiFi.config(ip, gateway, subnet)
- //IPAddress ip(192, 168, 0, TBD);
- IPAddress gateway(192, 168, 0, 1);
- IPAddress subnet(255, 255, 255, 0);
- // Types and variables for S20 indication
- int8_t bluePin = 12; // GPIO12, Also controls the relay
- int8_t greenPin = 13; // GPIO13
- enum blinkPattern : uint32_t {
- OFF, // 0
- fastBlinking, // 1
- slowBlinking // 2
- };
- blinkPattern greenBlinkMode = OFF;
- blinkPattern blueBlinkMode = OFF;
- // Create an ESP8266 WiFiClient class to connect to the MQTT server.
- WiFiClient client;
- // or... use WiFiFlientSecure for SSL
- //WiFiClientSecure client;
- // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
- Adafruit_MQTT_Client mqtt(&client, SERVER, SERVERPORT, USERNAME, KEY);
- /****************************** Feeds ***************************************/
- // Setup for subscribed feeds.
- Adafruit_MQTT_Subscribe greenblinkmodevalue = Adafruit_MQTT_Subscribe(&mqtt, USERNAME "S20Indicator1/greenBlinkMode");
- Adafruit_MQTT_Subscribe blueblinkmodevalue = Adafruit_MQTT_Subscribe(&mqtt, USERNAME "S20Indicator1/blueBlinkMode");
- void MQTT_connect();
- void greenblinkmodevaluecallback(uint32_t x) {
- //Serial.print("The ordered led1 colour value is: ");
- //Serial.println(x);
- //led1Colour = x;
- greenBlinkMode = blinkPattern (x);
- }
- void blueblinkmodevaluecallback(uint32_t x) {
- //Serial.print("The ordered led1 colour value is: ");
- //Serial.println(x);
- //led1Colour = x;
- blueBlinkMode = blinkPattern (x);
- }
- void OTA_setup() {
- ArduinoOTA.setHostname(host);
- ArduinoOTA.onStart([]() {
- String type;
- // if (ArduinoOTA.getCommand() == U_FLASH)
- type = "sketch";
- // else // U_SPIFFS
- // type = "filesystem";
- // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
- Serial.println("Start updating " + type);
- });
- ArduinoOTA.onEnd([]() {
- Serial.println("\nEnd");
- });
- ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
- Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
- });
- ArduinoOTA.onError([](ota_error_t error) {
- Serial.printf("Error[%u]: ", error);
- if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
- else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
- else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
- else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
- else if (error == OTA_END_ERROR) Serial.println("End Failed");
- });
- ArduinoOTA.begin();
- }
- void setGreenLED(uint8_t value) {
- digitalWrite(greenPin, !value);
- }
- void setBlueLEDandRelay(uint8_t value) {
- digitalWrite(bluePin, value);
- }
- void setup() {
- Serial.begin(115200);
- delay(10);
- // Set up S20 pins
- pinMode(bluePin, OUTPUT);
- pinMode(greenPin, OUTPUT);
- setBlueLEDandRelay(1);
- setGreenLED(1);
- // Connect to WiFi access point.
- Serial.println(); Serial.println();
- Serial.print("Connecting to ");
- Serial.println(WLAN_SSID);
- //WiFi.config(ip, gateway, subnet); // Only required for fixed IP
- WiFi.begin(WLAN_SSID, WLAN_PASS);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println();
- Serial.println("WiFi connected");
- Serial.println("IP address: "); Serial.println(WiFi.localIP());
- OTA_setup();
- // Setup MQTT subscriptions
- greenblinkmodevalue.setCallback(greenblinkmodevaluecallback);
- mqtt.subscribe(&greenblinkmodevalue);
- blueblinkmodevalue.setCallback(blueblinkmodevaluecallback);
- mqtt.subscribe(&blueblinkmodevalue);
- setBlueLEDandRelay(0);
- setGreenLED(0);
- }
- void loop() {
- ArduinoOTA.handle();
- // Ensure the connection to the MQTT server is alive (this will make the first
- // connection and automatically reconnect when disconnected). See the MQTT_connect
- // function definition further below.
- MQTT_connect();
- // Process mqtt data
- mqtt.processPackets(10);
- // ping the server to keep the mqtt connection alive
- // NOT required if you are publishing once every KEEPALIVE seconds
- /*
- if(! mqtt.ping()) {
- mqtt.disconnect();
- }
- */
- switch (greenBlinkMode) {
- case OFF:
- setGreenLED(0);
- break;
- case fastBlinking:
- setGreenLED( (millis()/100) % 2);
- break;
- case slowBlinking:
- setGreenLED( (millis()/1000) % 2);
- break;
- }
- switch (blueBlinkMode) {
- case OFF:
- setBlueLEDandRelay(0);
- break;
- case fastBlinking:
- setBlueLEDandRelay( (millis()/100) % 2);
- break;
- case slowBlinking:
- setBlueLEDandRelay( (millis()/1000) % 2);
- break;
- }
- }
- // Function to connect and reconnect as necessary to the MQTT server.
- // Should be called in the loop function and it will take care if connecting.
- void MQTT_connect() {
- int8_t ret;
- // Stop if already connected.
- if (mqtt.connected()) {
- return;
- }
- Serial.print("Connecting to MQTT... ");
- while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
- Serial.println(mqtt.connectErrorString(ret));
- Serial.println("Retrying MQTT connection in 5 seconds...");
- mqtt.disconnect();
- delay(5000); // wait 5 seconds
- }
- Serial.println("MQTT Connected!");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement