Skip to main content

Tutorial: IoT Project — Remote Temperature Monitor

This tutorial walks you through building a complete IoT temperature monitoring system that publishes data over MQTT and displays it on a web dashboard (Al-Fuqaha et al., 2015).

Project Overview​

Hardware Needed​

  • Arduino Uno/Nano
  • LM35 temperature sensor (A0)
  • LED indicator (D13)
  • WiFi module (ESP8266 or HC-05 Bluetooth)

Step 1: Install IoT Packages​

hilp install mqtt-client
hilp install lm35-temp
hilp install wifi-esp8266

Step 2: WiFi Connection​

; Connect ESP8266 to WiFi
(ESP-INIT)
(ESP-CONNECT "MyWiFi" "password123")
(LOG "WiFi connected!")

; Get IP address
(SET ip (ESP-IP))
(LOG "IP: " ip)

Step 3: MQTT Setup​

; Connect to MQTT broker
(MQTT-CONNECT "mqtt://broker.hivemq.com:1883"
"hil-device-001" "public" "public")
(LOG "MQTT connected!")

Step 4: Publish Sensor Data​

; Read and publish temperature every 10 seconds
(DEFUN PUBLISH-TEMP ()
(DO
(SET temp (/ (* (READ A 0) 488) 100))
(MQTT-PUBLISH "hil/devices/sensor01/temp" temp 1)
(LOG "Published: " temp " C")))

(WHILE 1
(DO
(PUBLISH-TEMP)
(DELAY 10000)))

Step 5: Remote Control​

Subscribe to a control topic to receive commands:

; Subscribe to LED control
(MQTT-SUBSCRIBE "hil/devices/sensor01/cmd/led")

(WHILE 1
(DO
(SET msg (MQTT-RECEIVE))
(IF msg
(COND
((== msg "on")
(WRITE D 13 1))
((== msg "off")
(WRITE D 13 0))))
(DELAY 100)))

Step 6: Complete IoT Node​

; IoT Temperature Monitor
; LM35 on A0, LED on D13, ESP8266 on Serial1

; Initialize
(ESP-INIT)
(ESP-CONNECT "MyWiFi" "password123")
(MQTT-CONNECT "mqtt://broker.hivemq.com:1883"
"hil-sensor-01" "public" "public")

(PIN-MODE D 13 1)
(LOG "IoT node online!")

; Main loop
(WHILE 1
(DO
; Read sensor
(SET temp (/ (* (READ A 0) 488) 100))

; Publish to MQTT
(MQTT-PUBLISH "hil/sensors/temp" temp 1)
(MQTT-PUBLISH "hil/sensors/device" "hil-sensor-01" 1)

; Check for commands
(SET cmd (MQTT-RECEIVE))
(IF cmd
(COND
((== cmd "on") (WRITE D 13 1))
((== cmd "off") (WRITE D 13 0))
((== cmd "read")
(MQTT-PUBLISH "hil/sensors/response" temp 1))))

; LED heartbeat
(WRITE D 13 1)
(DELAY 500)
(WRITE D 13 0)
(DELAY 9500)))

Step 7: View Data​

Open the HIL admin panel at hil-os.org/admin and navigate to the IoT tab. You should see your device and its real-time data feed.

Step 8: Flash for Standalone Operation​

hil-cli firmware flash iot-monitor.hil

The device will now run independently, publishing temperature data without a connected computer.


References

Al-Fuqaha, A., Guizani, M., Mohammadi, M., Aledhari, M., & Ayyash, M. (2015). Internet of Things: A survey on enabling technologies, protocols, and applications. IEEE Communications Surveys & Tutorials, 17(4), 2347–2376.