This section is under development
GitHub DEV branch: https://github.com/growboxguy/Gbox420/tree/Dev
Raspberry Pi Pico W
Goals:
Porting the Aero, Hempy, and Reservoir modules to Raspberry Pi Pico W
Get rid of ESP-Link and communicate with Home Assistant directly over Wi-Fi using MQTT messages (Publish / Subscribe topics)
Simplify the Gbox420 system: Merging the Mega2560 and Nano modules and components
MQTT Publish: Report sensor readings using JSON format
MQTT Subscribe: Receive commands via MQTT messages
Use Windows during the development
Evaluate using FreeRTOS real-time operating system for microcontrollers
Pico W vs Arduino Nano R3 main differences:
132 times more SRAM (264KB vs 2KB)
Flash (2MB vs 32KB)
No EEPROM : This is not ideal, at every boot the default settings defined in Settings.h are loaded - Need to find a way to store settings changes between reboots
Lower I/O pin voltage (3.3V vs 5V) - Most components support 3.3V, the AC power sensor could be problematic
26 GPIO pins (16 PWM, 3 Analog) vs 22 GPIO pins (6 PWM, 6 Analog)
Raspberry Pi Pico W
2 Mb flash26 × multi-function GPIO pins: 3 x Analog pins,2× UART, 2× SPI controllers, 2× I2C controllers, 16× PWM channels
Datasheet
Hold the BOOTSEL button while connecting the Raspberry Pi Pico to a PC and it will show up as a drive:
Development environment setup
UPDATE: The entire process of setting up the Pico C/C++ SDK (Cmake, Visual Studio,Pico SDK, downloading and building the Pico examples) on Windows is now automated. Download the latest release from: https://github.com/raspberrypi/pico-setup-windows
elect both the Show ReadMe (super useful if you are new to Visual Studio), and the Clone and build options.
Also install the latest Arm GNU Toolchain AArch32 bare-metal target (arm-none-eabi) and select it as an Active Kit in Visual Studio code.
Manual installation
To configure the development environment follow this Raspberry Pi Pico C/C++ Toolchain on Windows with VS Code guide. It will guide you through installing Visual Studio Code, Arm Toolchain, MinGW, Cmake, Python, Git, Pico SDK, and setting up the local environment variables.
Notes:
Skip the Build Blink Example chapter since the Pico W's LED is connected to the Wi-Fi module.
Install Visual Studio Code first and configure Git to use it as the default editor.
When installing Git select the "Add a Git Bash Profile to Windows Terminal" option
Select the MinGW i686 posix-sjlj release
Pico Examples
Install the pico examples using Git Bash:
echo mingw32-make %* > /c/VSARM/mingw/mingw32/bin/make.bat
mkdir /c/VSARM/sdk
mkdir /c/VSARM/sdk/pico
cd /c/VSARM/sdk/pico
git clone -b master https://github.com/raspberrypi/pico-sdk.git
cd pico-sdk
git submodule update --init
cd ..
git clone -b master https://github.com/raspberrypi/pico-examples.git
Based on the 2.2. Building an SDK example chapter of the Pico W - Connecting to the internet guide prepare the build files for the Pico examples using Git Bash:
cd /c/VSARM/sdk/pico/pico-examples
mkdir build
cd build
cmake -DPICO_BOARD=pico_w -DWIFI_SSID="Your Network" -DWIFI_PASSWORD="Your Password" .. -G "MinGW Makefiles"
Blink example
In Visual Studio Code open the folder: C:\VSARM\pico-examples\pico_w\wifi\blink
cd C:\VSARM\pico-examples\pico_w\wifi\blink
mingw32-make
The result should be several binary files generated inside the Blink folder.
Copy the picow_blink.uf2 file to the Pico W. The board will automatically reboot and the LED should start blinking.
Wi-Fi scan example
Open the C:\VSARM\sdk\pico\pico-examples\pico_w\wifi\wifi_scan folder with Visual Studio Code. By default, the wifi_scan example prints the available networks to the UART output. To change this to the USB add the following lines to the CMakeList.txt:
pico_enable_stdio_usb(picow_wifi_scan_background 1) # enable usb output
pico_enable_stdio_uart(picow_wifi_scan_background 1) # enable uart output
Save the changes and re-create the make files using the Integrated Terminal (View/Terminal) - Git Bash. To set Git Bash as default press CTRL + Shift + P and type: Default profile
Update the make files using Git Bash:
cd /c/VSARM/sdk/pico/pico-examples/build
cmake -DPICO_BOARD=pico_w -DWIFI_SSID="Your Network" -DWIFI_PASSWORD="Your Password" .. -G "MinGW Makefiles"
Next, open the wifi_scan make files and create a binary:
cd /c/VSARM/sdk/pico/pico-examples/build/pico_w/wifi/wifi_scan
mingw32-make
Upload the picow_wifi_scan_background.uf2 file to the Pico W. After it reboots the Pico W will print the detected Wi-Fi networks to the USB output. To see this we need the COM number of the Pico W and Putty. Run the following command to check the COM port and bitrate:
mode
Open Putty and select Serial connection type:
The list of available networks are printed every 10 seconds:
MQTT server connection
To test relaying MQTT Subscribe and Publish messages between the Raspberry Pico W and an MQTT broker ( Mosquitto broker running on Home Assistant OS in this case) use the following test program written in C++:
MQTT client test
Update the Constants section with your WiFi and MQTT server parameters in the MqttTest.cpp file:
// Constants - UPDATE THIS SECTION
#define WIFI_SSID "GboxNet" ///< WiFi SSID
#define WIFI_PASSWORD "SuperSecretPassword" ///< WiFi Password
#define MQTTServerDNS NULL ///< MQTT server DNS name, leave NULL to use MQTTServerIP instead
#define MQTTServerIP "192.168.1.100" ///< MQTT server IP
#define MQTTServerPort 1883 ///< MQTT server Port
#define MQTTServerUser "MqttUser" ///< MQTT server username, NULL if not needed
#define MQTTServerPassword "SuperSecretPassword" ///< MQTT server password, NULL if not needed
The test program utilizes the Light Weight IP open-source TCP/IP stack's MQTT client implementation (https://www.nongnu.org/lwip/2_0_x/group__mqtt.html). I made a middle layer called MQTTAPI to make it easier to interact with the LWIP MQTT client.
Expected test result: After a 10-second delay the Pico W should connect to the local WiFi, Find the IP address of the MQTT server, Connect to the MQTT server, and Subscribe to a test topic. Additionally every 30 seconds it should publish a randomly generated JSON to an MQTT topic. The test program currently supports MQTT 3.1.1 with password authentication. TLS cryptography is also supported by the LWIP library but is not implemented in the test.
You can use MQTT Explorer to view the Published messages, or the MQTT Last Will and Testament (LWT) message in case the Pico W goes offline. You can also send test messages to the Raspberry Pico W by publishing messages to the Subscribe topic set up in the test program. By default, the following topics are used during the test:
PubTopic: Gbox420/Hempy/ - Fake JSON reports are sent here every 30 seconds
SubTopic: Gbox420CMD/Hempy/# - Any message sent to this topic or any sub-topics will be displayed on the Pico W
LwtTopic: Gbox420LWT/Hempy/ - When the Pico W goes offline the MQTT broker will notify all subscribers by posting a message defined in LwtMessage to this topic.
Picoprobe
For debugging use a second Pico (Picoprobe) connected to the Gbox420 Pico's UART and SWD (Debug) port.
More info and setup guide: https://datasheets.raspberrypi.com/pico/getting-started-with-pico.pdf#picoprobe_section. It will guide you through installing MSYS2, building OpenOCD, and uploading the Picoprobe UF2 binary.
While running debugging, the USB output is not available on the Gbox420 Pico. Connect the Picoprobe's USB port to the machine you are using to develop, it should show up as an USB Serial Device on a COM port
Using Putty open the COM port using speed 115200
The Gbox420 Pico's UART output should be relayed to the Picoprobe's USB output
Building OpenOCD
Set up the prerequisites and build OpenOCD by running the following commands in MSYS2 - MINGW64:
pacman -Syu
pacman -Su
pacman --disable-download-timeout -S mingw-w64-x86_64-toolchain git make libtool pkg-config autoconf automake texinfo mingw-w64-x86_64-libusb
export PATH=$PATH:/mingw64/bin
git clone https://github.com/raspberrypi/openocd.git --branch rp2040-v0.12.0 --depth=1
cd openocd
./bootstrap
./configure --disable-werror
make -j4
Notes: Run pacman - Syu multiple times, until it reports all packages up-to-date.
Start using the Picoprobe with OpenOCD by running:
src/openocd -f interface/cmsis-dap.cfg -c "adapter speed 5000" -f target/rp2040.cfg -s tcl