Reputation: 1
I am super new to programming with MicroPyton. I watched a couple viddeos on circuit python as I wanted to get my Pico Pi W connected via bluetooth to be able to contorl the LEDs I attached to it. Come to find out that Circuit Python does not support bluetooth even though the Pico Pi W supports it. I was told that I would need to most likely use CircuitPython as its similar but should get the job done. I was looking and I couldnt find any code or something that would allow me to connect via bluetooth, but I did find a wifi connect code. My code lets me connect to a wifi network (as long as I know the SSID and password) I can then connect to if by surfing to the IP address the Pico picks up. It will project a very basic html page with buttons on it that will allow me to statically change the the color. I can change it from red to blue to green and then off. When i try to make it run an animation of sorts (needs to be in a loop to do so, at least my very little knowledge tells me I need to do so.) it seems to disconnect from the wifi connection(at least stop broadcasting itself) and not allow me to change colors back to a static color or even off. I have to either disconnect the power to the board/manually stop the program from running in Thonny. I will paste my code below to see if anyone can help.
import network
import socket
import machine, neopixel, random, math, time
from time import sleep
from picozero import pico_led
p = machine.Pin.board.GP0
n = neopixel.NeoPixel(p, 30)
ssid = ''
password = ''
def connect():
#Connect to the WLAN
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while wlan.isconnected() == False:
print('Waiting for connection...')
sleep(1)
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)
return connection
def webpage(state):
#Template HTML
html = f"""
<!DOCTYPE html>
<html>
<body>
<form action="./red">
<input type="submit" value="RED" />
</form>
<form action="./green">
<input type="submit" value="GREEN" />
</form>
<form action="./blue">
<input type="submit" value="BLUE" />
</form>
<form action="./xmas">
<input type="submit" value="xmas" />
</form>
<form action="./lightoff">
<input type="submit" value="light off" />
</form>
<p>LED is {state}</p>
</body>
</html>
"""
return str(html)
## Create a random function
def randint(lower, upper):
gap = upper - lower
if gap == 0:
return lower
return (random.getrandbits(int(math.log(gap, 2)))% gap) + lower
ChristmasColors = [(255, 0, 0), (0, 255, 0), (255, 0, 0), (0, 255, 0)]
AllColors = [(255, 0, 0), (0, 255, 0), (255, 255, 255), (0, 0, 255)]
WhiteBlue = [(255, 255, 255), (0, 0, 255), (255, 255, 255), (0, 0, 255)]
Red = [(255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0)]
Blue = [(0, 0, 255), (0, 0, 255), (0, 0, 255), (0, 0, 255)]
Green = [(0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0)]
Yellow = [(255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0)]
Orange = [(255, 165, 0),(255, 165, 0), (255, 165, 0), (255, 165, 0)]
Fire = [(255, 165, 0), (255, 0, 0), (255, 165, 0), (255, 0, 0)]
def serve(connection):
#Start a web server
state = 'OFF'
pico_led.off()
while True:
client = connection.accept()[0]
request = client.recv(1024)
request = str(request)
print(request)
try:
request = request.split()[1]
except IndexError:
pass
if request == '/red?':
pico_led.on()
for i in range(30):
n[i] = (i * 8, 0, 0)
n.write()
state = 'RED'
elif request == '/green?':
pico_led.on()
for i in range(30):
n[i] = (0, i * 8, 0)
n.write()
state = 'GREEN'
elif request == '/blue?':
pico_led.on()
for i in range(30):
n[i] = (0, 0, i * 8)
n.write()
state = 'BLUE'
elif request == '/xmas?':
pico_led.on()
try:
while True:
for i in range(30):
n[i] = ChristmasColors[randint(0,4)]
#n[i-2] = (0,0,0)
n.write()
machine.idle()
time.sleep(.01)
except KeyboardInterrupt:
pico_led.off()
state = 'YELLOW'
elif request == '/lightoff?':
pico_led.off()
for i in range(30):
n[i] = (0, 0, 0)
n.write()
state = 'OFF'
html = webpage(state)
client.send(html)
client.close()
try:
ip = connect()
connection = open_socket(ip)
serve(connection)
except KeyboardInterrupt:
machine.reset()
I do want to try and get this swapped over to bluetooth and use a bluetooth compatible app on my cell phone (iPhone 15) to connect instead of wifi as I would have to run a hot spot to get it to connect while out and a bout with my prop. Can anyone lend some assistance on this please and thank you in advance!
Regards, Andrew
I have tried to eliminate the loop but if I do then it becuase just a static xmas color (alternating red green) when its supposed to swap randomly between the two colors looking like xmas lights. I have tried adding break statements but they dont seem to work. I am not sure what exaclty I am doing wrong.
Upvotes: 0
Views: 53