Skip to content

Commit

Permalink
[iot] Fixes to Azure IoT Hub ESP8266 sample (Azure#899)
Browse files Browse the repository at this point in the history
* [iot] Fixes to Azure IoT Hub ESP8266 sample

- Renamed the directory so it can be opened directly on Arduino IDE
- Added call to .loop() on pubsubclient
- Added call to .setBufferSize() on pubsubclient, removed step to manually fix MQTT_MAX_PACKET_SIZE
  • Loading branch information
ewertons authored Jul 8, 2020
1 parent 9d157bb commit bbdfd6c
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 42 deletions.
2 changes: 1 addition & 1 deletion sdk/docs/iot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ The library targets made available via CMake are the following:

- Windows: [How to Setup and Run Azure SDK for Embedded C IoT Hub Samples on Microsoft Windows](../../../sdk/samples/iot/hub/windows/how_to_iot_hub_samples_windows.md).

- ESP8266: [How to Setup and Run Azure SDK for Embedded C IoT Hub Client on Esp8266 NodeMCU](../../../sdk/samples/iot/hub/esp8266nodemcu/how_to_esp8266_nodemcu.md)
- ESP8266: [How to Setup and Run Azure SDK for Embedded C IoT Hub Client on Esp8266 NodeMCU](../../../sdk/samples/iot/hub/aziot_esp8266/how_to_esp8266_nodemcu.md)

**Note**: While Windows and Linux devices are not likely to be considered as constrained ones, these samples were created to make it simpler to test the Azure SDK for Embedded C libraries, even without a real device.

Expand Down
2 changes: 1 addition & 1 deletion sdk/samples/iot/hub/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This document explains samples for the Azure SDK for Embedded C IoT Hub Client a
For step-by-step guides starting from scratch, you may refer to these documents:
- Linux: [How to setup and run Azure SDK for Embedded C IoT Hub Samples on Linux](./linux/how_to_iot_hub_samples_linux.md)
- Windows: [How to setup and run Azure SDK for Embedded C IoT Hub Samples on Microsoft Windows](./windows/how_to_iot_hub_samples_windows.md).
- ESP8266: [How to Setup and Run Azure SDK for Embedded C IoT Hub Client on Esp8266 NodeMCU](./esp8266nodemcu/how_to_esp8266_nodemcu.md)
- ESP8266: [How to Setup and Run Azure SDK for Embedded C IoT Hub Client on Esp8266 NodeMCU](./aziot_esp8266/how_to_esp8266_nodemcu.md)

**Note**: While Windows and Linux devices are not likely to be considered as constrained ones, these samples were created to make it simpler to test the Azure SDK for Embedded C libraries, even without a real device.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT

#include <string.h>
#include <stdbool.h>
#include <time.h>
Expand All @@ -21,6 +24,7 @@
#define sizeofarray(a) (sizeof(a) / sizeof(a[0]))
#define ONE_HOUR_IN_SECS 3600
#define NTP_SERVERS "pool.ntp.org", "time.nist.gov"
#define MQTT_PACKET_SIZE 1024

static const char* ssid = IOT_CONFIG_WIFI_SSID;
static const char* password = IOT_CONFIG_WIFI_PASSWORD;
Expand Down Expand Up @@ -213,6 +217,8 @@ static int connectToAzureIoTHub()
Serial.print("Username: ");
Serial.println(mqtt_username);

mqtt_client.setBufferSize(MQTT_PACKET_SIZE);

while (!mqtt_client.connected())
{
time_t now = time(NULL);
Expand Down Expand Up @@ -279,6 +285,8 @@ void loop()
sendTelemetry();
}

mqtt_client.loop();

next_telemetry_send_time_ms = millis() + TELEMETRY_FREQUENCY_MILLISECS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ _The following was run on an Ubuntu Desktop 18.04 environment, with Arduino IDE
01. Create an Arduino library for Azure Embedded SDK for C

```
$ wget https://raw.githubusercontent.com/Azure/azure-sdk-for-c/master/sdk/samples/iot/hub/esp8266nodemcu/generate_arduino_zip_library.sh
$ wget https://raw.githubusercontent.com/Azure/azure-sdk-for-c/master/sdk/samples/iot/hub/aziot_esp8266/generate_arduino_zip_library.sh
$ chmod 777 generate_arduino_zip_library.sh
$ ./generate_arduino_zip_library.sh
```
Expand Down Expand Up @@ -53,41 +53,9 @@ _The following was run on an Ubuntu Desktop 18.04 environment, with Arduino IDE
- Search for `PubSubClient` (by Nick O'Leary)
- Hover over the library item on the result list, then click on Install.
05. Fix PubSubClient library locally
05. Create a sketch on Arduino IDE for the IoT Hub client telemetry sample
PubSubClient defines the default MQTT maximum packet size to be 128 bytes.
That is not enough to send and receive packets to/from the Azure IoT Hub.
Currently the library also does not provide a dynamic way to change the maximum MQTT packet size ([see known issue](https://github.com/knolleary/pubsubclient/issues/110)).
Also, the Arduino IDE does not propage #defines into the library dependencies (they get compiled before the sketch).
Finally, the Arduino IDE does not take compiler flags.
So the PubSubClient library code must be changed to make `MQTT_MAX_PACKET_SIZE` larger (e.g, 1024).
To make the change, search for the file in your system
```
find / -iname PubSubClient.h
```
Open the PubSubClient.h file in your favorite text editor.
Search for the line that defines the macro `MQTT_MAX_PACKET_SIZE`.
Change the value of the macro to a higher number, like 1024.
```c
#define MQTT_MAX_PACKET_SIZE 1024
```
Save and close the `PubSubClient.h` file.
06. Create a sketch on Arduino IDE for the IoT Hub client telemetry sample
Copy the code from the [ESP8266 Node MCU sample](https://github.com/Azure/azure-sdk-for-c/blob/master/sdk/samples/iot/hub/esp8266nodemcu) into your sketch.
Clone the [azure-sdk-for-c](https://github.com/Azure/azure-sdk-for-c) repo locally then open the [ESP8266 sample](https://github.com/Azure/azure-sdk-for-c/blob/master/sdk/samples/iot/hub/aziot_esp8266) (from local clone) on Arduino IDE.
Edit the following parameters in `iot_configs.h`, filling in your own information:
Expand All @@ -104,14 +72,14 @@ _The following was run on an Ubuntu Desktop 18.04 environment, with Arduino IDE
Save the file.
07. Connect the Esp8266 NodeMCU microcontroller to your USB port
06. Connect the Esp8266 NodeMCU microcontroller to your USB port
08. On the Arduino IDE, select the board and port
07. On the Arduino IDE, select the board and port
- Go to menu `Tools`, `Board` and select `NodeMCU 1.0 (ESP-12E Module)`
- Go to menu `Tools`, `Port` and select the port where the microcontroller is connected to.
09. Upload the sketch
08. Upload the sketch
- Go to menu `Sketch` and click on `Upload`.
Expand Down Expand Up @@ -167,7 +135,7 @@ _The following was run on an Ubuntu Desktop 18.04 environment, with Arduino IDE
Hard resetting via RTS pin...
</details>
10. Monitor the micro-controller
09. Monitor the micro-controller
Go to menu `Tools`, `Serial Monitor`.
Expand All @@ -186,7 +154,7 @@ _The following was run on an Ubuntu Desktop 18.04 environment, with Arduino IDE
```
11. Monitor the telemetry messages sent to the Azure IoT Hub
10. Monitor the telemetry messages sent to the Azure IoT Hub
```
$ az iot hub monitor-events --login <your Azure IoT Hub connection string in quotes> --device-id <your device id>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT

// Wifi
#define IOT_CONFIG_WIFI_SSID "SSID"
#define IOT_CONFIG_WIFI_PASSWORD "PWD"
Expand Down

0 comments on commit bbdfd6c

Please sign in to comment.