Created
February 10, 2022 15:06
-
-
Save helgibbons/1dfcbf3d0f0a5507ae24dae6704bd588 to your computer and use it in GitHub Desktop.
Weather HAT adafruit.io example that also posts data into Home Assistant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from time import sleep | |
import weatherhat | |
from Adafruit_IO import Client, Feed, Dashboard, RequestError, AdafruitIOError | |
import json | |
import paho.mqtt.client as mqtt | |
import paho.mqtt.publish as publish | |
sensor = weatherhat.WeatherHAT() | |
print(f""" | |
adafruit-io.py - Example showing how to send sensor data from Weather HAT into adafruit.io. | |
Press Ctrl+C to exit! | |
""") | |
# Set to your Adafruit IO key. | |
# Remember, your key is a secret, | |
# so make sure not to publish it when you publish this code! | |
ADAFRUIT_IO_KEY = 'keygoeshere' | |
# Set to your Adafruit IO username. | |
# (go to https://accounts.adafruit.com to find your username) | |
ADAFRUIT_IO_USERNAME = 'usernamegoeshere' | |
# We can compensate for the heat of the Pi and other environmental conditions using a simple offset. | |
# Change this number to adjust temperature compensation! | |
OFFSET = -7.5 | |
# Create an instance of the REST client. | |
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) | |
# Create new feeds | |
try: | |
aio.create_feed(Feed(name="Temperature")) | |
aio.create_feed(Feed(name="Relative Humidity")) | |
aio.create_feed(Feed(name="Pressure")) | |
aio.create_feed(Feed(name="Light")) | |
aio.create_feed(Feed(name="Wind Speed")) | |
aio.create_feed(Feed(name="Wind Direction")) | |
aio.create_feed(Feed(name="Rain")) | |
print("Feeds created!") | |
except RequestError: | |
print("Feeds already exist!") | |
temperature_feed = aio.feeds('temperature') | |
humidity_feed = aio.feeds('relative-humidity') | |
pressure_feed = aio.feeds('pressure') | |
light_feed = aio.feeds('light') | |
windspeed_feed = aio.feeds('wind-speed') | |
winddirection_feed = aio.feeds('wind-direction') | |
rain_feed = aio.feeds('rain') | |
# Create new dashboard | |
try: | |
dashboard = aio.create_dashboard(Dashboard(name="Weather Dashboard")) | |
print("Dashboard created!") | |
except RequestError: | |
print("Dashboard already exists!") | |
dashboard = aio.dashboards('weather-dashboard') | |
print("Find your dashboard at: " + | |
"https://io.adafruit.com/{0}/dashboards/{1}".format(ADAFRUIT_IO_USERNAME, | |
dashboard.key)) | |
# We're using the Mosquitto HA add-on as our MQTT broker, other setups may vary! | |
mqtt_client = mqtt.Client() | |
mqtt_client.username_pw_set(username="brokerusernamegoeshere",password="brokerpasswordgoeshere") | |
mqtt_client.connect("homeassistant.local",1883) # you could also use the broker IP here | |
mqtt_client.loop_start() | |
#initial setup of mqtt sensors: | |
Dict1 = { | |
"name": "Weather Station Temperature", | |
"device_class": "temperature", | |
"state_topic": "homeassistant/sensor/WeatherTemp/state", | |
"unit_of_measurement": "C", | |
} | |
mqtt_client.publish("homeassistant/sensor/WeatherTemp/config",json.dumps(Dict1)) | |
Dict2 = { | |
"name": "Weather Station Pressure", | |
"device_class": "pressure", | |
"state_topic": "homeassistant/sensor/WeatherPres/state", | |
"unit_of_measurement": "hPa" | |
} | |
mqtt_client.publish("homeassistant/sensor/WeatherPres/config",json.dumps(Dict2)) | |
Dict3 = { | |
"name": "Weather Station Humidity", | |
"device_class": "humidity", | |
"state_topic": "homeassistant/sensor/WeatherHumi/state", | |
"unit_of_measurement": "%" | |
} | |
mqtt_client.publish("homeassistant/sensor/WeatherHumi/config",json.dumps(Dict3)) | |
Dict4 = { | |
"name": "Weather Station illuminance", | |
"device_class": "illuminance", | |
"state_topic": "homeassistant/sensor/WeatherLux/state", | |
"unit_of_measurement": "lx" | |
} | |
mqtt_client.publish("homeassistant/sensor/WeatherLux/config",json.dumps(Dict4)) | |
Dict5 = { | |
"name": "Weather Station Wind speed", | |
#"device_class": "none", | |
"state_topic": "homeassistant/sensor/WeatherWS/state", | |
"unit_of_measurement": "m/s" | |
} | |
mqtt_client.publish("homeassistant/sensor/WeatherWS/config",json.dumps(Dict5)) | |
Dict6 = { | |
"name": "Weather Station wind gusts", | |
#"device_class": "none", | |
"state_topic": "homeassistant/sensor/WeatherWG/state", | |
"unit_of_measurement": "m/s" | |
} | |
#mqtt_client.publish("homeassistant/sensor/WeatherWG/config",json.dumps(Dict6)) #Not done yet | |
Dict7 = { | |
"name": "Weather Station wind direction", | |
#"device_class": "none", | |
"state_topic": "homeassistant/sensor/WeatherWD/state", | |
"unit_of_measurement": "none" | |
} | |
mqtt_client.publish("homeassistant/sensor/WeatherWD/config",json.dumps(Dict7)) | |
Dict8 = { | |
"name": "Weather Station rain", | |
#"device_class": "none", | |
"state_topic": "homeassistant/sensor/WeatherRain/state", | |
"unit_of_measurement": "mm/s" | |
} | |
mqtt_client.publish("homeassistant/sensor/WeatherRain/config",json.dumps(Dict8)) | |
#read the BME280 and discard the initial readings | |
sensor.update(interval=10.0) | |
sensor.temperature_offset = OFFSET | |
temperature = sensor.temperature | |
humidity = sensor.relative_humidity | |
pressure = sensor.pressure | |
print("Discarding the first few BME280 readings...") | |
sleep(10.0) | |
# Now send some data | |
while True: | |
sensor.update(interval=30.0) | |
wind_direction_cardinal = sensor.degrees_to_cardinal(sensor.wind_direction) | |
temperature = sensor.temperature | |
humidity = sensor.relative_humidity | |
pressure = sensor.pressure | |
light = sensor.lux | |
windspeed = sensor.wind_speed | |
winddirection = wind_direction_cardinal | |
rain = sensor.rain | |
try: | |
aio.send_data(temperature_feed.key, temperature) | |
aio.send_data(humidity_feed.key, humidity) | |
aio.send_data(pressure_feed.key, pressure) | |
aio.send_data(light_feed.key, light) | |
aio.send_data(windspeed_feed.key, windspeed) | |
aio.send_data(winddirection_feed.key, winddirection) | |
aio.send_data(rain_feed.key, rain) | |
print('Data sent to adafruit.io') | |
mqtt_client.publish("homeassistant/sensor/WeatherTemp/state", round(temperature,2)) | |
mqtt_client.publish("homeassistant/sensor/WeatherPres/state", round(pressure,2)) | |
mqtt_client.publish("homeassistant/sensor/WeatherHumi/state", round(humidity,2)) | |
mqtt_client.publish("homeassistant/sensor/WeatherLux/state", round(light,2)) | |
mqtt_client.publish("homeassistant/sensor/WeatherWS/state", round(windspeed,2)) | |
mqtt_client.publish("homeassistant/sensor/WeatherWD/state", winddirection) | |
mqtt_client.publish("homeassistant/sensor/WeatherRain/state", round(rain,2)) | |
print('Data sent to Home Assistant') | |
except Exception as e: | |
print(e) | |
sleep(30.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment