Week 14 Networking- Merritt and Stuart¶
For this week’s group assignment, we are doing wireless communication between 2 Raspberry Pi Pico W’s, as well as UART communication. We used This Tutorial to understand wireless UART communications better. The Code takes the IP from one Pico, which can then be referenced to the other pico to establish a connection. Then, the host pico sends commands over the wireless network to the other pico, which decodes the commands and performs an action. W The example code was originally used to send RGB values between the 2 microcontrollers, but because we weren’t sending RGB, but rather instructions to blink an LED, we modifed some of the code.
Coding¶
Here is the code that establishes one pico as the server
# Webserver to send RGB data
# Tony Goodhew 5 July 2022
import network
import socket
import time
from machine import Pin, ADC
from secret import ssid,password
import random
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
# Wait for connect or fail
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection...')
time.sleep(1)
# Handle connection error
if wlan.status() != 3:
raise RuntimeError('network connection failed')
else:
print('connected')
status = wlan.ifconfig()
print( 'ip = ' + status[0] )
# Open socket
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
print('listening on', addr)
# Listen for connections
while True:
try:
cl, addr = s.accept()
print('client connected from', addr)
request = cl.recv(1024)
print(request)
# Do not unpack request
# We reply to any request the same way
# Generate 3 values to send back
b = random.randint(0,1) # originally a random int between 0 and 255, changed between 0 and 1 (on and off)
# Join to make a simple string with commas as separators
rgb = + ","+str(b) # Changed so that rgb only sends the on and off message
response = rgb # This is what we send in reply
cl.send(response)
print("Sent:" + rgb)
cl.close()
except OSError as e:
cl.close()
print('connection closed')
Here is the code that makes a Pico a client:
```
# Program to read RGB values from a local Pico Web Server
Tony Goodhew 5th July 2022¶
Connect to network¶
import network import time from secret import ssid, password import socket
wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(ssid, password) while not wlan.isconnected() and wlan.status() >= 0: print(“Waiting to connect:”) time.sleep(1)
Should be connected and have an IP address¶
wlan.status() # 3 == success wlan.ifconfig() print(wlan.ifconfig())
while True: ai = socket.getaddrinfo(“192.168.0.15”, 80) # Address of Web Server addr = ai[0][-1]
# Create a socket and make a HTTP request
s = socket.socket() # Open socket
s.connect(addr)
s.send(b"GET Data") # Send request
ss=str(s.recv(512)) # Store reply
# Print what we received
print(ss)
# Split into RGB components
l = len(ss)
ss = ss[2:l-1] # Strip to essentials
p = ss.find(",") # Find first comma
b = int(ss[p+1:]) # Extract BLUE value
print(r,g,b) # Print RGB values (Changed to only on and off value)
print()
# Set RGB LED here
s.close() # Close socket
time.sleep(0.2) # wait
```
Here is a video of the code working, one of them shows the server recieving the blink, and the other shows both the server and the client
/video>