Skip to content
Buy 10+ on select items — save 10% auto-applied
Free US shipping on orders $35+
Order by 3pm ET — ships same-day from the US
Skip to main content

Pico W WiFi: Send data between boards | ShillehTek

October 23, 2023 511 views

Pico W WiFi: Send data between boards | ShillehTek
Project

Build a two-device Raspberry Pi Pico W WiFi client and server in MicroPython using Thonny, so you can send messages and expand to distributed apps with ShillehTek.

15 min Beginner1 parts

Video Tutorial (Optional)

Watch first if you want to follow along as two Raspberry Pi Pico W boards send messages to each other in MicroPython using Thonny.

Project Overview

In this project, you use two Raspberry Pi Pico W boards and their built-in WiFi to send messages between devices in MicroPython using the Thonny IDE. One Pico W runs a simple socket server that returns a random number, and the other Pico W runs a client that requests and prints that value.

  • Time: 15 to 30 minutes
  • Skill level: Beginner
  • What you will build: A two-device Pico W client and server that exchange data over WiFi using sockets

Parts List

From ShillehTek

External

  • Computer with Thonny IDE installed
  • 2x USB cables (to connect both Pico W boards)
  • WiFi network credentials (SSID and password)

Note: This tutorial uses MicroPython on the Pico W and connects both boards to the same WiFi network using a constants file for credentials.

Step-by-Step Guide

Step 1 - Allow multiple Thonny instances

Goal: Make it possible to work with two Pico W boards at the same time.

What to do: In Thonny, go to Tools > Options > General and untick Allow only single Thonny instance.

Thonny IDE settings showing the option to allow multiple Thonny instances for connecting to two Raspberry Pi Pico W boards
Enable multiple Thonny instances so you can connect to two Pico W boards at once.

Expected result: Thonny is configured to allow more than one window/instance.

Step 2 - Restart Thonny

Goal: Apply the new Thonny setting.

What to do: Close Thonny completely and reopen it.

Expected result: The setting is active after restart.

Step 3 - Open a second Thonny instance

Goal: Run code on both Pico W boards simultaneously from separate Thonny windows.

What to do: On macOS, open Terminal and run:

/Applications/Thonny.app/Contents/MacOS/thonny

If you are using Windows or Linux, the process is different. You can open another instance using a similar command in your shell, or by opening the app again.

Expected result: A second Thonny window opens.

Step 4 - Connect each Pico W to its own Thonny window

Goal: Make sure each Thonny instance is talking to a different Pico W.

What to do: Plug both Pico W boards into your computer. In each Thonny window, select the corresponding device/interpreter for that board.

Expected result: You can run code independently on each Pico W from its assigned Thonny instance.

Step 5 - Run the server code on one Pico W

Goal: Start a socket server that listens for connections and sends back a random number.

What to do: In one Thonny instance (connected to the first Pico W), run the following code:

Code:

from machine import Pin, ADC
import network
import random
import socket
import time

import constants


# Just making our internet connection
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(constants.INTERNET_NAME, constants.INTERNET_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()
        request = cl.recv(1024)
        print(request)
        # No need to unpack request in this example
        ran_num = str(random.randint(0, 100))
        cl.send(ran_num)
        print("Sent: " + ran_num)
        cl.close()

    except OSError as e:
        cl.close()
        print('connection closed')

Expected result: The Pico W connects to WiFi and prints that it is listening on port 80. When the client connects, it sends a random number.

Step 6 - Run the client code on the other Pico W

Goal: Connect to the server Pico W and print the received value.

What to do: In the other Thonny instance (connected to the second Pico W), run the following code. Make sure to replace <Change this> with the server Pico W IP address.

Code:

from machine import Pin, ADC
import network
import random
import socket
import time

import constants

# Just making our internet connection
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(constants.INTERNET_NAME, constants.INTERNET_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])

while True:
    ai = socket.getaddrinfo(<Change this>, 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"Anything") # Send request
    ss=str(s.recv(512)) # Store reply
    # Print what we received
    print(ss)
    # Set RGB LED here
    s.close()          # Close socket
    time.sleep(0.2)    # wait

Expected result: The client prints the server response, and you should see random numbers being received repeatedly.

Step 7 - Find and set the server IP address

Goal: Ensure the client points to the correct Pico W server on your network.

What to do: In the server code, uncomment the line below to print the server IP address, then use that IP in the client code where it says <Change this>:

#print('ip = ' + status[0])

Expected result: The client connects to the correct address and you consistently see random numbers sent from server to client.

Conclusion

You set up two Raspberry Pi Pico W boards using their built-in WiFi to send data back and forth in MicroPython using Thonny. With a simple socket server and client, you can exchange messages and extend this pattern for your own distributed projects.

Want parts for your next Pico W build? Shop components at ShillehTek.com. If you want help customizing this project or designing a connected product, check out our IoT consulting services.