We've been testing this out for a few weeks we'll be showing off some great projects in HackSpace mag issue 57 (subscribe now to make sure you don't miss out: hsmag.cc/subscribe).
To give you a taste of what we've been doing, here's a wifi-controlled buggy.
We've based it on the Kitronik Autonomous Robot platform for Pico (https://kitronik.co.uk/collections/robotics/products/5335-autonomous-robotics-platform-for-pico). Since Pico W is pin-compatible with Pico, we can just drop in a Pico W instead.
We're programmed this in MicroPython. While there's a separate version of MicroPython available for Pico W, the same code should run on both.
Let's take a look at what code we need to get the buggy moving:
import network
import socket
import time
from machine import Pin
from secrets import secrets
from PicoAutonomousRobotics import KitronikPicoRobotBuggy
print("starting buggy")
buggy = KitronikPicoRobotBuggy()
print("started buggy")
buggy.setLED(0, buggy.YELLOW)
buggy.show()
ssid = secrets['ssid']
password = secrets['password']
print("starting connection")
wlan = network.WLAN(network.STA_IF)
buggy.setLED(1, buggy.YELLOW)
buggy.show()
wlan.active(True)
buggy.setLED(2, buggy.YELLOW)
buggy.show()
print("connecting ...")
wlan.connect(ssid, password)
buggy.setLED(3, buggy.YELLOW)
buggy.show()
# Wait for connect or fail
wait = 10
while wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
wait -= 1
print(wait)
if (wait % 2 == 0):
print("here")
buggy.setLED(0, buggy.BLUE)
else:
buggy.setLED(0, buggy.RED)
buggy.show()
print('waiting for connection...')
time.sleep(1)
# Handle connection error
if wlan.status() != 3:
raise RuntimeError('wifi connection failed')
else:
print('connected')
buggy.setLED(0, buggy.GREEN)
buggy.setLED(1, buggy.GREEN)
buggy.setLED(2, buggy.GREEN)
buggy.setLED(3, buggy.GREEN)
buggy.show()
status = wlan.ifconfig()
print( 'ip = ' + status[0] )
response = """<!DOCTYPE html>
<html>
<head> <title>Pico W</title> </head>
<body> <h1>Pico W</h1>
<p><a href="/buggy/forward">forward</a><br>
<a href="/buggy/left">left</a></br>
<a href="/buggy/right">right</a></p>
<h2> The next object in front is at %s</h2>
</body>
</html>
"""
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
print('listening on', addr)
def check_and_move(request, find_str, buggy, left_motor, right_motor):
request = str(request)
if (request.find(find_str) == 6):
buggy.motorOn("l","f",left_motor)
buggy.motorOn("r","f",right_motor)
time.sleep(1)
buggy.motorOn("l","f",0)
buggy.motorOn("r","f",0)
# Listen for connections
while True:
try:
cl, addr = s.accept()
print('client connected from', addr)
request = cl.recv(1024)
print(request)
request = str(request)
check_and_move(request, '/buggy/forward', buggy, 100,100)
check_and_move(request, '/buggy/left', buggy, 100,0)
check_and_move(request, '/buggy/right', buggy, 0,100)
cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
cl.send(response % str(buggy.getDistance("f")))
cl.close()
except OSError as e:
cl.close()
print('connection closed')
There are a few things going on. First, we use the networking module the connect to the network. We've used a secrets.py file to hold the networking information. This should be called secrets.py and saved to the MicroPython device, and contain the following:
secrets = {
'ssid' : 'your-ssid-here',
'password' : 'your-password-here',
}
Once we're connected to the network, we use the sockets module to listen on tcp port 80. This is the port that web servers listen on. When a web browser connects, it makes a HTTP request on this port (see here for more details: https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol).
This HTTP request contains quite a bit of information that the server can use to supply the data, but most critically, it contains a location, and this should be in position 6 in the request.
if you run this code, you should see the IP address of the Pico W appear in the serial monitor. In our case is was 192.168.1.125, but yours will probably be different. You can now put that IP address into a web browser to connect to your Pico W.
We use the check_and_move function to check if a what location the browser has requested. Specifically if it's /buggy/forward, /buggy/left or buggy/right. If it's one of these, then our code turns the appropriate motors on for one second.
Finally, we always respond with the same HTML file. This contains links to the forward, left and right locations. If the user clicks on these, then the buggy will move in that direction. There's also information from the on-board ultrasonic distance sensor on how much space there is in front.
That's all there is to getting our little wireless buggy working with it's own built-in HTTP server. We'll have more information on getting started with this project as well as some other great Pico W projects in HackSpace magazine issue 57. Subscribe today to be sure to get yours at https://hsmag.cc/subscribe