Relays turn high after reboot (Arduino MQTT)


#1

The relays (that are being used in my cayenne code) automaticly turn high when i reboot my arduino OR when i open the dashboard.

So even if my relays are turned off cayenne dashboard says they are on (when i remove my cache memory or restart browser).

I use BYOT (Bring your own thing) so i can’t add the widget “Relay”. instead of that i can only use Custom widget so i can’t click on “Reverse logics”.

I already tried putting: digitalWrite(28, LOW); in setup but that doesn’t work either.

Here is my code (only important parts):

//Relay Light Channel 2 Pin 24
CAYENNE_IN(2)
{
  // Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
  if (getValue.asInt() == 0) {
digitalWrite(24, HIGH);
  }
  else {
digitalWrite(24, LOW);
  }
}

//Relay Light 2 Channel 4 Pin 28
CAYENNE_IN(4)
{
  if (getValue.asInt() == 0) {
digitalWrite(28, HIGH);
  }
  else {
digitalWrite(28, LOW);
  }
}

This is how my dashboard looks when i open it every time:
Dashboard Screen

When i turn off those widgets (even if they are already off in real) i can use them and it works just fine.

So my problem is:

  • Relays are automaticly high on reboot
  • No matter what the real state is, dashboard always says relay is on when i refresh dahboard.

EDIT:
i fixed the problem where my relays automaticly turn on after reboot by setting them high in void setup. Doesn’t seems logic to me but it works so yeah…

But after i refresh my dashboard all the relay states are on so i still have that problem.


#2

I sort of have the same case. All widgets are turning on after reboot

I used customize widgets and BYOT.
This is my code:

#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneTemperature.h>
#include <CayenneMQTTEthernet.h>

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

unsigned long lastMillis = 0;

#define VIRTUAL_CHANNEL_YELLOW 2
#define ACTUATOR_PIN_YELLOW 2 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL_BLUE 3
#define ACTUATOR_PIN_BLUE 3
#define VIRTUAL_CHANNEL_RED 4
#define ACTUATOR_PIN_RED 4
#define VIRTUAL_CHANNEL_ALARM 5
#define ACTUATOR_PIN_ALARM 5
#define VIRTUAL_CHANNEL_MOTOR 8
#define ACTUATOR_PIN_MOTOR 8
#define VIRTUAL_CHANNEL_GREEN 9
#define ACTUATOR_PIN_GREEN 9
#define VIRTUAL_CHANNEL_MOTION 7
#define SENSOR_PIN_MOTION 7
#define VIRTUAL_CHANNEL_TEMP 1
#define SENSOR_PIN_LDR 2
#define VIRTUAL_CHANNEL_LDR 0


// Analog pin the TMP36 is connected to.
const int tmpPin = 1;

// Voltage to the TMP36. For 3v3 Arduinos use 3.3.
const float voltage = 5.0; 

TMP36 tmpSensor(tmpPin, voltage);

void setup()
{
  Serial.begin(9600);
  pinMode(ACTUATOR_PIN_YELLOW, OUTPUT);
  pinMode(ACTUATOR_PIN_BLUE, OUTPUT);
  pinMode(ACTUATOR_PIN_RED, OUTPUT);
  pinMode(ACTUATOR_PIN_ALARM, OUTPUT);
  pinMode(ACTUATOR_PIN_MOTOR, OUTPUT);
  pinMode(ACTUATOR_PIN_GREEN, OUTPUT);
  
  Cayenne.begin(username, password, clientID);
}

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, lastMillis);
  }
}

// This function is called when data is sent from Cayenne.
CAYENNE_IN(2)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_YELLOW, ACTUATOR_PIN_YELLOW, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_YELLOW, value);
}

CAYENNE_IN(3)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_BLUE, ACTUATOR_PIN_BLUE, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_BLUE, value);
}

CAYENNE_IN(4)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_RED, ACTUATOR_PIN_RED, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_RED, value);
}

CAYENNE_IN(5)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_ALARM, ACTUATOR_PIN_ALARM, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_ALARM, value);
}

CAYENNE_IN(8)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_MOTOR, ACTUATOR_PIN_MOTOR, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_MOTOR, value);
}

CAYENNE_IN(9)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_GREEN, ACTUATOR_PIN_GREEN, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_GREEN, value);
}

// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(7)
{
  // Read data from the sensor and send it to the virtual channel here.
  // For example, to send a digital value you can use the following:
  int value = digitalRead(SENSOR_PIN_MOTION);
  Cayenne.virtualWrite(VIRTUAL_CHANNEL_MOTION, value, TYPE_DIGITAL_SENSOR, UNIT_DIGITAL);
}

// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(1)
{
  // This command writes the temperature in Celsius to the Virtual Channel.
  Cayenne.celsiusWrite(VIRTUAL_CHANNEL_TEMP, tmpSensor.getCelsius());
  // To send the temperature in Fahrenheit or Kelvin use the corresponding code below.
  //Cayenne.fahrenheitWrite(VIRTUAL_CHANNEL_TEMP, tmpSensor.getFahrenheit());
  //Cayenne.kelvinWrite(VIRTUAL_CHANNEL_TEMP, tmpSensor.getKelvin());
}

// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(VIRTUAL_CHANNEL_LDR)
{
  Cayenne.virtualWrite(VIRTUAL_CHANNEL_LDR, analogRead(SENSOR_PIN_LDR));
}

Hope you can help Cayenne. Thanks in advance


#3

@gygonzales and @timoverberne2000 do you want your button on the dashboard to remain off on a reboot of device?


#4

yes


#5

Yes but also when i turn off my relay my dashboard shows its off. But when i simply refresh with F5 (no reboot) it shows on again when its off.

Note: Android works perfect, web dashboard not


#6

add this to setup for each channel you want to make it to zero.
Cayenne.virtualWrite(Channel, 0);


#7

can you provide a bit more detail and can you check the serial monitor if there is any error.


#8

Check my screenshot i provided called dashboard.

Everytime i refresh my web dashboard every button says its on just like the screenshot


#9

But android works perfect so maybe it is just one of the 1000 bugs that are currently in cayenne…


#10

okay, so you need to send the state of the widget to the dashboard in loop.


#11

Weird. Thats not in the relay mqtt example in arduino ide.

So every BYOT user should use that piece of code?

Dont you think its weird that relays dont work with normal script.


#12

that is just a workaround till all the bugs are fixed.


#13

oh ok,

but thats a fix for reboot right?
not for just browser refresh.

See this GIF:

This is what i saw after the refresh in GIF:


#14

i tested 2 things.

Use BYOT as Device:
Can use buttons in android app but button state doesn’t work on web dashboard.

Use arduino as Device:
Button state works perfect on web but can’t control button in android app at all.

So

  • Arduino works perfect on Web but not on mobile.
  • BYOT Works perfect on mobile but not on web

It looks like cayenne released one big bug


#15

i gave you two workaround and both should solve the problem beside the arduino working on mobile app. give it a try and let me know.


#16

didn’t work.
Slider is an well known bug so with byot slider doesn’t work on android and with arduino slider doesn’t work on dashboard and there is no workaround for that.

So i will continue workking with BYOT instead of arduino. Both have lots of bugs but arduino has the most.


#17

can you share the code for the workaround i suggested above. so we can tackle one bug at a time.