Esp8266 wifi and ds18b20

Hello,

I got a esp8266 up with one temp.sensor (DS18B20).
It works very well, but when i was trying to use one more sensor, then i got some trouble.
How can i insert one more DS18B20 in this code:

#include <CayenneMQTTESP8266.h>
#include <CayenneMQTTWiFiClient.h>

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial

//For temperaturføler
#include <DallasTemperature.h>
#include<OneWire.h>

char ssid = “”;
char password = “”;

char username = “”;
char mqtt_password = “”;
char client_id = “”;

//temperatur
#define ONE_WIRE_BUS 12

OneWire onewire(ONE_WIRE_BUS);
DallasTemperature sensors(&onewire);
float Celcius=0;

void setup() {
// put your setup code here, to run once:

Serial.begin(9600);

Cayenne.begin(username, mqtt_password, client_id, ssid, password);
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
sensors.begin();
}

void loop() {
// put your main code here, to run repeatedly:

sensors.requestTemperatures();
Celcius=sensors.getTempCByIndex(0);
Serial.print(" C ");
Serial.println(Celcius);

Cayenne.loop();

sensors.requestTemperatures();
Cayenne.celsiusWrite(1, sensors.getTempCByIndex(0));

}
CAYENNE_IN(0)
{
digitalWrite(2, !getValue.asInt());

}

you can get the second sensor reading using temp2 = sensors.getTempCByIndex(i);

Continuing the discussion from Esp8266 wifi and ds18b20:

Did try like this, but it wont work.
The first sensor send right temp to cayenne desktop and the second sensor is shown in the desktop ut the value is wrong, it just say -127.

#include <CayenneMQTTESP8266.h>
#include <CayenneMQTTWiFiClient.h>

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial

//For temperaturføler
#include <DallasTemperature.h>
#include<OneWire.h>

char ssid = “”;
char password = “”;

char username = “”;
char mqtt_password = “”;
char client_id = “”;

//temperatur
#define ONE_WIRE_BUS 12
#define ONE_WIRE_BUSb 13 //føler 2

OneWire onewire(ONE_WIRE_BUS);
DallasTemperature sensors(&onewire);

OneWire onewireb(ONE_WIRE_BUSb); //føler 2
DallasTemperature sensorsb(&onewireb); //føler 2

float Celcius=0;

void setup() {
// put your setup code here, to run once:

Serial.begin(9600);

Cayenne.begin(username, mqtt_password, client_id, ssid, password);
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
sensors.begin();
}

void loop() {
// put your main code here, to run repeatedly:

sensors.requestTemperatures();
Celcius=sensors.getTempCByIndex(0);
Serial.print(" C ");
Serial.println(Celcius);

Cayenne.loop();

sensors.requestTemperatures();
Cayenne.celsiusWrite(1, sensors.getTempCByIndex(0));

sensorsb.requestTemperatures(); //føler 2
Cayenne.celsiusWrite(2, sensorsb.getTempCByIndex(0)); //føler 2
}
CAYENNE_IN(0)
{
digitalWrite(2, !getValue.asInt());

}

First of all, you are sending data continuously in the main loop. have a look at this topic on Sending MQTT messages within rate limits

your code is quite bad. you don’t need to setup 2 onewire instances. you can just the sensor data by using the different index. Have a look at this tutorial https://lastminuteengineers.com/multiple-ds18b20-arduino-tutorial/ and see if you can read data from both sensors.

Once done, edit the cayenne code accordingly.

Thank for the help, that guide did help me alot:)

2 Likes