Arduino with W5100 + 8 Relay


#1

Hello

I am trying to add 8 relay to dashboard but it generates everytime code for one relay

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[] = “xxxx”;
char password[] = “xxxx”;
char clientID[] = “xxxx”;

#define VIRTUAL_CHANNEL 28
#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);
}
}

how to add 8 relays i cant control more than one relay


#2

welcome to cayenne community @majko811. you don’t have to generate code each time. once generated you have to modify your previous code accordingly. in your case for 8 relays:

you have to add more 7 of this

#define VIRTUAL_CHANNEL 28
#define ACTUATOR_PIN 4 

like

#define VIRTUAL_CHANNEL_1 29
#define ACTUATOR_PIN_1 5
#define VIRTUAL_CHANNEL_2 30
#define ACTUATOR_PIN_2 6
//simillarly for more 5

add this in your setup:

pinMode(ACTUATOR_PIN_1, OUTPUT);
pinMode(ACTUATOR_PIN_2, OUTPUT);
//simillarly for more 5.

and last add this:

CAYENNE_IN(VIRTUAL_CHANNEL_1)
{
// 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_1, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_1, LOW);
}
}
CAYENNE_IN(VIRTUAL_CHANNEL_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(ACTUATOR_PIN_2, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_2, LOW);
}
}
//simillary for more 5

#3

Thx for help

i changed to this

#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[] = "xxx";
char password[] = "xxx";
char clientID[] = "xxx";

#define VIRTUAL_CHANNEL_1 22
#define ACTUATOR_PIN_1 22 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_2 24
#define ACTUATOR_PIN_2 24 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_3 26
#define ACTUATOR_PIN_3 26 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_4 28
#define ACTUATOR_PIN_4 28 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_5 30
#define ACTUATOR_PIN_5 30 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_6 32
#define ACTUATOR_PIN_6 32 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_7 34
#define ACTUATOR_PIN_7 34 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_8 36
#define ACTUATOR_PIN_8 36 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

void setup()
{
  Serial.begin(9600);
  pinMode(ACTUATOR_PIN_1, OUTPUT);
  pinMode(ACTUATOR_PIN_2, OUTPUT);
  pinMode(ACTUATOR_PIN_3, OUTPUT);
  pinMode(ACTUATOR_PIN_4, OUTPUT);
  pinMode(ACTUATOR_PIN_5, OUTPUT);
  pinMode(ACTUATOR_PIN_6, OUTPUT);
  pinMode(ACTUATOR_PIN_7, OUTPUT);
  pinMode(ACTUATOR_PIN_8, OUTPUT);
  Cayenne.begin(username, password, clientID);
}

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

// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL_1)

{
  // 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_1, HIGH);
  }
  else {
    digitalWrite(ACTUATOR_PIN_1, LOW);
  }
}

CAYENNE_IN(VIRTUAL_CHANNEL_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(ACTUATOR_PIN_2, HIGH);
  }
  else {
    digitalWrite(ACTUATOR_PIN_2, LOW);
  }
}

CAYENNE_IN(VIRTUAL_CHANNEL_3)
{
  // 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_3, HIGH);
  }
  else {
    digitalWrite(ACTUATOR_PIN_3, LOW);
  }
}

CAYENNE_IN(VIRTUAL_CHANNEL_4)
{
  // 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_4, HIGH);
  }
  else {
    digitalWrite(ACTUATOR_PIN_4, LOW);
  }
}

CAYENNE_IN(VIRTUAL_CHANNEL_5)
{
  // 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_5, HIGH);
  }
  else {
    digitalWrite(ACTUATOR_PIN_5, LOW);
  }
}

CAYENNE_IN(VIRTUAL_CHANNEL_6)
{
  // 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_6, HIGH);
  }
  else {
    digitalWrite(ACTUATOR_PIN_6, LOW);
  }
}

CAYENNE_IN(VIRTUAL_CHANNEL_7)
{
  // 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_7, HIGH);
  }
  else {
    digitalWrite(ACTUATOR_PIN_7, LOW);
  }
}

CAYENNE_IN(VIRTUAL_CHANNEL_8)
{
  // 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_8, HIGH);
  }
  else {
    digitalWrite(ACTUATOR_PIN_8, LOW);
  }
}

but its working only relay 1-5 6,7,8 didnt work

chan you check my code i think i cant see the problem


#4

currently the max channel by default is set to 30 (but you increase it). for now change this:

to

#define VIRTUAL_CHANNEL_6 1
#define ACTUATOR_PIN_6 32 // Do not use digital pins 0 or 1 since those conflict with the use of Serial. 
#define VIRTUAL_CHANNEL_7 2
 #define ACTUATOR_PIN_7 34 // Do not use digital pins 0 or 1 since those conflict with the use of Serial. 
 #define VIRTUAL_CHANNEL_8 3 
#define ACTUATOR_PIN_8 36 //

#5

big thx
its working :slight_smile:

i didnt know about 30 max channels

i chnaged to channels 1-8

finished code

#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[] = “xxx”;
char password[] = “xxx”;
char clientID[] = “xxx”;

#define VIRTUAL_CHANNEL_1 1
#define ACTUATOR_PIN_1 22 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_2 2
#define ACTUATOR_PIN_2 24 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_3 3
#define ACTUATOR_PIN_3 26 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_4 4
#define ACTUATOR_PIN_4 28 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_5 5
#define ACTUATOR_PIN_5 30 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_6 6
#define ACTUATOR_PIN_6 32 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_7 7
#define ACTUATOR_PIN_7 34 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_8 8
#define ACTUATOR_PIN_8 36 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

void setup()
{
Serial.begin(9600);
pinMode(ACTUATOR_PIN_1, OUTPUT);
pinMode(ACTUATOR_PIN_2, OUTPUT);
pinMode(ACTUATOR_PIN_3, OUTPUT);
pinMode(ACTUATOR_PIN_4, OUTPUT);
pinMode(ACTUATOR_PIN_5, OUTPUT);
pinMode(ACTUATOR_PIN_6, OUTPUT);
pinMode(ACTUATOR_PIN_7, OUTPUT);
pinMode(ACTUATOR_PIN_8, OUTPUT);
Cayenne.begin(username, password, clientID);
}

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

// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL_1)

{
// 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_1, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_1, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_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(ACTUATOR_PIN_2, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_2, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_3)
{
// 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_3, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_3, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_4)
{
// 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_4, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_4, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_5)
{
// 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_5, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_5, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_6)
{
// 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_6, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_6, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_7)
{
// 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_7, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_7, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_8)
{
// 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_8, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_8, LOW);
}
}


#6

it is set to 30. but if you want you can use as many you want by using this:

CAYENNE_IN_DEFAULT()
   {
CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %u", request.channel, getValue.getId(), 
    getValue.asInt());
    switch (request.channel) {
 case 1 :
   int x = getValue.asInt();
   Serial.println(x);
   break;
}
}