Relay not working

Hello folks,

Since the update Nothing work.

I have a relay using 3 channel and i am able to only make work one a the time.

here the code working for 1 channel :

/*
Cayenne Relay Switch Example

This sketch sample file shows how to set up a Relay Switch with Cayenne.

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.

Steps:
1. In the Cayenne Dashboard add a new Relay Switch widget.
2. Select a virtual channel number for the widget.
3. Set the VIRTUAL_CHANNEL value below to the virtual channel you selected.
4. Connect your relay switch to a digital pin.
5. Set the ACTUATOR_PIN value below to the digital pin number you selected.
6. Set the Cayenne authentication info to match the authentication info from the Dashboard.
7. Compile and upload this sketch.
8. Once the Arduino connects to the Dashboard you can use the widget button to turn the relay switch on and off.
*/

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

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

#define VIRTUAL_CHANNEL 3
#define ACTUATOR_PIN 4 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

void setup()
{
  Serial.begin(9600);
  pinMode(ACTUATOR_PIN, OUTPUT);
  Cayenne.begin(username, password, clientID);
}

void loop()
{
  Cayenne.loop();
}

// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL)
{
	// 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(ACTUATOR_PIN, HIGH);
	}
	else {
		digitalWrite(ACTUATOR_PIN, LOW);
	}
}

So i though to add my other relay i should add the following line

#define VIRTUAL_CHANNEL 3
#define ACTUATOR_PIN 4 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL 5
#define ACTUATOR_PIN 3 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL 4
#define ACTUATOR_PIN 2 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

It’s work once and then Nothing so if i switch the actuactor pin i able to make work different channel on the relay but only one a the Time

I don’t get what have changed from before

Appreciate any help thanks

change

to #define VIRTUAL_CHANNEL_1 5 and #define VIRTUAL_CHANNEL_2 4.

you need to add more two of this function to read status and turn on relay for respective virtual channel and actuator pin.

#define VIRTUAL_CHANNEL1 1
#define ACTUATOR_PIN4 4

CAYENNE_IN(VIRTUAL_CHANNEL1)
{
	if (getValue.asInt() == 0) {
		digitalWrite(ACTUATOR_PIN4, LOW);
	}
	else {
		digitalWrite(ACTUATOR_PIN4, HIGH);
	}
}

ok thanks for your help i gonna try that.

/*
Cayenne Relay Switch Example

YILDIZ ELEKTRİK RÖLE OTOMASYON KODU
*/

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

// Cayenne authentication info. BAĞLANTI AYARLARI

char username = “------------------------------”;
char password = “-----------------”;
char clientID = “----------------”;

#define VIRTUAL_CHANNEL1 1
#define ACTUATOR_PIN2 2 // 0 ve 1. pinleri kullanma

#define VIRTUAL_CHANNEL2 2
#define ACTUATOR_PIN3 3

#define VIRTUAL_CHANNEL3 3
#define ACTUATOR_PIN4 4

// DİGİTAL PİN TANIMLAMA İÇİN SIRASIYLA ACTUATOR PİN YAZ.
void setup()
{
Serial.begin(9600);
pinMode(ACTUATOR_PIN2, OUTPUT);
pinMode(ACTUATOR_PIN3, OUTPUT);
pinMode(ACTUATOR_PIN4, OUTPUT);
Cayenne.begin(username, password, clientID);
}

void loop()
{
Cayenne.loop();
}

// BİRİNCİ SANAL KANAL + DİGİTAL BAĞLANTISI
CAYENNE_IN(VIRTUAL_CHANNEL1)
{
// RÖLE ANAHTARINI AÇMAK KAPATMAK İÇİN. BU KOD RÖLEYİ AÇIK OLARAK KABUL EDER.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN2, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN2, LOW);
}
}

// İKİNCİ SANAL KANAL + DİGİTAL BAĞLANTISI
CAYENNE_IN(VIRTUAL_CHANNEL2)
{
// RÖLE ANAHTARINI AÇMAK KAPATMAK İÇİN. BU KOD RÖLEYİ AÇIK OLARAK KABUL EDER.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN3, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN3, LOW);

}

}

// ÜÇÜNCÜ SANAL KANAL + DİGİTAL BAĞLANTISI
CAYENNE_IN(VIRTUAL_CHANNEL3)
{
// RÖLE ANAHTARINI AÇMAK KAPATMAK İÇİN. BU KOD RÖLEYİ AÇIK OLARAK KABUL EDER.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN4, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN4, LOW);

}
}

I have the same issue of the creator of this post, and i can solved it with the replys you write here. Thank you very much!!

This worked for me once I paid attention to details. Funny how that works.

#define VIRTUAL_CHANNEL2 2
#define ACTUATOR_PIN2 2// Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL3 3
#define ACTUATOR_PIN3 3
#define VIRTUAL_CHANNEL4 4
#define ACTUATOR_PIN4 4
#define VIRTUAL_CHANNEL5 5
#define ACTUATOR_PIN5 5
#define VIRTUAL_CHANNEL6 6
#define ACTUATOR_PIN6 6
#define VIRTUAL_CHANNEL7 7
#define ACTUATOR_PIN7 7 

const int numReadings = 10;

int readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average

int inputPin = A3;


Adafruit_ILI9341 tft = Adafruit_ILI9341(PLDuino::LCD_CS, PLDuino::LCD_DC);
PLDTouch touch(PLDuino::TOUCH_CS, PLDuino::TOUCH_IRQ);

Button btnVoltage("Front", ILI9341_YELLOW, ILI9341_BLACK);

Button btnVoltage1("Back", ILI9341_YELLOW, ILI9341_BLACK);

Button btnVoltage2("Battery", ILI9341_BLUE, ILI9341_BLACK);

