Right now we have intgrated our weather station with thingspeak and WindyApp.
But as we do have Home Assistant installed why not keeping track of our weather station directly into HASS ?
Doing this will allow us to use the full power of automations provided by HASS.
Such as : temperature is high and humidity is low ==> increase watering time of our gardens.
So what we will have to do is :
- modify our ESP32 GTW0 code to publish sensors values to MQTT broker
- configure HASS to subscribe to our weather station sensors.
As an example we will add temperature and humidity to HASS.
GTW0 code update
Modification of the code will be very simple as all the "difficult" job of connecting to MQTT broker is already handled to receive "commands" from HASS.
So we are already connected to our broker!
To send MQTT messages to our broker we just have to define "topics" for our messages and publish them with the sensor value into the payload.
if ((((millis() - timeOut) > sendTimeTimeOut) && (hasRtcTime)) || (gtwStatus == sleeping)) //if (hasReceivedSensors) ThingSpeakPost();
{
hasReceivedSensors = true;
for (int i = 1; i < nbGtw + 1; i++) hasReceivedSensors = hasReceivedSensors & gtwHasSentStatus[i];
if (hasReceivedSensors) {
ThingSpeakPost();
#ifdef USE_MQTT
//send sensors values temperature and humidity to MQTT broker
//convert float into string for "MQTT pubSub"
char buffer[8];
double temp = sensorValues[1][0][0];
String s = dtostrf(temp, 5, 2, buffer);
client.publish("sensorValue/temperature", buffer);
temp = sensorValues[1][1][0];
s = dtostrf(temp, 5, 2, buffer);
client.publish("sensorValue/humidity", buffer);
#endif
#ifdef DEBUG_TELNET
In this example we do send temperature and humidity values after convertion from float to char array.
- the topic for temperature will be "sensorValue/temperature"
- while the topic for humidity will be "sensorValue/humidity "
These lines of code should be present only if using MQTT... thus we have the conditional compilation #ifdef USE_MQTT.
And that's it. Compile and run! now temperature and humidity will be published to the MQTT broker.
Subscribe to temperature and humidity sensors in HomeAssistant
We will have to add "sensors" into HASS. This can be done very easily into our configuration.yaml file.
Syntax is explained into the offcial documentation and couldn't be more simple !
Thus for temperature and humidity we add 6 lines into the configuration.yaml at the end of the "MQTT" block
sensor:
- name: "Temperature"
state_topic: "sensorValue/temperature"
- name: "Humidity"
state_topic: "sensorValue/humidity"
save the file, relaunch HASS and you have now access to your sensors in the main screen
Bingo, you can do what you want with these new sensors !
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.