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+

Kepler Kit

You can also buy them separately from the links below.

SN

COMPONENT

QUANTITY

LINK

1

Raspberry Pi Pico W

1

BUY

2

Micro USB Cable

1

3

Breadboard

1

BUY

4

Jumper Wires

Several

BUY

5

Servo

1

BUY

6

Ultrasonic Module

1

BUY

7

Li-po Charger Module

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.

../_images/9.sc_bb.png

3. Setup SunFounder Controller

  1. Install SunFounder Controller APP from APP Store(iOS) or Google Play(Android).

  2. Open the APP and click the + button on the home page to create a controller.

    ../_images/sc-a-2.jpg
  3. Here we choose Blank and Dual Stick.

    ../_images/sc-a-3.jpg
  4. Now we get an empty controller.

    ../_images/sc-a-4.jpg
  5. Click on the H area and add a Slider widget.

    ../_images/sc-a-5.jpg
  6. Click the gear on the control to open the settings window.

    ../_images/sc-a-6.png
  7. Set Maximum to 180 and Minimum to 0, then click to Confirm.

    ../_images/sc-a-7.jpg
  8. Click on the L area and add a Gauge widget.

    ../_images/sc-a-8.jpg
  9. Click the gear of the Gauge, open the settings window, set Maximum to 100, Minimum to 0, and unit to cm.

    ../_images/sc-a-9.jpg
  10. After finishing the widget settings, click Save.

    ../_images/sc-a-10.png

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.

  1. Upload ws.py and websocket_helper.py from the path of kepler-kit-main/libs to the Raspberry Pi Pico W.

    ../_images/9_sc3.png
  2. Double click the ws.py script and fill your WiFi’s SSID and PASSWORD.

    ../_images/9_sc1.png
  3. Open the 9_sunfounder_controller.py under the path of kepler-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.

    ../_images/9_sc2.png

    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.

  4. Back to SunFounder Controller APP, click the Connect button.

    ../_images/sc-c-4.jpg
  5. If PicoW is detected, tap it directly to connect.

    ../_images/sc-c-5.jpg
  6. If it doesn’t search automatically, you can also manually enter the IP to connect.

    ../_images/sc-c-6.png
  7. 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.

    ../_images/sc-c-8.jpg

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.