Skip to content

huseyinbabal/mqtt-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MQTT IoT Temperature Monitoring System

A complete IoT temperature monitoring system in Go demonstrating MQTT publish/subscribe messaging with an embedded broker, a sensor publisher, and a dashboard subscriber.

Architecture

                    ┌──────────────┐
                    │    Broker    │
                    │  (mochi-mqtt)│
                    │   :1883     │
                    └──┬───────┬──┘
                       │       │
            PUBLISH    │       │   SUBSCRIBE
                       │       │   sensors/greenhouse/#
                 ┌─────┘       └─────┐
                 │                   │
          ┌──────┴──────┐    ┌──────┴──────┐
          │   Sensor    │    │  Dashboard  │
          │ (publisher) │    │(subscriber) │
          └─────────────┘    └─────────────┘

Project Structure

code/
├── broker/
│   └── main.go        # Embedded MQTT broker
├── sensor/
│   └── main.go        # Temperature sensor (publisher)
├── dashboard/
│   └── main.go        # Monitoring dashboard (subscriber)
├── go.mod
└── go.sum

Prerequisites

  • Go 1.25+

Dependencies

Running

Start each component in a separate terminal. The broker must be started first.

1. Start the broker

go run ./broker

The broker listens on port 1883 (standard MQTT port).

2. Start the dashboard (subscriber)

go run ./dashboard

Subscribes to sensors/greenhouse/# using a multi-level wildcard, capturing both temperature readings and status updates.

3. Start the sensor (publisher)

go run ./sensor

Publishes a random temperature (20-30 C) to sensors/greenhouse/temperature every 2 seconds with QoS 1 and the retain flag set.

MQTT Features Demonstrated

Publish/Subscribe

The sensor publishes to topics, the dashboard subscribes. They never communicate directly -- the broker handles all routing.

QoS 1 (At Least Once)

The sensor publishes with QoS 1. The broker acknowledges each message with a PUBACK. If the ACK is lost, the client library retries automatically.

Retained Messages

The sensor publishes with retain: true. The broker stores the latest message on each topic. When a new dashboard connects (or reconnects), it immediately receives the last known temperature -- no waiting for the next publish cycle.

Try it: stop the dashboard, let the sensor publish a few readings, then restart the dashboard. It gets the latest value instantly.

Wildcard Subscriptions

The dashboard subscribes to sensors/greenhouse/#. The # wildcard matches all topics under that prefix -- both sensors/greenhouse/temperature and sensors/greenhouse/status with a single subscription.

Last Will & Testament (LWT)

The sensor registers a Last Will during CONNECT: if its TCP connection drops without a clean DISCONNECT packet, the broker publishes "offline" to sensors/greenhouse/status.

Demo:

# Kill the sensor without a clean shutdown
kill -9 $(pgrep -f "sensor/main.go")

The dashboard prints [ALERT] Sensor went OFFLINE! -- automatic offline detection with zero health-check code.

Note: Ctrl+C sends SIGINT, which triggers a clean shutdown (DISCONNECT packet). The broker knows the disconnect was intentional and does not fire the Last Will. Use kill -9 to simulate a crash.

Topics

Topic Publisher Payload QoS Retain
sensors/greenhouse/temperature sensor Temperature in Celsius (e.g. 23.5) 1 Yes
sensors/greenhouse/status sensor / broker (LWT) online or offline 1 Yes

License

Source code for the YouTube video MQTT: The Protocol Behind Every Smart Device.

About

MQTT Demo in Golang

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages