Alan Wang
Published © CC BY-NC-SA

Web Weather Forecast Display on ESP8266 and MicroPython

Using a D1 mini to download and show one-week weather forecast data on an LCD 20x4 display.

BeginnerFull instructions provided7,387
Web Weather Forecast Display on ESP8266 and MicroPython

Things used in this project

Hardware components

Wemos D1 Mini
Espressif Wemos D1 Mini
×1
Alphanumeric LCD, 20 x 4
Alphanumeric LCD, 20 x 4
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×6

Software apps and online services

MicroPython
MicroPython

Story

Read more

Schematics

Web Weather Forecast Display on ESP8266/D1 mini Circuit Diagram

RIP my Fritzing.

Code

Web Weather Forecast Display on ESP8266/D1 mini MicroPython Code

MicroPython
Change ssid and pw to your WiFi AP name and password. Do not share code containing these information online.
# Web Weather Forecast Display on ESP8266/D1 mini by Alan Wang
import network, urequests, utime, gc
import urequests
from machine import I2C, Pin, reset
from esp8266_i2c_lcd import I2cLcd # https://github.com/dhylands/python_lcd

gc.enable()


# user data
ssid = 'your_wifi_ssid'
pw   = 'your_wifi_password'
url  = 'https://www.metaweather.com/api/location/2306179/' # see https://www.metaweather.com/api/


# push button -> D3 and G
button = Pin(0, Pin.IN)


# LCD 2004 I2C: Vcc -> 5V, Gnd -> G, SCL -> D1,  SDA -> D2
lcd = I2cLcd(I2C(scl=Pin(5), sda=Pin(4)), 0x27, 4, 20)

# clear lcd
def lcd_clear():
    lcd.move_to(0, 0)
    lcd.putstr(' ' * 80)

# show text on lcd
def lcd_show(text, line):
    lcd.move_to(0, line)
    lcd.putstr(text)


# connecting to WiFi
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(ssid, pw)
lcd_clear()
lcd_show('Connecting to WiFi..', 0)
print('Connecting to WiFi...')
while not wifi.isconnected():
    pass
lcd_show('  connected.', 1)
print('  connected.')
print('')
utime.sleep(0.5)


# querying data
lcd_clear()
lcd_show('Querying data...', 0)
print('Querying data...')
date = []
w_state = []
min_temp = []
max_temp = []
humid = []
predic = []
loc = ''
time = ''
response = urequests.get(url)


if response.status_code == 200: # query successful
    lcd_show('  successful.', 1)
    print('  successful.')
    print('')
    utime.sleep(0.2)
    
    # parse JSON
    parsed = response.json()
    
    # excract data
    loc = (parsed['title'])[:18]
    time = parsed['time']
    print('Weather forecast for [' + loc + ']')
    print('Data time: [' + time + ']\n')

    for i in range(6):
        w_data = parsed['consolidated_weather'][i]
        date.append(w_data['applicable_date'][5:10])
        w_state.append(w_data['weather_state_name'])
        min_temp.append(round(float(w_data['min_temp'])))
        max_temp.append(round(float(w_data['max_temp'])))
        humid.append(int(w_data['humidity']))
        predic.append(int(w_data['predictability']))
        output_str = 'Date: {0} [{1}%], Temp: {2}/{3} *C, Humid: {4}, State: {5}'
        print(output_str.format(date[i], predic[i], max_temp[i], min_temp[i], humid[i], w_state[i]))

else: # query failed, roboot
    lcd_show('  failed.\n  Rebooting...', 1)
    print('  failed. Rebooting...')
    utime.sleep(0.5)
    machine.reset()


print('')
print('Press button or unplug/replug device to update weather data.')
lcd_clear()
index = 0


# display data on LCD
while True:
    
    if button.value() is 0:
        break

    if len(date) > 0:
        lcd_show('{:^20}'.format('[' + loc + ']'), 0)
        if index is 0:
            date_str = 'Today'
        else:
            date_str = 'Day {0}'.format(index + 1)
        lcd_show('{0}: {1} ({2}%)'.format(date_str, date[index], predic[index]), 1)
        lcd_show('{:^20}'.format('T: {0}/{1} *C, H: {2}% '.format(max_temp[index], min_temp[index], humid[index])), 2)
        lcd_show('{:^20}'.format(w_state[index]), 3)
        
        if index is 5:
            index = 0
        else:
            index += 1

    utime.sleep(1.5)


# user reboot
lcd_clear()
while button.value() is 0:
    pass
lcd_show('Rebooting...', 0)
print('Rebooting...\n')

utime.sleep(0.5)
reset()

Credits

Alan Wang

Alan Wang

32 projects • 101 followers
Please do not ask me for free help for school or company projects. My time is not open sourced and you cannot buy it with free compliments.

Comments