Association informatique libre, solidaire

Forum d'échange entre les membres

Vous n'êtes pas identifié(e).

#1 2018-10-29 09:48:55

Benoît
Administrator
Inscription : 2015-06-21

relevé de températures dans les pièces

Hello à tous,

Pour ceux que ça intéresse, voici un récap de mon utilisation d'un ESP-01 raccordé à des sondes étanches de température, DS18b20.

Le programme utilisé:

/***********************************************************
 * lecture températures chambres / salle de bain / couloir *										   *
 *														   *
 ***********************************************************/

#include <OneWire.h>
#include <DallasTemperature.h>
#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 2 //branché sur GPIO2
#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);

/* Adresses des capteurs de température */
DeviceAddress chambrPar = { 0x28, 0xFF, 0xD4, 0x18, 0x00, 0x17, 0x05, 0x90 };    // Num°1 Chambre parentale Idx=8
DeviceAddress salleBain = { 0x28, 0xFF, 0x05, 0x36, 0x00, 0x17, 0x05, 0x66 };   // Num°2 Salle de bain Idx=6
DeviceAddress chambEnf = { 0x28, 0xFF, 0xDA, 0xB4, 0x02, 0x17, 0x04, 0x98 }; // Num°3 Chambre Enfant Idx=9
DeviceAddress couLoir = { 0x28, 0xFF, 0x89, 0x26, 0x02, 0x17, 0x05, 0xFB };   // Num°4 Couloir Idx=10

// Parametres WIFI - WiFi settings
#define wifi_ssid "MonSSID"
#define wifi_password "MonSuperMotDePasse"

// Paramètres HTTP Domoticz - HTTP Domoticz settings
const char* host = "192.168.1.98764";
const int   port = 8080;
#define IDX_chambrPar 8
#define IDX_salleBain 6
#define IDX_chambEnf 9
#define IDX_couLoir 10
HTTPClient http;

void setup() {
  // Connexion au réseau WiFi, connexion aux sondes
  setup_wifi();
  sensors.begin();

  // set the resolution to 9 bit per device
  sensors.setResolution(chambrPar, TEMPERATURE_PRECISION);
  sensors.setResolution(salleBain, TEMPERATURE_PRECISION);
  sensors.setResolution(chambEnf, TEMPERATURE_PRECISION);
  sensors.setResolution(couLoir, TEMPERATURE_PRECISION);
}

void printTemperature(String label, DeviceAddress deviceAddress){
  float tempC = sensors.getTempC(deviceAddress);
    // Format JSON à respecter pour l'API Domoticz - Domoticz JSON API
    // /json.htm?type=command&param=udevice&idx=IDX&nvalue=0&svalue=TEMP
    // https://www.domoticz.com/wiki/Domoticz_API/JSON_URL%27s#Temperature
    if (label == "chambre parents: ") {
	  String url = "/json.htm?type=command&param=udevice&idx=";
	  url += String(IDX_chambrPar);
	  url += "&nvalue=0&svalue=";
	  url += String(tempC);
	  sendToDomoticz(url);
    }
	if (label == "salle de bain : ") {
	  String url = "/json.htm?type=command&param=udevice&idx=";
	  url += String(IDX_salleBain);
	  url += "&nvalue=0&svalue=";
	  url += String(tempC);
	  sendToDomoticz(url);
	  }
	if (label == "chambre Enfant : ") {
	  String url = "/json.htm?type=command&param=udevice&idx=";
	  url += String(IDX_chambEnf);
	  url += "&nvalue=0&svalue=";
	  url += String(tempC);
	  sendToDomoticz(url);
	  }
	if (label == "couloir : ") {
	  String url = "/json.htm?type=command&param=udevice&idx=";
	  url += String(IDX_couLoir);
	  url += "&nvalue=0&svalue=";
	  url += String(tempC);
	  sendToDomoticz(url);
	  }
      
  }

void loop() {
  sensors.requestTemperatures();
  // print the device information
  printTemperature("chambre parents : ", chambrPar);
  delay(2000);
  printTemperature("salle de bain : ", salleBain);
  delay(2000);
  printTemperature("chambre Enfant : ", chambLison);
  delay(2000);
  printTemperature("couloir : ", couLoir);
  delay(60000);
}

//Connexion au réseau WiFi
void setup_wifi() {
  delay(10);
  WiFi.begin(wifi_ssid, wifi_password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
}


void sendToDomoticz(String url){
  http.begin(host,port,url);
  int httpCode = http.GET();
    if (httpCode) {
      if (httpCode == 200) {
        String payload = http.getString();
      }
    }
  http.end();
}

Hors ligne

Pied de page des forums