Ds18b20 issues

Hi guys, im having some issues with cayenne and getting sensors going - this code (or very close to it) was going fine, then i changed some things unrelated to the onewire sensors and they’re just coming up with -127 degrees.

If i comment out the cayenne.begin part the serial monitor fills with the data i would expect.

Is there some stuff going on in the cayenne libraries that could stop this working? very confused, not a coder; strange to me that it was working then it just stopped.

Cheers

#define CAYENNE_PRINT Serial
#include <OneWire.h>
#include <DallasTemperature.h>
#include <CayenneMQTTEthernet.h>

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

#define ONEWIRESENSOR_PIN 10 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define OUTSIDE_NUMERIC 5
#define CEILING_NUMERIC 6
#define ROOM_NUMERIC 7
#define OUTSIDE_GRAPH 8
#define CEILING_GRAPH 9
#define ROOM_GRAPH 10
#define MOISTURE_VALUE 11
#define LIGHT_VALUE 15

const float AsensorPin = A0; // select the input pin for the potentiometer
const int relayPin = 2; // select the pin for the relay
float moisture = 0; // variable to store the value coming from the sensor
const int fanPin = 4; // fan output pin
const float LightPin = A1; // light pin
float light = 0; // light variable

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

void setup()
{
pinMode(relayPin, OUTPUT);
pinMode(fanPin, OUTPUT);
pinMode(AsensorPin, INPUT);
pinMode(LightPin, INPUT);
Serial.begin(9600);
Cayenne.begin(username, password, clientID);
sensors.begin();
}

void loop()

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

// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(OUTSIDE_NUMERIC)
//{

// Send the command to get temperatures.
sensors.requestTemperatures();
light = analogRead(LightPin);
moisture = analogRead(AsensorPin);
moisture = (moisture / 700) * 100;
sensors.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Channel.

Serial.println(“”);
Serial.println(“Temperatures in house”);
Serial.println(“=====================”);

// OUTSIDE SENSOR TEMP

Cayenne.celsiusWrite(OUTSIDE_NUMERIC, sensors.getTempCByIndex(0));
Cayenne.celsiusWrite(OUTSIDE_GRAPH, sensors.getTempCByIndex(0));
Serial.print("Temperature OUTSIDE is: ");
Serial.println(sensors.getTempCByIndex(0));

// CEILING SENSOR TEMP

Cayenne.celsiusWrite(CEILING_NUMERIC, sensors.getTempCByIndex(1));
Cayenne.celsiusWrite(CEILING_GRAPH, sensors.getTempCByIndex(1));
Serial.print("Temperature CEILING is: ");
Serial.println(sensors.getTempCByIndex(1));

// ROOM SENSOR TEMP

Cayenne.celsiusWrite(ROOM_NUMERIC, sensors.getTempCByIndex(2));
Cayenne.celsiusWrite(ROOM_GRAPH, sensors.getTempCByIndex(2));
Serial.print("Temperature ROOM is: ");
Serial.println(sensors.getTempCByIndex(2));

// MOISTURE PRINT

Cayenne.virtualWrite(MOISTURE_VALUE, moisture, UNIT_PERCENT);
Serial.print(“Soil Moisture Percentage is: “);
Serial.print(moisture);
Serial.print(” %”);
Serial.println(" ");

// LIGHT PRINT

