This sketch connects to the Cayenne server using an Arduino WiFi shield
and runs the main communication loop.
The Cayenne 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. Set the token variable to match the Arduino token from the Dashboard.
2. Set the network name and password.
3. Compile and upload this sketch.
For Cayenne Dashboard widgets using digital or analog pins this sketch will automatically
send data on those pins to the Cayenne server. If the widgets use Virtual Pins, data
should be sent to those pins using virtualWrites. Examples for sending and receiving
Virtual Pin data are under the Basics folder.
*/
#define CAYENNE_DEBUG // Uncomment to show debug messages
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneWiFi.h>
// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "xxxxxxxxxx";
// Your network name and password.
char ssid[] = "Telpage Bench";
char password[] = "xxxxxx";
void setup()
{
Serial.begin(9600);
// delay(1000);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(9, LOW);
Cayenne.begin(token, ssid, password);
}
void loop()
{
// while(!digitalRead(0)){
// //pulling gpio0 to gnd for few seconds resets the device due to wdt reset.
// }
Cayenne.run();
}
Any Ideas on what I can do? Other sketches work fine.
The Cayenne library includes the Blynk library which has support for the ESP. Just compile this sketch and see if it works for you:
#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[] = "<your_token>";
// Your network name and password.
char ssid[] = "<your_ssid>";
char password[] = "<your_pwd>";
void setup()
{
Serial.begin(9600);
Cayenne.begin(token, ssid, password);
}
void loop()
{
Cayenne.run();
}
Awesome! Thank you. Let me know if I should start a new thread for this question.
I got 1 relay added and working. But I need to add the other 3. When I try to add another in cayenne it just gives me the same code as the first one. How do I extend this for all four? #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"
#define VIRTUAL_PIN 1
#define RELAY_DIGITAL_PIN 16
// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "xxxxxxxxxxx";
// Your network name and password.
char ssid[] = "xxxxxxxxxx";
char password[] = "xxxxxxxxxxx";
void setup()
{
// set digital pin to output
pinMode(RELAY_DIGITAL_PIN, OUTPUT);
Serial.begin(9600);
Cayenne.begin(token, ssid, password);
}
CAYENNE_IN(VIRTUAL_PIN)
{
// get value sent from dashboard
int currentValue = getValue.asInt(); // 0 to 1
// assuming you wire your relay as normally open
if (currentValue == 0) {
digitalWrite(RELAY_DIGITAL_PIN, HIGH);
} else {
digitalWrite(RELAY_DIGITAL_PIN, LOW);
}
}
void loop()
{
Cayenne.run();
}
You actually don’t need virtual functions or pinmode settings, just use digital pins when creating your relay actuators, and Cayenne takes care of the rest.
You only need virtual pins if you are doing something fancy and need to do calculations or use a driver not natively supported by Cayenne.
Okay, I removed that widget and while recreating it, I chose digital instead of virtual. The sketch it gives me looks the same. Also, how do I change the part below to accommodate 4 relays?
// get value sent from dashboard
int currentValue = getValue.asInt(); // 0 to 1
// assuming you wire your relay as normally open
if (currentValue == 0) {
digitalWrite(RELAY_DIGITAL_PIN, HIGH);
} else {
digitalWrite(RELAY_DIGITAL_PIN, LOW);
}
}