Build an IoT Time-Series Data Application for Industrial Energy Monitoring with MQTT and Timescale
Published: 2023-09-26 14:31:33

## Introduction

As industrial production continues to grow, monitoring energy consumption in factories is crucial for optimizing energy management strategies and reducing costs. EMQX and Timescale provide a scalable IoT platform for efficient collection and analysis of energy consumption data in real-time. This article shows how to use EMQX to collect data from the plant and integrate it with Timescale for reliable data storage and analysis.

## Prerequisites

* Git
* Docker Engine: v20.10+
* Docker Compose: v2.20+

## How it Works


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ws4zzodnzfse1toy1d65.png)



This is a simple and effective architecture that avoids complex components. It utilizes the following three key components:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/68ynvhtik2qjuvy9lu64.png)



In addition to the basic components, EMQX provides comprehensive observability capabilities. You can use the following components to monitor EMQX metrics and load when the system is running:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/16ikkj6vf11e5ecshup6.png)



## Clone the Project Locally

Clone the [**emqx/mqtt-to-timescaledb**](https://github.com/emqx/mqtt-to-timescaledb) repository locally, and initialize the submodule to enable the EMQX Exporter (optional):

```
git clone https://github.com/emqx/mqtt-to-timescaledb
cd mqtt-to-timescaledb

# Optional
git submodule init
git submodule update

```

The codebase consists of four parts:

* The emqx folder contains EMQX-TimescaleDB integration configurations to automatically create rules and data bridges when launching EMQX.
* The emqx-exporter, prometheus and grafana-provisioning folders include observability and energy consumption data visualization configurations for EMQX.
* The create-table.sql file defines the database table schema, which will create the tables in Timescale during initialization.
* The docker-compose.yml orchestrates all components to launch the project with one click.

## Start MQTTX CLI, EMQX, and Timescale

Please make sure you have installed the [**Docker**](https://www.docker.com/), and then run Docker Compose in the background to start the demo:

```
docker-compose up -d 
```

Now, MQTTX CLI will simulate 10 factories connecting to EMQX, and report the energy consumption data of major equipment on their production lines at a frequency of 1 message per second. The energy data in JSON format will be sent to the topic mqttx/simulate/IEM/{clientid}.

All equipment will collect the current instantaneous power usage (randomly simulated, not exceeding max power), calculate the electricity consumption for 1 second, and publish the data to EMQX.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/29oq4ixskf1vu0gku9lj.png)

```
{
    "factory_id": "08",
    "factory": "Miller and Sons",
    "values": {
        "air_compressor_1": 3.72,
        "air_compressor_2": 5.01,
        "lighting": 0.95,
        "cooling_equipment": 23.19,
        "heating_equipment": 52.66,
        "conveyor": 10.66,
        "coating_equipment": 5.21,
        "inspection_equipment": 2.6,
        "welding_equipment": 5.27,
        "packaging_equipment": 7.38,
        "cutting_equipment": 12.56
    },
    "timestamp": 1691144157583
}
```

EMQX will create a rule to ingest messages from the factory. You can also modify this rule later to add custom processing using EMQX's[ **built-in SQL functions**](https://docs.emqx.com/en/enterprise/v5.1/data-integration/rule-sql-builtin-functions.html):

```
SELECT
  payload
FROM
  "mqttx/simulate/#"

```

After the rules process the data, EMQX will insert the energy consumption data from the message payload into the specified table in Timescale through data bridges. Configuring INSERT SQL on the data bridges allows flexibly accomplishing this operation.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ncoxs2fok3kutxscg2uo.png)



## Subscribe to Data from EMQX

Docker Compose has included a subscriber to print all energy consumption data. You can view the data with this command:

```
$ docker logs -f mqttx
[8/4/2023] [10:15:57 AM] › topic: mqttx/simulate/IEM/mqttx_85df7038
payload: {"factory_id":"08","factory":"Miller and Sons","values":{"air_compressor_1":3.72,"air_compressor_2":5.01,"lighting":0.95,"cooling_equipment":23.19,"heating_equipment":52.66,"conveyor":10.66,"coating_equipment":5.21,"inspection_equipment":2.6,"welding_equipment":5.27,"packaging_equi...
```

To subscribe and receive the data with any [**MQTT client**](https://www.emqx.com/en/blog/mqtt-client-tools):

```
mqttx sub -t mqttx/simulate/IEM/+ 
```

## View Enengy Data in Grafana

To view enengy data in the Grafana dashboard, open http://localhost:3000 in your browser, log in with username admin and password public.

After successful login, go to “Home → Dashboards” page and select “Energy Monitoring data”. The dashboard comprehensively displays the key energy consumption metrics of various industrial equipment, including the cumulative energy consumption value of each equipment and the energy consumption share of each plant, which fully presents the real-time energy usage of the industrial system and facilitates data-driven energy-saving management.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cd7ssheq4pg8omybiamz.png)


## Conclusion

In this post, we have explored how to integrate EMQX and Timescale to build an industrial energy monitoring pipeline. By leveraging EMQX as a real-time [**MQTT broker**](https://www.emqx.com/en/blog/the-ultimate-guide-to-mqtt-broker-comparison) and utilizing its SQL data integration to ingest data into Timescale, we have created an end-to-end solution for collecting and analyzing time-series energy consumption data.

This demo project provides a starting point for building scalable time-series data platforms, opening opportunities for real-time monitoring, optimization, and intelligence in industrial facilities and other time-sensitive use cases. The reliability of EMQX and the analytical power of Timescale unlock valuable insights from time-series data.

>*Please Visit the* [***GitHub link***](https://github.com/emqx/mqtt-to-timescaledb) *for the Demo of ingesting time series data into Timescale.*

_Originally published at [www.emqx.com](https://www.emqx.com/en/blog/build-an-iot-time-series-data-application-for-energy-storage-with-mqtt-and-timescale)_