Send and receive IoT Hub messages from ESP8266 Arduino
This is an extraction of the Get Started with Microsoft Azure IoT Starter Kit - Adafruit Feather Huzzah ESP8266 (Arduino-compatible) sample. The difference is that this repo does not require a sensor or the simulation of one and also includes all configuration information in the code so you do not need to use the Serial Monitor to input credentials.
Download the three files in this repo:
- Azure-IoT-Hub-ESP8266-Easy-Connect.ino
- config.h
- iothubClient.ino
Update the config.h file towards the bottom:
#define CONNECTION_STRING "YOUR_DEVICE_CONNECTION_STRING_HERE"
#define WIFI_SSID "YOUR_WIFI_SSID_HERE"
#define WIFI_PASS "YOUR_WIFI_PASSWORD_HERE"
Install the ESP8266 board and required libraries from the Arduino IDE. You can use Visual Studio Code with the Arduino extension to do this as well.
- AzureIoTHub
- AzureIoTUtility
- AzureIoTProtocol_MQTT
- ArduinoJSON
Upload to Arduino. Open serial monitor to see messages being sent to IoT Hub.
Open the Device Explorer and send Messages to Device.
Board will use built-in LED and blink when sending and receiving messages.
The code in the loop()
function is responsible for sending and receiving messages. Customize the message YOUR_IOT_HUB_MESSAGE_HERE
with your desired message.
void loop()
{
if (!messagePending && messageSending)
{
char messagePayload[MESSAGE_MAX_LEN] = "YOUR_IOT_HUB_MESSAGE_HERE";
sendMessage(iotHubClientHandle, messagePayload);
messageCount++;
delay(interval);
}
IoTHubClient_LL_DoWork(iotHubClientHandle);
delay(10);
}