added ESP32 example

This commit is contained in:
OddlyTimbot 2024-09-01 19:19:04 -04:00
parent 8671363ee4
commit 9705840750
3 changed files with 58 additions and 9 deletions

Binary file not shown.

51
rp2040_client/README.md Normal file
View File

@ -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.

View File

@ -7,26 +7,23 @@ import network
import time import time
from math import sin 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 # Received messages from subscriptions will be delivered to this callback
def sub_cb(topic, msg): def sub_cb(topic, msg):
print((topic, msg)) print((topic, msg))
if msg == b'on': if msg == b'on':
print("turn LED on") print("turn LED on")
led.value(1) #can do some hardware control here
elif msg == b'off': elif msg == b'off':
print("turn LED 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: # Fill in your WiFi network name (ssid) and password here:
wifi_ssid = "OddlyAsus" wifi_ssid = "XXXXXXXXX"
wifi_password = "8699869986" wifi_password = "XXXXXXXXX"
# Connect to WiFi # Connect to WiFi
wlan = network.WLAN(network.STA_IF) wlan = network.WLAN(network.STA_IF)
@ -37,6 +34,7 @@ def main(server="192.168.1.59"):
time.sleep(1) time.sleep(1)
print("Connected to WiFi") print("Connected to WiFi")
#client name should be unique
c = MQTTClient("pico_board", server, 1883) c = MQTTClient("pico_board", server, 1883)
c.set_callback(sub_cb) c.set_callback(sub_cb)
c.connect() c.connect()