Use Cayenne with ESP8266 is very complicated for me

I’m doing my first test with a microcontroller.
Until now I used Cayenne with the RPI.

"#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

// WiFi network info.
char ssid = “";
char wifiPassword[] = "
”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “";
char password[] = "
”;
char clientID = “***”;

unsigned long lastMillis = 0;

int led=LOW;

void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
pinMode(4, INPUT); // button
pinMode(5, OUTPUT); // relay
}

void loop() {
Cayenne.loop();

if (millis() - lastMillis > 100) {
lastMillis = millis();
Cayenne.virtualWrite(0, lastMillis);
}

if(digitalRead(4)==HIGH && led==LOW)
{
digitalWrite(5, HIGH);
led=HIGH;
Cayenne.virtualWrite(1,“1”);
while(digitalRead(4)==HIGH)
{
delay(10);
}
}

if(digitalRead(4)==HIGH && led==HIGH)
{
digitalWrite(5, LOW);
led=LOW;
Cayenne.virtualWrite(1,“0”);
while(digitalRead(4)==HIGH)
{
delay(10);
}
}
}

CAYENNE_IN_DEFAULT()
{
CAYENNE_LOG(“CAYENNE_IN_DEFAULT(%u) - %s, %s”, request.channel, getValue.getId(), getValue.asString());

if (request.channel = 1)
{
String value;
value = (String)getValue.asString();

if (value == "1")
{
  digitalWrite(5, HIGH);
  led=HIGH;
}
else
{
  digitalWrite(5, LOW);
  led=LOW;
}

}
}
"
Who can help me make the program work?
The LED remains off and I will not do anything by closing the switch
The button is not in the dashboard
After a while the board ends up offline.
Who can help me ?
Please

Hello fammagg,
Try this
This is the basic skeleton … millis and led command … add it what you need

//#define CAYENNE_DEBUG
//#define CAYENNE_PRINT Serial

#include <ESP8266WiFi.h>
#include <CayenneMQTTESP8266.h>

char ssid = “ssid”;
char wifiPassword = “wifipassword”;

// Cayenne authentication info.
char username = “username”;
char password = “password”;
char clientID = “ClientID”;

unsigned long lastMillis = 0;

