Analyzing IoT Data Using InfluxDB, Python, and Modbus
IoT Data Sources for Industrial and Smart Applications
IoT devices generate real-time data from various sensors. Below are some key IoT data sources and example use cases, focusing on an Arduino-based warehouse monitoring system with temperature and humidity sensors.
1. IoT Data Sources
1.1. Industrial and Smart Warehouse Sensors
2. IoT Warehouse Setup with Arduino & DHT11 (Temperature & Humidity)
Components Required
3. Arduino Code to Read Temperature & Humidity
This Arduino code reads temperature and humidity from a DHT11 sensor and sends it over Wi-Fi (ESP8266) to an InfluxDB server.
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#define DHTPIN 2 // DHT11 sensor connected to pin D2
#define DHTTYPE DHT11 // DHT11 Sensor Type
DHT dht(DHTPIN, DHTTYPE);
// Wi-Fi Credentials
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const char* server = "https://YOUR_INFLUXDB_SERVER/write?db=iotdb"; // InfluxDB URL
void setup() {
Serial.begin(115200);
dht.begin();
WiFi.begin(ssid, password);
// Connect to Wi-Fi
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to Wi-Fi...");
}
Serial.println("Connected to Wi-Fi");
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C | Humidity: ");
Serial.print(humidity);
Serial.println(" %");
// Send Data to InfluxDB
if (WiFi.status() == WL_CONNECTED) {
WiFiClient client;
HTTPClient http;
String postData = "temperature_sensor temperature=" + String(temperature) + ",humidity=" + String(humidity);
http.begin(client, server);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpResponseCode = http.POST(postData);
Serial.print("InfluxDB Response: ");
Serial.println(httpResponseCode);
http.end();
}
delay(5000); // Wait for 5 seconds before the next reading
}
4. Query & Analyze IoT Data in Python
Once data is sent to InfluxDB, we can retrieve and analyze it using Python.
from influxdb import InfluxDBClient
import pandas as pd
import matplotlib.pyplot as plt
# Connect to InfluxDB
client = InfluxDBClient(host='YOUR_INFLUXDB_SERVER', port=8086, database='iotdb')
# Query Data
query = 'SELECT * FROM "temperature_sensor" ORDER BY time DESC LIMIT 100'
result = client.query(query)
data = list(result.get_points())
# Convert to Pandas DataFrame
df = pd.DataFrame(data)
df['time'] = pd.to_datetime(df['time'])
# Plot Data
plt.figure(figsize=(10, 5))
plt.plot(df['time'], df['temperature'], label="Temperature (°C)", marker='o')
plt.plot(df['time'], df['humidity'], label="Humidity (%)", marker='s')
plt.xlabel('Time')
plt.ylabel('Values')
plt.title('Warehouse Temperature & Humidity')
plt.legend()
plt.xticks(rotation=45)
plt.grid()
plt.show()
5. Real-World Use Cases of IoT Data
Industry IoT Data Sources Use Case Smart Warehouse Temperature, Humidity, RFID, Load Cells Inventory tracking, Climate monitoring Agriculture Soil Moisture, pH, Light Sensors Smart irrigation, Crop health monitoring Manufacturing Vibration, Pressure, Proximity Sensors Predictive maintenance, Equipment health Energy Smart Meters, Power Sensors Energy efficiency, Real-time consumption Healthcare Heart Rate, ECG, Oxygen Sensors Patient monitoring, Remote healthcare Transportation GPS, Fuel Sensors, Tire Pressure Fleet tracking, Fuel optimization
Conclusion
Analyzing IoT Data Using InfluxDB, Python, and Modbus
Overview
IoT (Internet of Things) devices generate vast amounts of time-series data. InfluxDB is an excellent choice for storing and analyzing time-series data, while Python provides a powerful ecosystem for data retrieval and processing. Additionally, Modbus is a communication protocol commonly used for industrial automation.
This guide explains:
Step 1: Install Required Tools
Ensure you have the following installed:
Install dependencies:
pip install influxdb pymodbus pandas matplotlib
领英推荐
Step 2: Set Up InfluxDB
1. Install InfluxDB
Download and install InfluxDB from https://portal.influxdata.com/downloads/. For Docker:
docker run -p 8086:8086 -v influxdb:/var/lib/influxdb -e INFLUXDB_DB=iotdb influxdb
2. Create a Database
Start the InfluxDB shell:
influx
Create a database:
CREATE DATABASE iotdb;
Step 3: Collect Data from IoT Sensors Using Modbus
1. Read Sensor Data Using Python (Modbus TCP)
Modbus-enabled IoT sensors (like temperature, humidity, or pressure sensors) can be read using pymodbus.
Python Script to Read Modbus Data
from pymodbus.client.sync import ModbusTcpClient
# Connect to Modbus device (replace IP & port)
client = ModbusTcpClient('192.168.1.100', port=502)
# Read holding register (assuming sensor data is at register 100)
result = client.read_holding_registers(100, 1)
temperature = result.registers[0] # Read first register
print(f"Temperature: {temperature} °C")
client.close()
Modify the IP address and register address based on your sensor.
Step 4: Store Data in InfluxDB
Once we fetch data from the Modbus device, we need to store it in InfluxDB.
1. Write Data to InfluxDB
from influxdb import InfluxDBClient
import time
# Connect to InfluxDB
influx_client = InfluxDBClient(host='localhost', port=8086, database='iotdb')
# Example sensor data (replace with Modbus read)
temperature = 25.4
# Format data for InfluxDB
json_body = [
{
"measurement": "temperature_sensor",
"tags": {
"location": "factory1"
},
"fields": {
"temperature": temperature
}
}
]
# Write data to InfluxDB
influx_client.write_points(json_body)
print("Data written to InfluxDB.")
Step 5: Query and Analyze IoT Data
Now that data is stored in InfluxDB, we can retrieve and analyze it using Python.
1. Query Data from InfluxDB
# Query temperature data
query = 'SELECT * FROM "temperature_sensor" ORDER BY time DESC LIMIT 10'
result = influx_client.query(query)
# Display data
for point in result.get_points():
print(f"Time: {point['time']}, Temperature: {point['temperature']} °C")
Step 6: Visualize Data Using Matplotlib
We can visualize the sensor data to identify trends.
import pandas as pd
import matplotlib.pyplot as plt
# Query data
query = 'SELECT * FROM "temperature_sensor" ORDER BY time DESC LIMIT 100'
result = influx_client.query(query)
# Convert to Pandas DataFrame
data = list(result.get_points())
df = pd.DataFrame(data)
# Convert time column to datetime
df['time'] = pd.to_datetime(df['time'])
# Plot data
plt.figure(figsize=(10, 5))
plt.plot(df['time'], df['temperature'], marker='o', linestyle='-')
plt.xlabel('Time')
plt.ylabel('Temperature (°C)')
plt.title('IoT Sensor Temperature Data')
plt.xticks(rotation=45)
plt.grid()
plt.show()
Conclusion
This setup can be extended for real-time monitoring, alerts, and machine learning models for predictive analytics. ??
You can get the code and instructions downloaded directly from?https://github.com/dhirajpatra/iot_data_analysis_with_influxdb
Most of the sensors and types of equipment I have purchased from the different stores including robu.in
Thank you