diff --git a/resources/ESP32_GENERIC-20240602-v1.23.0.bin b/resources/ESP32_GENERIC-20240602-v1.23.0.bin new file mode 100644 index 0000000..16f70b5 Binary files /dev/null and b/resources/ESP32_GENERIC-20240602-v1.23.0.bin differ diff --git a/rp2040_client/README.md b/rp2040_client/README.md new file mode 100644 index 0000000..1b9e396 --- /dev/null +++ b/rp2040_client/README.md @@ -0,0 +1,51 @@ +In this folder you will find an example of using MQTT with a simple client. Note that this example uses MicroPython as the development environment. + +To use the example, MicroPython must be flashed on the device. + +In the Resources folder you will find a UF2 file that can be used to flash the Raspberry Pi Pico W board. Simply hold down the boot button while powering on the board. It will be reset and will show up as a simple drive on your computer. Drag the UF2 file to the board - it will reboot and have MicroPython installed. + +## ESP32 + +It is possible to use the example with an ESP board by Espressive as well (tested with ESP32 Wroom) + +Installing MicroPython on the ESP32 is slightly more complex, and requires the installation of "esptool", a python library that will allow you to flash a binary file to the board. + +The steps include: + +* Download the propery binary for the board +* Set up a virtual environment for Python +* Install esptool +* Identify the port the device is on +* Use esptool to flash the binary your downloaded to the board + +### Set up Virtual Environment + +Setting up a virtual environment for Python is necessary because Python is now regularly used by the operating system. Accordingly, the OS will not typically allow packages to be installed by another package manager, otherwise critical packages could be overwritten. A virtual environment allows all package dependencies to be installed in a virtual location, protecting system Python packages. + +`python -m venv esptoolenv` + +Now you can activate the environment, which will allow us to install the tool (see below) + +`source esptoolenv/bin/activate` + +### Install ESPTool + +In the virutal environment you can use the pip installer to get the tool. + +`pip install esptool` + +### Identify the port device is on + +`ls /dev/tty*` + +### Flash the binary + +`esptool.py --chip esp32 --port /dev/ttyUSB0 write_flash -z 0x1000 ESP32_GENERIC-20240602-v1.23.0.bin` + +### Deactivate the virtual environment + +`deactivate` + +You can now use your code editor (like Thonny) to connect to your device and write the python code you find here. + +As with the RP2040 you'll need to copy the MQTT client library onto the device. \ No newline at end of file diff --git a/rp2040_client/main.py b/rp2040_client/main.py index 79e39d2..c9d261e 100644 --- a/rp2040_client/main.py +++ b/rp2040_client/main.py @@ -7,26 +7,23 @@ import network import time from math import sin -led = Pin("LED", Pin.OUT) -led.value(1) - -button = Pin(14, Pin.IN, Pin.PULL_UP) # Received messages from subscriptions will be delivered to this callback def sub_cb(topic, msg): print((topic, msg)) if msg == b'on': print("turn LED on") - led.value(1) + #can do some hardware control here elif msg == b'off': print("turn LED off") - led.value(0) -def main(server="192.168.1.59"): +# Replace details in code below + +def main(server="192.168.1.XX"): # Fill in your WiFi network name (ssid) and password here: - wifi_ssid = "OddlyAsus" - wifi_password = "8699869986" + wifi_ssid = "XXXXXXXXX" + wifi_password = "XXXXXXXXX" # Connect to WiFi wlan = network.WLAN(network.STA_IF) @@ -37,6 +34,7 @@ def main(server="192.168.1.59"): time.sleep(1) print("Connected to WiFi") + #client name should be unique c = MQTTClient("pico_board", server, 1883) c.set_callback(sub_cb) c.connect()