Let’s learn how to use the Wio dev board RP2040 microcontroller distributed by Seeed Studio to build a client-server socket based project.
We recently presented the Wio Dev Board RP2040 distributed by Seeed Studio, a high performance microcontroller based on dual core ARM Cortex M0 + processor. Today we will learn how to program its activities through the Python language.
The project that we are showing will allow to keep under control the transmission characteristics of our wireless network.
The basic program
First you need to define a program capable of acquiring information through the wifi antenna of the microcontroller.
We open our trusty Thonny, and after installing the latest firmware version of the card, we write the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import network import usocket from machine import Pin, I2C, ADC, UART, SPI, PWM from time import sleep N1 = network.WLAN_SPI(network.STA_IF) N1.active(True) print("API list:") dir(N1) print("wifi list:") lis = N1.scan() for q in lis: print(q) N1.connect("FASTWEB-3CZFU9","KHDSNUK69G") if N1.isconnected(): print(" ip gateway netmask MAC ssid") print(N1.ifconfig()) |
After importing the necessary micropython libraries, we create the N1 connection as WLAN_SPI, and activate it. Then we request the list of nearby networks (N1.scan) and print the result. Finally, if the connection works, we print the configuration of the board.
The result is the following:
Interesting … but how can it come in handy?
Python is your friend
The isconnected () method returns a list of objects. We can access each of them through positional notation like this:
1 2 3 4 5 6 |
l = N1.ifconfig() print ("IP = ", l[0]) print ("gateway = ", l[1]) print ("netmask = ", l[2]) print ("MAC = ", l[3]) print ("ssid = ", l[4]) |
In the same way we can run the scan of lis, which contains the scan values (as indicated in line 13-15 of the previous listing). Each element of the list is a string, so it is possible to extract the necessary parameters through Python functions. Let’s say we want to manage a small db containing the network name and signal strength in dB: it will be sufficient to create a simple function to acquire the values through the .split () method and save the parameters placed in position 1 and 2.
1 2 3 4 5 6 |
reti = list() potenza = list() for testo in lis: lista2 = testo.split(",") reti.append(lista2[1]) potenza.append(lista2[2]) |
At this point we just have to save the acquired data in a database to manage the time series later.
But we are on a microcontroller, and the available memory may not be enough … how to do it?
Socket programming
The idea is to use a client-server system based on socket programming, in which the client is represented by the Wio RP2040 manager and the server by a PC on the network.
The client source will be changed like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import network import usocket from machine import Pin, I2C, ADC, UART, SPI, PWM from time import sleep reti = list() potenza = list() N1 = network.WLAN_SPI(network.STA_IF) N1.active(True) print("Preparazione dati:") dir(N1) # Create a list with the output lis = N1.scan() # Extract the elements we want to monitor for testo in lis: lista2 = testo.split(",") reti.append(lista2[1]) potenza.append(lista2[2]) N1.connect("FASTWEB-3XXXX0","XXXXXXX00X") #Insert your SSID and password here if N1.isconnected(): s = usocket.socket() addr=('192.168.1.50',20000) s.connect(addr) for i in range(len(reti)): comando = "" comando = "Elemento " + str(i) + " Rete = " + reti[i] + " " + " - potenza = " + potenza[i] s.send(comando.encode()) s.close() |
This time the client, instead of printing the list of data on the terminal, will format them and insert them in the networks and power lists.
On line 28 the connection to the server socket will be made, while the loop on lines 30-33 will create the information strings to be sent.
The last command closes the connection.
On the server side, the code is even simpler:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import socket def ricevi_comandi(conn): while True: richiesta = conn.recv(4096) risposta = richiesta.decode() print(risposta) def sub_server(indirizzo, backlog=1): try: s = socket.socket() s.bind(indirizzo) s.listen(backlog) print("Server Inizializzato. In ascolto...") except socket.error as errore: print(f"Something went wrong... \n{errore}") print(f"Trying to reinitialize the server...") sub_server(indirizzo, backlog=1) conn, indirizzo_client = s.accept() #conn = socket_client print(f"Server - Client connection established: {indirizzo_client}") ricevi_comandi(conn) if __name__ == '__main__': sub_server(("192.168.1.50", 20000)) |
The code defines the server listening settings (lines 13-16), then, if all went well, it gets ready to receive the client’s address to print a message of successful connection.
Then it enters the listening configuration (ricevi_comandi(), line 3), receiving the strings transmitted by the client, decoding their content and printing the result on the screen.
Implementation notes:
The system was created to evaluate the characteristics of the SeeedStudio Wio RP2040 card (available on their website). The programs presented were written trying to evaluate if, with the latest firmware release, the product was “compliant” with the MicroPython programming libraries. The tests we have done show that the client server system works as expected, even if the implementation of the sources is not foolproof.
A possible subsequent development could be represented by a change to the server: on line 7, instead of just printing a string, we could send the information to a database. Maybe after extracting the components as we have seen on the client. Using the RTC module present on the card, we could time the sendings every few minutes, and have on the database the set of network coverage values in the medium term. Or sound an alarm in the event of a drop in signal strength. With a little imagination it is easy to realize the infinite possibilities of application of the card.
Final remarks
After a large exchange of information and messages with Seeed Studio, we were able to get our hands on a sufficiently stable board firmware, which makes the board perfectly usable. We kept the card running for several hours, without experiencing any overheating or blocking of operation. We therefore believe that we can conclude the tests with a more than sufficient vote: even if limited to 2.4G, the RP2040 board coupled with the WiFi system is a winner in all those situations in which we have a distributed control system, based on sensors and piloted. from an MCU, but we want to centralize its information.
Join our groups onTelegram…