Relay control based on temperature (using ESP8266 and Cayenne)

Hi,

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.

Regards,
Fatmir Prekupi

have you tried connecting your esp to cayenne? Have a look at the example folder to get the all the required codes Cayenne-MQTT-Arduino/examples at master ยท myDevicesIoT/Cayenne-MQTT-Arduino ยท GitHub

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 ?

Dear,

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:-

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.
Steps:
1. If you have not already installed the ESP8266 Board Package install it using the instructions here: https://github.com/esp8266/Arduino#installing-with-boards-manager.
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_DEBUG
#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 VIRTUAL_CHANNEL 1
#define ACTUATOR_PIN 4

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

void loop() {
	Cayenne.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.
CAYENNE_OUT_DEFAULT()
{
	// 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.
CAYENNE_IN(VIRTUAL_CHANNEL)
{
	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.

Hi,

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.

Thanks.

2 Likes