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>
}
// 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());
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).
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?
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);
}