Skip to content

Commit

Permalink
Add Input: Weather conditions from openweathermap.org for current and…
Browse files Browse the repository at this point in the history
… future conditions; add speed and direction measurements; add m/s, knots, and bearing units; add conversions for the new units
  • Loading branch information
kizniche committed Jan 12, 2021
1 parent 6ae07cf commit ec60908
Show file tree
Hide file tree
Showing 4 changed files with 530 additions and 1 deletion.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 8.9.0 (Unreleased)

This release contains bug fixes and several new types of Inputs and Outputs. These include stepper motors, digital-to-analog converters, a multi-channel PWM output, as well as an input to acquire current and future weather conditions.

This new weather input acquires current and future weather conditions from openweathermap.org with either a city (200,000 to choose from) or latitude/longitude for a location and a time frame from the present up to 7 days in the future, with a resolution of days or hours. An API key to use the service is free and the measurements returned include temperature (including minimum and maximum if forecasting days in the future), humidity, dew point, pressure, wind speed, and wind direction. This can be useful for incorporating current or future weather conditions into your conditional controllers or other functions or calculations. For instance, you may prevent Mycodo from watering your outdoor plants if the forecasted temperature in the next 12 to 24 hours is below freezing. You may also want to be alerted by email if the forecasted weather conditions are extreme. Not everyone wants to set up a weather station, but might still want to have local outdoor measurements, so this input was made to bridge that gap.

### Bugfixes

