A step-by-step guide about how to build an IoT system using Cayenne IoT with ESP8266 through MQTT.

This Cayenne IoT tutorial covers how to build an IoT system based on Cayenne IoT and ESP8266 using MQTT protocol to exchange data. In more details, it covers how to use ESP8266 to send data to Cayenne IoT using MQTT protocol and vice-versa how to use Cayenne IoT to control ESP822 remotely. During this step by step tutorial, we will use Cayenne IoT platform from two different points of view:

  • the first where there is the need to send data from ESP8266 to an IoT cloud platform  (i.e Cayenne IoT platform)
  • the second where we have to control remotely an ESP8266

The first scenario takes place when we have to send to the cloud the data acquired from a sensor connected to ESP8266. This is a common scenario, and generally speaking, it happens every time it is necessary to acquire and send data through MQTT. The other scenario happens when there is the need to remotely control a device exploiting MQTT. In this case, we will control ESP8266 through the MQTT protocol from the Cayenne IoT platform.

We all know the importance of IoT nowadays and the fact that in the near future there will be always more connected devices. Home appliances, smart devices, wearable and so on will be part of our lives. It is clear then the importance of IoT (Internet of things) and the fact we have to be ready for the next future. During this Cayenne IoT tutorial, you will acquire some important concepts that are at the base of an IoT system.

We have already extensively covered how to use MQTT protocol and if you are new you should read before starting this tutorial what is MQTT and how to use it.

On the other hand, Cayenne is an IoT cloud platform that provides several cloud services as:

  • Data visualization
  • IoT cloud
  • Alerts

We will focus our attention on data visualization and on the IoT cloud services.

How to send data from ESP8266 to Cayenne IoT using MQTT

In this first part of this project, we will cover how to send data from ESP8266 to Cayenne IoT using MQTT. To keep things simple, this IoT project uses a simple BMP280 sensor. This sensor is able to measure:

  • temperature
  • humidity
  • pressure

The main target of this Cayenne IoT project is focusing on how to use MQTT to send data from ESP8266 to Cayenne IoT platform and we want to keep other things simple. Before delving into the description of how to connect the BMP280 sensor to ESP8266, it is useful to configure the Cayenne IoT platform so that it accepts data from our IoT device.

How to connect ESP8266 to Cayenne IoT

The first step is configuring a device on Cayenne IoT platform. To do it, it is necessary to create a free account using this link.

Once you are ready,  move to Cayenne IoT platform and click on “Add a new…” button. Select the “Device/Widget” and then select your IoT board. In this example, we are using ESP8266. In the end, the Cayenne IoT platform should show a web paging containing all the details about how to connect Arduino to Cayenne through MQTT.

Once you have correctly configured the Cayenne IoT, we can focus on the schematic. In the picture below, it is shown how to connect Wemos D1 to BMP/BME280. Anyway if you use Arduino the schematic is very similar.

 

Cayenne IoT with ESP8266

 

Once the connections are ready, we can focus our attention on configuring the Cayenne IoT platform to accept data from ESP8266.

Configuring Cayenne IoT for MQTT

The first step is creating a new device as shown in the picture below:

ESP8266 Cayenne IoT MQTT

 

That’s all. It is time to connect our IoT device. To do it you can import the Cayenne Library into your Arduino IDE:

 

 

Once the library is installed, you can move to File->Examples->CayenneMQTT->Connections and you have the code that matches your IoT device. The code below is for ESP8266:

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

// WiFi network info.
char ssid[] = "ssid";
char wifiPassword[] = "wifi_pwd";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "your usename";
char password[] = "your pwd";
char clientID[] = "your client_id";

void setup() {
Serial.begin(9600);
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

void loop() {
  Cayenne.loop();
}

CAYENNE_OUT_DEFAULT()
{
 // Here the data we want to send
}

Now replace in the code above, the parameters with those shown in the first picture (MQTT username, MQTT password, Client Id) and try to connect your IoT device to Cayenne IoT through MQTT. If everything is configured correctly, you should get the result shown below:

 

Connecting ESP8266 to Cayenne IoT using MQTT

 

The last step before sending data is configuring the channels that is something like a virtual MQTT channel where ESP8266 sends data to Cayenne. Usually, each physical quantity we want to measure should be related to a channel.

In this example, we add three different channels because of ESP8266 sends to Cayenne Temperature, Humidity and Pressure. Channels are useful when we want to create a dashboard with all the data coming from sensors. The image below shows how to add a pressure channel:

 

Configuring Cayenne IoT dashboard

Finally, it is time to send data using MQTT to Cayenne. We have to modify the source code so that ESP8266 reads data from sensors and send these values to Cayenne. The source code is quite simple:

....
CAYENNE_OUT_DEFAULT() {
  Cayenne.virtualWrite(0, millis() / 1000);
  Cayenne.celsiusWrite(1, temp);
  Cayenne.virtualWrite(2, press);
  Cayenne.virtualWrite(3, hum);
}

That’s all, now we can see the live data or in other words, the data coming from sensors connected to Arduino:

 

cayenne live IoT data

You can notice the virtual channels used in the ESP8266 source code. In this first part of this Cayenne IoT tutorial, you have sent data from ESP8266 to Cayenne IoT using MQTT.

Read: How to use ESP8266 with Alexa and Amazon Echo

How to control Arduino ESP8266 from Cayenne through MQTT

In this second part of this IoT project based on Arduino and Cayenne, we use the MQTT to remotely control ESP8266 (or Arduino). In this example, a Neopixel bar is connected to ESP8266. The target is controlling the colors using MQTT through a web interface (Cayenne interface). This example can be further expanded and it is possible to connect other kinds of peripherals. The image below shows the Cayenne dashboard that will control the RGB LEDs in the Neopixel bar:

 

ESP8266 with Cayenne dashboard based on MQTT

 

As you can notice, there are three different sliders that control the Red, Green, and Blue color components.

How to connect Arduino ESP8266 to RGB LEDs

The first step is connecting the Neopixels to the ESP8266. Notice that if you use an Arduino board the connections are almost the same. The picture below shows the schematic:

Arduino ESP8266 with neopixel bar

There are only three wires:

  • Vcc (5v)
  • GND (Ground)
  • Signal (ESP8266 D1 pin)

To control the RGB LEDs, it is necessary to import the neopixel library and add the following code to the sketch:

#include <Adafruit_NeoPixel.h>
#define PIN            D1
#define NUMPIXELS      8

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN,
                          NEO_GRB + NEO_KHZ800);

void updateLeds() {
  for (int i=0; i < NUMPIXELS;i++)
    pixels.setPixelColor(i, pixels.Color(red,green,blue));
    pixels.show();
}

The code is very simple, it is necessary to define the data pin (D1 in this example) and the number of pixels supported by the neopixel bar.

How to configure Cayenne dashboard

This is the last step of this IoT project. In this step, we will create the Cayenne dashboard that sends data to Arduino ESP8266 through MQTT. The IoT Cayenne dashboard has to contain three different sliders that represent the three color components. The picture below shows how to add the slider to the Cayenne dashboard:

 

 

It is important to notice to select the right channel for each color component:

  • Red (channel 1)
  • Green (channel 2)
  • Blue (channel 3)

You have to repeat this step for all the three color components. In the end, you will get the Cayenne dashboard shown above.

From the code point of view, we can reuse the code shown in the previous steps and modify it:

void setup() {
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  pixels.begin();
}

void loop() {
  Cayenne.loop();
}

CAYENNE_OUT_DEFAULT() { }

CAYENNE_IN(RED_CHANNEL) {
   red = getValue.asInt(); // 0 to 255
   CAYENNE_LOG("Channel %d,  value %d", RED_CHANNEL, red);
   updateLeds();
}
CAYENNE_IN(BLUE_CHANNEL) {
   blue = getValue.asInt(); // 0 to 255
   CAYENNE_LOG("Channel %d,  value %d", BLUE_CHANNEL, blue);
   updateLeds();
}

CAYENNE_IN(GREEN_CHANNEL) {
 green = getValue.asInt(); // 0 to 255
 CAYENNE_LOG("Channel %d,  value %d", GREEN_CHANNEL, green);
 updateLeds();
}

There are three different,CAYENNE_IN one for each channel. In each channel, the sketch handles the new value sent using MQTT. Every time, the Arduino ESP8266 receives a new value from the Cayenne IoT dashboard through the MQTT it updates the RGB LEDs to reflect the new color components.

If you are interested in power management and want to optimize the battery you can read power management in IoT. Moreover, another article covers how to use ESP8266 with Firebase realtime database to control connected devices.

Summary

At the end of this post, you hopefully gained the knowledge of how to integrate ESP822 and Cayenne IoT platform using MQTT. You have built an IoT system that uses sensors that measure physical quantities and send data to the cloud using MQTT protocol. In the second part, we have explored how to control peripherals connected to ESP8266 (or Arduino) using MQTT. Through the IoT Cayenne dashboard, the ESP8266 selects the RGB LEDs color exploiting the data sent using MQTT. This IoT project can be further expanded and you can apply the same principles to different scenarios and handle different sensors or peripherals.

LEAVE A REPLY

Please enter your comment!
Please enter your name here