About This Project
The SonOff is a small device containing an ESP8266, relay, power supply LED and button on a footprint of 8.5x4 cm. AC/DC goes straight in (and out) the device so precaution should be well in place;
- The device gets hotter (than i.e. a Wemos D1 R2)
- the AC/DC lines do not have an 100% strain relief.
But for $5 a piece you have a complete device to play with. There is already a lot to be found in forums about the SonOff. So far I missed an useful sketch which connected the SonOff to Cayenne MQTT.
- With Over the Air.
- Without credentials for Wifi and Cayenne MQTT inside sketch but inside EEPPROM
And more important I was able to read and understand a bit of it, at least enough to tweak it to my liking.
The sketch is Heavily inspired by: https://github.com/idreamsi/SonoffBoilerplate and GitHub - tzapu/WiFiManager: ESP8266 WiFi Connection manager with web captive portal . I only convert it and made it suitable for myself and the Cayenne MQTT environment.
This is my second attempt to convert the SonOffBoilerplate. In my first attempt I realised I remove too much functionality:smiley:
What’s Connected
Nothing more than the Device and two AC/DC lines
PRECAUTION
My MQTT credentials have in total a length of 112 positions, I use this information to decide if the device needs to go into AP mode to update this, If 112 is not a standard lenght this part of the sketch should be updated
// standard lenght of Cayenne MQTT settings (?) are 36 + 40 + 36 = 112 so if the total size not equal to 112 reset (go into AP-mode)
int sizeme = strlen(settings.MQTT_username) + strlen(settings.MQTT_Password) + strlen(settings.MQTT_ClientID);
Serial.print("lenght setting MQTT strings : ");
Serial.println(sizeme);
if ( sizeme != 112) {
reset();
}
BTW Each field stored in EEPROM is one position longer so this explains the following lines:
#include <EEPROM.h>
#define EEPROM_SALT 12663
typedef struct {
int salt = EEPROM_SALT;
char MQTT_username[37] ; // MQTT username
char MQTT_Password[41] ; // MQTT password
char MQTT_ClientID[37] ; // MQTT Client ID
}
With this principle in mind it becomes easy to add parameter/fields to your sketch, update these in AP modes and store in EEPROM
Preparation:
- follow instructions: GitHub - mirko/SonOTA: Flashing Itead Sonoff devices with custom firmware via original OTA mechanism
- create hex from verified sketch and upload though: GitHub - arendst/Tasmota: Alternative firmware for ESP8266 with easy configuration using webUI, OTA updates, automation using timers or rules, expandability and entirely local control over MQTT, HTTP, Serial or KNX. Full documentation at
- continue inside Arduino IDE
Remarks:
- no need to connect first to eWelink
- DO NOT INSTALL LATEST FIRMWARE eWelink (did once result divice could not use SonOTA)
- Make sketch non distructive comment out things like reset(), restart() if you loop into this you are screwed!
- preferred testing sketch on easy access ESP8266 device like Wemos D1 R2
- a reboot/restart normally goes into error and freeze so this is removed from the Cayenne dashboard as button, with the physical button the SonOff automatically goes into a reboot it you continuously hold the button, if you would do this inside the sketch it will create errors do to a programming error inside the ESP8266
Objectives:
- Solderless programming
- Over the Air updating sketch
- No Wifi and Cayenne MQTT credentials in sketch
- On the fly change of Wi-Fi and MQTT credentials and store in EEPROM (this sometimes requires a second time)
- Synchronisation of SonOff Relay, button and Cayenne Actuator (regardless initiator)
Additional Functionality
- time (from NTP-server)
- Dailight Saving Time / Summertime
- Scheduling based on sunset and sunrise
- Display Wifi signal
- uptime in days, hours minutes (bit stupid)
- version displayed (bit more stupid)
A Puzzle to solve
It puzzled me for a long time how to access Cayenne MQTT, First my sketch stopped at point:
- Cayenne.begin(settings.MQTT_username, settings.MQTT_Password, settings.MQTT_ClientID, SSID, wifiPassword)
At one point a solution was to disconnect WiFI, which worked but still required hard coded SSID and wifiPassword. Next I tried to pick up the SSID and password from WifiManager which is possible but I stranded to convert the array into a string. With my next T&A I just did a very simple thing. Realising I am already on Wi-Fi I just shortened the Cayenne.begin to:
Cayenne.begin(settings.MQTT_username, settings.MQTT_Password, settings.MQTT_ClientID)
Life can be simple sometimes
Holding physical button SonOff
What happens if you hold the physical button?
- shorter than 1.5 seconds → toggle relay and sync
- between 1.5 - 5.0 seconds → going into AP-modus
- longer → restart SonOff
This is a small change to the original sketch.
Additional SONOFF details:
- Upload as Generic ESP8266 - Flash mode: DOUT - size: 1MB (64 SPIFFS)
- Sonoff header (if you are not working solderless anymore)
1 - vcc 3v3
2 - rx
3 - tx
4 - gnd
5 - gpio 14
The Sketch
SonOff_BoilerPlate_Cayenne.txt (17.9 KB)
Due to problems with upload previous file:
SonOff_BoilerPlate_Cayenne.ino.txt (18.0 KB)