Cayenne.virtualWrite(LIGHT_VALUE, light, UNIT_PERCENT);
Serial.print("Light Level is: “);
Serial.print(light);
Serial.println(” ");

////////////////////////////////// HRV SYSTEM ///////////////////////////////////////

if (sensors.getTempCByIndex(1) < sensors.getTempCByIndex(2))
{
Serial.println(“high temp”);
digitalWrite(fanPin, HIGH);
}

else
{
Serial.println(“low temp”);

digitalWrite(fanPin, LOW);

}

////////////////////////////////// WATERING SYSTEM ///////////////////////////////////////

if (moisture < 30 and light < 300)
{
Serial.println(“Soil Moisture low, too bright to water”);
digitalWrite(relayPin, LOW);
}

else

if (moisture < 30 and light > 300)
{
Serial.println(“Soil Moisture low, Light level low - watering”);
digitalWrite(relayPin, HIGH);
}
else
{
Serial.println(“soil OK”);

digitalWrite(relayPin, LOW);

}

Serial.println(“end of loop”);

}

ignore the obvious commented out parts - this was just to get it working. The other obvious thing is im not 100% sure on how you’re meant to code the non-cayenne parts into your loop - is there a good example of this?

Ideally id like to use cayenne as a simple web monitor, Im not worried about triggers or actuators at this stage, just having the data present online would be useful.

have a look at this code Converting Cayenne Arduino LIbrary sketches to Cayenne MQTT Arduino - #2 by rsiegel

Hi, i have read conversion sketch you linked - as i said in my post, it was going, then it suddenly wasnt under mqtt. Is there a way of seeing exactly what the cayenne part of the code is doing, because if i comment it out it all works fine. All i want cayenne to do is to publish data to its mqtt cloud, without screwing with the rest of the sketch - is this possible? can i have some kind of code fallback that if cayenne doesnt connect it doesnt prevent the rest of the sketch from running?

Also, i am being booted off the mqtt mydevices server constantly when i try to connect to it (even with basic sketches). Why would this be, is it a server fault or am i sending too much data?

add this to check

     #define CAYENNE_DEBUG

in the previous link there is DS18B20 example. give it a try.

You have an electrical problem. Check wires, soldering, pull-up resistor, etc.

127 in binary 1111111 - not getting any signal

yeah, i thought so too - but without the Cayenne parts of the sketch it works perfectly :frowning:

Ok, ive added the sketch above and it wont read the sensor - if i use the example in the onewire library it works perfectly, or if i remove the cayenne parts of the sketch and just print the value, it works. Theres something in the Cayenne code thats causing this…? There seems to be a little bit of voltage sending over the source/sensor wire on the one wire bus with the cayenne code enabled, i have a ds18b20 with an inbuilt LED, and it glows very slightly with the Cayenne code, but doesnt with it enabled - it also pulses bright when the bus sends a signal, not so with the cayenne code enabled.

Highly frustrating! this was working perfectly earlier in the day…

can you share both the codes.

Ok, so i managed to sort it but im not 100% sure how. Ive attached the code im using now, and its working perfectly. Im not 100% but i think it might have been to do with code orders (where i put the define for the channels?), the channels i used perhaps (after seeing the debug i avoided the channel numbers its using for other things, im not sure this is neccessary) - and i originally had wire.h in the sketch, as soon as i deleted that, was the first time it started working with the sensor. At any rate, i hope my code might help someone else trying to sort it. It was possibly to do with the functions within the main cayenne loop as im still learning syntax!

/*
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  // Comment this out to disable prints and save space
#include <OneWire.h>
#include <DallasTemperature.h>
#include <CayenneMQTTEthernet.h>



const float AsensorPin = A0;          // select the input pin for the potentiometer
const int relayPin = 2;               // select the pin for the relay
float moisture = 0;                   // variable to store the value coming from the sensor
const int fanPin = 4;                 // fan output pin
const float LightPin = A1;            // light pin
float light = 0;                      // light variable

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

////////////////VIRTUAL CHANNELS/////////////////

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


#define OUTSIDE_NUMERIC 10
#define CEILING_NUMERIC 11
#define ROOM_NUMERIC 12
#define OUTSIDE_GRAPH 20
#define CEILING_GRAPH 21
#define ROOM_GRAPH 22
#define MOISTURE_VALUE 15
#define LIGHT_VALUE 16

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

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

void loop()
{
  Cayenne.loop();


// This function is called at intervals to send sensor data to Cayenne.
// Send the command to get temperatures.

  sensors.requestTemperatures();
  Serial.println("");
  Serial.println("Temperatures in house");
  Serial.println("=====================");
 
// OUTSIDE SENSOR TEMP

  Serial.print("Temperature OUTSIDE is: ");
  Serial.println(sensors.getTempCByIndex(0));

// CEILING SENSOR TEMP

  Serial.print("Temperature CEILING is: ");
  Serial.println(sensors.getTempCByIndex(1));

// ROOM SENSOR TEMP

  Serial.print("Temperature ROOM is: ");
  Serial.println(sensors.getTempCByIndex(2));

// MOISTURE PRINT

  moisture = analogRead(AsensorPin);
  moisture = (moisture / 700) * 100;
  Serial.print("Soil Moisture Percentage is: ");
  Serial.print(moisture);
  Serial.print(" %");
  Serial.println(" ");

// LIGHT PRINT

  light = analogRead(LightPin);
  Serial.print("Light level is:  ");
  Serial.print(light);




   // This command writes the temperature in Celsius to the Virtual Channel.








  
  Cayenne.celsiusWrite(OUTSIDE_NUMERIC, sensors.getTempCByIndex(0));
  Cayenne.celsiusWrite(CEILING_NUMERIC, sensors.getTempCByIndex(1));
  Cayenne.celsiusWrite(ROOM_NUMERIC, sensors.getTempCByIndex(2));
  Cayenne.virtualWrite(LIGHT_VALUE, light, UNIT_PERCENT);
  Cayenne.virtualWrite(MOISTURE_VALUE, moisture, UNIT_PERCENT);
  
  // To send the temperature in Fahrenheit use the corresponding code below.
  //Cayenne.fahrenheitWrite(VIRTUAL_CHANNEL, sensors.getTempFByIndex(0));
}
/* 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  // Comment this out to disable prints and save space
#include <OneWire.h>
#include <DallasTemperature.h>
#include <CayenneMQTTEthernet.h>



const int AsensorPin = A0;          // select the input pin for the potentiometer
const int relayPin = 2;               // select the pin for the relay
int moisture = 0;                   // variable to store the value coming from the sensor
const int fanPin = 4;                 // fan output pin
const int LightPin = A1;            // light pin
int light = 0;                      // light variable

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

////////////////VIRTUAL CHANNELS/////////////////

#define SENSOR_PIN 7 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define OUTSIDE_NUMERIC 10
#define CEILING_NUMERIC 11
#define ROOM_NUMERIC 12
#define OUTSIDE_GRAPH 20
#define CEILING_GRAPH 21
#define ROOM_GRAPH 22
#define MOISTURE_VALUE 15
#define LIGHT_VALUE 16


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

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

void loop()
{
  Cayenne.loop();


// This function is called at intervals to send sensor data to Cayenne.
// Send the command to get temperatures.

  sensors.requestTemperatures();
  Serial.println("");
  Serial.println("Temperatures in house");
  Serial.println("=====================");
 
// OUTSIDE SENSOR TEMP

  Serial.print("Temperature OUTSIDE is: ");
  Serial.println(sensors.getTempCByIndex(0));

// CEILING SENSOR TEMP

  Serial.print("Temperature CEILING is: ");
  Serial.println(sensors.getTempCByIndex(1));

// ROOM SENSOR TEMP

  Serial.print("Temperature ROOM is: ");
  Serial.println(sensors.getTempCByIndex(2));

// MOISTURE PRINT

  moisture = analogRead(AsensorPin);
  moisture = (moisture / 700) * 100;
  Serial.print("Soil Moisture Percentage is: ");
  Serial.print(moisture);
  Serial.print(" %");
  Serial.println(" ");

// LIGHT PRINT

  light = analogRead(LightPin);
  Serial.print("Light level is:  ");
  Serial.println(light);
  Serial.println(" ");
  Serial.println(" ");
  Serial.println(" ");

  ////////////////////////////////// HRV SYSTEM ///////////////////////////////////////

  if (sensors.getTempCByIndex(1) < sensors.getTempCByIndex(2))
                        {
                          Serial.println("high temp");
                          digitalWrite(fanPin, HIGH);
                        }

                              else
                              {
                                Serial.println("low temp");
                                digitalWrite(fanPin, LOW);
                              }

 ////////////////////////////////// WATERING SYSTEM ///////////////////////////////////////
/*
if (moisture < 30)
                {
                  Serial.println("Soil Moisture low, too bright to water");
                  digitalWrite(relayPin, LOW);
                }

else

if (moisture < 30)
                      {
                      Serial.println("Soil Moisture low, Light level low - watering");
                      digitalWrite(relayPin, HIGH);
                      } 


*/
/////////////////////////WRITE THIS DATA TO MQTT FOR CAYENNE/////////////////////////////

  Cayenne.celsiusWrite(OUTSIDE_NUMERIC, sensors.getTempCByIndex(0));
  Cayenne.celsiusWrite(CEILING_NUMERIC, sensors.getTempCByIndex(1));
  Cayenne.celsiusWrite(ROOM_NUMERIC, sensors.getTempCByIndex(2));
  Cayenne.virtualWrite(LIGHT_VALUE, light, UNIT_PERCENT);
  Cayenne.virtualWrite(MOISTURE_VALUE, moisture, UNIT_PERCENT);
  
  // To send the temperature in Fahrenheit use the corresponding code below.
  //Cayenne.fahrenheitWrite(VIRTUAL_CHANNEL, sensors.getTempFByIndex(0));
} 

