|
| 1 | +# AtomVM MQTT Client Example Program |
| 2 | + |
| 3 | +The `mqtt_client_example` program illustrates use of the MQTT API by connecting to a well-known MQTT broker, and sending and receiving a message on a topic every 5 seconds. Results are displayed on the console. |
| 4 | + |
| 5 | +> Note. Building and flashing the `mqtt_client_example` program requires installation of the [`rebar3`](https://www.rebar3.org) Erlang build tool. |
| 6 | +
|
| 7 | +Start my copying the `src/config.erl-template` file to `src/config.erl` and edit the `ssid` and `psk` entries to match your environment. |
| 8 | + |
| 9 | + get() -> |
| 10 | + #{ |
| 11 | + sta => [ |
| 12 | + {ssid, "myssid"}, |
| 13 | + {psk, "mypassword"} |
| 14 | + ] |
| 15 | + }. |
| 16 | + |
| 17 | +Build the example program and flash to your device: |
| 18 | + |
| 19 | + shell$ rebar3 packbeam -p |
| 20 | + shell$ rebar3 esp32_flash -p /dev/ttyUSB0 |
| 21 | + |
| 22 | +> Note. This build step makes use of the [`atomvm_rebar3_plugin`](https://github.com/atomvm/atomvm_rebar3_plugin). See the `README.md` for information about parameters for setting the serial port and baud rate for your platform. |
| 23 | +
|
| 24 | +Attach to the console using `minicom` or equivalent: |
| 25 | + |
| 26 | + shell$ minicom -D /dev/ttyUBS0 |
| 27 | + ... |
| 28 | + I (1789) NETWORK: SYSTEM_EVENT_STA_CONNECTED received. |
| 29 | + I (2899) event: sta ip: 192.168.211.53, mask: 255.255.255.0, gw: 192.168.211.1 |
| 30 | + I (2899) NETWORK: SYSTEM_EVENT_STA_GOT_IP: 192.168.211.53 |
| 31 | + Acquired IP address: {192,168,211,53} Netmask: {255,255,255,0} Gateway: {192,168,211,1} |
| 32 | + I (2939) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE |
| 33 | + I (2939) atomvm_mqtt: MQTT_EVENT_BEFORE_CONNECT msg_id: 0 |
| 34 | + MQTT started. |
| 35 | + Connected to "mqtt://mqtt.eclipseprojects.io"; subscribing to <<"atomvm/qos0">>... |
| 36 | + Subscribed to <<"atomvm/qos0">>. |
| 37 | + Publishing data on topic <<"atomvm/qos0">> |
| 38 | + Received data on topic <<"atomvm/qos0">>: echo |
| 39 | + Publishing data on topic <<"atomvm/qos0">> |
| 40 | + Received data on topic <<"atomvm/qos0">>: echo |
| 41 | + Publishing data on topic <<"atomvm/qos0">> |
| 42 | + Received data on topic <<"atomvm/qos0">>: echo |
| 43 | + Publishing data on topic <<"atomvm/qos0">> |
| 44 | + Received data on topic <<"atomvm/qos0">>: echo |
| 45 | + ... |
0 commit comments