Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added home assistant auto discovery #19

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
secrets.h
.vscode/
.vscode/
debug_custom.json
arduino.json
debug.cfg
esp32.svd
124 changes: 93 additions & 31 deletions mqtt.ino
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
Expand All @@ -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;i<sizeof sensor/sizeof sensor[0];i++){
configMQTT_HA(sensor[i]);
};
}
MQTTPublish("boot/state", (int)bootCount, true);
MQTTPublish("rssi/state", rssi, true);
MQTTPublish("temperatureF/state", (int)environment->temperatureF, 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
//=======================================================================
Expand Down
8 changes: 7 additions & 1 deletion sec.h
Original file line number Diff line number Diff line change
Expand Up @@ -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";



//===========================================
Expand Down Expand Up @@ -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;