import network
import socket
from utime import sleep
from picozero import pico_temp_sensor, pico_led, Button, LED
import machine
from lcd1602 import LCD
global score_1
global score_2
global games_played
ssid = "pico_ella"
password = "helloworld"
score_1 = 0
score_2 = 0
games_played = 0
lcd = LCD()
cup_1 = Button(16)
cup_2 = Button(17)
reset_btn = Button(28)
def cup_1_triggered():
global score_1
score_1 += 1
print(f"Player 1: {score_1}")
def cup_2_triggered():
global score_2
score_2 += 1
print(f"Player 2: {score_2}")
def reset_game():
global score_1
global score_2
global games_played
score_1 = 0
score_2 = 0
games_played += 1
print(f"Player 1: {score_1}")
print(f"Player 2: {score_2}")
cup_1.when_pressed = cup_1_triggered
cup_2.when_pressed = cup_2_triggered
reset_btn.when_pressed = reset_game
lcd.message("Welcome...")
def connect():
#Connect to WLAN
wlan = network.WLAN(network.AP_IF) #STA_IF
wlan.config(essid=ssid)
wlan.config(password=password)
wlan.active(True)
max_tries = 20
try_count = 0
while wlan.isconnected() == False\
and try_count < max_tries\
and wlan.status() > 0:
try_count = try_count + 1
print(f'{try_count}: Waiting for connection... {wlan.status()}')
lcd.clear()
lcd.message(f'{try_count}: Waiting for connection... {wlan.status()}')
sleep(1)
if (wlan.status() < 0):
raise Exception("failed to connect")
print(f'wlan.isconnected():{wlan.isconnected()} wlan.status(): {wlan.status()}')
print(wlan.ifconfig())
ip = wlan.ifconfig()[0]
print(f'Connected on {ip}')
return ip
def open_socket(ip):
# Open a socket
address = (ip, 80)
connection = socket.socket()
connection.bind(address)
connection.listen(1)
print(connection)
return connection
global num
num = 0
def serve(connection,ip):
global num
state = 'ON'
color = 'green'
pico_led.on()
temperature = 0
while(True):
client = connection.accept()[0]
request = client.recv(1024)
request = str(request)
try:
request = request.split()[1]
except IndexError:
pass
temperature = pico_temp_sensor.temp
temperature = (temperature * 9/5) + 32
temperature = round(temperature, 2)
my_table_style="""
<style type="text/css">
.tg {
border-collapse:collapse;
border-spacing:0;
margin-left: auto;
margin-right: auto;
margin-top: 60px;
}
.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;
font-size:100px;
overflow:hidden;padding:10px 5px;word-break:normal;}
.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;
font-size:100px;
font-weight:normal;overflow:hidden;padding:20px 20px;word-break:normal;}
.tg .tg-0lax{text-align:center;vertical-align:top}
.center {
margin-left: auto;
margin-right: auto;
text-align: center;
}
p { margin:0 }
</style>
"""
my_html = f"""
<html>
<meta http-equiv="refresh" content="1; URL=http://{ip}/">
{my_table_style}
<title>Carnival Game</title>
<body>
<table class="tg">
<tbody>
<tr>
<th class="tg-0lax"><span style="font-weight:bold">Team one</span></th>
<th class="tg-0lax">{score_1}</th>
</tr>
<tr>
<td class="tg-0lax"><span style="font-weight:bold">Team two</span></td>
<td class="tg-0lax">{score_2}</td>
</tr>
</tbody>
</table>
<div class="center" style="width:75%;">
<p style="font-size:70px;color:green">
Games played: {games_played}
</p>f
<p style="font-size:40px;">
Temperature of inner components is about {temperature} F
</p>
</div>
</body>
</html>
"""
num += 1
client.send(my_html)
client.close()
try:
ip = connect()
print(ip)
lcd.clear()
lcd.message(ip)
connection = open_socket(ip)
serve(connection,ip)
except KeyboardInterrupt:
machine.reset()