Ok, so if i comment out the section under the watering system - it stops working correctly.
i think i am approaching the code size limit maybe of the Uno? Its not the code, because if i comment out the HRV section instead, the watering section works and the DOW sensors still go.

Live and learn?

Ok, just tried the code on a mega 2560 i had floating around for a 3d printer - code works perfectly. Obviously a limitation of the UNOs memory size, i guess?

make this change in your code.

    unsigned long lastMillis = 0;

//below in void loop:

    if (millis() - lastMillis > 10000) {
    lastMillis = millis();
    Cayenne.celsiusWrite(OUTSIDE_NUMERIC, sensors.getTempCByIndex(0));
    Cayenne.celsiusWrite(CEILING_NUMERIC, sensors.getTempCByIndex(1));
    Cayenne.celsiusWrite(ROOM_NUMERIC, sensors.getTempCByIndex(2));
    Cayenne.virtualWrite(LIGHT_VALUE, light, UNIT_PERCENT);
    Cayenne.virtualWrite(MOISTURE_VALUE, moisture, UNIT_PERCENT);

    // To send the temperature in Fahrenheit use the corresponding code below.
    //Cayenne.fahrenheitWrite(VIRTUAL_CHANNEL, sensors.getTempFByIndex(0));
  }

All good, have done - is updating slower now, i assume to stop the server being flooded? Boards working perfectly now :smiley: one thing i am however having trouble with - the cayenne app on ipad is not showing this board as online, its just grayed out - another board i have connected however is showing

1 Like

i am not sure about this bug. try to refresh the arduino and the app.