Skip to content

Instantly share code, notes, and snippets.

@aallan
Last active March 22, 2024 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aallan/aa21ebdf5da26fa3674ed2fad61dae9d to your computer and use it in GitHub Desktop.
Save aallan/aa21ebdf5da26fa3674ed2fad61dae9d to your computer and use it in GitHub Desktop.
# Author: Alasdair Allan <alasdair@babilim.co.uk>
# SPDX-License-Identifier: MIT
# Simple RESTful web server for the Pimoroni WeatherHAT + Sensor Kit
# https://shop.pimoroni.com/products/weather-hat?variant=39672186765395
# WeatherHAT Python Library
# https://github.com/pimoroni/weatherhat-python
# Calling GET endpoints, e.g.
#
# http://weather.local:5000/api/1.0/temperature/external
# Calling a POST endpoint from command line:
#
# wget -O- --post-data='{"temperature":"-10.0","units":"C"}' --header='Content-Type:application/json' \
# 'http://weather.local:5000/api/1.0/temperature/offset'
import json
import weatherhat
from flask import Flask, request
from datetime import datetime
sensor = weatherhat.WeatherHAT()
app = Flask(__name__)
@app.route('/api/1.0/raw')
def data():
sensor.update(interval=30.0)
wind_direction_cardinal = sensor.degrees_to_cardinal(sensor.wind_direction)
current_date = datetime.now().isoformat(sep='T',timespec='auto')
data = {
'time': f"{current_date}",
'system_temp': f"{sensor.device_temperature:0.2f}",
'external_temp': f"{sensor.temperature:0.2f}",
'offset_temp' : f"{sensor.temperature_offset:0.2f}",
'humidity': f"{sensor.humidity:0.2f}",
'dew_point': f"{sensor.dewpoint:0.2f}",
'light': f"{sensor.lux:0.2f}",
'pressure': f"{sensor.pressure:0.2f}",
'wind_speed': f"{sensor.wind_speed:0.2f}",
'rain': f"{sensor.rain:0.2f}",
'wind_direction': f"{sensor.wind_direction:0.2f}",
'wind_cardinal': f"{wind_direction_cardinal}"
}
return data
@app.route('/api/1.0/temperature')
def temp():
sensor.update(interval=30.0)
current_date = datetime.now().isoformat(sep='T',timespec='auto')
data = { 'time': f"{current_date}",
'external': f"{sensor.temperature:0.2f}",
'system': f"{sensor.device_temperature:0.2f}",
'dewpoint': f"{sensor.dewpoint:0.2f}",
'offset' : f"{sensor.temperature_offset:0.2f}",
'units': 'C'}
return data
@app.route('/api/1.0/temperature/external')
def ext_temp():
sensor.update(interval=30.0)
current_date = datetime.now().isoformat(sep='T',timespec='auto')
data = { 'time': f"{current_date}", 'temperature': f"{sensor.temperature:0.2f}", 'units': 'C'}
return data
@app.route('/api/1.0/temperature/system')
def dev_temp():
sensor.update(interval=30.0)
current_date = datetime.now().isoformat(sep='T',timespec='auto')
data = { 'time': f"{current_date}", 'temperature': f"{sensor.device_temperature:0.2f}", 'units': 'C'}
return data
@app.route('/api/1.0/temperature/dewpoint')
def dewpoint():
sensor.update(interval=30.0)
current_date = datetime.now().isoformat(sep='T',timespec='auto')
data = { 'time': f"{current_date}", 'temperature': f"{sensor.dewpoint:0.2f}", 'units': 'C'}
return data
@app.route('/api/1.0/temperature/offset', methods= ['GET','POST'] )
def offset():
if request.method == 'GET':
current_date = datetime.now().isoformat(sep='T',timespec='auto')
data = { 'time': f"{current_date}", 'temperature': f"{sensor.temperature_offset:0.2f}", 'units': 'C'}
return data
if request.method == 'POST':
offset = float(request.json['temperature'])
sensor.temperature_offset = offset
current_date = datetime.now().isoformat(sep='T',timespec='auto')
data = { 'time': f"{current_date}", 'temperature': f"{sensor.temperature_offset:0.2f}", 'units': 'C'}
return data
@app.route('/api/1.0/humidity')
def humidity():
sensor.update(interval=30.0)
current_date = datetime.now().isoformat(sep='T',timespec='auto')
data = { 'time': f"{current_date}", 'humidity': f"{sensor.humidity:0.2f}", 'units': '%'}
return data
@app.route('/api/1.0/light')
def light():
sensor.update(interval=30.0)
current_date = datetime.now().isoformat(sep='T',timespec='auto')
data = { 'time': f"{current_date}", 'light': f"{sensor.lux:0.2f}", 'units': 'lux'}
return data
@app.route('/api/1.0/pressure')
def press():
sensor.update(interval=30.0)
current_date = datetime.now().isoformat(sep='T',timespec='auto')
data = { 'time': f"{current_date}", 'pressure': f"{sensor.pressure:0.2f}", 'units': 'hPa'}
return data
@app.route('/api/1.0/rain')
def rain():
sensor.update(interval=30.0)
current_date = datetime.now().isoformat(sep='T',timespec='auto')
data = { 'time': f"{current_date}", 'rain': f"{sensor.rain:0.2f}", 'units': 'mm/sec'}
return data
@app.route('/api/1.0/wind')
def wind():
sensor.update(interval=30.0)
current_date = datetime.now().isoformat(sep='T',timespec='auto')
wind_direction_cardinal = sensor.degrees_to_cardinal(sensor.wind_direction)
data = { 'time': f"{current_date}",
'speed':{
'speed': f"{sensor.wind_speed:0.2f}",
'units': 'm/sec'},
'direction':{
'direction': f"{sensor.wind_direction:0.2f}",
'units': 'degrees',
'cardinal': f"{wind_direction_cardinal}" }
}
return data
@app.route('/api/1.0/wind/speed')
def wind_speed():
sensor.update(interval=30.0)
current_date = datetime.now().isoformat(sep='T',timespec='auto')
data = { 'time': f"{current_date}", 'speed': f"{sensor.wind_speed:0.2f}", 'units': 'm/sec'}
return data
@app.route('/api/1.0/wind/direction')
def wind_direction():
sensor.update(interval=30.0)
current_date = datetime.now().isoformat(sep='T',timespec='auto')
data = { 'time': f"{current_date}", 'speed': f"{sensor.wind_direction:0.2f}", 'units': 'degrees'}
return data
@app.route('/api/1.0/wind/cardinal')
def wind_cardinal():
sensor.update(interval=30.0)
current_date = datetime.now().isoformat(sep='T',timespec='auto')
wind_direction_cardinal = sensor.degrees_to_cardinal(sensor.wind_direction)
data = { 'time': f"{current_date}", 'speed': f"{wind_direction_cardinal}" }
return data
if __name__ == '__main__':
app.run(host='0.0.0.0')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment