Hello,
I've found a example code from the internet that reads 4 x ds18b20 temperature sensors and I'm modifying it to send through MQTT. The sketch is written to call a function to serial print the sensor values one by one. I've added code in the function to then send the data to the MQTT server and the MQTT server receives the temperature details. However, it sends it with the same name for each ds18b20 sensor. Each sensor is about 1 meter away and i want to know which marry up the sensor name with the sensor values. Please help, I've included the snippit of the code
// the below code calls the function printTemperature
// Command all devices on bus to read temperature
sensors.requestTemperatures();
Serial.print("Probe 01 temperature is: ");
printTemperature(Probe01);
Serial.println();
// the function printTemperature send the reading to mqtt with the name device. I want this to be the name probeX e.g. probe1...probe4.
/*-----( 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));
//wchar_t device = deviceAddress;
//Serial.print (device) ;
if (client.connect("ClientXXX" )) {
client.publish("device",String(tempC).c_str()); //logs temperature of Oil under
client.subscribe("CoolantTemp");
}
delay(1000); // every one second.
client.loop();
}
}// End printTemperature
//*********( THE END )***********
Here's a full listing of the code just incase it helps :
/*
Basic MQTT example with Authentication
- connects to an MQTT server, providing username
and password
- publishes "hello world" to the topic "outTopic"
- subscribes to the topic "inTopic"
Todo : save each reading by name, i need to work out how the function works
*/
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(172,16,24,91); // IP Address of the client
IPAddress server(172,16,24,11); // The IP address of the MQTT server
void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
}
EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);
/*-----( 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
// end node labelled
DeviceAddress Probe01 = { 0x28, 0xFF, 0xFB, 0xD6, 0xA2, 0x16, 0x04, 0x08 };
DeviceAddress Probe02 = { 0x28, 0xFF, 0x4F, 0xD5, 0xA2, 0x16, 0x04, 0xD0 };
DeviceAddress Probe03 = { 0x28, 0x13, 0xBB, 0x03, 0x00, 0x00, 0x80, 0x27 };
DeviceAddress Probe04 = { 0x28, 0xFF, 0xDF, 0xAC, 0xA2, 0x16, 0x04, 0x5C };
void setup()
{
Ethernet.begin(mac, ip);
// Note - the default maximum packet size is 128 bytes. If the
// combined length of clientId, username and password exceed this,
// you will need to increase the value of MQTT_MAX_PACKET_SIZE in
// PubSubClient.h
if (client.connect("TBL165-26203" )) {
client.publish("outTopic","Hello world");
client.subscribe("inTopic");
}
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
sensors.begin();
// 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);
}
void loop()
{
Serial.println();
Serial.print("Number of Devices found on bus = ");
Serial.println(sensors.getDeviceCount());
Serial.print("Getting temperatures... ");
Serial.println();
// Command all devices on bus to read temperature
sensors.requestTemperatures();
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("Temperature is: ");
Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"?
// You can have more than one IC on the same bus.
// 0 refers to the first IC on the wire
float temp = 0.0;
temp = sensors.getTempCByIndex(0);
Ethernet.begin(mac, ip);
// Note - the default maximum packet size is 128 bytes. If the
// combined length of clientId, username and password exceed this,
// you will need to increase the value of MQTT_MAX_PACKET_SIZE in
// PubSubClient.h
}
/*-----( 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));
//wchar_t device = deviceAddress;
//Serial.print (device) ;
if (client.connect("ClientXXX" )) {
client.publish("device",String(tempC).c_str()); //logs temperature of Oil under
client.subscribe("CoolantTemp");
}
delay(1000); // every one second.
client.loop();
}
}// End printTemperature
//*********( THE END )***********