Skip to content

13. Networking and Communications

This week I worked on networking two rp2040’s wired and unwired. Here is my group work. This is the board I made for the week Kicad Folder For the Week.

Wired

I started off by watching a lecture from Dr. Harris that was on Ms. Morrow’s site to help learn about networking. After that I started to get to work on connecting two rp2040s using wires. For this I used a Seeed Xiao RP2040 and a Raspverry Pi Pico, the Pico as the parent and the Xiao as the child. To wire it I connected ground between the two and then connected the Pico’s TX to the Xiao’s RX, and the Pico’s RX to the Xiao’s TX. TX = Transmiting. RX = Reseiving. You can see what that looked like wired here:

Then using this tutorial I was able to get the two communitcating aswell as getting the on board LED’s to blink. The code form the video is on the Git page. The code works by sending a message betweeen the two micorcontrollers and one of them has a counter that goes up every time. The Pico is a parent and the Xiao is the child, the Xiao is has the counter and and sends it the Pico.

Here you can see the serial monitor for the Pico update:

Then I used the Led_system and Led_clint code and instead of sending messages between serial monitor it uses the on board LED’s to show the connection between the two of them. You can see that here:

Here I changed the code to blink 2 diffrent LED’s that were off the board:

Wireless

To work with wireless I used this tutorial. I was able to get the tutorial working just fine. I then tried to get it working with a neo-pixel code that I got form Dylan.

For the code to work you need to create a small library for your wifi name and password. secret.py

ssid = 'Network ID'
password = 'password'

Here is the updated client.py and server.py.

client.py

# 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
import machine, neopixel
np = neopixel.NeoPixel(machine.Pin(28), 1)

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("###.##.##.#", 80) # Address of Web Server, input your ip here
    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
    r = int(ss[0:p])   # Extract RED value
    ss = ss[p+1:]      # Remove red part
    p = ss.find(",")   # Find comma separator
    g = int(ss[0:p])   # Extract GREEN value
    b = int(ss[p+1:])  # Extract BLUE value
    print(r,g,b)       # Print RGB values
    print()
    np[0] = (r, g, b)
    np.write()
    s.close()          # Close socket
    time.sleep(0.2)    # wait

server.py

# 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
        r = random.randint(0,255)
        g = random.randint(0,255)
        b = random.randint(0,255)
        # Join to make a simple string with commas as separators
        rgb = str(r) + "," + str(g) + ","+str(b)

        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')

With this code I was still able to get the connection between the two boards but after trying to change what pin the neopixel was on I couldn’t get my neopixle to turn on. This is what the client looked like after connecting the two boards:


Last update: June 5, 2023