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
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.
What was the arduino script that you used to connect the esp and allow access to those pins?
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();
}
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
Thanks!
Hi Devon
Thanks for this post, i have some esp’s on order
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).
Rich
Awesome let me know
I got Adafruti’s Huzzah board working, and am waiting on the NodeMcu Lua board to come in too
-B
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);
}
}
Love this! You should submit your project to the Projects Made With Cayenne category
-B
kewl
hi i tried your code but i get error. Can you please tell me which library you used? i get this errors:
error: CayenneDefines.h: No such file or directory
error: BlynkSimpleEsp8266.h: No such file or directory
error: CayenneWiFiClient.h: No such file or directory
thankyou!
can you install this library GitHub - myDevicesIoT/Cayenne-MQTT-Arduino: Cayenne MQTT Arduino Library and this example code Cayenne-MQTT-Arduino/ESP8266.ino at master · myDevicesIoT/Cayenne-MQTT-Arduino · GitHub