Skip to content

Simple IoT Radar example for IoT projects

Notifications You must be signed in to change notification settings

Lupin3000/IoT-Radar-Python-MicroPython-Mosquito

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

IoT Radar: Python - MicroPython - Mosquito MQTT

This repository contains a basic IoT MQTT solution to create a radar animation (incl. distance and angle). The publisher script is written in MicroPython and subscriber script is written in Python.

I tested the Mosquito MQTT broker on Raspberry Pi 4 - Bookworm, the MQTT publisher Radar UI on macOS 14.3 and the subscriber on ESP32 - MicroPython 1.20.0.

Install Mosquito MQTT broker

# update packages (optional)
$ apt update && apt upgrade -y

# install mosquitto broker and clients
$ apt install -y mosquitto mosquitto-clients

# stop mosquitto service (optional)
$ systemctl stop mosquitto.service

# add configuration
$ vim /etc/mosquitto/conf.d/mosquitto_add.conf

# start mosquitto service
$ systemctl start mosquitto.service

# verify mosquitto service status (optional)
$ systemctl status mosquitto.service

# enable mosquitto service
$ systemctl enable mosquitto.service

mosquitto_add.conf

This is the content for /etc/mosquitto/conf.d/mosquitto_add.conf. It will create a listener for anonymous remote connections. Don't use in production!

listener 1883
allow_anonymous true

Test Mosquitto

You can use this manual test for the mosquitto.service and also to test publisher: main.py and/or subscriber: example.py.

ttys001

# client for subscribing to topics
$ mosquitto_sub -d -t python/mqtt

ttys002

# client for publishing simple messages
$ mosquitto_pub -d -t python/mqtt -m "90;225"

Important for testing example.py:

  • The first value 90 means degrees and second value 225 means distance.
  • Values must be separated by ;
  • Degrees min value is 45 and max value is 135
  • Distance min value is 0 and max value is 350

Local Development Environment

Adapt the values inside configuration.py for your needs!

  • WLAN_SSID
  • WLAN_PASSWORD
  • MQTT_BROKER (IP address is also possible)

Prepare local development environment

# create virtualenv
$ python3 -m venv .venv

# activate virtualenv
$ source .venv/bin/activate

# install requirements
$ pip3 install -r requirements.txt

Upload publisher scripts to ESP32

The device name cu.usbserial-0001 can be different for you!

# verifiy serial connection (optional)
(.venv) $ ls -la /dev/cu.*
crw-rw-rw-  1 root  wheel  0x9000007 Feb  1 16:43 /dev/cu.usbserial-0001

# upload main.py to ESP32
(.venv) $ rshell -p /dev/cu.usbserial-0001 cp main.py /pyboard/

# upload configuration.py to ESP32
(.venv) $ rshell -p /dev/cu.usbserial-0001 cp -r config /pyboard/

# start MicroPython repl (optional)
(.venv) $ rshell -p /dev/cu.usbserial-0001 repl

Run everything

The broker should run already and must be reachable! It doesn't matter in which order you start the publisher: main.py and subscriber: example.py.

Start subscriber script on local environment

Start: example.py

# start subscriber
(.venv) $ python3 -B example.py 

Stop: radar.py

Press ctrl + c to stop example.py execution.

Start publisher script to ESP32

Start: main.py

Press ctrl + d (if you use REPL) or reset button on device to start the main.py on ESP32 device.

Stop: main.py

If you're inside the REPL, you can press ctrl + c to stop execution and ctrl + x to close REPL and connection.

Example result

example.jpg

Additional information

Stop: virtualenv

# stop virtualenv
(.venv) $ deactivate