void setup()
{
Serial.begin(115200);
pinMode(D0, OUTPUT);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

void loop()
{
Cayenne.loop();
if (millis() - lastMillis > 10000)
{
lastMillis = millis();
Cayenne.virtualWrite(0, lastMillis);
}
}

CAYENNE_IN(1) // Led command
{
int currentValue = getValue.asInt();
if (currentValue == 1)
{
digitalWrite(D0, HIGH);
}
else
{
digitalWrite(D0, LOW);
}
}

CAYENNE_IN_DEFAULT()
{
CAYENNE_LOG(“CAYENNE_IN_DEFAULT(%u) - %s, %s”, request.channel, getValue.getId(), getValue.asString());
}

Salutations.

1 Like

ok thanks Bernard , but I insert I am new to arduino
Where do I insert the D5 LED pin and the D4 Button pin?
and the run the code is error : "
exit status 1
macro “CAYENNE_IN_DEFAULT” passed 1 arguments, but takes just 0"

Thank you for your help

Hello,
The sketch compiles here without errors.
In my example I used D0 …change for D5

Hello
I change D0 in D5 and I see this mistake:

@fammagg you can delete the cayenne in default function.

CAYENNE_IN_DEFAULT()
{
CAYENNE_LOG(“CAYENNE_IN_DEFAULT(%u) - %s, %s”, request.channel, getValue.getId(), getValue.asString());
}

ok shramiksalgaonkar delete this part of code .No error But now how do I add the button in the Dashboard?

If I go to add an actuator I do not have the possibility to choose ESP8266 but only the RPI?

The only possibility I have is this :slight_smile:

Hello fammagg,
I use CAYENNE_IN_DEFAULT on all my esp8266 and never got dashboard error … If you can tell me why in your this case you get an error @shramiksalgaonkar
What is the button purpose in your project ? displaying the state of button on dashboard ?

Hello Bernard
I was doing a first test with esp8266
The project is simple
I would like to turn on and off a led from my dashboard.
In addition use a real switch to turn on the same led
So a double switch. Real and Virtual

This is my code:

"#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

// WiFi network info.
char ssid = “";
char wifiPassword[] = "
”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “";
char password[] = "
”;
char clientID = “***”;

unsigned long lastMillis = 0;

void setup()
{
Serial.begin(115200);
pinMode(D5, OUTPUT);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

void loop()
{
Cayenne.loop();
if (millis() - lastMillis > 10000)
{
lastMillis = millis();
Cayenne.virtualWrite(0, lastMillis);
}
}

CAYENNE_IN(1) // Led command
{
int currentValue = getValue.asInt();
if (currentValue == 1)
{
digitalWrite(D5, HIGH);
}
else
{
digitalWrite(D5, LOW);
}
}

CAYENNE_IN_DEFAULT()
{
CAYENNE_LOG(“CAYENNE_IN_DEFAULT(%u) - %s, %s”, request.channel, getValue.getId(), getValue.asString());
}

The problem that does not allow me to compile the code:


If you see me give me an Arduino error
Maybe something is missing or there is some wrong parenthesis. What do you think?
In the dashboard I do not understand how to insert the button to turn on the led.

Hello,

What kind of ESP8266 module do you use ?

Although it doesn’t give an error on mi side, Delete this function:

CAYENNE_IN_DEFAULT()
{
CAYENNE_LOG(“CAYENNE_IN_DEFAULT(%u) - %s, %s”, request.channel, getValue.getId(), getValue.asString());
}
Adding button to control LED

Add new
Device/Widget
Custom Widgets
Button

addbutton

Tell me if you have it working .

ok @Bernard is WORK .PERFECT .Thanks very much I use this ESP8266

Last two curiosities:

1)If I wanted to use the external switch, connected to pin D4, I should add:

int pulsante = 0;
unsigned long lastMillis = 0;

void setup()
{
Serial.begin(115200);
pinMode(D5, OUTPUT);
pinMode(D4, INPUT);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

void loop()
pulsante = digitalRead(D4);
{
Cayenne.loop();
if (millis() - lastMillis > 10000)
{
lastMillis = millis();
Cayenne.virtualWrite(0, lastMillis);
}
}

if (pulsante == HIGH){
digitalWrite(D5, HIGH);
} else {
digitalWrite(D5, LOW);
}

CAYENNE_IN(1) // Led command
{
int currentValue = getValue.asInt();
if (currentValue == 1)
{
digitalWrite(D5, HIGH);
}
else
{
digitalWrite(D5, LOW);
}
}

Can this script work?

2)If I wanted to use a slider to control the strength of the LED light, should I use a pwm card connected to the ESP8266 pin?Can the ESP8266 card already manage a pwm control?

Hello,

Good to read you have led command working.
About button I am a bit lost … the code is not correct. I will have a try later.

Led dimming:
You don’t need a pwm card.

Just add:

In setup:
pinMode(D1, OUTPUT); // D1 in my test

In loop:

CAYENNE_IN(2) //choose a free virtual channel … there are many
{
int sliderValue = (getValue.asInt());

analogWrite(D1, sliderValue);
}

Add the slider to Dashboard (min : 0 max: 1023)

Hope this help

1 Like

Hello @Bernard , it works perfectly . The only drawback that the slider from the app does not work, while the web works great. I think it’s a bug of the app. Now I do a bit 'of testing to make even the real switch. Thanks

Hello @fammagg,

Good news :joy:

I can confirm there is a bug with the slider in the mobile app. There should be a fix released soon. Glad you got everything working!

Hello @adam I laughed at doing everything I wanted but one thing. I would like to detect if an on / off switch is on a pin of the esp8266 and if the switch is on turn on a led on another pin of the ESP8266.The Led that can also be switched on by a cayenne widget. Have you already created a similar code?

It’s possible, but I don’t think I’ve seen it posted anywhere here yet. Do you have any code yet that I can make suggestions on?

@fammagg with cayenne triggers its very easy. create a value widget on one esp8266 to read the status of switch and button widget on second esp8266 to turn on led.
now create a trigger if value widget is on then turn on button.

Hi shramiksalgaonkar
I wanted to make a code that would work with both cayenne and without.
For example if the card is online cayenne works, if the card is offline it works through normal code.

#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

// WiFi network info.
char ssid = “";
char wifiPassword[] = "
.”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “";
char password[] = "
”;
char clientID = “***”;

unsigned long lastMillis = 0;

const int ledPin = 14;
const int inputPin = 4;

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(ledPin,OUTPUT);
pinMode(inputPin,INPUT);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

void loop() {
Cayenne.loop();
// put your main code here, to run repeatedly:
int val = digitalRead(inputPin);
if (val == HIGH)
{
digitalWrite(ledPin,HIGH);
}
else
{
digitalWrite(ledPin,LOW);
}
}

CAYENNE_IN(1) // Led command
{
int currentValue = getValue.asInt();
if (currentValue == 1)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
}

The code still does not work perfectly.

cayenne offline is currently not supported but is on roadmap. but there is way to do it. have a look at this Arduino Online/Offline Override - #9 by noriel.instructor

1 Like