DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Stream Sensor Data to Apache Pinot for Real Time Analysis
  • OPC-UA and MQTT: A Guide to Protocols, Python Implementations
  • Connecting the Dots: Unraveling IoT Standards and Protocols
  • Real-Time Remediation Solutions in Device Management Using Azure IoT Hub

Trending

  • Developers Beware: Slopsquatting and Vibe Coding Can Increase Risk of AI-Powered Attacks
  • Performing and Managing Incremental Backups Using pg_basebackup in PostgreSQL 17
  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 1
  • Why High-Performance AI/ML Is Essential in Modern Cybersecurity
  1. DZone
  2. Data Engineering
  3. IoT
  4. Build an IoT System Using Arduino/ESP8266, Cayenne, and MQTT

Build an IoT System Using Arduino/ESP8266, Cayenne, and MQTT

Follow along with this guide to learn how to build an IoT system to read data from and control peripheral devices, as well as how to create a dashboard.

By 
Francesco Azzola user avatar
Francesco Azzola
·
Apr. 23, 18 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
24.7K Views

Join the DZone community and get the full member experience.

Join For Free

This article describes how to build an IoT system using an Arduino/ ESP8266, myDevices Cayenne, and MQTT. In more detail, this IoT tutorial discovers how to use an Arduino (or ESP8266) to send data to Cayenne using the MQTT protocol. Moreover, this Arduino/ESP8266 MQTT project investigates how to use MQTT to control remote peripheral devices using a web interface. This is a complete step-by-step tutorial on building an IoT system using Cayenne and an Arduino.

We will experiment with how to use MQTT with an Arduino or ESP8266 from two different points of view:

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

We have already extensively covered how to use the MQTT protocol. If you are new, you should read up on that before starting this tutorial.

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

  • Data visualization
  • IoT cloud
  • Alerts

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

Send Data From an Arduino/ESP8266 to Cayenne Using MQTT

In this first part of this project, we will cover how to send data from Arduino/ESP8266 to Cayenne 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 IoT project is focusing on how to use MQTT to send data from an Arduino to Cayenne, and we want to keep other things simple. Before delving into the description of how to connect the BMP280 sensor to your Arduino, it is useful to configure the Cayenne IoT platform so that it accepts data from our IoT device.

How to Connect Your Arduino to Cayenne

The first step is configuring a device on Cayenne. To do it, it is necessary to create a free account.

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

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

Configuring Cayenne 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 that, you can import the Cayenne Library into your Arduino IDE:


Once the library is installed, you can move to File->Examples->CayenneMQTT->Connections and your 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 MQTT. The result is below:

Arduino/ESP8266 with Cayenne using MQTT

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

In this example, we add three different channels because Arduino 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 the Arduino/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:

cayenne live IoT data

You can notice the virtual channels used in the Arduino source code.

How to Control Your Arduino/ESP8266 From Cayenne Using MQTT

In the second part of this IoT project based on Arduino and Cayenne, we use MQTT to remotely control an ESP8266 (or Arduino). In this example, a Neopixel bar is connected to an 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 see, there are three different sliders that control the Red, Green, and Blue color components.

How to Connect an ESP8266 to RGB LEDs

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


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,</pre>
<pre>                           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 the Cayenne Dashboard Using MQTT

This is the last step of this IoT project. In this step, we will create the Cayenne dashboard that sends data to the ESP8266 using 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 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 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_INs — one for each channel. In each channel, the sketch handles the new value sent using MQTT. Every time the ESP8266 receives a new value from the Cayenne IoT dashboard through MQTT, it updates the RGB LEDs to reflect the new color components.

Summary

At the end of this post, you hopefully gained the knowledge of how to integrate an Arduino/ESP822 and Cayenne using MQTT. You have built an IoT system that uses sensors that measure physical quantities and send data to the cloud using MQTT. In the second part, we explored how to control peripherals connected to an ESP8266 (or Arduino) using MQTT. Through an 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.

MQTT IoT Data visualization arduino Build (game engine)

Published at DZone with permission of Francesco Azzola, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Stream Sensor Data to Apache Pinot for Real Time Analysis
  • OPC-UA and MQTT: A Guide to Protocols, Python Implementations
  • Connecting the Dots: Unraveling IoT Standards and Protocols
  • Real-Time Remediation Solutions in Device Management Using Azure IoT Hub

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!