From ee5a375fd8445111069a50ef9df5f4e10444c36e Mon Sep 17 00:00:00 2001 From: Alexander Gee Date: Sun, 6 Dec 2020 14:11:36 -0600 Subject: [PATCH 1/2] Update to latest images for influxdb and grafana Updating to these versions adds support for accessing the data in Influx using the new Flux language --- docker-compose.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index ae2283f..6492c8a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,7 +14,7 @@ services: restart: always influxdb: - image: influxdb:1.7.10 + image: influxdb:1.8.3 container_name: influxdb ports: - 8086:8086 @@ -23,7 +23,7 @@ services: restart: always grafana: - image: grafana/grafana:6.7.3 + image: grafana/grafana:7.3.4 container_name: grafana depends_on: - influxdb From 554b844d82c3b3fd04efa5fdfc931db02a34e290 Mon Sep 17 00:00:00 2001 From: Alexander Gee Date: Wed, 9 Dec 2020 11:22:56 -0600 Subject: [PATCH 2/2] Added openweather data collection --- .gitignore | 2 + 06-openweather/.dockerignore | 5 ++ 06-openweather/Dockerfile | 12 ++++ 06-openweather/README.md | 21 +++++++ 06-openweather/main.py | 89 +++++++++++++++++++++++++++ 06-openweather/openweather.env.editme | 5 ++ 06-openweather/requirements.txt | 1 + README.md | 2 +- docker-compose.yml | 11 ++++ 9 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 06-openweather/.dockerignore create mode 100644 06-openweather/Dockerfile create mode 100644 06-openweather/README.md create mode 100644 06-openweather/main.py create mode 100644 06-openweather/openweather.env.editme create mode 100644 06-openweather/requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4ad06b3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +06-openweather/openweather.env +**/.vscode/** diff --git a/06-openweather/.dockerignore b/06-openweather/.dockerignore new file mode 100644 index 0000000..004a7c7 --- /dev/null +++ b/06-openweather/.dockerignore @@ -0,0 +1,5 @@ +.git +Dockerfile +*~ +.DS_Store +README.md diff --git a/06-openweather/Dockerfile b/06-openweather/Dockerfile new file mode 100644 index 0000000..6a420ab --- /dev/null +++ b/06-openweather/Dockerfile @@ -0,0 +1,12 @@ +FROM python:3.7-alpine + +LABEL maintainer="Bostwickenator" \ + description="Open Weather to InfluxDB logger https://openweathermap.org/" + +COPY requirements.txt / +RUN pip install -r /requirements.txt + +COPY . /app +WORKDIR /app + +CMD ["python3", "-u", "main.py"] diff --git a/06-openweather/README.md b/06-openweather/README.md new file mode 100644 index 0000000..e9e62da --- /dev/null +++ b/06-openweather/README.md @@ -0,0 +1,21 @@ +# MQTT to InfluxDB Bridge + +## Build + +```sh +$ docker build -t bostwickenator/openweather . +``` + + +## Run + +```sh +$ docker run -d --name openweather bostwickenator/openweather +``` + + +## Dev + +```sh +$ docker run -it --rm -v `pwd`:/app --name python python:3.7-alpine sh +``` diff --git a/06-openweather/main.py b/06-openweather/main.py new file mode 100644 index 0000000..d42b67e --- /dev/null +++ b/06-openweather/main.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python3 + +""" +Open Weather to InfluxDB logger https://openweathermap.org/ +""" + +from influxdb import InfluxDBClient +import requests +from datetime import datetime +import sched +import time +import os + +# instance is created +scheduler = sched.scheduler(time.time, + time.sleep) + +INFLUXDB_ADDRESS = 'influxdb' +INFLUXDB_USER = 'root' +INFLUXDB_PASSWORD = 'root' +INFLUXDB_DATABASE = 'home_db' + +POLLING_INTERVAL_SEC = 60 * 5 + +OPENWEATHER_ZIP = os.getenv('OPENWEATHER_ZIP') +OPENWEATHER_API_KEY = os.getenv('OPENWEATHER_API_KEY') +OPENWEATHER_UNITS = os.getenv('OPENWEATHER_UNITS') +OPENWEATHER_API_PATH = f'https://api.openweathermap.org/data/2.5/weather?zip={OPENWEATHER_ZIP}&units={OPENWEATHER_UNITS}&appid={OPENWEATHER_API_KEY}' + +MQTT_ADDRESS = 'mosquitto' +MQTT_USER = 'mqttuser' +MQTT_PASSWORD = 'mqttpassword' +MQTT_TOPIC = 'home/+/+' +MQTT_REGEX = 'home/([^/]+)/([^/]+)' +MQTT_CLIENT_ID = 'MQTTInfluxDBBridge' + +influxdb_client = InfluxDBClient(INFLUXDB_ADDRESS, 8086, INFLUXDB_USER, INFLUXDB_PASSWORD, None) + + +def _send_sensor_data_to_influxdb(weather): + json_body = [ + { + 'measurement': 'weather', + 'tags': { + 'location': 'outside' + }, + 'fields': { + "temp": float(weather['main']['temp']), + "feels_like": float(weather['main']['feels_like']), + "pressure": float(weather['main']['pressure']), + "humidity": float(weather['main']['humidity']), + "wind": float(weather['wind']['speed']), + }, + "time": datetime.utcfromtimestamp(weather['dt']).isoformat() + 'Z', + } + ] + print(json_body) + influxdb_client.write_points(json_body) + + +def _init_influxdb_database(): + databases = influxdb_client.get_list_database() + if len(list(filter(lambda x: x['name'] == INFLUXDB_DATABASE, databases))) == 0: + influxdb_client.create_database(INFLUXDB_DATABASE) + influxdb_client.switch_database(INFLUXDB_DATABASE) + +last_weather_time = 0 + +def get_weather(): + global last_weather_time + r = requests.get(OPENWEATHER_API_PATH) + if(r.status_code == requests.codes.ok): + if(r.json()['dt'] != last_weather_time): + last_weather_time = r.json()['dt'] + _send_sensor_data_to_influxdb(r.json()) + + +def repeat(): + scheduler.enter(POLLING_INTERVAL_SEC, 1, repeat) + get_weather() + +def main(): + _init_influxdb_database() + repeat() + scheduler.run() + +if __name__ == '__main__': + print('OpenWeather to InfluxDB bridge') + main() diff --git a/06-openweather/openweather.env.editme b/06-openweather/openweather.env.editme new file mode 100644 index 0000000..406cb93 --- /dev/null +++ b/06-openweather/openweather.env.editme @@ -0,0 +1,5 @@ +# rename me to openweather.env +# see https://openweathermap.org/current +OPENWEATHER_ZIP = '' +OPENWEATHER_API_KEY = '' +OPENWEATHER_UNITS = '' \ No newline at end of file diff --git a/06-openweather/requirements.txt b/06-openweather/requirements.txt new file mode 100644 index 0000000..026e36a --- /dev/null +++ b/06-openweather/requirements.txt @@ -0,0 +1 @@ +influxdb==5.3.0 diff --git a/README.md b/README.md index dc193ce..ac61b3d 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Blog post: [http://nilhcem.com/iot/home-monitoring-with-mqtt-influxdb-grafana](h - `03-bme280_mqtt`: Arduino sketch file for the ESP8266 and the BME280 that publishes sensor data to MQTT - `04-mijia_ble_mqt`: Python script that connects to a BTLE MiJia Temperature & Humidity sensor and publishes data to MQTT - `05-dht22_mqtt`: Arduino sketch file for the ESP8266 and the DHT22 that publishes sensor data to MQTT - +- `06-openweather`: Pulls weather data for your location into InfluxDB ## Setup diff --git a/docker-compose.yml b/docker-compose.yml index 6492c8a..bacdeaf 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -41,3 +41,14 @@ services: - mosquitto - influxdb restart: always + + openweather: + build: ./06-openweather + image: bostwickenator/openweather + container_name: openweather + env_file: + - ./06-openweather/openweather.env + depends_on: + - mosquitto + - influxdb + restart: always