Je débute avec le Wemos D1 mini que je viens d'acheter sur Aliexpress plus une carte DS18B20.
J'ai réussi à téléverser le code suivant, mais mon capteur virtuel dans domoticz ne se mets pas à jour...
J'avoue que ça fait 3 heures que je me débat et que je commence à perdre espoir... d'où ce post.
Code : Tout sélectionner
/*
* 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 4
#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, 0xB2, 0x2F, 0x35, 0x16, 0x13, 0x1, 0x55 };
// Parametres WIFI - WiFi settings
#define wifi_ssid "wifi_maison"
#define wifi_password "mdp_perso"
// Paramètres HTTP Domoticz - HTTP Domoticz settings
const char* host = "192.168.1.100";
const int port = 8080;
#define IDX_insideTemp 621
HTTPClient http;
void setup() {
Serial.begin(115200);
// 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");
// set the resolution to 9 bit per device
sensors.setResolution(insideThermometer, 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();
}
void printTemperature(String label, DeviceAddress deviceAddress){
float tempC = sensors.getTempC(deviceAddress);
Serial.print(label);
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
String url = "/json.htm?type=command¶m=udevice&idx=";
url += String(IDX_insideTemp);
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(5000);
}
//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();
}Et pourtant quand je passe par le navigateur et que j'envoie la commande directement ça le met bien à jour...Requesting temperatures...DONE
Inside : Connecting to 192.168.1.100
Requesting URL: /json.htm?type=command¶m=udevice&idx=621&nvalue=0&svalue=21.12
closing connection
Je ne sais plus quoi faire. Si quelqu'un peut m'aider ?
Merci à la communauté.