Github Repo | C Header | C source | JS source |
---|---|---|---|
mongoose-os-libs/azure | mgos_azure.h | api_azure.js |
This library provides Azure IoT Hub support for Mongoose OS.
Currently only plain MQTT is supported.
See Azure IoT + Mongoose OS tutorial at https://mongoose-os.com/docs/quickstart/cloud/azure.md
Authentication by both SAS token and X.509 certificate is supported. See the Authentication section of the documentation for explanation.
mos azure-iot-setup
The easiest way to setup Azure cloud connection is by using mos azure-iot-setup
. Makes sure you have the az
CLI tool installed, create an IoT Hub, then run:
$ mos azure-iot-setup --azure-hub-name MY-HUB-NAME --azure-device-id NEW-DEVICE-ID
To use symmetric key authentication, obtain the connection string from the web interface or by using the az
CLI utility:
$ az iot hub device-identity show-connection-string --hub-name my-hub --device-id test1
{
"cs": "HostName=my-hub.azure-devices.net;DeviceId=test1;SharedAccessKey=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
}
Enable the Azure client and set the azure.cs
config setting:
$ mos config-set azure.enable=true "azure.cs=HostName=my-hub.azure-devices.net;DeviceId=test1;SharedAccessKey=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
To use authentication by an X.509 certificate, upload the certificate and private key files in PEM format on the device and configure azure.host_name
, azure.device_id
, azure.cert
and azure.key
:
$ mos put test4.crt.pem
$ mos put test4.key.pem
$ mos config-set azure.enable=true azure.host_name=my-hub.azure-devices.net azure.device_id=test4 \
azure.cert=test4.crt.pem azure.key=test4.key.pem
Note: It is possible to store private key in a cryptochip, such as ATECC508A (for example, as described here for Google IoT Core). Just specify azure.key=ATCA:0
to use private key in slot 0 of the chip. [mos azure-iot-setup] supports ATECC508 key storage - just add --use-atca
to the setup command above.
bool mgos_azure_send_d2c_msg(const struct mg_str props,
const struct mg_str body);
Send a Device to Cloud message. If present, the properties string must be URL-encoded.
bool mgos_azure_send_d2c_msgf(const struct mg_str props, const char *json_fmt,
...);
A variant of mgos_azure_send_d2c_msg that formats a JSON message.
bool mgos_azure_send_d2c_msgp(const struct mg_str *props,
const struct mg_str *body);
A variant of mgos_azure_send_d2c_msg that takes pointers, for easy FFI.
struct mg_str mgos_azure_get_host_name(void);
Returns host name of the Azure hub
struct mg_str mgos_azure_get_device_id(void);
Returns Azure device ID
bool mgos_azure_is_connected(void);
Returns true if Azure connection is up, false otherwise.
bool mgos_azure_dm_response(struct mg_str id, int status,
const struct mg_str *resp);
Respond to a Direct Method call.
bool mgos_azure_dm_responsef(struct mg_str id, int status, const char *json_fmt,
...);
Respond to a Direct Method call with a JSON message.
Azure.isConnected()
Return value: true if Azure connection is up, false otherwise.
Azure.sendD2CMsg(props, body)
Send a Device to Cloud message. props
, if specified, must be URL-encoded.