Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
9. Play in @SunFounder Controller¶
In this project, you will learn how to build a remote project using Sunfounder Controller APP. In a LAN environment, you can control your Pico W circuit with your phone/tablet. You will find this app very useful if you want to build a simple robot with Pico W.
Here, we will use the slider bar on the APP to control the servo angle and the gauge on the APP to show the distance detected by ultrasonic.
1. Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Kepler Kit |
450+ |
You can also buy them separately from the links below.
SN |
COMPONENT |
QUANTITY |
LINK |
---|---|---|---|
1 |
1 |
||
2 |
Micro USB Cable |
1 |
|
3 |
1 |
||
4 |
Several |
||
5 |
1 |
||
6 |
1 |
||
7 |
1 |
||
8 |
18650 Battery |
1 |
|
9 |
Battery Holder |
1 |
2. Build the Circuit
Warning
Make sure your Li-po Charger Module is connected as shown in the diagram. Otherwise, a short circuit will likely damage your battery and circuitry.
3. Setup SunFounder Controller
Install SunFounder Controller APP from APP Store(iOS) or Google Play(Android).
Open the APP and click the + button on the home page to create a controller.
Here we choose Blank and Dual Stick.
Now we get an empty controller.
Click on the H area and add a Slider widget.
Click the gear on the control to open the settings window.
Set Maximum to 180 and Minimum to 0, then click to Confirm.
Click on the L area and add a Gauge widget.
Click the gear of the Gauge, open the settings window, set Maximum to 100, Minimum to 0, and unit to cm.
After finishing the widget settings, click Save.
4. Run the Code
Note
If your Pico W is now using the Anvil firmware, then you will need to 1.3 Install MicroPython on Your Pico.
Upload
ws.py
andwebsocket_helper.py
from the path ofkepler-kit-main/libs
to the Raspberry Pi Pico W.Double click the
ws.py
script and fill your WiFi’sSSID
andPASSWORD
.Open the
9_sunfounder_controller.py
under the path ofkepler-kit-main/iot
. Click the Run current script button or press F5 to run it. After successful connection, you will see the IP of Pico W.Note
If you want this script to be able to boot up, you can save it to the Raspberry Pi Pico W as
main.py
.Back to SunFounder Controller APP, click the Connect button.
If PicoW is detected, tap it directly to connect.
If it doesn’t search automatically, you can also manually enter the IP to connect.
When you slide the slider bar in the H area after clicking the Run button, the servo will adjust its angle. The gauge in the L area will show the distance if your hand is within 100cm of the ultrasonic sensor.
How it works?
The WS_Server
class in the ws.py
library implements communication with the APP. Below is the framework for implementing its basic functionality.
from ws import WS_Server
import json
import time
ws = WS_Server(8765) # init websocket
def main():
ws.start()
while True:
status,result=ws.transfer()
time.sleep_ms(100)
try:
main()
finally:
ws.stop()
First, we need to create a WS_Server
object.
ws = WS_Server(8765)
Star it.
ws.start()
Next, a while True
loop is used to perform the data transfer between Pico W and the SunFounder Controller APP.
while True:
# websocket transfer data
status,result = ws.transfer()
# the status of transfer data
print(status)
# the data you recv
print(result)
# the data you send
print(ws.send_dict)
time.sleep_ms(100)
status
is False
if it fails to get data from the SunFounder Controller APP.
And result
is the data that Pico W fetched from the SunFounder Controller APP.
Print it out and you will see something like the following. This is the value of all Widget areas.
{'C': None, 'B': None, 'M': None,,,,, 'A': None, 'R': None}
As in this case, we print the values of the H area separately and use them to operate the circuit.
status,result=ws.transfer()
#print(result)
if status == True:
print(result['H'])
And the ws.send_dict
dictionary is the data that Pico W sends to the SunFounder Controller APP. It is created in the WS_Server
class. It will be sent when ws.transfer()
is executed.
Its message is shown below.
{'Check': 'SunFounder Controller', 'Name': 'PicoW', 'Type': 'Blank'}
This is a blank message, to copy it to the widget on SunFounder Controller APP, we need to assign the value to the corresponding area in the dictionary. For example, assign the value 50
to the L area.
ws.send_dict['L'] = 50
The data is shown below:
{'L': 50, 'Type': 'Blank', 'Name': 'PicoW', 'Check': 'SunFounder Controller'}
For more details on using SunFounder Controller, please see SunFounder Controller APP.