Relay control based on temperature (using ESP8266 and Cayenne)


I want to control a relay based on temperature, using ESP8266 and Cayenne.
Can you help me how to do that?

Waiting for your response.

Fatmir Prekupi

have you tried connecting your esp to cayenne? Have a look at the example folder to get the all the required codes

I need to control a relay using NODEMCU ESP8266 and CAYENNE.

These codes are for Arduino and Cayenne.

What do i need to change to use these codes for NODEMCU ESO8266 ?


I have tried to control a relay through Cayenne and ESP8266, but i have a problem.
I cant add a relay widget on Cayenne (I cant choose a device).
Why is happening this, can you help me ?

Delete the previous device from cayenne dashboard and follow this step:-

  1. Add a new device by following these steps:- add new ----> device/widget ---->microcontroller —> arduino ----> next —> you will get MQTT credentials.
  2. Add the credentials in below code:-


This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
1. If you have not already installed the ESP8266 Board Package install it using the instructions here:
2. Select your ESP8266 board from the Tools menu.
3. Set the Cayenne authentication info to match the authentication info from the Dashboard.
4. Set the network name and password.
5. Compile and upload the sketch.
6. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.

#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

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

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "MQTT_USERNAME";
char password[] = "MQTT_PASSWORD";
char clientID[] = "CLIENT_ID";

#define ACTUATOR_PIN 4

void setup() {
	Cayenne.begin(username, password, clientID, ssid, wifiPassword);

void loop() {

// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
	// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
	Cayenne.virtualWrite(0, millis());
	// 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.
// This function is called when data is sent from Cayenne.
	int value = getValue.asInt();
	CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
	// Write the value received to the digital pin.
	digitalWrite(ACTUATOR_PIN, value);
  1. Add the green widget to your dashboard.
  2. Navigate to Add new ----> Device/Widgets —> Actuator ----> Generic -----> Digital Output —> Fill all details with channel 1 select and click add.


Thanks for the code. I have used this code and its correct. I have added the widget in dashboard but cant change the state of relay.

Do you know why is happening this, can you help me to solve this problem ?

add this in your code and see what do you get in the serial monitor when you press the button.

This is what i get in serial monitor:

23:29:40.582 -> [35770] Connection ok
23:29:41.597 -> [36784] Publish: topic 1, channel 0, value 36784, subkey , key
23:29:48.806 -> [44006] Message received: topic 2, channel 1
23:29:48.874 -> [44006] In: value 1, channel 1
23:29:48.907 -> [44006] Channel 1, pin 4, value 1
23:29:48.942 -> [44006] publishState: topic 1 channel 1
23:29:48.976 -> [44034] Publish: topic 1, channel 1, value 1, subkey , key
23:29:49.044 -> [44098] Send response: 3CIF4zp7nMsFw2y
23:29:50.736 -> [45926] Connection ok
23:29:55.774 -> [50975] Message received: topic 2, channel 1
23:29:55.842 -> [50975] In: value 0, channel 1
23:29:55.875 -> [50975] Channel 1, pin 4, value 0
23:29:55.909 -> [50976] publishState: topic 1 channel 1
23:29:55.943 -> [51003] Publish: topic 1, channel 1, value 0, subkey , key
23:29:56.011 -> [51067] Send response: qrDtJILLvLqEDzL
23:29:57.331 -> [52524] Publish: topic 1, channel 0, value 52524, subkey , key
23:30:01.422 -> [56600] Connection ok
23:30:11.595 -> [66787] Connection ok
23:30:12.614 -> [67802] Publish: topic 1, channel 0, value 67801, subkey , key
23:30:21.779 -> [76967] Connection ok

Can you tell me why i cant control the relay through Cayenne ?

Dear shramik_salgaonkar,

Now everything is ok, i can control the relay. I just changed the pin, now i am using pin 14 and with the same code.