[SOLVED] Multiple DS18B20


#1

Hi

I got this sketch for multiple DS18B20. It deliver temperatures from all sensors in terminal window, but i can’t figure out how to got all sensors to publish temperatures to ubidots.com. The sketch makes a new device and one variable.


#include <UbidotsESPMQTT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
/****************************************
 * Define Constants
 ****************************************/
#define TOKEN "XXXXXXXXXXXXXX" // Your Ubidots TOKEN
#define WIFINAME "XXXXXXX" //Your SSID
#define WIFIPASS "XXXXXX" // Your Wifi Pass
#define MQTTCLIENTNAME "XXXXXXX" // Your MQTT Client Name, it must be unique so we recommend to choose a random ASCCI name
#define Pin D3

float temp;
OneWire ourWire(Pin);
DallasTemperature sensors(&ourWire);

Ubidots client(TOKEN, MQTTCLIENTNAME);

/*-----( Declare Variables )-----*/
// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

DeviceAddress Probe01 = { 0x28, 0xFF, 0x8D, 0xB2, 0x2, 0x17, 0x4, 0x55 }; 
DeviceAddress Probe02 = { 0x28, 0xFF, 0x66, 0xB3, 0x2, 0x17, 0x4, 0x70 };
DeviceAddress Probe03 = { 0x28, 0xFF, 0x63, 0xF0, 0x2, 0x17, 0x3, 0x49 };
DeviceAddress Probe04 = { 0x28, 0xFF, 0x45, 0xDE, 0x2, 0x17, 0x4, 0xCB };
DeviceAddress Probe05 = { 0x28, 0xFF, 0x24, 0x24, 0x3, 0x17, 0x4, 0xD6 };
/****************************************
 * Auxiliar Functions
 ****************************************/
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
}
/****************************************
 * Main Functions
 ****************************************/
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  client.wifiConnection(WIFINAME, WIFIPASS);
  sensors.begin();
  client.begin(callback);

  // set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
  sensors.setResolution(Probe01, 10);
  sensors.setResolution(Probe02, 10);
  sensors.setResolution(Probe03, 10);
  sensors.setResolution(Probe04, 10);
  sensors.setResolution(Probe05, 10);
  }
void loop() {
  // put your main code here, to run repeatedly:
  if(!client.connected()){
      client.reconnect();
      }
  
  sensors.requestTemperatures();       //Prepare the sensor for reading

  Serial.print("Probe 01 temperature is:   ");
  printTemperature(Probe01);
  Serial.println();

  Serial.print("Probe 02 temperature is:   ");
  printTemperature(Probe02);
  Serial.println();
 
  Serial.print("Probe 03 temperature is:   ");
  printTemperature(Probe03);
  Serial.println();
   
  Serial.print("Probe 04 temperature is:   ");
  printTemperature(Probe04);
  Serial.println();
  
  Serial.print("Probe 05 temperature is:   ");
  printTemperature(Probe05);
  Serial.println();
  
  temp = sensors.getTempCByIndex(0);
  
  Serial.print(sensors.getTempCByIndex(0)); //Read and print the temperature in Celsius
  client.add("temperature", temp); //Insert your variable Labels and the value to be sent
  delay(1000);
  Serial.println("Grader");
  client.ubidotsPublish("motorrom");
  client.loop();
  }
  /*-----( Declare User-written Functions )-----*/
void printTemperature(DeviceAddress deviceAddress)
{

float tempC = sensors.getTempC(deviceAddress);

   if (tempC == -127.00) 
   {
   Serial.print("Error getting temperature  ");
   } 
   else
   {
   Serial.print("C: ");
   Serial.print(tempC);
   Serial.print(" F: ");
   Serial.print(DallasTemperature::toFahrenheit(tempC));
   }
}// End printTemperature
//*********( THE END )***********

Please help me.

Regards

Kjenset


#2

Hi there, use the add()five times to send individually your sensor’s reads, i.e if you want to send two different temperature sensors, the piece of code inside your firmware should look like the one below:

temp1 = sensors.getTempCByIndex(0);
client.add("temp-probe01", temp1);
temp2 = sensors.getTempCByIndex(1);
client.add("temp-probe02", temp2);
client.ubidotsPublish("motorrom");
client.loop();

Regards


#3

Hi

I appreciate your help! When I tried to take your suggestion into my code I got these errors. Don’t be hard at me, I’m an beginner in coding… :wink:

Regards
Kristian


#4

You are receiving the issue ->“was not declared in this scope”, because the variables were not declared into the code. In order to fix the issue declare all the variable as float, for example:

float temp1 = sensors.getTempCByIndex(0);
float temp2 = sensors.getTempCByIndex(1);

All the best,
Maria C.


#5

Your are my heros!

Is works, but when I add more than three “client.add”-lines it stop working. When I add the fourth line it don’t add another value at ubidots.com and the other values freezes. Is it some restriction as it don’t allow more than three variables in one device? Or is it something in my code as make the error?

Regards
Kristian


#6

The Ubidots MQTT library depends on a Pubsubclient client and the max length of the JSON dictionary to send by default is 128 bytes, if you want to send more than 3 variables please reference to the README of the library and follow the instrucctions provided.

All the best,
Maria C.


#7

Believe it or not; I got it working! Learing new things every day. :slight_smile:

I have now connected five DS18B20-sensors (cheap ones), they show a bit different temperature. I it possible to set an temperature correction in the code? Or maybe it is the powersupply that is to week? What do you think?

I appreciate your help!

Regards
Kristian


#8

Hi there, take a look to the datasheet of the device on page 3, it seems that it has errors for temperatures range higher than 20 °C so that it is the reason for the differente lectures from your sensors. Also, if you use a higher resolution ADC than the one embedded in the Arduino or ESP boards, you may reduce your lecture errors.

Regards