ESP8266 : using syncAll() function

i’m on ESP-01 with arduino IDE
I want to synchronise my Button State on Cayenne with my pin state on the ESP . For example if my board reset when batteries low …
i saw a syncAll() function example for Ethernet arduino board. So i transforme the sketch to ESP01 :
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

// WiFi network info.
char ssid[] = "aaaaa";
char wifiPassword[] = "bbbbbb";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "xxxxxxxxx";
char password[] = "yyyyyyyyy";
char clientID[] = "zzzzzzz";

// This flag is used so the sync only happens on the first connection.
bool isFirstConnect = true;

//=              SETUP              =
void setup() {
  Serial.println("\n++ Demarrage de Cayenne ++");
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);

} // FIN de setup

//=              LOOP               =
void loop() {

} //FIN de loop

//======================    AUTRES FONCTIONS     ==================================

// This function will run every time the Cayenne connection is established.
  CAYENNE_LOG("Connection established");
  if (isFirstConnect)
    // This causes Cayenne to resend data for any virtual pins.
    isFirstConnect = false;

  CAYENNE_LOG("Current Virtual Pin 6 value: %s", getValue.asStr());

  CAYENNE_LOG("Current Virtual Pin 7 value: %s", getValue.asStr());

On cayenne, i created a button on chanel 6 & 7 : when i push v7 button : Arduino serial monitor display :

[1316551] In: value 1, channel 7
[1316551] publishState: topic 1 channel 7
[1316551] Publish: topic 1, channel 7, value 1, subkey , key 
[1317839] Send response: DHIDoxHDFFIGtyv 
[1326015] Connection ok

But if i Reset my board i don’t see v7 state : :slightly_frowning_face:

[3253] Connected to WiFi
[3253] IP:
[3253] Connecting to
[3610] Connected
[3610] Connection established
[3885] Publish: topic 5, channel 65534, value ESP8266, subkey , key 
[4051] Publish: topic 7, channel 65534, value Xtensa32, subkey , key 
[4176] Publish: topic 8, channel 65534, value 80000000, subkey , key 
[4298] Publish: topic 6, channel 65534, value v1, subkey , key 
[15453] Connection ok

Could someone help me with this function on ESP8266 board ?

Hi @samarchri,

The syncAll() function isn’t necessary for MQTT-connected devices so it isn’t implemented there. If you have a look at the code that defines it you’ll see the same there:

	* Requests Server to re-send current values for all widgets.
	void syncAll()
		//Not implemented. This is not needed with MQTT since the broker keeps the last message so we don't need to request it.
1 Like

Hi @rsiegel,
I unterstand … it’s the same for: syncVirtual(int channel) so …

i thought that Cayenne could “resend” commands if the board have been reseted

Ex: On the Cayenne Dashboard i push button : “Channel 0 = 1”
=> unfortunately, few minutes after, the board reset and the sketch alaways beging by “Channel 0 = 0”
when board connect to cayenne , it synchronise to the last states on Cayenne Dashboard, see button pushed, and put => “Channel 0 = 1” … it’s my dream to save space on programme memory.

So sad :wink:

Hi @samarchri,

I’ve been working on how to get the last stored valued for an actuator via MQTT with the developer of our client software today, and we think after some test that this may be a flag that needs to be set on our MQTT Broker (server side) in order to retain the last actuator message and re-send on a new launch of the client.

I’m checking with our server guys now to see if that flag is correctly set. If it’s not, we’ll investigate changing it’s setting and re-test. If it is, we’ll continue to debug. I’ll update you when I know more.

1 Like


I’m actually working on something similar, for my home central heater.
I want to retain the previous value. My approach is storing the last value on the EEPROM,
so if the device restarts, it reads the last stored value. As soon as I have it working, I can share the code. Hope it helps!

If anybody has done it, please let us know! :slight_smile:



