mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-07 00:20:20 +07:00
Mongoose MQTT client
This MQTT client implementation implements the following:
- Connects to the MQTT server specified by MQTT_SERVER_URL
- When connected, subscribes to the topic MQTT_SUBSCRIBE_TOPIC
- When it receives a message, echoes it back to MQTT_PUBLISH_TOPIC
- Timer-based reconnection logic revives the connection when it is down
- Ping server periodically. When disconnected, a last will is published
- Implements "ota.update" for OTA updates, see https://mongoose.ws/mqtt/
By default, it uses HiveMQ public broker, and can be tested with the HiveMQ Websocket Client:
- Subscribe to
mg/123/# - Send a message to
mg/123/rx
Integrating into an embedded project
- Copy
mongoose_mqtt.cto your embedded project and add it to the build - Add
mg_mqtt_init(&mgr)aftermg_mgr_init() - Add
mg_mqtt_poll(&mgr)aftermg_mgr_poll()
Microsoft Azure IoT Hub
- Create IoT Hub
- Generate device self-signed certificates:
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -pkeyopt ec_param_enc:named_curve -nodes -keyout device.key -out device.crt -days 3650 -subj "/CN=MYDEVICE"
- Show certificate thumbprint:
openssl x509 -in device.crt -noout -fingerprint -sha256 | tr -d ':' | cut -d= -f2
- Register a device with the thumbprint from above, and set the following:
#define AZURE_HUB_NAME "HUB_NAME" // Change this
#define AZURE_DEVICE_ID "DEVICE_ID" // Change this
// Do not change this
#define MQTT_SERVER_URL "mqtts://" AZURE_HUB_NAME ".device.azure-devices.net"
#define MQTT_CLIENT_ID AZURE_DEVICE_ID
#define MQTT_USER AZURE_HUB_NAME ".azure-devices.net/" AZURE_DEVICE_ID "/?api-version=2021-04-12"
#define MQTT_PUBLISH_TOPIC "devices/" AZURE_DEVICE_ID "/messages/events/"
#define MQTT_SUBSCRIBE_TOPIC "devices/" AZURE_DEVICE_ID "/messages/devicebound/#"
- Set
TLS_CA. Visit https://mongoose.ws/tls/, enter "HUB_NAME.device.azure-devices.net:8883" into the CA field and click on the "Get CA Certificate" button. Enable the "Show as C/C++ constant", copy-paste to your code. - Set
TLS_KEYandTLS_CRTwith the output of this commands:
sed 's/\r$//; s/.*/ "&\\n"/; $!s/$/ \\/' device.key
sed 's/\r$//; s/.*/ "&\\n"/; $!s/$/ \\/' device.crt
- Add to your
mongoose_config.h:
#define MG_ENABLE_CHACHA20 0
Microsoft Azure Event Grid
-
Create Azure Event Grid (EG) instance
-
Generate device self-signed certificates:
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -pkeyopt ec_param_enc:named_curve -nodes -keyout device.key -out device.crt -days 3650 -subj "/CN=MYDEVICE"
- Show certificate thumbprint:
openssl x509 -in device.crt -noout -fingerprint -sha256 | tr -d ':' | cut -d= -f2
-
Register a client on EG: choose a name, and use the thumbprint from above
-
Set URL, username and client ID:
#define MQTT_SERVER_URL "mqtts://INSTANCE.REGION.ts.eventgrid.azure.net:8883"
#define MQTT_CLIENT_ID "CLIENT_NAME"
#define MQTT_USER "CLIENT_NAME"
- Set
TLS_CA. Visit https://mongoose.ws/tls/, enter "INSTANCE.REGION.ts.eventgrid.azure.net:8883" into the CA field and click on the "Get CA Certificate" button. Enable the "Show as C/C++ constant", copy-paste to your code. - Set
TLS_KEYandTLS_CRTwith the output of this commands:
sed 's/\r$//; s/.*/ "&\\n"/; $!s/$/ \\/' device.key
sed 's/\r$//; s/.*/ "&\\n"/; $!s/$/ \\/' device.crt
- In Azure EG, go to Instance / MQTT Broker / Topic spaces, add "space1" with
mg/#pattern - In Azure EG, go to Instance / MQTT Broker / Permissions bindings, add binding1 and binding2:
- $all , space1 , Publisher
- $all , space1 , Subscriber