Fixed connection between multiple DS18B20 sensors and cloud variables

So after a wile playing and a few basic tutorials. I finally started with my first serious project.

Scope of project: at the end, I like all variables of our home like energy usage, energy production, temperatures in house and temperatures of our heating pump in a dashboard.

First part of my mission is to connect multiple temperature sensors on the complete heating system. We are the owners of a heating pump and I want to learn in detail how the system reacts to tweaking. This is not only for personal use, but I think it will provide me with knowledge that is usefull to use professional ( technical installer).

So part one:

Create a dashboard with sensors to see temperature through several DS1820 sensors. Ive got the necessary items, created a sketch and installed the dashboard. Everything is working fine but one thing:

Make a fixed connection between a DS18B20 sensor and variabel in the dashboard.

I've re-write/used a lot of sketches to work with the factory serial of the sensor, but nothing wil work.
Perhaps someone can take a look and guide me (or deliver the piece of code) to the solution.

Thanks in advance. Your input is appriciated
Marco


------ Sketch--------

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/714ef962-fa37-4453-835a-dbfa8ab9b190 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  float temp100;
  float temp200;
  float temp300;
  float temp400;
  float temp500;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"
#include <OneWire.h>
#include <DallasTemperature.h>

// Define variables for DS1820 sensors
#define ONE_WIRE_BUS 26 // Pin for OneWire communication
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(5000); 
  sensors.begin(); // initialize DallasTemperature library

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
  // Your code here 
  // Request temperature to all devices on the data line
  byte addr[8];

    sensors.requestTemperatures();

  
  

 
  // We can connect morte tan one IC on the same datawire. 0 refers to the first IC on  the wire
 // -Serial.print(sensors.getTempCByIndex(0));
 // -Serial.print(sensors.getTempCByIndex(1));
 // -Serial.print(" - Fahrenheit temperature; ");
 Serial.print("Celsius temperature 100: ");
     Serial.println(sensors.getTempCByIndex(0));
 Serial.print("Celsius temperature 200: ");
     Serial.println(sensors.getTempCByIndex(1));
 Serial.print("Celsius temperature 300: ");
     Serial.println(sensors.getTempCByIndex(2));
 Serial.print("Celsius temperature 400: ");
     Serial.println(sensors.getTempCByIndex(3));
 Serial.print("Celsius temperature 500: ");
     Serial.println(sensors.getTempCByIndex(4));
 // -Serial.println(sensors.getTempFByIndex(1));

  temp100=(sensors.getTempCByIndex(0));
  temp200=(sensors.getTempCByIndex(1));
  temp300=(sensors.getTempCByIndex(2));
  temp400=(sensors.getTempCByIndex(3));
  temp500=(sensors.getTempCByIndex(3));
  int tempC=(sensors.getTempCByIndex(0));

// -int tempF=sensors.getTempFByIndex(0);
// -Wait for 15 seconds before sending data again

  delay(5000);
}