I’ve been tinkering with storing the actuator state to the ESP’s EEPROM, so in case the power goes out, the ESP restarts etc, we can load the value from there, and send this values through MQTT to Cayenne.

I must admit at first I was a bit scared and intimidated, but it’s actually fairly easy and quite straight forward (at least something simple as storing a state [0 or 1])

I’ve followed an amazing and in-depth tutorial
Tech Note 015 How to Use ESP8266 EEPROM

This is the idea…

#include <EEPROM.h>

void setup() {
EEPROM.begin(4); //EEPROM.begin(Size), parameter is the number of bytes you want to use store
// Size can be anywhere between a minimum of 4 and maximum of 4096 bytes.

void writeToEeprom() {
int EEaddress = 0;
EEPROM.write(EEaddress,1); // Writes the value 1 to EEPROM
EEPROM.commit(); // Perform the actual write function

void readFromEeprom() {
// Contents of EEPROM (read values, you could put it into a variable and send it to Cayenne)
int EEaddress = 0;
Serial.print("EEPROM contents at Address=0 is : ");

This is a very simple pseudo code. But I’ve inserted it to my sketch and it works fine!
You could give it a try, at least until some new function is provided by Cayenne.

Feel free to ask…


Hi Marc, Great code i will try. Thanks for sharing


Hi marc, i just test your piece of code in my sketch and works perfectly . I could return to Cayenne the last state of the Led even if the ESP reboot. :ok_hand:


Hi @samarchri

Glad to hear I could help. It’s a very useful piece of code!
We should really thank the guy from the youtube tutorial :rofl:




Wondering if the flag was set or not and if there is anything in the works if it isn’t set. I’m working with a slider value and a rebooting ESP so a resend would be handy. Thank you.

Ultimately I found out the retain flag was not set – its a matter of changing it in our development environment and doing some QA on our side. Thus far there has not been any action on it – I’ll make a renewed request and see if we can prioritize it on our end.




any update on this topic (regarding the retain flag)?

Hi, Like Vapor83, I’m also interested as my sliders and buttons also show wrong values … looking forward to latest update :wink:

1 Like

No update just yet, apologies @Jodi, @3magku. We’ll update when there is any change in status here. Tagging @bestes here (and internally) to make him aware of the request.

1 Like
This example shows how to connect to Cayenne using an Ethernet W5100 shield and send/receive sample data.
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
1. Set the Cayenne authentication info to match the authentication info from the Dashboard.
2. Compile and upload the sketch.
3. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.

//#define CAYENNE_DEBUG       // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>
#define SENSOR_PIN 5

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";
int sensorValue = A5;
int mq3_analogPin = 5; // connected to the output pin of MQ3
byte red=4, green=5, yellow=6; // pins for leds
// remember 100 to 1000 ohm resistor in series with each LED

void setup() 
  Serial.print("Alcohol value:");
  Cayenne.begin(username, password, clientID);

void loop() 
  // give ample warmup time for readings to stabilize
  int mq3_value = analogRead(5);
  float voltage = mq3_value* (5.0/1023.0);
  Serial.print("Alcohol value:");
  if (mq3_value<400)
    Serial.print("\nPass. Safe drive!");
  else if(mq3_value>900)
    Serial.print("\nWARNING! You can't drive.");
    Serial.print("\nYou're still under control. But be careful!");
  delay(300); //Just here to slow down the output.

  Cayenne.virtualWrite (VIRTUAL_CHANNEL, map(analogRead(5), 0, 1023, 0, 100));

why is my result in serial monitor dont sync with the result shown in cayenne dashboard? can anyone help me?

on your dashboard you have three widget. uncomment //#define CAYENNE_DEBUG and check what value the device is sending to cayenne.

why don’t you write simply ? :

Cayenne.virtualWrite(VIRTUAL_CHANNEL, mq3_value);

Hi all,
Any update on this topic yet? (syncAll)

No update yet. Cayenne does not allow to set retain flag.