diff --git a/.gitignore b/.gitignore index c70c7d5..2c26ceb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,6 @@ secrets.h -.vscode/ \ No newline at end of file +.vscode/ +debug_custom.json +arduino.json +debug.cfg +esp32.svd \ No newline at end of file diff --git a/mqtt.ino b/mqtt.ino index dc42938..c799e05 100644 --- a/mqtt.ino +++ b/mqtt.ino @@ -1,10 +1,12 @@ //mqtt data send - - - WiFiClient espClient; PubSubClient client(espClient); +struct config { + char name[25]; + char device_class[25]; + char unit_of_measurement[20]; +}; //======================================================================= // SendDataMQTT: send MQTT data to broker with 'retain' flag set to TRUE @@ -15,8 +17,6 @@ void SendDataMQTT (struct sensorData *environment) char bufferTempC[5]; char bufferRain[10]; char bufferRain24[10]; - - int hourPtr = timeinfo.tm_hour; client.setServer(mqttServer, mqttPort); //client.setCallback(callback); @@ -35,40 +35,102 @@ void SendDataMQTT (struct sensorData *environment) delay(1000); } } - MQTTPublish("boot/", (int)bootCount, true); - MQTTPublish("rssi/", rssi, true); - MQTTPublish("temperatureF/", (int)environment->temperatureF, true); - MQTTPublish("temperatureC/", (int)environment->temperatureC, true); - MQTTPublish("windSpeed/", environment->windSpeed, true); - MQTTPublish("windDirection/", (int)environment->windDirection, true); - MQTTPublish("windCardinalDirection/", environment->windCardinalDirection, true); - MQTTPublish("photoresistor/", (int)environment->photoresistor, true); + if (discovery_enable){ + struct config sensor[22]={ + {"boot", "","#"}, + {"rssi", "","#"}, + {"temperatureF", "temperature","°F"}, + {"temperatureC", "temperature","°C"}, + {"windSpeed", "wind_speed",""}, + {"windDirection", "",""}, + {"windCardinalDirection", "",""}, + {"photoresistor", "",""}, + {"rainfallInterval", "",""}, + {"rainfall", "volume","L"}, + {"rainfall24", "volume","L"}, + {"batteryVoltage", "voltage","V"}, + {"lux", "illuminance","lx"}, + {"UVIndex", "",""}, + {"relHum", "","%"}, + {"pressure", "",""}, + {"caseTemperature", "temperature","°C"}, + {"batteryADC", "",""}, + {"ESPcoreF", "temperature","°F"}, + {"ESPcoreC", "temperature","°C"}, + {"timeEnabled", "",""}, + {"lowBattery", "",""}, + }; + for (int i=0;itemperatureF, true); + MQTTPublish("temperatureC/state", (int)environment->temperatureC, true); + MQTTPublish("windSpeed/state", environment->windSpeed, true); + MQTTPublish("windDirection/state", (int)environment->windDirection, true); + MQTTPublish("windCardinalDirection/state", environment->windCardinalDirection, true); + MQTTPublish("photoresistor/state", (int)environment->photoresistor, true); #ifndef METRIC - MQTTPublish("rainfallInterval/", (float) (rainfall.intervalRainfall * 0.011), true); - MQTTPublish("rainfall/", (float) (rainfall.hourlyRainfall[hourPtr] * 0.011), true); - MQTTPublish("rainfall24/", (float) (last24() * 0.011), true); + MQTTPublish("rainfallInterval/state", (float) (rainfall.intervalRainfall * 0.011), true); + MQTTPublish("rainfall/state", (float) (rainfall.hourlyRainfall[hourPtr] * 0.011), true); + MQTTPublish("rainfall24/state", (float) (last24() * 0.011), true); #else - MQTTPublish("rainfallInterval/", (float) (rainfall.intervalRainfall * 0.011 * 25.4), true); - MQTTPublish("rainfall/", (float) (rainfall.hourlyRainfall[hourPtr] * 0.011 * 25.4), true); - MQTTPublish("rainfall24/", (float) (last24() * 0.011 * 25.4), true); + MQTTPublish("rainfallInterval/state", (float) (rainfall.intervalRainfall * 0.011 * 25.4), true); + MQTTPublish("rainfall/state", (float) (rainfall.hourlyRainfall[hourPtr] * 0.011 * 25.4), true); + MQTTPublish("rainfall24/state", (float) (last24() * 0.011 * 25.4), true); #endif - - MQTTPublish("batteryVoltage/", environment->batteryVoltage, true); - MQTTPublish("lux/", environment->lux, true); - MQTTPublish("UVIndex/", environment->UVIndex, true); - MQTTPublish("relHum/", environment->humidity, true); - MQTTPublish("pressure/", environment->barometricPressure, true); - MQTTPublish("caseTemperature/", environment->BMEtemperature, true); - MQTTPublish("batteryADC/", (int)environment->batteryADC, true); - MQTTPublish("ESPcoreF/", (int)environment->coreF, true); - MQTTPublish("ESPcoreC/", (int)environment->coreC, true); - MQTTPublish("timeEnabled/", (int)elapsedTime, true); - MQTTPublish("lowBattery/", lowBattery, true); + MQTTPublish("batteryVoltage/state", (float) environment->batteryVoltage, true); + MQTTPublish("lux/state", environment->lux, true); + MQTTPublish("UVIndex/state", environment->UVIndex, true); + MQTTPublish("relHum/state", environment->humidity, true); + MQTTPublish("pressure/state", environment->barometricPressure, true); + MQTTPublish("caseTemperature/state", environment->BMEtemperature, true); + MQTTPublish("batteryADC/state", (int)environment->batteryADC, true); + MQTTPublish("ESPcoreF/state", (int)environment->coreF, true); + MQTTPublish("ESPcoreC/state", (int)environment->coreC, true); + MQTTPublish("timeEnabled/state", (int)elapsedTime, true); + MQTTPublish("lowBattery/state", lowBattery, true); MonPrintf("Issuing mqtt disconnect\n"); client.disconnect(); MonPrintf("Disconnected\n"); } +void configMQTT_HA( struct config sensor) +{ + char orgmainTopic[20]; + char confJson[300]; + char topic[60]; + memcpy( orgmainTopic, mainTopic, sizeof(orgmainTopic)); + memcpy( mainTopic, "homeassistant/", sizeof(mainTopic)); + strcpy(confJson, "{"); + if(strlen(sensor.device_class) != 0) { + strcat(confJson, "\"device_class\": \""); + strcat(confJson, sensor.device_class ); + strcat(confJson, "\","); + } + strcat(confJson, " \"name\": \"" ); + strcat(confJson, prefix ); + strcat(confJson, sensor.name ); + strcat(confJson, "\", \"state_topic\": \"" ); + strcat(confJson, orgmainTopic); + strcat(confJson, sensor.name); + strcat(confJson, "/state\""); + if(strlen(sensor.unit_of_measurement) != 0) { + strcat(confJson, ", \"unit_of_measurement\": \""); + strcat(confJson, sensor.unit_of_measurement); + strcat(confJson, "\""); + } + strcat(confJson, "}"); + strcpy(topic, "sensor/"); + strcat(topic, orgmainTopic); + strcat(topic, sensor.name); + strcat(topic, "/config"); + MQTTPublish(topic,confJson, true); + memcpy( mainTopic, orgmainTopic, sizeof(mainTopic)); +} + //======================================================================= // MQTTPublishString: routine to publish string //======================================================================= diff --git a/sec.h b/sec.h index 4de11b2..8759415 100644 --- a/sec.h +++ b/sec.h @@ -35,7 +35,9 @@ const char* mqttServer = "192.168.5.74"; const int mqttPort = 1883; const char* mqttUser = "username"; const char* mqttPassword = "password"; -const char mainTopic[20] = "MainTopic/"; +char mainTopic[20] = "MainTopic/"; +char prefix[20] = "prefix"; + //=========================================== @@ -97,3 +99,7 @@ const int daylightOffset_sec = 3600; //const String App = "BLYNK"; // alternative is line below //const String App = "Thingspeak"; // alternative is line above const String App = "MQTT"; // alternative is line below + + +//========================= enable homeassistant auto disc. =================================== +const bool discovery_enable=true; \ No newline at end of file