ESP8266 with multiple relays


#1

I am just starting to play with some ESP8266 boards and was wondering if anyone had experience or any example sketches for working with relays? I am looking to have one ESP8266 hidden in a door panel that will pulse the door lock, and another unit that will turn 4 different relays off and on (for a fan). Any pointing in the right direction would be helpful :slight_smile:


#2

Welcome to Cayenne!

I’m doing exactly that with a broken dryer that I inherited with my new house. You can just set the pin HIGH to turn the relay off or LOW to turn it on using digitalWrite(relay1Pin, HIGH);/digitalWrite(relay1Pin, LOW); You will of course need a source for 5v to power the relays.


#3

What was the arduino script that you used to connect the esp and allow access to those pins?


#4

Happy to say it was waaaaayyyy easier then I expected it to be! Just used the following arduino code, the ESP is running three relays (could probably do more but I only need 3 for my application):

//#define CAYENNE_DEBUG         // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include "CayenneDefines.h"
#include "BlynkSimpleEsp8266.h"
#include "CayenneWiFiClient.h"

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "XXX";
// Your network name and password.
char ssid[] = "Van-a-roke WiFi";
char password[] = "XXX";

void setup()
{
  Serial.begin(9600);
  Cayenne.begin(token, ssid, password);
}

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

#5

@devonmeyers

Devon,
I hope you don’t mind, I formatted the code in the above message so it presents a little more like code.

I can help you with posting code-
Paste the code
Highlight all the code
Click on the ‘Preformatted text’ button


Hope this helps,
-Ian


Arduino Uno + DS18B20 temp sensor
#6

Thanks!


#7

Hi Devon
Thanks for this post, i have some esp’s on order :slight_smile:
They are NodeMcu Lua esp8266-12E. I’ll be in touch when i can’t get it to work (he says with a cheeky grin). :+1:
Rich


#8

Awesome let me know


#9

I got Adafruti’s Huzzah board working, and am waiting on the NodeMcu Lua board to come in too :slight_smile:
-B


#10

Enjoying this setup currently

#include "CayenneDefines.h"
#include "BlynkSimpleEsp8266.h"
#include "CayenneWiFiClient.h"
#include "ESP8266WiFi.h"

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "yourtoken";
// Your network name and password.
char ssid[] = "yourssid";
char password[] = "yourpassword";

void setup()
{
  // initialize digital pin 2 as an output.
  pinMode(16, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(D2, OUTPUT);
  pinMode(D3, OUTPUT);
  pinMode(D4, OUTPUT);
  pinMode(D5, OUTPUT);
  pinMode(D6, OUTPUT);
  pinMode(D7, OUTPUT);
  Serial.begin(9600);
  Cayenne.begin(token, ssid, password);
}

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

// This function will be called every time a Dashboard widget writes a value to Virtual Pin 2.
CAYENNE_IN(V0)
{
  CAYENNE_LOG("Got a value: %s", getValue.asStr());
  int i = getValue.asInt();
  
  if (i == 0)
  {
    digitalWrite(D0, HIGH);
  }
  else
  {
    digitalWrite(D0, LOW);
  }  
}
CAYENNE_IN(V1)
{
  CAYENNE_LOG("Got a value: %s", getValue.asStr());
  int i = getValue.asInt();
  
  if (i == 0)
  {
    digitalWrite(D1, HIGH);
  }
  else
  {
    digitalWrite(D1, LOW);
  }  
}
CAYENNE_IN(V2)
{
  CAYENNE_LOG("Got a value: %s", getValue.asStr());
  int i = getValue.asInt();
  
  if (i == 0)
  {
    digitalWrite(D2, HIGH);
  }
  else
  {
    digitalWrite(D2, LOW);
  }  
}
CAYENNE_IN(V3)
{
  CAYENNE_LOG("Got a value: %s", getValue.asStr());
  int i = getValue.asInt();
  
  if (i == 0)
  {
    digitalWrite(D3, HIGH);
  }
  else
  {
    digitalWrite(D3, LOW);
  }  
}
CAYENNE_IN(V4)
{
  CAYENNE_LOG("Got a value: %s", getValue.asStr());
  int i = getValue.asInt();
  
  if (i == 0)
  {
    digitalWrite(D4, HIGH);
  }
  else
  {
    digitalWrite(D4, LOW);
  }  
}
CAYENNE_IN(V5)
{
  CAYENNE_LOG("Got a value: %s", getValue.asStr());
  int i = getValue.asInt();
  
  if (i == 0)
  {
    digitalWrite(D5, HIGH);
  }
  else
  {
    digitalWrite(D5, LOW);
  }  
}
CAYENNE_IN(V6)
{
  CAYENNE_LOG("Got a value: %s", getValue.asStr());
  int i = getValue.asInt();
  
  if (i == 0)
  {
    digitalWrite(D6, HIGH);
  }
  else
  {
    digitalWrite(D6, LOW);
  }  
}
CAYENNE_IN(V7)
{
  CAYENNE_LOG("Got a value: %s", getValue.asStr());
  int i = getValue.asInt();
  
  if (i == 0)
  {
    digitalWrite(D7, HIGH);
  }
  else
  {
    digitalWrite(D7, LOW);
  }  
}

LinkNode R4 (ESP8266-12F) WDT reset loop when Cayenne sketch loaded
#11

Love this! You should submit your project to the Projects Made With Cayenne category :slight_smile:

-B


#12

Hi @usmcwarmachine,

You should submit this to the contest :slight_smile:

-B


#13

kewl