Initialization after dormant period

Community,

I have encountered false readings when starting up my web browser or iphone app to control simple LEDs. My app or web browser will show false states. LEDs which are on can be indicated as off and vica versa. After pushing control buttons a few times things initialize and all works well.

Using a wemos D1 mini.

Can I improve my code to ensure correct and consistent readings? Code listed below.

Thanks,

tett

#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

// WiFi network info.
char ssid = “";char wifiPassword[] = "”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “";
char password[] = "
”;
char clientID = “***”;

void setup() {
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
pinMode(4, OUTPUT);pinMode(2, OUTPUT);pinMode(5, OUTPUT);pinMode(0, INPUT);
digitalWrite(4, HIGH);digitalWrite(2, HIGH);digitalWrite(5, HIGH);digitalWrite(0, LOW);
}

void loop()
{
Cayenne.loop();

}
// Following assigns channels 0,1,2 to Wemos pins and gets values entered on web and sends to pins
CAYENNE_IN(0)
{
digitalWrite(2, getValue.asInt());
}
CAYENNE_IN(1)
{
digitalWrite(4, getValue.asInt());
}
CAYENNE_IN(2)
{
digitalWrite(5, getValue.asInt());

I would say syncall(); in the setup would be the function to call, but from the source:

	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.
}

Maybe do a cayenne.write and send those values in the setup?

@rsiegel or @jburhenn any suggestions on this?

Yeah, I’d try using Cayenne write calls in setup to make sure the dashboard is updated with the initial states for the pins on each channel, e.g. Cayenne.virtualWrite(0, HIGH).

Setup is onl run once. How would this keep things inititllized? Would it not be better to have this in the loop function?

Thanks

My mistake, I thought your issue was only when starting up the device. This is consistently reproducible any time the web dashboard is loaded even if the D1 mini has been running the whole time? If it’s in the correct state then you close the dashboard and re-open it the readings go back to an incorrect state?

Potentially it could be network issues causing messages to be lost, though if nothing changed on the D1 mini and closing/re-opening the dashboard causes the state to change then it’s possibly a bug on the dashboard side.

You could send the state in the loop (or via CAYENNE_OUT_DEFAULT) as a workaround, though that wouldn’t really fix the issue if it’s actually a bug. Can you verify that the state is lost each time you close/re-open the dashboard?

@tett you can try this its just a workaround.

 void loop() {
  Cayenne.loop();

  //Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
  if (millis() - lastMillis > 10000) {
    lastMillis = millis();
    //Write data to Cayenne here. This example just sends the current uptime in milliseconds.
    Cayenne.virtualWrite(0, x);
    Serial.println(x);
    //Some examples of other functions you can use to send data.
    //Cayenne.celsiusWrite(1, 22.0);
    //Cayenne.luxWrite(2, 700);
    //Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
  }
}

CAYENNE_IN(0)
{
 x = getValue.asInt();
digitalWrite(2, x);
}

Same here, interested to hear what your results are with his suggestion @tett.

This does help. I gues the consistent communications keep things working.

Thanks!

1 Like