/*
  Since Teet is READ_WRITE variable, onTeetChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onTeetChange()  {
  // Add your code here to act upon Teet change
}


----- thinproperties.h ---------------------------

#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

const char DEVICE_LOGIN_NAME[]  = "XXXX";

const char SSID[]               = SECRET_SSID;    // Network SSID (name)
const char PASS[]               = SECRET_OPTIONAL_PASS;    // Network password (use for WPA, or use as key for WEP)
const char DEVICE_KEY[]  = SECRET_DEVICE_KEY;    // Secret device password


float temp100;
float temp200;
float temp300;
float temp400;
float temp500;

void initProperties(){

  ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
  ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
  ArduinoCloud.addProperty(temp100, READ, 10 * SECONDS, NULL);
  ArduinoCloud.addProperty(temp200, READ, 10 * SECONDS, NULL);
  ArduinoCloud.addProperty(temp300, READ, 10 * SECONDS, NULL);
  ArduinoCloud.addProperty(temp400, READ, 10 * SECONDS, NULL);
  ArduinoCloud.addProperty(temp500, READ, 10 * SECONDS, NULL);

}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);



Hi @mjammie.

Please provide a detailed description of what you mean by "nothing wil work" in a reply on this forum thread, including:

  • What did you do?
  • What were the results you expected from doing that thing?
  • What were the results you observed that did not match your expectations?

Make sure to include the full and exact text of any error or warning messages you might have encountered.

Nothing wil work means:

The uploaded sketch works fine. Sensor readings are found and are send to the cloud.
Just the change/adding a piece of code that connects a sensor (based on factory adres/ hex adress) to specific cloud variable is an issue. I don't seem to get it right.

Every time I take the uploaded sketch as starting point an try to adjust it with different code and eventually I went searching on the web/other projects/fora. But all I put in the code results in errors. Most of them are related to unknow functions like "getadress" or Serial.println(sensors.getAddress(i), HEX);.

I also took some time to take the code generator with different approaches to see how the generator builds up code to learn from it. But also this won't help me to get on the right track.

Please post your best effort code that you are having a problem with.

Please also post the full and exact text of the error messages you get when you try to compile that code.

I've made variuos code's so I will post the code I think is the most promessing (It's missing three sensors, that is correct). It's accepted through the compolier, I can see temperature output in the serial, but the error: IArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to iot.arduino.cc:8883 Error: -2 stays present.

After searching at the fora I checked some solutions (new account, erase the device and make new, new sketch etc etc)


--- Sketch ---
/* 
 

  Arduino IoT Cloud Variables description


  float temp100;
  float temp200;
  float temp300;
  float temp400;
  float temp500;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"
#include <OneWire.h>
#include <DallasTemperature.h>




// Define variables for DS1820 sensors
#define ONE_WIRE_BUS 26 // Pin for OneWire communication

// Define serial numbers of DS18B20 sensors
DeviceAddress sensor1 = {0x28, 0x61, 0x64, 0xA, 0x70, 0x7A, 0x1B, 0xFC};
DeviceAddress sensor2 = {0x28, 0x61, 0x64, 0xA, 0x72, 0x36, 0x7D, 0xDD};


// Initialize OneWire and DallasTemperature libraries
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// Initialize Arduino IoT Cloud
 void initArduinoIoTCloud() {
 // ArduinoCloud.setThingId("df723042-8e3b-4c6a-8161-dea51ed37043");
 // ArduinoC//loud.addProperty(temp100, READ, ON_CHANGE, NULL);
//  ArduinoCloud.addProperty(temp200, READ, ON_CHANGE, NULL);
}


void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(5000); 
  sensors.begin(); // initialize DallasTemperature library

  // Register sensors with their respective serial numbers
  sensors.setResolution(sensor1, 12);
  sensors.setResolution(sensor2, 12);
  
  // Defined in thingProperties.h
  // initProperties();

  // Initialize Arduino IoT Cloud
  initArduinoIoTCloud();
  
  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(1);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  
    // Update sensor readings
  sensors.requestTemperatures();

  // Read temperature from each sensor
  temp100 = sensors.getTempC(sensor1);
  temp200 = sensors.getTempC(sensor2);
  
  ArduinoCloud.update();
  // Your code here 
  

  
  

 
  // We can connect morte tan one IC on the same datawire. 0 refers to the first IC on  the wire
 // -Serial.print(sensors.getTempCByIndex(0));
 // -Serial.print(sensors.getTempCByIndex(1));
 // -Serial.print(" - Fahrenheit temperature; ");
 Serial.print("Celsius temperature 100: ");
     Serial.println(sensors.getTempCByIndex(0));
 Serial.print("Celsius temperature 200: ");
     Serial.println(sensors.getTempCByIndex(1));
 Serial.print("Celsius temperature 300: ");
     Serial.println(sensors.getTempCByIndex(2));
 Serial.print("Celsius temperature 400: ");
     Serial.println(sensors.getTempCByIndex(3));
 Serial.print("Celsius temperature 500: ");
     Serial.println(sensors.getTempCByIndex(4));
 // -Serial.println(sensors.getTempFByIndex(1));

  temp100=(sensors.getTempCByIndex(0));
  temp200=(sensors.getTempCByIndex(1));
  temp300=(sensors.getTempCByIndex(2));
  temp400=(sensors.getTempCByIndex(3));
  temp500=(sensors.getTempCByIndex(3));
  int tempC=(sensors.getTempCByIndex(0));

// -int tempF=sensors.getTempFByIndex(0);
// -Wait for 15 seconds before sending data again

  delay(5000);
}

/*
  Since Teet is READ_WRITE variable, onTeetChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onTeetChange()  {
  // Add your code here to act upon Teet change
}



-- thingProperties.h ---



#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

const char DEVICE_LOGIN_NAME[]  = "XXXXXXXX";

const char SSID[]               = SECRET_SSID;    // Network SSID (name)
const char PASS[]               = SECRET_OPTIONAL_PASS;    // Network password (use for WPA, or use as key for WEP)
const char DEVICE_KEY[]  = SECRET_DEVICE_KEY;    // Secret device password


float temp100;
float temp200;
float temp300;
float temp400;
float temp500;

void initProperties(){

  ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
  ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
   ArduinoCloud.addProperty(temp100, READ, 10 * SECONDS, NULL);
  ArduinoCloud.addProperty(temp200, READ, 10 * SECONDS, NULL);
  ArduinoCloud.addProperty(temp300, READ, 10 * SECONDS, NULL);
  ArduinoCloud.addProperty(temp400, READ, 10 * SECONDS, NULL);
  ArduinoCloud.addProperty(temp500, READ, 10 * SECONDS, NULL);

}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

Thanks grtz

Oeps, just a moment. Mayby I've solved it. Ill be back.

Yes first part is correct. i now can send data to the clout based on a fixed connection between (for example) sensor 1 to dedicated variabele. When i now put additional sensors in the line, I can ad them based on serial connected to fixed variabel.

Just one part to do: Getting a readout in the dashboard with two digits. Any advise?


/* 

  float temp100;
  float temp200;
  float temp300;
  float temp400;
  float temp500;
  float temp600;
  float temp700;
  float temp800;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"
#include <OneWire.h>
#include <DallasTemperature.h>

// Define variables for DS1820 sensors
#define ONE_WIRE_BUS 26 // Pin for OneWire communication

// Define serial numbers of DS18B20 sensors
DeviceAddress sensor1 = {0x28, 0x61, 0x64, 0xA, 0x70, 0x7A, 0x1B, 0xFC};
DeviceAddress sensor2 = {0x28, 0x61, 0x64, 0xA, 0x72, 0x36, 0x7D, 0xDD};
DeviceAddress sensor3 = {0x28, 0x61, 0x64, 0xA, 0x70, 0x7B, 0x52, 0xE2};
DeviceAddress sensor4 = {0x28, 0x61, 0x64, 0xA, 0x73, 0x36, 0x6, 0xAE};
DeviceAddress sensor5 = {0x28, 0x61, 0x64, 0xA, 0x73, 0x75, 0x6B, 0xF8};
DeviceAddress sensor6 = {0x28, 0x61, 0x64, 0xA, 0x70, 0x2A, 0x1E, 0xB4};
DeviceAddress sensor7 = {0x28, 0x61, 0x64, 0xA, 0x70, 0x7A, 0x84, 0xAC};
DeviceAddress sensor8 = {0x28, 0x61, 0x64, 0xA, 0x70, 0x1D, 0x4B, 0x13};

// Initialize OneWire and DallasTemperature libraries
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// Initialize Arduino IoT Cloud
 void initArduinoIoTCloud() {
}


void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(5000); 
  sensors.begin(); // initialize DallasTemperature library

  // Register sensors with their respective serial numbers
  sensors.setResolution(sensor1, 9);
  sensors.setResolution(sensor2, 9);
  sensors.setResolution(sensor3, 9);
  sensors.setResolution(sensor4, 9);
  sensors.setResolution(sensor5, 9);
  sensors.setResolution(sensor6, 9);
  sensors.setResolution(sensor7, 9);
  sensors.setResolution(sensor8, 9);
  
  // Defined in thingProperties.h
  initProperties();

  // Initialize Arduino IoT Cloud
  initArduinoIoTCloud();
  
  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(4);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  
    // Update sensor readings
  sensors.requestTemperatures();

  // Read temperature from each sensor
  temp100 = sensors.getTempC(sensor1);
  temp200 = sensors.getTempC(sensor2);
  temp300 = sensors.getTempC(sensor3);
  temp400 = sensors.getTempC(sensor4);
  temp500 = sensors.getTempC(sensor5);
  temp600 = sensors.getTempC(sensor6);
  temp700 = sensors.getTempC(sensor7);
  temp800 = sensors.getTempC(sensor8);
  
  ArduinoCloud.update();
  // Your code here 
  
 Serial.print("Celsius temperature 100: ");
     //Serial.println(sensors.getTempCByIndex(0));
 Serial.println(sensors.getTempC(sensor1));
 Serial.print("Celsius temperature 200: ");
    Serial.println(sensors.getTempC(sensor2));
 Serial.print("Celsius temperature 300: ");
   Serial.println(sensors.getTempC(sensor3));;
 Serial.print("Celsius temperature 400: ");
    Serial.println(sensors.getTempC(sensor4));
 Serial.print("Celsius temperature 500: ");
     Serial.println(sensors.getTempC(sensor5));
 Serial.print("Celsius temperature 600: ");
     Serial.println(sensors.getTempC(sensor6));
 Serial.print("Celsius temperature 700: ");
     Serial.println(sensors.getTempC(sensor7));
 Serial.print("Celsius temperature 800: ");
     Serial.println(sensors.getTempC(sensor8));

  delay(5000);
}

/*
  Since Teet is READ_WRITE variable, onTeetChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onTeetChange()  {
  // Add your code here to act upon Teet change
}

- thingpropertie.h-

// Code generated by Arduino IoT Cloud, DO NOT EDIT.

#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

const char DEVICE_LOGIN_NAME[]  = "ac54ab3b-d18a-46b1-a5e6-f92b7482fa44";

const char SSID[]               = SECRET_SSID;    // Network SSID (name)
const char PASS[]               = SECRET_OPTIONAL_PASS;    // Network password (use for WPA, or use as key for WEP)
const char DEVICE_KEY[]  = SECRET_DEVICE_KEY;    // Secret device password


float temp100;
float temp200;
float temp300;
float temp400;
float temp500;
float temp600;
float temp700;
float temp800;

void initProperties(){

  ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
  ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
  ArduinoCloud.addProperty(temp100, READ, 5 * SECONDS, NULL);
  ArduinoCloud.addProperty(temp200, READ, 5 * SECONDS, NULL);
  ArduinoCloud.addProperty(temp300, READ, 5 * SECONDS, NULL);
  ArduinoCloud.addProperty(temp400, READ, 5 * SECONDS, NULL);
  ArduinoCloud.addProperty(temp500, READ, 5 * SECONDS, NULL);
  ArduinoCloud.addProperty(temp600, READ, 5 * SECONDS, NULL);
  ArduinoCloud.addProperty(temp700, READ, 5 * SECONDS, NULL);
  ArduinoCloud.addProperty(temp800, READ, 5 * SECONDS, NULL);

}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

Thanks grtz

I'm glad it is working now. Thank you for taking the time to post an update!

I'm not sure I understood correctly what you mean by this. Please provide a more detailed description of what you mean by this in a reply on this forum thread to help us to understand it.

Maybe you are asking about controlling the number of decimal points (which is not the same thing as the number of digits)?

Which Widget type did you use for your variables in the dashboard?

Thans for your reply.

Its about the decimal points. I'm using the Gauge-widget and the advanced chart-widget . In the Gauge-widget two decimals are enough.

grtz

Unfortunately I don't think there is any way to control the decimal points in the "Advanced Chart" widget.

You can control them in the configuration of the "Value" widget. Maybe it would be sufficient to add a value widget for each of the Variables in addition to the "Advanced Chart" widget? Something like this:

image

OK, clear.

For now I'll first concentrate on the adding the other temp sensors and heating sensor of our heating pump and heating applications.

To be complete, the outcome at this moment. An overview of temps of my heating pump and floorheating.

Thansk for your reply.

You are welcome. I'm glad if I was able to be of assistance.

Your dashboards look very nice!

Regards,
Per

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.