I am unable to read out Temperature data from a DS1820 sensor, connected to Virtual Pin 7. I have a temperature widget defined for Pin 7 and it just shows a blank value. The problem seems to be that my function “CAYENNE_OUT(7)” is never called (verfied by a Serial.printl() at the start of the function). CAYENNE_IN functions to switch on and off LEDs work fine. This is my code (only the relevant parts):
void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
sensors.begin();
pinMode(D5, OUTPUT);
digitalWrite(D5, HIGH); // LED to indicate that setup is running
delay (1000);
digitalWrite(D5, LOW);
}
void loop() {
Cayenne.loop();
}
CAYENNE_OUT(7)
{
Serial.println(“Cayenne_out7”); //check whether the function has been called
// Send the command to get temperatures.
sensors.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Pin.
Cayenne.celsiusWrite(7, sensors.getTempCByIndex(0));
Serial.println (sensors.getTempCByIndex(0));
}
welcome to cayenne community @mike2506red
if possible can you upload the entire code you are using.
as in the above code you have not include cayenne library.
Sure - here it is - everything works, except for the CAYENNE_OUT() function.
// 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>
#include <RCSwitch.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// WiFi network info.
char ssid[] = "xxxx";
char wifiPassword[] = "yyy";
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "uuuu";
char password[] = "ppp";
char clientID[] = "ccc";
unsigned long lastMillis = 0;
boolean Ledflash = true;
boolean Blink = false;
RCSwitch mySwitch = RCSwitch();
OneWire oneWire(4);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
sensors.begin();
pinMode(D5, OUTPUT);
digitalWrite(D5, HIGH);
delay (1000);
digitalWrite(D5, LOW);
mySwitch.enableTransmit(2); // Pin "D4" = GPIO2 !!
}
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);
if (Blink) {
digitalWrite(D5, Ledflash);
Ledflash =! Ledflash;
//delay(100);
}
}
CAYENNE_OUT(7)
{
// Send the command to get temperatures.
Serial.println("Cayenne_out7");
sensors.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Pin.
Cayenne.celsiusWrite(7, sensors.getTempCByIndex(0));
Serial.println (sensors.getTempCByIndex(0));
// To send the temperature in Fahrenheit use the corresponding code below.
//Cayenne.fahrenheitWrite(VIRTUAL_PIN, sensors.getTempFByIndex(0));
}
CAYENNE_IN(5){
int currentValue = getValue.asInt(); // Hole Wert 0 oder 1
if (currentValue == 0) {
digitalWrite(D5, LOW);
Blink = false;
} else {
Blink = true;
}
//digitalWrite(D5, getValue.asInt());
}
CAYENNE_IN(2) // Funksteckdose Hof über Virtual Pin 2
{
// get value sent from dashboard
Serial.println("Cayennein2");
int currentValue = getValue.asInt(); // 0 to 1
// assuming you wire your relay as normally open
if (currentValue == 0) {
for(int i=0; i<4; i++){
mySwitch.switchOff("00100","00101");
// Serial.println("switch off");
delay(50);
}
} else {
for(int i=0; i<4; i++){
mySwitch.switchOn("00100","00101");
// Serial.println("switch on");
delay (50);
}
}
}
CAYENNE_IN(6) // Funksteckdose Teich über Virtual Pin 6
{
// get value sent from dashboard
int currentValue = getValue.asInt(); // 0 to 1
// assuming you wire your relay as normally open
if (currentValue == 0) {
for(int i=0; i<4; i++){
mySwitch.switchOff("00100","01000");
delay(50);
}
} else {
for(int i=0; i<4; i++){
mySwitch.switchOn("00100","01000");
delay (50);
}
}
}
CAYENNE_IN(4) // Funksteckdosen Garten über Virtual Pin 4
{
// get value sent from dashboard
int currentValue = getValue.asInt(); // 0 to 1
// assuming you wire your relay as normally open
if (currentValue == 0) {
for(int i=0; i<4; i++){
mySwitch.switchOff("00100","00100");
delay(50);
}
} else {
for(int i=0; i<4; i++){
mySwitch.switchOn("00100","00100");
delay (50);
}
}
}
//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");
//}
Thank you Shramik! This worked - even though I don’t completly understand why
So I guess I have to use CAYENNE_IN(Virtual_pin) functions only when I want to read data from a switch or button and then I have to add this button from the dashboard. When I want to
display something, the code should go to the main body of the loop() function and I should not add the widget from the dashboard, instead rely on the Cayenne.virtualWrite() function to put it there.
You’ve basically nailed it @mike2506red. To put it slightly more broadly, when using our MQTT connectivity, you only need to create widgets on the web dashboard for actuators, and then have appropriate code to handle them on the device side. For sensors you use virtualWrite() statements which when run will auto-create a temporary widgets which can then be made permanent and customized via the web (or mobile) dashboard.
Simple Home automation application. I have a banana and a palm tree in my garden and they are not made for winters in the Black Forest. When temperatures drop below -5°C the 8266 would switch on a heating device via an RC controlled plug. Information on temperature (and total #hours heating maybe) is fed back to the Cayenne dashboard. I tried last year with a simple thermostat-controlled heater, but that one was heating too much and one of the plants died. Still unclear how the 8266 and WLAN will behave at very low temperatures and potentially under snow (in a waterproof housing - of course). I guess I will find out…