SonOff (or other ESP8266 device) with Cayenne MQTT OTA & AP

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: 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


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 : ");
  if ( sizeme != 112) {

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



  • 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


  • 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 :smiley::smiley::smiley:

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)

The Dashboard


Thank you for sharing this post @wj4me! I think there are many who will appreciate your work when it saves them time to doing something similar :slight_smile:


Great stuff!

Hi @bestes, did you see my indirect/hidden question about the standard total lenght of the

Because if it is not always 112 the precaution becomes an real emergengy number

if ( sizeme != 112) {

Which only can be taccled prefixing reset(); to //reset(); or adjust into yiur MQTT lenght.

I didn’t see that until now. So you are saying Cayenne does not always give 112 length of mqtt credentials??


Hi @bestes,

I am only wondering if the total lenght of the Cayenne MQTT credentials is always 112. To be honest I should have checked my other decives to see if at least these are 112 positions :grin:
If it is not always 112 I should make the sketch less rigid on this point.

Groeten van …
…Wouter Jan

how do you can make the Scheduling on the dashboard mate ?

on widget there is icon on top right corner of settings. click on it and you will get scheduling feature.

Hi @rizqiv2 the internal scheduling is based upon sunset and sunsire of your specific location (you might need to adjust this when not in the Netherlands / europe). I added this internal scheduling because there is no flooting scheduling based upon sunset / sunrise possible. You can deactivate the internal scheduling if you have added an actuator button for channel 26.

The standard scheduling from Cayenne itself like @shramik_salgaonkar is mentioning is reported from time to time not working hence my preference to do it inside the sketch. In this case Cayenne’s scheduling is likely to work but check for double entries after adjustments.

1 Like


Hi there!

Have you had a look at the ESP Easy Mega project?
They have a software for the ESP8266, ESP32 and some other chips. When you install it you get the normal AP behavior for WiFi onbording then the intressting part begins.
You have webpage where you can configure nearly everything. Drop-down menus for sensors you connect, rules engine, ESP->ESP communication and much more.

They have MQTT to several home automation servers, but they are missing MQTT integration to Cayenne :frowning:

List of officially supported HW. (A loot more exist that you can add or write you own)

Maybe your the right person to add the MQTT support form Cayenne? :slight_smile:

@michael.stjerna, I see myself more as a lego- constructor who with C&P and T&E tries to accomplish something. Your idea is bridge too far for me.


I am trying to download the sketch SonOff_BoilerPlate_Cayenne.txt, but I get Oops! That page doesn’t exist or is private. Any one else having this problem?

Thank you


@heinzhp, yes even the writer of this project has the same problem so I have uploaded a new version. Please take some precaution: I have bricked 3 SonOff devices, not really bricked, more OTA-bricked and soldering is now required :blush:

  1. One SonOff was uploaded with a older sketch with a more or less endless loop inside
  2. *Uploaded one with the latest official firmware from ITEAD Studio and now awaiting the Ability to downgrade the firmware again.
  3. More important with this sketch I was experimenting with small changes in my cayenne login credentials and not all changes are reason to get stored properly so perhaps sketch improvements are required at this point.

I am at the moment experimenting with a MQTT-server of my own and awaiting a shipment of SonOff Devices (with hopefully old firmware). I haven’t made any effort to improve this sketch lately so you get this as is. In the end soldering might be required.

Hope these additional comments helps.

Thank you for your quick response, I can now download the new version file. Soldering is not a problem :wink: