After my previous success getting data into Azure IoT Central I now wanted a way to send data the other way. That is to the device from the cloud. However, I could see this being a challenge if I continued to use Azure as my cloud endpoint. What I really needed was something simpler to help me better understand the core communication concepts.
With this in mind I discovered:
that is much easier to use to do the basic stuff I wanted.
I therefore went to Adafruit IO and created a free account. This will then give you access to:
I then needed to create a ‘Feed’ into which the data from my device would be sent so that it could then appear in the Adafruit IO console. You can follow these instructions to create a ‘Feed’:
https://learn.adafruit.com/adafruit-io-basics-feeds/overview
To get the credentials you’ll need to connect to the feed you just created you’ll need to select the ‘key’ icon in the top right of the window as shown above to reveal the details.
You should see a dialog like that shown above with your Active key. It is also handy to grab the sample code displayed below.
You’ll need the following headers/libraries in your code:
#include <Arduino.h>
#include <WiFi.h>
#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>
you’ll also need to define a few things:
#define AIO_SERVER “io.adafruit.com”
#define AIO_SERVERPORT 1883
#define AIO_USERNAME “<Adafruit IO user name>“
#define AIO_KEY “<Your Active key>“
#define WLAN_SSID “<Your WiFi name>“
#define WLAN_PASS “<Your Wifi password>“
You’ll need to enter your own values for the last few to suit your own environment.
Next, you need to connect to the feed you created using:
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Publish feed= Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME “/feeds/<Your feed name>”);
The feed variable here is where you will ‘publish’ data to be sent to the Adafruit IO console. The main loop running on the device will then contain something like this:
if (!feed.publish(ciaaht_getTemp())) {
Serial.println(F(“Failed”));}
else {
Serial.println(F(“Success”));}
which in essence, publishes data to the feed you created. In my case, I’m feeding in temperatures data via the function ciaaht_getTemp() which get the temperature from the DHT20.
There is a little bit more to it than that, but all the code is here:
https://github.com/directorcia/Azure/tree/master/Iot/ESP32-S2/IO-Adafruit-Publish
including the custom temperature sensor functions code I created previously.
With the code now uploaded to the ESP-32 I was able to see the data appearing in my feed in Adafruit IO as shown above. You’ll also see in my code that I used the on board LED as kind of indicator to let me know if things were working. A visual debugger if you like.
As you can see, getting data into Adafruit IO is much, much easier than into Azure IoT Central and I understand why that is and I’d suggest people start with Adafruit IO before moving to Azure. There is far less code with Adafruit IO making far easier to debug and understand.
With this now working and displaying temperature from my sensor, it is time to try and now send data to the device to control it from the cloud. Look out for that update coming soon!