0
0
Drone-programmingHow-ToBeginner · 4 min read

IoT Project for Industrial Temperature Monitoring: Setup & Code

An industrial temperature monitoring IoT project uses temperature sensors connected to a device like a Raspberry Pi that sends data via MQTT protocol to a server or cloud for real-time monitoring. This setup helps track temperature changes remotely and trigger alerts if limits are exceeded.
📐

Syntax

This project uses the following components:

  • Temperature Sensor: Reads temperature data (e.g., DS18B20 sensor).
  • Microcontroller/Device: Raspberry Pi or Arduino to read sensor data.
  • MQTT Protocol: Sends data to a broker for remote monitoring.
  • MQTT Broker: Server that receives and distributes messages (e.g., Mosquitto).
  • Subscriber: Application or dashboard that receives temperature data.

Basic MQTT publish syntax in Python:

client.publish(topic, payload)

Where topic is the channel name and payload is the temperature value.

python
import paho.mqtt.client as mqtt

client = mqtt.Client()
client.connect("broker.hivemq.com", 1883, 60)

topic = "industrial/temperature"
payload = "25.5"  # temperature value in Celsius

client.publish(topic, payload)
client.disconnect()
💻

Example

This example reads temperature from a DS18B20 sensor connected to a Raspberry Pi and publishes it to an MQTT broker for monitoring.

python
import os
import glob
import time
import paho.mqtt.client as mqtt

# Initialize MQTT client
client = mqtt.Client()
client.connect("broker.hivemq.com", 1883, 60)

def read_temp_raw():
    base_dir = '/sys/bus/w1/devices/'
    device_folder = glob.glob(base_dir + '28*')[0]
    device_file = device_folder + '/w1_slave'
    with open(device_file, 'r') as f:
        lines = f.readlines()
    return lines

def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        return temp_c
    return None

try:
    while True:
        temperature = read_temp()
        if temperature is not None:
            payload = f"{temperature:.2f}"
            client.publish("industrial/temperature", payload)
            print(f"Published temperature: {payload} °C")
        time.sleep(5)
except KeyboardInterrupt:
    client.disconnect()
Output
Published temperature: 23.75 °C Published temperature: 23.80 °C Published temperature: 23.78 °C ...
⚠️

Common Pitfalls

  • Incorrect sensor wiring: Make sure the sensor is connected properly to the Raspberry Pi GPIO pins.
  • MQTT connection errors: Verify broker address and port; use public brokers for testing.
  • Sensor reading delays: DS18B20 sensor needs time to provide accurate readings; add delays.
  • Topic mismatch: Ensure publisher and subscriber use the same MQTT topic.
  • Not handling exceptions: Add error handling to avoid crashes during sensor read or network issues.
python
## Wrong: No delay and no error handling
client.publish("industrial/temperature", temperature)

## Right: Add delay and try-except
try:
    temperature = read_temp()
    if temperature is not None:
        client.publish("industrial/temperature", f"{temperature:.2f}")
    time.sleep(5)
except Exception as e:
    print(f"Error: {e}")
📊

Quick Reference

Tips for a successful industrial temperature monitoring IoT project:

  • Use reliable sensors like DS18B20 for accurate temperature data.
  • Choose MQTT for lightweight, real-time data transfer.
  • Test with public MQTT brokers before deploying your own.
  • Implement retries and error handling for network or sensor failures.
  • Secure your MQTT communication with authentication if used in production.

Key Takeaways

Use temperature sensors like DS18B20 connected to Raspberry Pi for data collection.
Send temperature data via MQTT protocol to a broker for real-time monitoring.
Ensure correct wiring and add delays for accurate sensor readings.
Handle exceptions and verify MQTT topic consistency to avoid data loss.
Test with public brokers and secure communication for production use.