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