Button btnVoltage3("Inverter", ILI9341_RED, ILI9341_BLACK);

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
//char token[] = "assword.
char ssid[] = "free";
char WiFipassword[] =

// Set ESP8266 Serial object
 #define EspSerial Serial2
 ESP8266 wifi(&EspSerial);

void setup()

{
  PLDuino::init();
  PLDuino::enableLCD();
  PLDuino::enableESP();
  tft.begin();
  tft.setRotation(3);
  touch. init(1);

  // initialize all the readings to 0: 
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;

  Serial.begin(9600);
  delay(100);
  
pinMode(ACTUATOR_PIN2, OUTPUT);
pinMode(ACTUATOR_PIN3, OUTPUT);
pinMode(ACTUATOR_PIN4, OUTPUT);
pinMode(ACTUATOR_PIN5, OUTPUT);
pinMode(ACTUATOR_PIN6, OUTPUT);
pinMode(ACTUATOR_PIN7, OUTPUT);

 
  // Set ESP8266 baud rate
  EspSerial.begin(115200);
  delay(100);

  Cayenne.begin(username, password, clientID, wifi, ssid, WiFipassword);

  btnVoltage.setPositionAndSize(01, 01, 100, 75);
  btnVoltage.draw(tft);

  btnVoltage1.setPositionAndSize(01, 75, 100, 75);
  btnVoltage1.draw(tft);

  btnVoltage2.setPositionAndSize(01, 150, 100, 75);
  btnVoltage2.draw(tft);

  btnVoltage3.setPositionAndSize(175, 1, 150, 110);
  btnVoltage3.draw(tft);
  
 
}

void loop()
{
  Cayenne.loop();
}
  // This function is called when data is sent from Cayenne.


CAYENNE_IN_DEFAULT()
{
  CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
}


//front solar panel voltage
CAYENNE_OUT(V1)

{
  int adcVal = analogRead(A0);
  float adcVoltage = 55.0*adcVal/1024;
  Cayenne.virtualWrite(V1,adcVoltage);
  btnVoltage.setText(String(adcVoltage));
  btnVoltage.draw(tft);
  //delay (4000);
}
//back solar panel voltage
CAYENNE_OUT(V2)

{
  int adcVal1 = analogRead(A1);
  float adcVoltage1 = 55.0*adcVal1/1024;
  Cayenne.virtualWrite(V2,adcVoltage1);
  btnVoltage1.setText(String(adcVoltage1));
  btnVoltage1.draw(tft);
  //delay (4000);
}
//battery voltage
CAYENNE_OUT(V3)

{
  int adcVal2 = analogRead(A2);
  float adcVoltage2 = 55.0*adcVal2/1024;
  Cayenne.virtualWrite(V3,adcVoltage2);
  btnVoltage2.setText(String(adcVoltage2));
  btnVoltage2.draw(tft); 
  //delay (4000);
}
//inverter voltage
CAYENNE_OUT(V4)

{
  
  // subtract the last reading:
  total= total - readings[index];         
  // read from the sensor:  
  readings[index] = analogRead(inputPin); 
  // add the reading to the total:
  total= total + readings[index];       
  // advance to the next position in the array:  
  index = index + 1;                    

  // if we're at the end of the array...
  if (index >= numReadings)              
    // ...wrap around to the beginning: 
    index = 0;                           

  // calculate the average:
  average = total / numReadings * 575.0 / 1024;         
  // send it to the computer as ASCII digits
  //float average = 575.0 * numReadings/1024;
  Cayenne.virtualWrite(V4,average);
  btnVoltage3.setText(String(average));
  btnVoltage3.draw(tft);
  //Serial.println(average);   
  delay(500);        // delay in between reads for stability
              
}
CAYENNE_IN(VIRTUAL_CHANNEL2)

{
  
  if (getValue.asInt() == 0) {
    digitalWrite(ACTUATOR_PIN2, HIGH);
  }
  else {
    digitalWrite(ACTUATOR_PIN2, LOW);
  }
}
CAYENNE_IN(VIRTUAL_CHANNEL3)
{
  
  if (getValue.asInt() == 0) {
    digitalWrite(ACTUATOR_PIN3, HIGH);
  }
  else {
    digitalWrite(ACTUATOR_PIN3, LOW);
  }
}
CAYENNE_IN(VIRTUAL_CHANNEL4)
{
  
  if (getValue.asInt() == 0) {
    digitalWrite(ACTUATOR_PIN4, HIGH);
  }
  else {
    digitalWrite(ACTUATOR_PIN4, LOW);
  }
}
CAYENNE_IN(VIRTUAL_CHANNEL5)
{
  
  if (getValue.asInt() == 0) {
    digitalWrite(ACTUATOR_PIN5, HIGH);
  }
  else {
    digitalWrite(ACTUATOR_PIN5, LOW);
  }
}  
CAYENNE_IN(VIRTUAL_CHANNEL6) 
{
  
  if (getValue.asInt() == 0) {
    digitalWrite(ACTUATOR_PIN6, HIGH);
  }
  else {
    digitalWrite(ACTUATOR_PIN6, LOW);
  }
} 
CAYENNE_IN(VIRTUAL_CHANNEL7)
{
  
  if (getValue.asInt() == 0) {
    digitalWrite(ACTUATOR_PIN7, HIGH);
  }
  else {
    digitalWrite(ACTUATOR_PIN7, LOW);
  }
}

Hey there @flammeur Make sure to delete you credentials when you post your sketch. Anyone could highjack your board or worse.

Time to edit.

Bill

hello,

@wmontg5988 thanks for tip did edit it i can’t see it ?

And thanks for the code

2 Likes