154 lines
5.4 KiB
C++
154 lines
5.4 KiB
C++
#include <WiFiS3.h>
|
|
#include <PubSubClient.h>
|
|
#include "HAMqttDevice.h"
|
|
|
|
// Informations WiFi
|
|
const char* ssid = "WifiHome2";
|
|
const char* password = "louca2212";
|
|
|
|
// Informations MQTT
|
|
const char* mqtt_server = "10.0.0.3";
|
|
const int mqtt_port = 1883;
|
|
|
|
// Objets WiFi et MQTT
|
|
WiFiClient wifiClient;
|
|
PubSubClient mqttClient(wifiClient);
|
|
|
|
// Identifiants pour Home Assistant
|
|
const String uniqueID = "temp01ae"; // Identifiant unique pour le capteur
|
|
const String haPrefix = "homeassistant"; // Préfixe pour le topic de découverte Home Assistant
|
|
const String sensorName = "sensorBedroomT"; // Nom du capteur
|
|
const String deviceName = "Bedroom"; // Nom de l'appareil
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
|
|
// Connexion au WiFi
|
|
connectWiFi();
|
|
|
|
// Configuration du client MQTT
|
|
mqttClient.setServer(mqtt_server, mqtt_port);
|
|
|
|
// Connexion au serveur MQTT
|
|
connectMQTT();
|
|
|
|
// Construire le topic de configuration
|
|
String configTopic = haPrefix + "/sensor/" + sensorName + "/config";
|
|
|
|
// Message de configuration pour Home Assistant
|
|
String configPayload = "{";
|
|
configPayload += "\"device_class\":\"temperature\",";
|
|
configPayload += "\"state_topic\":\"" + haPrefix + "/sensor/" + sensorName + "/state\",";
|
|
configPayload += "\"unit_of_measurement\":\"°C\",";
|
|
configPayload += "\"value_template\":\"{{ value_json.temperature}}\",";
|
|
configPayload += "\"unique_id\":\"" + uniqueID + "\",";
|
|
configPayload += "\"device\":{";
|
|
configPayload += "\"identifiers\":[\"" + deviceName + uniqueID + "\"],";
|
|
configPayload += "\"name\":\"" + deviceName + "\",";
|
|
configPayload += "\"manufacturer\":\"Example sensors Ltd.\",";
|
|
configPayload += "\"model\":\"K9\",";
|
|
configPayload += "\"serial_number\":\"12AE3010545\",";
|
|
configPayload += "\"hw_version\":\"1.01a\",";
|
|
configPayload += "\"sw_version\":\"2024.1.0\",";
|
|
configPayload += "\"configuration_url\":\"https://example.com/sensor_portal/config\"";
|
|
configPayload += "}";
|
|
configPayload += "}";
|
|
|
|
// Affichage du message de configuration sur la console série
|
|
Serial.println("Envoi du message de configuration Home Assistant :");
|
|
Serial.println("Topic : " + configTopic);
|
|
Serial.println("Payload : " + configPayload);
|
|
|
|
// Publier la configuration de l'appareil sur le topic MQTT
|
|
mqttClient.publish(configTopic.c_str(), configPayload.c_str(), true);
|
|
}
|
|
|
|
void loop() {
|
|
if (!mqttClient.connected()) {
|
|
connectMQTT();
|
|
}
|
|
|
|
// Simuler une mesure de température (remplacer par une mesure réelle)
|
|
float temperature = analogRead(A0) * (5.0 / 1023.0) * 100.0;
|
|
|
|
// Préparer le message d'état
|
|
String statePayload = "{\"temperature\":" + String(temperature) + "}";
|
|
String stateTopic = haPrefix + "/sensor/" + sensorName + "/state";
|
|
|
|
// Affichage du message d'état sur la console série
|
|
Serial.println("Envoi du message d'état :");
|
|
Serial.println("Topic : " + stateTopic);
|
|
Serial.println("Payload : " + statePayload);
|
|
|
|
// Publier la valeur du capteur de température dans Home Assistant
|
|
mqttClient.publish(stateTopic.c_str(), statePayload.c_str());
|
|
|
|
// Gestion du client MQTT
|
|
mqttClient.loop();
|
|
|
|
// Attendre 10 secondes avant la prochaine mesure
|
|
delay(10000);
|
|
}
|
|
|
|
void connectWiFi() {
|
|
Serial.print("Connexion à ");
|
|
Serial.println(ssid);
|
|
|
|
WiFi.begin(ssid, password);
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(1000);
|
|
Serial.print(".");
|
|
}
|
|
|
|
Serial.println("\nWiFi connecté");
|
|
Serial.print("Adresse IP : ");
|
|
Serial.println(WiFi.localIP());
|
|
}
|
|
|
|
void connectMQTT() {
|
|
while (!mqttClient.connected()) {
|
|
Serial.print("Connexion au serveur MQTT...");
|
|
|
|
if (mqttClient.connect("ArduinoClient")) {
|
|
Serial.println("connecté");
|
|
|
|
// Assurez-vous de publier le message de configuration juste après la connexion
|
|
String configTopic = haPrefix + "/sensor/" + sensorName + "/config";
|
|
String configPayload = "{";
|
|
configPayload += "\"device_class\":\"temperature\",";
|
|
configPayload += "\"state_topic\":\"" + haPrefix + "/sensor/" + sensorName + "/state\",";
|
|
configPayload += "\"unit_of_measurement\":\"°C\",";
|
|
configPayload += "\"value_template\":\"{{ value_json.temperature}}\",";
|
|
configPayload += "\"unique_id\":\"" + uniqueID + "\",";
|
|
configPayload += "\"device\":{";
|
|
configPayload += "\"identifiers\":[\"" + deviceName + uniqueID + "\"],";
|
|
configPayload += "\"name\":\"" + deviceName + "\",";
|
|
configPayload += "\"manufacturer\":\"Example sensors Ltd.\",";
|
|
configPayload += "\"model\":\"K9\",";
|
|
configPayload += "\"serial_number\":\"12AE3010545\",";
|
|
configPayload += "\"hw_version\":\"1.01a\",";
|
|
configPayload += "\"sw_version\":\"2024.1.0\",";
|
|
configPayload += "\"configuration_url\":\"https://example.com/sensor_portal/config\"";
|
|
configPayload += "}";
|
|
configPayload += "}";
|
|
|
|
Serial.println("Envoi du message de configuration Home Assistant :");
|
|
Serial.println("Topic : " + configTopic);
|
|
Serial.println("Payload : " + configPayload);
|
|
|
|
// Publier la configuration de l'appareil sur le topic MQTT
|
|
if (mqttClient.publish(configTopic.c_str(), configPayload.c_str(), true)) {
|
|
Serial.println("Message de configuration envoyé avec succès");
|
|
} else {
|
|
Serial.println("Échec de l'envoi du message de configuration");
|
|
}
|
|
} else {
|
|
Serial.print("Échec de la connexion, rc=");
|
|
Serial.print(mqttClient.state());
|
|
Serial.println("; nouvelle tentative dans 5 secondes");
|
|
|
|
delay(5000);
|
|
}
|
|
}
|
|
}
|