Interface and application programming¶
Group Assignment¶
This week our team explored how to create interfaces that connect hardware inputs to software environments. We decided to use Unity to build a simple application where we could control a plane with real sensor data coming from an Arduino and IMU.
I worked on setting up the serial communication part—writing the code that reads data from the Arduino and properly parses it in Unity. It was a bit tricky to handle incomplete serial strings, so we used a buffer and wrote a ProcessBuffer() function that could extract valid lines. Once we had the x, y, z rotation values coming in cleanly, we used them to control the orientation of the plane in the Unity scene.
It was fun to see how physical movement could directly affect a virtual object. This was my first time connecting Unity to real hardware, and I think this kind of interface could be useful in many projects, especially in robotics or simulat
You can check all work here
Microcontroller: Raspberry Pi Pico 2W¶
This week, I explored how to send data wirelessly from the Raspberry Pi Pico 2W to a PC using the Blynk.cloud platform. The Pico 2W has a built-in Wi-Fi module, which I used to connect to the internet and stream data to a custom dashboard. I wanted to send data about string name and tune from my device which I described in my Electronics Production week
Connecting to Wi-Fi¶
To connect my Pico 2W to a Wi-Fi network, I wrote a function that activates the interface, attempts to connect, and checks the connection status:
def connect_to_internet(ssid, password):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
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)
if wlan.status() != 3:
print(wlan.status())
raise RuntimeError('network connection failed')
else:
print('connected')
print(wlan.status())
status = wlan.ifconfig()
Creating the Blynk Library¶
Since MicroPython doesn’t include a native Blynk library, I created a file called blynklib.py
and copied the official Blynk MicroPython client code into it.
This file includes:
- Blynk authentication
- Virtual pin writing via
virtual_write()
- Maintaining communication with
run()
Example method from the library:
def virtual_write(self, pin, *val):
self._send(MSG_HW, 'vw', pin, *val)
Configuration File¶
To keep sensitive information cleanly separated, I created a file named constants.py
:
INTERNET_NAME = '####'
INTERNET_PASSWORD = '####'
BLYNK_AUTH_TOKEN = '####'
This made it easier to reuse the same values in the main program.
Creating a Blynk Device and Dashboard¶
I created a new device and dashboard on the Blynk platform:
Went to the Devices section and clicked “New Device”
Selected “From Template”
and then Quickstart Template
After setup, I received an Auth Token, which I placed into the constants.py
file.
In the dashboard editor, I added the following widgets:
- A String Label on virtual pin
V5
- A Gradient Ramp on virtual pin
V8
Main Program¶
In the main code, I connected to Wi-Fi and Blynk, then sent values to the dashboard:
import network
from blynklib import Blynk
import constants
I created the Blynk object:
BLYNK = Blynk(constants.BLYNK_AUTH_TOKEN)
Then I wrote the logic to send data to specific virtual pins:
BLYNK.virtual_write(5, string_name) # Send string to label
BLYNK.virtual_write(8, pos) # Send numeric value to ramp
BLYNK.run() # Keep the connection alive
Final Result¶
Here is final interface
After I runned program from Thonny I could see the data from the Pico 2W update live on the Blynk web dashboard:
Conclusion¶
This weeked I used Blynk Cloud to show data from my device. I liked that very much, as it gives huge opportunity to use Analog or Digital data from real world, to display something you want on PC.
Files¶
Thonny main.py file
Blynk library file
Constants file file