- Fix broken Output API get/post calls
Expand Down Expand Up @@ -32,8 +36,13 @@
- Add Input: Grove Pi DHT11/22 sensor
- Add Input: HC-SR04 Ultrasonic Distance sensor
- Add Input: SCD30 CO2/Humidity/Temperature sensor
- Add Input: Current Weather from OpenWeatherMap.org (Free API Key, Latitude/Longitude, 200,000 cities, Humidity/Temperature/Pressure/Dewpoint/Wind Speed/Wind Direction)
- Add Input: Forecast Hourly/Daily Weather from OpenWeatherMap.org (Free API Key, , Humidity/Temperature/Pressure/Dewpoint)
- Add Measurement and Unit: Speed, Meters/Second
- Add Measurement and Unit: Direction, Bearing
- Add Conversions: m/s <-> mph <-> knots, hour <-> minutes and seconds
- Add LCD: Grove RGB LCD
- Add Function: bang-bang/hysteretic
- Add Function: Bang-bang/hysteretic
- Add Function Action: Output Value
- Add Function Action: Set LCD Backlight Color
- Add configurable link for navbar brand link
Expand Down
32 changes: 32 additions & 0 deletions mycodo/config_devices_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@
'name': lazy_gettext('Dewpoint'),
'meas': 'temperature',
'units': ['C', 'F', 'K']},
'direction': {
'name': lazy_gettext('Direction'),
'meas': 'direction',
'units': ['bearing']},
'disk_space': {
'name': lazy_gettext('Disk'),
'meas': 'disk_space',
Expand Down Expand Up @@ -225,6 +229,10 @@
'name': lazy_gettext('Specific Volume'),
'meas': 'specific_volume',
'units': ['m3_kg']},
'speed': {
'name': lazy_gettext('Speed'),
'meas': 'speed',
'units': ['m_s', 'mph', 'kn']},
'temperature': {
'name': lazy_gettext('Temperature'),
'meas': 'temperature',
Expand Down Expand Up @@ -268,6 +276,9 @@
'A': {
'name': lazy_gettext('Amp'),
'unit': 'A'},
'bearing': {
'name': lazy_gettext('Bearing'),
'unit': 'bearing'},
'bool': {
'name': lazy_gettext('Boolean'),
'unit': 'bool'},
Expand Down Expand Up @@ -334,6 +345,9 @@
'kJ_kg': {
'name': lazy_gettext('Kilojoule per kilogram'),
'unit': 'kJ/kg'},
'kn': {
'name': lazy_gettext('Knot'),
'unit': 'knot'},
'kPa': {
'name': lazy_gettext('Kilopascal'),
'unit': 'kPa'},
Expand All @@ -355,6 +369,9 @@
'm': {
'name': lazy_gettext('Meter'),
'unit': 'm'},
'm_s': {
'name': lazy_gettext('Meters per second'),
'unit': 'm/s'},
'm_s_s': {
'name': lazy_gettext('Meters per second per second'),
'unit': 'm/s/s'},
Expand All @@ -373,6 +390,9 @@
'mm': {
'name': lazy_gettext('Millimeter'),
'unit': 'mm'},
'mph': {
'name': lazy_gettext('Miles per hour'),
'unit': 'mph'},
'mV': {
'name': lazy_gettext('Millivolt'),
'unit': 'mV'},
Expand Down Expand Up @@ -436,6 +456,14 @@
# These are added to the SQLite database when it's created
# Users may add or delete after that
UNIT_CONVERSIONS = [
# Speed
('m_s', 'mph', 'x*2.2369362920544'),
('m_s', 'kn', 'x*1.9438444924406'),
('mph', 'm_s', 'x/2.2369362920544'),
('mph', 'kn', 'x/1.1507794480235'),
('kn', 'm_s', 'x/1.9438444924406'),
('kn', 'mph', 'x*1.1507794480235'),

# Acceleration
('g_force', 'm_s_s', 'x*9.80665'),
('m_s_s', 'g_force', 'x/9.80665'),
Expand Down Expand Up @@ -503,7 +531,11 @@

# Time
('s', 'minute', 'x/60'),
('s', 'h', 'x/60/60'),
('minute', 's', 'x*60'),
('minute', 'h', 'x/60'),
('h', 's', 'x*60*60'),
('h', 'minute', 'x*60'),

# Volt
('V', 'mV', 'x*1000'),
Expand Down
175 changes: 175 additions & 0 deletions mycodo/inputs/weather_openweathermap_city.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# coding=utf-8
#
# Copyright 2014 Matt Heitzenroder
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Python wrapper exposes the capabilities of the AOSONG AM2315 humidity
# and temperature sensor.
# The datasheet for the device can be found here:
# http://www.adafruit.com/datasheets/AM2315.pdf
#
# Portions of this code were inspired by Joehrg Ehrsam's am2315-python-api
# code. http://code.google.com/p/am2315-python-api/
#
# This library was originally authored by Sopwith:
# http://sopwith.ismellsmoke.net/?p=104
import copy

import requests
from flask_babel import lazy_gettext

from mycodo.inputs.base_input import AbstractInput
from mycodo.inputs.sensorutils import calculate_dewpoint
from mycodo.inputs.sensorutils import convert_from_x_to_y_unit

# Measurements
measurements_dict = {
0: {
'measurement': 'temperature',
'unit': 'C'
},
1: {
'measurement': 'humidity',
'unit': 'percent'
},
2: {
'measurement': 'pressure',
'unit': 'Pa'
},
3: {
'measurement': 'dewpoint',
'unit': 'C'
},
4: {
'measurement': 'speed',
'unit': 'm_s',
'name': 'Wind'
},
5: {
'measurement': 'direction',
'unit': 'bearing',
'name': 'Wind'
}
}

# Input information
INPUT_INFORMATION = {
'input_name_unique': 'WEATHER_OPENWEATHERMAP_CITY',
'input_manufacturer': 'Weather',
'input_name': 'OpenWeatherMap.org (City, Current)',
'measurements_name': 'Humidity/Temperature/Pressure/Wind',
'measurements_dict': measurements_dict,
'url_additional': 'openweathermap.org',
'measurements_rescale': False,

'message': 'Obtain a free API key at openweathermap.org. '
'If the city you enter does not return measurements, try another city. '
'Note: the free API subscription is limited to 60 calls per minute',

'options_enabled': [
'measurements_select',
'period',
'pre_output'
],
'options_disabled': ['interface'],

'dependencies_module': [],
'interfaces': ['Mycodo'],

'custom_options': [
{
'id': 'api_key',
'type': 'text',
'default_value': '',
'required': True,
'name': lazy_gettext('API Key'),
'phrase': lazy_gettext("The API Key for this service's API")
},
{
'id': 'city',
'type': 'text',
'default_value': '',
'required': True,
'name': lazy_gettext('City'),
'phrase': "The city to acquire the weather data"
}
]
}


class InputModule(AbstractInput):
"""A sensor support class that gets weather for a city"""
def __init__(self, input_dev, testing=False):
super(InputModule, self).__init__(input_dev, testing=testing, name=__name__)

self.api_url = None
self.api_key = None
self.city = None
self.setup_custom_options(
INPUT_INFORMATION['custom_options'], input_dev)

if not testing:
self.initialize_input()

def initialize_input(self):
if self.api_key and self.city:
self.api_url = "http://api.openweathermap.org/data/2.5/weather?appid={key}&units=metric&q={city}".format(
key=self.api_key, city=self.city)
self.logger.debug("URL: {}".format(self.api_url))

def get_measurement(self):
""" Gets the weather data """
if not self.api_url:
self.logger.error("API Key and City required")
return

self.return_dict = copy.deepcopy(measurements_dict)

try:
response = requests.get(self.api_url)
x = response.json()
self.logger.debug("Response: {}".format(x))

if x["cod"] != "404":
temperature = x["main"]["temp"]
pressure = x["main"]["pressure"]
humidity = x["main"]["humidity"]
wind_speed = x["wind"]["speed"]
wind_deg = x["wind"]["deg"]
else:
self.logger.error("City Not Found")
return
except Exception as e:
self.logger.error("Error acquiring weather information: {}".format(e))
return

self.logger.debug("Temp: {}, Hum: {}, Press: {}, Wind Speed: {}, Wind Direction: {}".format(
temperature, humidity, pressure, wind_speed, wind_deg))

if self.is_enabled(0):
self.value_set(0, temperature)
if self.is_enabled(1):
self.value_set(1, humidity)
if self.is_enabled(2):
self.value_set(2, pressure)

if self.is_enabled(0) and self.is_enabled(1) and self.is_enabled(3):
self.value_set(3, calculate_dewpoint(temperature, humidity))

if self.is_enabled(4):
self.value_set(4, wind_speed)
if self.is_enabled(5):
self.value_set(5, wind_deg)

return self.return_dict
Loading

0 comments on commit ec60908

Please sign in to comment.