Sparkfun Esp8266 thing dev board & relays

Hello, does the Sparkfun Esp8266 Thing dev board need additional code to control relays, LEDs etc other than what is included on the Github site to connect the board to Cayenne? or do you just add them in the dashboard like with the Raspberry Pis?

You will have to upload a sketch to the ESP. There are two ways to do it now:

Unofficial: Esp8266 - #20 by kreggly
Official: Select an ESP8266 device from the dashboard and follow the directions on the screen

Ok, I was able to flash to code to the board from the github link. I got the Sparkfun esp8266 thing dev board connected to Cayenne but I’m unable to add the relay to it and I wasn’t sure if that part has to be added in the github code also?

Hello, would it be possible to take a look at Benny’s code he used at Sparkfun to control the IOT relay with the Sparkfun esp8266 thing dev board?

You could add a actuator and put your code under the function in your sketch. Just specify the cannel so the actuator and the function are the same. here is a example:

CAYENNE_IN(1)   // trigger chip restart
{
 restartChip = getValue.asInt();
 delay(500);  //slight pause after button push

 if (restartChip == 1)
 {
	Cayenne.virtualWrite(1, 0, "digital_sensor", "d");      
	Cayenne.loop();    // writes reboot button back low before restart
	delay(1500);

	ESP.restart();
}

}

1 Like

Ok, so you would have to use Cayenne_in (5)}Digital write Pin (5, high);} Else {Digital write pin (5, low);
}

vapor83
September 13 |

You could add a actuator and put your code under the function in your sketch. Just specify the cannel so the actuator and the function are the same. here is a example:
CAYENNE_IN(1) // trigger chip restart
{
restartChip = getValue.asInt();
delay(500); //slight pause after button push

if (restartChip == 1)
{
Cayenne.virtualWrite(1, 0, “digital_sensor”, “d”);
Cayenne.loop(); // writes reboot button back low before restart
delay(1500);

ESP.restart();

}

}

Visit Topic or reply to this email to respond.

To unsubscribe from these emails, click here.

Yea thats how I would do it.

// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling.

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#define pin (2) output
// 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()
{
pinMode(5, OUTPUT);
Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

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);
}
}

WHAT NEEDS TO GO HERE?

digitalWrite(5, HIGH);

}
else
{
digitalWrite(5, LOW);
}
}
//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 (5)
{
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”);

}

You need a CAYENNE_IN to get your button state. I took your code & cleaned it up a bit. The “define pin (2)” was wrong. If your going to do it with define, it should read “#define relayPin 5”. Also need to have your digital writes in a loop or function. I put comments by the parts I changed. The code should compile fine and run.

// #define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

char ssid[] = "";
char wifiPassword[] = "";

char username[] = "";
char password[] = "";
char clientID[] = "";

unsigned long lastMillis = 0;

int relayButton;  // variable to hold button state
const int relayPin = 5;  // constant variable to hold pin #

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

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);
	  }



    if (relayButton == 1)  // checks if button is pressed or not
      {
    	  digitalWrite(relayPin, HIGH);
      }

    else  // if not pressed
      {
    	  digitalWrite(relayPin, LOW);
      }
}



CAYENNE_IN(10)  //channel 10 but can be any channel thats not used
{
	relayButton = getValue.getId();  //sets variable to button state
}

Great thanks, just having one error message from the Arduino IDE.

cayenne_esp8266__2:55: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]
relayButton = getValue.getId(); //sets variable to button state
^
exit status 1
invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]

and I have no idea what that means.

Thanks for all the help!

replace that line you got the error on with:

relayButton = getValue.asInt();

Thanks @adam ! i used the wrong getValue… sorry about that. :slight_smile:

As you can see there are several data types you can get from cayenne to suit all needs. Good luck!

No problem, you had I figured out much better than I ever could have, I was totally lost. Thanks a lot for all the help.

Ok,Great thanks.

adam Leader
September 14 |

replace that line you got the error on with:

relayButton = getValue.asInt();

Visit Topic or reply to this email to respond.

In Reply To

adbrig4
September 14 |

Great thanks, just having one error message from the Arduino IDE. cayenne_esp8266__2:55: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive] relayButton = getValue.getId(); //sets variable to button state ^ exit status 1 invalid conversion from ‘const char*’ to ‘int’ [-fpermis…
Visit Topic or reply to this email to respond.

To unsubscribe from these emails, click here.