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

Update to latest images for influxdb and grafana #5

Open
wants to merge 2 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
06-openweather/openweather.env
**/.vscode/**
5 changes: 5 additions & 0 deletions 06-openweather/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.git
Dockerfile
*~
.DS_Store
README.md
12 changes: 12 additions & 0 deletions 06-openweather/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
21 changes: 21 additions & 0 deletions 06-openweather/README.md
Original file line number Diff line number Diff line change
@@ -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
```
89 changes: 89 additions & 0 deletions 06-openweather/main.py
Original file line number Diff line number Diff line change
@@ -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()
5 changes: 5 additions & 0 deletions 06-openweather/openweather.env.editme
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# rename me to openweather.env
# see https://openweathermap.org/current
OPENWEATHER_ZIP = ''
OPENWEATHER_API_KEY = ''
OPENWEATHER_UNITS = ''
1 change: 1 addition & 0 deletions 06-openweather/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
influxdb==5.3.0
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 13 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ services:
restart: always

influxdb:
image: influxdb:1.7.10
image: influxdb:1.8.3
container_name: influxdb
ports:
- 8086:8086
Expand All @@ -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
Expand All @@ -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