156 lines
4.9 KiB
C++
156 lines
4.9 KiB
C++
/*
|
|
* Read multiple One-Wire DS18B20 probes and publish value on Domoticz with HTTP request
|
|
* Lecture multiple de sonde OneWire DS18B20 et plublication des mesures sur un serveur Domoticz requete HTTP
|
|
* Code adapté - Code adaptated
|
|
*
|
|
*/
|
|
#include <OneWire.h>
|
|
#include <DallasTemperature.h>
|
|
// Pour un ESP32 (le SDK Espressif doit être installé) | For ESP32 (Espressif SDK must be installed)
|
|
//#include <WiFi.h>
|
|
//#include <HTTPClient.h>
|
|
// Pour une carte ESP8266 | For ESP8266 development board
|
|
#include <ESP8266WiFi.h>
|
|
#include <ESP8266HTTPClient.h>
|
|
#include <PubSubClient.h>
|
|
|
|
// Data wire is plugged into port 4 on the Arduino or ESP32
|
|
#define ONE_WIRE_BUS A0
|
|
#define TEMPERATURE_PRECISION 10
|
|
|
|
// 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);
|
|
|
|
// Tableaux contenant l'adresse de chaque sonde OneWire | arrays to hold device addresses
|
|
DeviceAddress insideThermometer = { 0x28, 0x61, 0x64, 0x11, 0xB1, 0x94, 0xAE, 0xC };
|
|
//DeviceAddress outsideThermometer = { 0x28, 0xF4, 0xBC, 0x26, 0x0, 0x0, 0x80, 0x2B };
|
|
|
|
// Parametres WIFI - WiFi settings
|
|
#define wifi_ssid "Livebox-37cc"
|
|
#define wifi_password "8A6060920A8A86896F770F2C47"
|
|
|
|
// Paramètres HTTP Domoticz - HTTP Domoticz settings
|
|
const char* host = "192.168.1.3";
|
|
const int port = 81;
|
|
#define IDX_insideTemp 66
|
|
//#define IDX_outsideTemp 31
|
|
|
|
HTTPClient http;
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
|
|
// Connexion au réseau WiFi, connexion aux sondes
|
|
// Start WiFi connexion and probes
|
|
setup_wifi();
|
|
sensors.begin();
|
|
|
|
// locate devices on the bus
|
|
Serial.print("Locating devices...");
|
|
Serial.print("Found ");
|
|
Serial.print(sensors.getDeviceCount(), DEC);
|
|
Serial.println(" devices.");
|
|
|
|
// report parasite power requirements
|
|
Serial.print("Parasite power is: ");
|
|
if (sensors.isParasitePowerMode()) Serial.println("ON");
|
|
else Serial.println("OFF");
|
|
|
|
// Vérifie sir les capteurs sont connectés | check and report if sensors are conneted
|
|
if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
|
|
// if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1");
|
|
|
|
// set the resolution to 9 bit per device
|
|
sensors.setResolution(insideThermometer, TEMPERATURE_PRECISION);
|
|
// sensors.setResolution(outsideThermometer, TEMPERATURE_PRECISION);
|
|
|
|
// On vérifie que le capteur st correctement configuré | Check that ensor is correctly configured
|
|
Serial.print("Device 0 Resolution: ");
|
|
Serial.print(sensors.getResolution(insideThermometer), DEC);
|
|
Serial.println();
|
|
|
|
// Serial.print("Device 1 Resolution: ");
|
|
// Serial.print(sensors.getResolution(outsideThermometer), DEC);
|
|
// Serial.println();
|
|
}
|
|
|
|
void printTemperature(String label, DeviceAddress deviceAddress){
|
|
float tempC = sensors.getTempC(deviceAddress);
|
|
Serial.print(label);
|
|
tempC = 25.3;
|
|
if (tempC == -127.00) {
|
|
Serial.print("Error getting temperature");
|
|
} else {
|
|
// Format JSON à respecter pour l'API Domoticz - Domoticz JSON API
|
|
// /json.htm?type=command¶m=udevice&idx=IDX&nvalue=0&svalue=TEMP
|
|
// https://www.domoticz.com/wiki/Domoticz_API/JSON_URL%27s#Temperature
|
|
if ( label == "Inside : " ) {
|
|
String url = "/json.htm?type=command¶m=udevice&idx=";
|
|
url += String(IDX_insideTemp);
|
|
url += "&nvalue=0&svalue=";
|
|
url += String(tempC);
|
|
sendToDomoticz(url);
|
|
} else {
|
|
// String url = "/json.htm?type=command¶m=udevice&idx=";
|
|
// url += String(IDX_outsideTemp);
|
|
// url += "&nvalue=0&svalue=";
|
|
// url += String(tempC);
|
|
// sendToDomoticz(url);
|
|
}
|
|
}
|
|
}
|
|
|
|
void loop() {
|
|
Serial.print("Requesting temperatures...");
|
|
sensors.requestTemperatures();
|
|
Serial.println("DONE");
|
|
|
|
// print the device information
|
|
printTemperature("Inside : ", insideThermometer);
|
|
// printTemperature("Outside : ", outsideThermometer);
|
|
|
|
delay(10000);
|
|
}
|
|
|
|
//Connexion au réseau WiFi
|
|
void setup_wifi() {
|
|
delay(10);
|
|
Serial.println();
|
|
Serial.print("Connecting to ");
|
|
Serial.println(wifi_ssid);
|
|
|
|
WiFi.begin(wifi_ssid, wifi_password);
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
|
|
Serial.println("");
|
|
Serial.println("WiFi connexion OK ");
|
|
Serial.print("=> Addresse IP : ");
|
|
Serial.print(WiFi.localIP());
|
|
}
|
|
|
|
|
|
void sendToDomoticz(String url){
|
|
Serial.print("Connecting to ");
|
|
Serial.println(host);
|
|
Serial.print("Requesting URL: ");
|
|
Serial.println(url);
|
|
http.begin(host,port,url);
|
|
int httpCode = http.GET();
|
|
if (httpCode) {
|
|
if (httpCode == 200) {
|
|
String payload = http.getString();
|
|
Serial.println("Domoticz response ");
|
|
Serial.println(payload);
|
|
}
|
|
}
|
|
Serial.println("closing connection");
|
|
http.end();
|
|
}
|