Created
March 11, 2023 20:05
-
-
Save Ajak58a/479ac643c82fab89334ab133f595c280 to your computer and use it in GitHub Desktop.
MicroPython weather station
This file contains hidden or 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
import time | |
import urequests as requests | |
try: | |
import ujson as json | |
except: | |
import json | |
import network | |
import esp | |
esp.osdebug(None) | |
import gc | |
gc.collect() | |
ssid = 'SSID' | |
password = 'NETWORK_KEY' | |
station = network.WLAN(network.STA_IF) | |
station.active(True) | |
station.connect(ssid, password) | |
while station.isconnected() == False: | |
pass | |
print('Connection successful') | |
print(station.ifconfig()) | |
#set your unique OpenWeatherMap.org URL | |
open_weather_map_url = 'https://api.openweathermap.org/data/2.5/weather?lat={latitude}&lon={longitude}&appid={APIKey}' | |
weather_data = requests.get(open_weather_map_url) | |
print(weather_data.json()) | |
# Location (City and Country code) | |
location = 'Location: ' + weather_data.json().get('name') + ' - ' + weather_data.json().get('sys').get('country') | |
print(location) | |
# Weather Description | |
description = 'Description: ' + weather_data.json().get('weather')[0].get('main') | |
print(description) | |
# Temperature | |
raw_temperature = weather_data.json().get('main').get('temp')-273.15 | |
# Temperature in Celsius | |
temperature = 'Temperature: ' + str(raw_temperature) + '*C' | |
print(temperature) | |
# Pressure | |
pressure = 'Pressure: ' + str(weather_data.json().get('main').get('pressure')) + 'hPa' | |
print(pressure) | |
# Humidity | |
humidity = 'Humidity: ' + str(weather_data.json().get('main').get('humidity')) + '%' | |
print(humidity) | |
# Wind | |
wind = 'Wind: ' + str(weather_data.json().get('wind').get('speed')) + 'mps ' + str(weather_data.json().get('wind').get('deg')) + '*' | |
print(wind) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment