Skip to content

Commit

Permalink
move samples to use cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanwinter committed Dec 15, 2022
1 parent 3886bd4 commit 45040ef
Show file tree
Hide file tree
Showing 27 changed files with 221 additions and 1,364 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*.userprefs

# Build results
[Bb]uild/
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
Expand Down
File renamed without changes.
File renamed without changes.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ For more information, refer to the [Overview of Azure IoT Device SDKs](https://d

This repository contains the following samples:

* [Python](src/Python)
* [Mosquitto_pub CLI](src/Mosquitto_pub)
* [Mosquitto on Linux](src/Linux)
* [Mosquitto on Windows](src/Windows)
* [Mosquitto](mosquitto)
* [Mosquitto_pub](mosquitto_pub)
* [Python](python)

## Root certificates

Expand All @@ -45,7 +44,7 @@ The following root certificates are provided in the repository root:

| Certificate | Path | Description |
|-|-|-|
| [Baltimore CyberTrust Root](https://www.digicert.com/kb/digicert-root-certificates.htm) | IoTHubRootCA_Baltimore.pem | Current Root CA set to **expire in 2025** |
| [Baltimore CyberTrust Root](https://www.digicert.com/kb/digicert-root-certificates.htm) | BaltimoreCyberTrustRoot.crt.pem | Current Root CA set to **expire in 2025** |
| [DigiCert Global Root G2](https://www.digicert.com/kb/digicert-root-certificates.htm) | DigiCertGlobalRootG2.crt.pem | Future Root CA which will become active in Feb 2023 |

## General Prerequisites
Expand Down
19 changes: 19 additions & 0 deletions mosquitto/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

cmake_minimum_required(VERSION 3.10)

project(mosquitto)

# Include the Windows Mosquitto installation
if(WIN32)
set(MOSQUITTO_PATH "$ENV{ProgramFiles}/mosquitto/devel")
include_directories(${MOSQUITTO_PATH})
link_directories(${MOSQUITTO_PATH})
endif(WIN32)

link_libraries(mosquitto)

add_executable(mosquitto_telemetry src/mosquitto_telemetry.cpp)
add_executable(mosquitto_device_twin src/mosquitto_device_twin.cpp)
add_executable(mosquitto_subscribe src/mosquitto_subscribe.cpp)
125 changes: 125 additions & 0 deletions mosquitto/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# Using the Mosquitto library on Linux

This samples uses the Mosquitto (MQTT) library to send a message to an Azure IoT Hub from a Linux machine.

## Windows prerequisites

1. Follow the [general prerequisites](/README.md#general-rerequisites)
1. Install [Visual Studio](https://visualstudio.microsoft.com/downloads)

> **NOTE**
> Enable the `Desktop development with C++` workload
1. Install [CMake](https://cmake.org/download/)

> **NOTE**
> Enable the `Add CMake to the system PATH for all users` option
1. Install the **x64 version** of [Mosquitto](https://mosquitto.org/download/)

## Linux prerequisites

1. Follow the [general prerequisites](/README.md#general-rerequisites)
1. Install the build tools:

```Shell
sudo apt install cmake g++
```

1. Install Mosquitto client library:

```Shell
sudo apt install libmosquitto-dev
```

## Build

1. Change to the `mosquitto` directory in the cloned repository
1. Edit the `config.h` file and expand the required connection information:

```c
#define IOTHUBNAME "{iothub_name}"
#define DEVICEID "{device_id}"
#define SAS_TOKEN "{sas_token}"
```

1. Build the application:

```Shell
cmake -Bbuild
cmake --build build
```

1. Resulting binaries can be found in the following locations:
1. Linux: `./build`
1. Windows `.\build\Debug`

## Run telemetry sample

1. Start monitoring incoming messages on your Hub:

```Shell
az iot hub monitor-events -n {iothub_name}
```

1. Run the sample executable:

```Shell
mosquitto_telemetry
```

1. Confirm the following output is seen from monitor-events:

```json
{
"event": {
"origin": "pubsub",
"module": "",
"interface": "",
"component": "",
"payload": "Bonjour MQTT from Mosquitto"
}
}
```

## Run subscribe sample

1. Run the sample executable:

```Shell
mosquitto_subscribe
```

1. Send an event from the Hub to the device:

```Shell
az iot device c2d-message send --hub-name {iothub_name} --device-id {device_id} --data "hello world"
```

1. Confirm the following output from the sample:

```Shell
Waiting for C2D messages...
C2D message 'hello world' for topic 'devices/pubsub/messages/devicebound/...`
Got message for devices/pubsub/messages/# topic
```
## Run device twin sample
1. Run the sample executable:
```Shell
mosquitto_device_twin
```
1. Confirm the following output from the sample, setting and getting a device twin update:
```Shell
Setting device twin reported properties....
Device twin message '' for topic '$iothub/twin/res/204/?$rid=patch_temp&$version=2'
Setting device twin properties SUCCEEDED.
Getting device twin properties....
Device twin message '{"desired":{"$version":1},"reported":{"temperature":32,"$version":2}}' for topic '$iothub/twin/res/200/?$rid=123'
Getting device twin properties SUCCEEDED.
```
8 changes: 8 additions & 0 deletions mosquitto/src/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#define IOTHUBNAME "{iothub_name}"
#define DEVICEID "{device_id}"
#define SAS_TOKEN "{sas_token}"

#define CERTIFICATEFILE "../BaltimoreCyberTrustRoot.crt.pem"
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,12 @@

#include "mosquitto.h"

// Azure IoT Hub CONNECTION information to complete
#define IOTHUBNAME "{iothub_name}"
#define DEVICEID "{device_id}"
#define SAS_TOKEN "{sas_token}"
#define CERTIFICATEFILE "IoTHubRootCA_Baltimore.pem"
#include "config.h"

// Certificate
// Server certs available here for download:
// https://raw.githubusercontent.com/Azure/azure-iot-sdk-c/master/certs/certs.c

// PWD
// generated via Azure CLI, Device explorer or VSCode Azure IoT extension (Generate SAS Token for device)
// az iot hub generate-sas-token -d EM_AZSphere -n EricmittHub --duration 2600000

// Username
// username format for MQTT connection to Hub: $hub_hostname/$device_id/?api-version=2018-06-30"
// #define USERNAME "EricmittHUB.azure-devices.net/EM_AZSphere/?api-version=2018-06-30"

// Computed Host Username
#define HOST IOTHUBNAME ".azure-devices.net"
#define PORT 8883
#define USERNAME HOST "/" DEVICEID "/?api-version=2020-09-30"

// MQTT
#define DEVICEMESSAGE "devices/" DEVICEID "/messages/#"
#define DEVICETWIN_SUBSCRIPTION "$iothub/twin/res/#"
#define DEVICETWIN_MESSAGE_GET "$iothub/twin/GET/?$rid=123"
Expand All @@ -41,7 +23,7 @@ void connect_callback(struct mosquitto* mosq, void* obj, int result)
printf("Connect callback returned result: %s\r\n", mosquitto_strerror(result));
if (result == MOSQ_ERR_CONN_REFUSED)
{
printf("Connection refused. Please check your SAS Token, expired ?\r\n");
printf("Connection refused. Please check DeviceId, IoTHub name or if your SAS Token has expired.\r\n");
}
else
{
Expand Down Expand Up @@ -107,26 +89,24 @@ int main(int argc, char* argv[])
// init the mosquitto lib
mosquitto_lib_init();

// create a mosquito object
// create the mosquito object
struct mosquitto* mosq = mosquitto_new(DEVICEID, false, NULL);

// add callback fuctions
// add callback functions
mosquitto_connect_callback_set(mosq, connect_callback);
mosquitto_message_callback_set(mosq, message_callback);
mosquitto_disconnect_callback_set(mosq, disconnect_callback);
mosquitto_message_callback_set(mosq, message_callback);

// specify mosquitto username, password and options
// set mosquitto username & password
mosquitto_username_pw_set(mosq, USERNAME, SAS_TOKEN);

// specify the certificate to use
mosquitto_tls_set(mosq, CERTIFICATEFILE, NULL, NULL, NULL, NULL);

// specify the mqtt version to use
int option = MQTT_PROTOCOL_V311;
rc = mosquitto_opts_set(mosq, MOSQ_OPT_PROTOCOL_VERSION, &option);
// specify the certificate
printf("Using certificate: %s\r\n", CERTIFICATEFILE);
rc = mosquitto_tls_set(mosq, CERTIFICATEFILE, NULL, NULL, NULL, NULL);
if (rc != MOSQ_ERR_SUCCESS)
{
return mosquitto_error(rc, "Error: opts_set protocol version");
printf("Error: Couldn't find file '%s'\r\n", CERTIFICATEFILE);
return mosquitto_error(rc);
}

// connect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,24 @@

#include <cstdio>
#include <cstdlib>
#include <cstring>

#include "mosquitto.h"

// Azure IoT Hub CONNECTION information to complete
#define IOTHUBNAME "{iothub_name}"
#define DEVICEID "{device_id}"
#define SAS_TOKEN "{sas_token}"
#define CERTIFICATEFILE "IoTHubRootCA_Baltimore.pem"
#include "config.h"

#define HOST IOTHUBNAME ".azure-devices.net"
#define PORT 8883
#define USERNAME HOST "/" DEVICEID "/?api-version=2020-09-30"

// Computed Host Username and Topic
#define HOST IOTHUBNAME ".azure-devices.net"
#define PORT 8883
#define USERNAME HOST "/" DEVICEID "/?api-version=2020-09-30"
#define DEVICEMESSAGE "devices/" DEVICEID "/messages/#"

void connect_callback(struct mosquitto* mosq, void* obj, int result)
{
printf("Connect callback returned result: %s\r\n", mosquitto_strerror(result));
if (result == MOSQ_ERR_CONN_REFUSED)
{
printf("Connection refused. Please check your SAS Token, expired ?\r\n");
printf("Connection refused. Please check DeviceId, IoTHub name or if your SAS Token has expired.\r\n");
}
else
{
Expand All @@ -33,7 +30,7 @@ void connect_callback(struct mosquitto* mosq, void* obj, int result)

void disconnect_callback(struct mosquitto* mosq, void* obj, int rc)
{
printf("Disconnect callback %d : %s", rc, mosquitto_strerror(rc));
printf("Disconnect callback %d : %s\r\n", rc, mosquitto_strerror(rc));
}

void message_callback(struct mosquitto* mosq, void* obj, const struct mosquitto_message* message)
Expand Down Expand Up @@ -69,35 +66,35 @@ int main(int argc, char* argv[])
// init the mosquitto lib
mosquitto_lib_init();

// create a mosquito object
// create the mosquito object
struct mosquitto* mosq = mosquitto_new(DEVICEID, false, NULL);

// add callback fuctions
// add callback functions
mosquitto_connect_callback_set(mosq, connect_callback);
mosquitto_message_callback_set(mosq, message_callback);
mosquitto_disconnect_callback_set(mosq, disconnect_callback);
mosquitto_message_callback_set(mosq, message_callback);

// specify mosquitto username, password and options
// set mosquitto username & password
mosquitto_username_pw_set(mosq, USERNAME, SAS_TOKEN);

// specify the certificate to use
mosquitto_tls_set(mosq, CERTIFICATEFILE, NULL, NULL, NULL, NULL);

// specify the mqtt version to use
int option = MQTT_PROTOCOL_V311;
rc = mosquitto_opts_set(mosq, MOSQ_OPT_PROTOCOL_VERSION, &option);
// specify the certificate
printf("Using certificate: %s\r\n", CERTIFICATEFILE);
rc = mosquitto_tls_set(mosq, CERTIFICATEFILE, NULL, NULL, NULL, NULL);
if (rc != MOSQ_ERR_SUCCESS)
{
return mosquitto_error(rc, "Error: opts_set protocol version");
printf("Error: Couldn't find file '%s'\r\n", CERTIFICATEFILE);
return mosquitto_error(rc);
}

// connect
printf("Connecting...\r\n");
rc = mosquitto_connect(mosq, HOST, PORT, 10);
if (rc != MOSQ_ERR_SUCCESS)
{
return mosquitto_error(rc);
}

printf("Connect returned OK\r\n");

rc = mosquitto_subscribe(mosq, NULL, "#", 0);
if (rc != MOSQ_ERR_SUCCESS)
{
Expand Down
Loading

0 comments on commit 45040ef

Please sign in to comment.