ESP8266 + ds18b20+Display+ Problems

I am using an ESP8266 12E with a waterproof ds18b20 sensor to monitor a fish tank. When the widget initially displays it appears to show Celsius values (24.6 for example) although it is set for Fahrenheit (and displays “Fahrenheit” at the bottom of the widget. I refresh the dashboard and the display refreshes and shows the “correct” temperature. A few seconds latter the display “automagically” changes back to displaying Celsius values while still showing “Fahrenheit” for the label. I have attached a link to a youtube vid showing the issue: - YouTube . The same widget on my phone shows the correct values all of the time. This would just be an annoyance except I have a trigger set to email me if the temperature falls below 60 degrees Fahrenheit. When the displayed values go to “Celsius (24.6 for example)” it triggers the alert and emails me.
Here is the code I’m running:
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct board and BAUD rate before compiling.

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <OneWire.h>
#include <DallasTemperature.h>
#include <CayenneMQTTESP8266.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>

// Data wire is conntec to the Arduino digital pin 4
#define ONE_WIRE_BUS 4

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature sensor

DallasTemperature DS18B20(&oneWire);

// WiFi network info.
char ssid = “XXXXXX”;
char wifiPassword = “XXXXXXXXX”;

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

unsigned long lastMillis = 0;

void SetupDS18B20(){
DS18B20.begin();
}

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

void loop() {
Cayenne.loop();
}

// Default function for sending sensor data at intervals to Cayenne.
CAYENNE_OUT_DEFAULT()
{
// Write data to Cayenne here.
DS18B20.requestTemperatures();
//Read temperature from DS18b20
float tempC = DS18B20.getTempCByIndex(0);
Cayenne.celsiusWrite(1, tempC);
//Switch wifi and wait
//WiFi.disconnect();
//WiFi.forceSleepBegin();
delay(6000);

//Switch wifi on again
//WiFi.mode(WIFI_STA);
//WiFi.begin(ssid, wifiPassword);

// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(200);
}

}

// 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(“Channel %u, value %s”, request.channel, getValue.asString());
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError(“Error message”);
}

Any idea why the widget changes for no apparent reason? Also, when the device sends the next packet/message the display remains displaying Celsius values until I manually refresh the dashboard.

can you try deleting the widget and when the device publish next data, a green widget will appear on the dashboard. Make it permanent by clicking on the + of the widget.

Thanks for the suggestion. Yes, already tried that ( a couple of
different times :-)). I’ve had this happen on a different project
using a raspberry pi too so it’s not something specific to the
esp8266. It seems to be an issue possibly with the dashboard since
the phone app reports the temperature correctly consistently. Did
you happen to view the video to see exactly what is happening? It’s
on my YouTube channel so please give it a look if you have a
chance. I’m really hoping to get this working.

Thanks again for the suggestion!!!

Mike

lets try this: Add a new Arduino device to your dashboard and use this code:

/*
Cayenne DS18B20 Example
This sketch shows how to send DS18B20 Sensor data to the Cayenne Dashboard.
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. Install the OneWire library (http://www.pjrc.com/teensy/td_libs_OneWire.html) from the Arduino Library Manager.
2. Install the DallasTemperature library (http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library#Code.2FLibrary) from the Arduino Library Manager.
3. Attach a DS18B20 to a digital pin on your Arduino.
   Schematic:
   [Ground] -- [DS18B20] -- [4.7k resistor] -- [5V]
                   |______________|
                   |
              Digital Pin
4. Set the SENSOR_PIN value below to the pin number you used when connecting the sensor.
5. Set the VIRTUAL_CHANNEL value below to a free virtual channel (or the virtual channel of a DS18B20 Sensor widget you have added) in the Dashboard.
6. Set the Cayenne authentication info to match the authentication info from the Dashboard.
7. Compile and upload this sketch.
8. Once the Arduino connects to the Dashboard it should automatically create a temporary display widget (or update the DS18B20 Sensor widget you have added) with data.
   To make a temporary widget permanent click the plus sign on the widget.
*/
#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <OneWire.h>
#include <DallasTemperature.h>
#include <CayenneMQTTESP8266.h>

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

#define SENSOR_PIN 2 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL 1

OneWire oneWire(SENSOR_PIN);
DallasTemperature sensors(&oneWire);

// WiFi network info.
 char ssid[] = "ssid";
 char wifiPassword[] = "wifiPassword";
void setup()
{
	Serial.begin(9600);
   Cayenne.begin(username, password, clientID, ssid, wifiPassword);
	sensors.begin();
}

void loop()
{
	Cayenne.loop();
}

// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(VIRTUAL_CHANNEL)
{
  	sensors.requestTemperatures();
	Cayenne.virtualWrite(VIRTUAL_CHANNEL, sensors.getTempCByIndex(0). "temp", "c");
}

I saw this code also. I noticed a couple of “issues” with it
though:

1) It doesn't include any information regarding connecting to my

local network and
2) I would like the temp to be displayed in Fahrenheit only.
3) There is no “delay” specified which I think might be an issue
since Cayenne doesn’t want a constant stream of data sent (no more
than 60 connections per minute according to the website).

Most similar code snippets I've looked at specify that the device

turn on, read the sensor, upload the data to Cayenne, go into
“forced sleep” for a certain period (usually 60000 mils), wake up,
and repeat the loop.

I'm aware that I can change the temperature display in the dashboard

but thought it might be the root of my issue since the code
specifies and defaults to Celsius. I would like to specify
Fahrenheit directly in the code so it is the default and remove all
references to Celsius.

Thanks for the suggestion!!

Mike

i have added it now in the code.

CAYENNE_OUT(VIRTUAL_CHANNEL)
{
  	sensors.requestTemperatures();
 	Cayenne.virtualWrite(VIRTUAL_CHANNEL, sensors.getTempFByIndex(0), "temp", "f");
}

have a look at this Sending MQTT messages within rate limits

Beautiful! It worked!!

It was caused by the code defaulting to Celsius like I thought :-). 

Been watching it for the last hour and it never switches back to
Celsius like it did before.

Thanks so much for your help!!!!

Mike
1 Like