# Author: Pascual Alarcon Cortijo
# Date: August 2023
import machine
import time
import network
import socket
import json
import max7219
'''
We configure the WiFi connection.
Arguments:
- ssid
- password
'''
def connect_to_wifi(ssid, password):
print('Connecting to WiFi...')
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while not wlan.isconnected():
print('Waiting for connection...')
time.sleep(1)
print(wlan.isconnected(), '--> Connected!')
print(wlan.ifconfig())
'''
We get the data from the 'server' through
a HTTP request using socket library.
Arguments:
- None
'''
def get_net_data(City='Warsaw'):
print('Getting data...')
get = 'GET /api/timezone/Europe/' + City # Sin espacio al final
request = get + ' HTTP/1.1\r\nHost: www.worldtimeapi.org\r\nUser-Agent: MicroPython\r\nAccept: application/json\r\n\r\n'
request = bytes(request.encode('utf-8'))
BUFFER_SIZE = 512
addr = socket.getaddrinfo('www.worldtimeapi.org', 80)[0][-1]
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(addr)
s.send(request)
full_recv_data = b""
# Recibe y almacena los fragmentos de la respuesta en el búfer
i=0
while i<3:
full_recv_data += s.recv(BUFFER_SIZE)
i += 1
if i==3:
break
s.close()
end_of_headers = full_recv_data.find(b'\r\n\r\n') # The five is necessary
if end_of_headers != -1:
good_data = full_recv_data[end_of_headers + 4:]
else:
good_data = full_recv_data
good_data = good_data.decode('utf-8')
good_data = json.loads(good_data)
datetime = good_data['datetime']
place = good_data['timezone']
date = datetime[8:10] + '-' + datetime[5:7] + '-' + datetime[0:4]
hour = datetime[11:19]
city = place[place.find('/') + 1: len(place)]
print('Data acquired!')
return date, hour, city
'''
We configure the displays (max7219).
Arguments:
- sck: clock for SPI protocol.
- mosi: Master output slave input.
- cs: Chip select.
- width: Of the display.
- length: Of the display.
'''
def configure_display(sck, mosi, cs, width, length):
spi = machine.SPI(0, baudrate=10000000, sck=machine.Pin(sck), mosi=machine.Pin(mosi))
display = max7219.Max7219(width, length, spi, machine.Pin(cs), False)
display.marquee('OK')
return display
'''
Function for scrolling info through the display.
Arguments:
- display: The actual display in which we want to scroll info.
- text: Info we want to display.
'''
def display_scroll_text(display, text):
display.marquee(text)
'''
Function for showing info through the display.
Arguments:
- display: The actual display in which we want to show info.
- text: Info we want to display.
'''
def display_show_text(display, text, pos1=0, pos2=0):
display.text(text, pos1, pos2)
display.show()
time.sleep(1)
'''
Function for configuring the swithces.
Arguments:
- None
'''
def configure_switches():
pin = [10, 11, 12, 13, 14, 15, 20, 21]
switch= []
for i in range(8):
switch.append(None)
switch[i] = machine.Pin(pin[i], machine.Pin.IN, machine.Pin.PULL_DOWN)
return switch
'''
We get the info from the city we want.
Arguments.
- switch: The set of swithces for selecting the city.
- button: Button for confirming selection.
'''
def get_info_city(switch, button):
print('Choosing city...')
while True:
if button.value()==0:
if switch[0].value():
return 'Madrid' # Switch 8
break
elif switch[1].value():
return 'Paris' # Switch 7
break
elif switch[2].value():
return 'London' # Switch 6
break
elif switch[3].value():
return 'Berlin' # Switch 5
break
elif switch[4].value():
return 'Rome' # Switch 4
break
elif switch[5].value():
return 'Warsaw' # Switch 3
break
elif switch[6].value():
return 'Prague' # Switch 2
break
elif switch[7].value():
return 'Lisbon' # Switch 1
break
'''
Main program
'''
# Configuration of gadgets.
display_city = configure_display(2, 3, 5, 64, 8)
display_date = configure_display(6, 7, 9, 64, 8)
display_hour = configure_display(18, 19, 17, 64, 8)
push_button = machine.Pin(28, machine.Pin.IN, machine.Pin.PULL_UP)
switches_places = configure_switches()
# Connection to WiFi
connect_to_wifi('Wokwi-GUEST', '')
# Main loop program
while True:
# We get the city we want data from
capital = get_info_city(switches_places, push_button)
#We get the data
d, h, c = get_net_data(capital)
# We display the data
display_scroll_text(display_city, ' ')
display_scroll_text(display_date, ' ')
display_scroll_text(display_hour, ' ')
display_show_text(display_city, c)
display_show_text(display_date, d)
display_show_text(display_hour, h)