Hi Guys,
I’m having problems getting a single relay to operate. I have it wired up to GPIO4 but can’t get it to switch (and running on 5v)…
Here is my code (probably something simple!)
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#define VIRTUAL_PIN 4
#define RELAY_DIGITAL_PIN 4
// WiFi network info.
char ssid = “My SSID”;
char wifiPassword = “IEatChicken”;
char username = “asdfasfasfdsafd”;
char password = “asdfsafdsafdasf”;
char clientID = “asdfasfdsafdsafd”;
unsigned long lastMillis = 0;
void setup() {
pinMode(RELAY_DIGITAL_PIN, OUTPUT);
Serial.begin(115200);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}
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.loop();//Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
if (millis() - lastMillis > 10000) {
lastMillis = millis();
//Write data to Cayenne here. This example just sends the current uptime in milliseconds.
Cayenne.virtualWrite(0, lastMillis);
//Some examples of other functions you can use to send data.
//Cayenne.celsiusWrite(1, 22.0);
//Cayenne.luxWrite(2, 700);
//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
}
}
//Default function for processing actuator commands from the Cayenne Dashboard.
//You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
CAYENNE_LOG(“CAYENNE_IN_DEFAULT(%u) - %s, %s”, request.channel, getValue.getId(), getValue.asString());
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError(“Error message”);
}
Thanks in advance!
Benny