Embedded Networking and Communications¶
Group Assignment¶
This week we made connection between two boards made by Mkhitar and Zhirayr to exchange data using I2C bus. We wrote simple programs for each board using C.
From one board, we were sending some character, which was recognized by the other board (from Zhirayr) and this board was turning on and off a small DC motor.
About I2C, I remembered that it can be used to exchange information between up to 127 devices (because it has 8 bits, 1 bit for start and 7 for address) and it has speed from 100 kbit/s up to 3.4Mbit/s (depends on work mode)
One program was made to send data, and the other to receive it.
You can check our work here
Individual Assignment¶
For this week, I focused on building wireless communication between an Android phone and RP2350. The goal was to control the onboard LED of the custom-built mobile application using Wi-Fi communication.
Android Studio¶
I used Android Studio to create the app. To start I downloaded Android Studio from official website
I began by designing the user interface for the mobile application. Below you can see the initial layout of the app’s opening screen, followed by the progress I made as I continued developing its features and functionality.
I designed a simple Android application that contains two buttons: ON and OFF. Pressing each button sends a specific HTTP request to the Raspberry Pi Pico 2W over the local network.
After completing the design of the app interface. I proceeded to implement the hardware functionality of the application. I programmed the app to send HTTP requests to the Raspberry Pi 2W based on user input (button presses).This stage connected the app’s visual interface to real-world hardware behavior.
In this step, I assigned specific ON and OFF commands to the buttons within the mobile application. When the ON button is pressed, the app sends an HTTP GET request to the Raspberry Pi Pico 2W, instructing it to turn the onboard LED ON. Similarly, pressing the OFF button sends a command to switch the LED OFF.
Here are commands for ON and OFF functions
To enable IP-based communication, I used a socket on the Raspberry Pi Pico 2W. This approach provides a structured way to send and receive data over the network. Once the HTTP request is sent from the phone, the operating system handles the rest, ensuring the packet is properly delivered and processed. Using sockets allowed for a simple and efficient server setup to control the onboard LED
After setting up the communication, I added a text message output to confirm when data is sent.
When the app sends the command to turn the LED ON, the Pico 2W receives it, activates the LED, and prints a “successful command” message in the terminal.
This helped verify that the system was working correctly.
While running the program, I faced an issue where it failed to virtualize during execution. This prevented the system from initializing the network communication properly.
To resolve the virtualization issue, I did some research and found a helpful solution on GitHub. The fix was outlined in two steps: Step 1: I applied the first solution, which partially resolved the issue. Step 2: I’m currently implementing the second step to fully restore functionality. This shows how useful community resources can be when troubleshooting technical problems during development.
Solution Text
Hi. I have solved my problem.
For those who have the sames issues you have to make 2 things:
Activate Virtualization in bios
Deactivate Hyper-v and hypervisor platform in "activate/deactivate windows function"
& run powershell as admin and make: Disable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V
It should be work after a reboot.
After applying both steps, the program worked successfully. The virtualization issue was resolved
Thonny IDE¶
the final step was to write the code for the Raspberry Pi Pico 2W to receive and process commands over Wi-Fi.
Code explanation¶
My code consists of few parts, and here is explanation for them
1. Import required libraries¶
Imports modules to manage WiFi, sockets, hardware pins, JSON, and timing.
import network
import socket
import machine
import ujson
import time
from mywifi import networksetting
2. Get WiFi credentials¶
Calls your custom function to get the SSID and password for WiFi.
ssid, password = networksetting()
3. Set up WiFi interface¶
Initializes and activates the WiFi station mode, then starts connecting.
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
4. Wait for WiFi connection¶
Loops for up to 10 seconds checking for a successful WiFi connection.
max_wait = 10
print('Waiting for connection...')
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
time.sleep(1)
5. Handle connection result¶
Raises error if connection fails, or prints IP address if successful.
if wlan.status() != 3:
raise RuntimeError('Connection failed')
else:
status = wlan.ifconfig()
print(f'Connection to {ssid} successful!')
print('IP address: ' + status[0])
ip_address = status[0]
6. Configure LED pin¶
Sets up GPIO2 as an output pin to control the LED.
led = machine.Pin(2, machine.Pin.OUT)
7. Create and bind TCP socket¶
Creates a TCP socket, binds to all interfaces on port 5000, and starts listening.
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('0.0.0.0', 5000))
s.listen(1)
print('Waiting for a connection...')
8. Handle client connections and commands¶
Accepts client connections, receives data, and parses it as a JSON command.
while True:
conn, addr = s.accept()
print('Connection from', addr)
data = conn.recv(1024).decode()
print('Raw data:', data)
9. Parse and execute GPIO command¶
Parses the JSON and changes LED state depending on the ‘gpio’ field value.
try:
command = ujson.loads(data)
print('Parsed command:', command)
if command.get('gpio') == 'on':
print('Turning LED on...')
led.value(1)
elif command.get('gpio') == 'off':
print('Turning LED off...')
led.value(0)
else:
print('Invalid GPIO command')
10. Error handling and cleanup¶
Catches errors and always closes the client connection after handling.
except Exception as e:
print('Error processing command:', e)
conn.close()
Code Summary¶
This microcontroller script connects to WiFi, opens a TCP port, listens for JSON commands like {"gpio": "on"}
, and toggles the onboard LED accordingly.
Smartphone settings¶
To connect Smartphone I had to change some settings
I found Build number
Then enabled wireless debugging
And selected “Pair with QR code” option
Work Video¶
So after everything is set up, I connected smartphone to Android Studio, runned program on board and
Conclusion¶
It was really fun to work with WiFi network. In 2014 I fellt in love with robotics, when I saw robot moved by smartphone using WiFi. So now I can do it by myself))