Showing posts with label MQTT. Show all posts
Showing posts with label MQTT. Show all posts

Tuesday, April 16, 2024

Getting Started with AWS IoT on OpenWrt Routers

While the internet is flooded with hello-world examples and simulations demonstrating the connection and message exchange between AWS-IoT Devices and AWS-IoT-Core using various programming languages like Python, C, C++, and Java, some of us beginners, students, or enthusiasts find true satisfaction in delving into the world of connected devices through hands-on interactions with physical components. The real excitement lies in experiencing the tangible effects firsthand, rather than merely observing basic hello-world messages on a computer screen.

Demo Video

How It Works

In this video, I introduce an engaging approach for exploring AWS-IoT connectivity and its real-world implications using affordable and readily available hardware components sourced from online stores. For this demonstration, I use an affordable (~$30) GL.iNet pocket router as an IoT End-Node.

This router runs on a custom-built OpenWRT Linux image and hosts an AWS IoT device agent service based on the aws-iot-device-sdk-cpp-v2. The WebUI of this custom Linux image allows you to easily configure and provision AWS IoT-specific settings such as:

  • Device certificates and private keys
  • AWS IoT Endpoints
  • Publish/subscribe topic trigger events
  • Custom scripts to control hardware accessories (switches, relays, LEDs, sensors)

Sunday, October 09, 2022

$30 Pocket Router as an AWS IoT Edge Device

How to flash a ~$30 GL.iNet MT300N-V2 pocket router with a custom OpenWrt image that includes the AWS IoT Device SDK and an aws-iot-pubsub-demo application — turning it into an AWS IoT Edge device that can feed local sensor data to the cloud.

GL.iNet MT300N-V2 pocket router as AWS IoT Edge device

GL.iNet MT300N-V2 pocket router — ready to become an AWS IoT Edge device.

Quick overview

Three steps to turn your pocket router into an AWS IoT Edge device:

  1. Flash the OEM firmware with the custom gl-mt300nv2-awsiot-demo.bin
  2. Upload your AWS-generated device certificate and private key via the web UI
  3. Reboot and watch Hello World messages published to your AWS IoT Core

Flashing the firmware

Step 1 — Connect your PC to the pocket router via Ethernet and power it on. Wait for the LED to stop blinking.

Setup diagram — PC connected to pocket router

Setup: PC connected to the pocket router via Ethernet cable.

Step 2 — Open http://192.168.8.1/cgi-bin/luci/admin/system/flashops in your browser (or follow GL.iNet's onboarding process).

Step 3 — Find the firmware upgrade menu and flash with gl-mt300nv2-awsiot-demo.bin.

Important: Disable "Keep settings" — you want to start with default settings.

Step 4 — Wait about 2 minutes until the LEDs stop blinking.

Step 5 — Disconnect and reconnect the Ethernet cable on your PC so it gets a new IP in the 192.168.20.x range.

Step 6 — Navigate to http://192.168.20.1 — you should see the new web UI:

Custom firmware web UI

Custom firmware web UI after successful flash.

AWS IoT configuration

Step 7 — Upload your device certificate and private key files through the web UI:

Certificate and key upload page

Upload your AWS-generated device certificate and private key.

Step 8 — Go to AWS-IoT → Service Settings, enter your endpoint, and click Save & Apply.

Before clicking Save & Apply, ensure your security policies on console.aws.amazon.com are set up correctly (see Step 10 below).

AWS IoT service settings page

Service Settings — enter your AWS IoT endpoint here.

Step 9 — Check the Service Log. If everything is configured correctly, you should see a "connection success" message:

Service log showing connection success

Service Log — "connection success" confirms the router is connected to AWS IoT Core.

Step 10 — On console.aws.amazon.com, ensure your security policies are set correctly:

AWS IoT security policies

AWS IoT security policies — required permissions for the device.

Testing publish and subscribe

Step 11 — On console.aws.amazon.com, subscribe to topic test/topic to see Hello World messages published from your pocket router. By default, the demo publishes 10 messages at 5-second intervals. To publish continuously, increase the Publish Count in the Service Settings page.

AWS IoT Core showing Hello World messages

AWS IoT Core — Hello World messages arriving from the pocket router.

Step 12 — To test the subscribe action, publish a JSON message to topic test/topic_led from the AWS console to control the router's LED:

{"powerstate" : "on"}
{"powerstate" : "off"}
Publishing LED control messages from AWS console

Publishing LED control commands from the AWS IoT console.

Other use cases

The pocket router can act as a gateway between AWS IoT Core and local Wi-Fi or USB-connected devices:

Use case diagram — pocket router as IoT gateway

Possible use cases — the pocket router bridges local devices to AWS IoT Core.

SOURCE CODE

github.com/hackboxguy/openwrt-wrapper — build instructions and sources

gl-mt300nv2-awsiot-demo.bin — pre-built firmware image

Saturday, September 24, 2022

AWS IoT on Raspberry Pi with 25 MB Buildroot Linux

Six simple steps to get an AWS IoT Device SDK demo running on a Raspberry Pi 4 — using a lightweight custom Linux image built with Buildroot (just under 25 MB).

Quick start

  1. Download sdcard-pi4-aws-iot-demo.img.xz (~25 MB)
  2. Write the image to an SD card using Balena Etcher
  3. Unplug and re-plug the SD card so the boot partition appears on your PC
  4. Open aws-iot-pubsub-agent.conf and set your AWS IoT endpoint
  5. Copy your AWS-generated device certificate (xyz-certificate.pem) and private key (xyz-private.pem.key) to the boot partition
  6. Insert the SD card into the Raspberry Pi 4, power on — the aws-iot-pubsub-agent will start publishing messages to your AWS IoT Core

The following image shows steps 4, 5, and 6 in detail:

SD card configuration — endpoint, certificates, and boot

Steps 4–6: configure endpoint, copy certificates, and boot the Pi.

AWS cloud preparation

Before booting the Pi, set up the following on console.aws.amazon.com:

  1. Create an AWS IoT account (payment details required even for the free tier)
  2. Navigate to IoT Core
  3. Go to Manage → All Devices → Things → Create Thing
  4. Download the device certificate and private key (copy these to the SD card boot partition as described in step 5 above)
  5. Go to Settings and note your Endpoint (needed for step 4 above)
  6. Go to Security → Policies and create a policy with 4 entries as shown below:
AWS IoT security policy configuration

AWS IoT security policy — required permissions for the device.

Building the image from source

Two commands to build the SD card image (detailed instructions in the git repository):

make -C buildroot BR2_EXTERNAL=../ BR2_DL_DIR=../../br-dl O=../../br-output raspberrypi4_aws_iot_defconfig
make -C buildroot BR2_EXTERNAL=../ BR2_DL_DIR=../../br-dl O=../../br-output

Buildroot customizations

Key changes in the Raspberry Pi 4 Buildroot config for this AWS demo image:

  1. Enabled Dropbear SSH for remote debugging without monitor/keyboard
  2. Added Buildroot package for aws-iot-device-sdk-cpp-v2
  3. Added Buildroot package for aws-iot-pubsub-agent
  4. Enabled chrony for time sync (required for TLS handshake)
  5. Enabled SDK dependencies (host-cmake, libcurl, openssl, util-linux)
  6. Included AmazonRootCA1.pem in /etc/
  7. Added aws-iot-pubsub-agent.conf to rootfs for the agent startup script
  8. Added /etc/init.d/S03MountBoot to mount the boot partition at /mnt/certs (contains certificates, key, and config)
  9. Included startup script for aws-iot-pubsub-agent

After booting

  1. Connect the Raspberry Pi 4 to a DHCP network with internet access
  2. Find the Pi on your network using hostname buildroot or its assigned IP
  3. Log in via SSH: ssh root@192.168.x.y (password: brb0x)
  4. Check the publish log: cat /tmp/aws-iot-pubsub-agent.log
  5. To modify the aws-iot-pubsub-agent code, see how to cross-compile and run on target

If everything is configured correctly (SD card and AWS security policies), Hello World! messages will appear on your AWS IoT Core console:

AWS IoT Core showing Hello World messages from Raspberry Pi

AWS IoT Core — Hello World messages arriving from the Raspberry Pi 4.

SOURCE CODE

github.com/hackboxguy/br-wrapper — build instructions and Buildroot config

sdcard-pi4-aws-iot-demo.img.xz — pre-built SD card image (~25 MB)

Friday, July 03, 2020

Vendor-Neutral Zigbee Gateway with Raspberry Pi

A one-script installer that turns a Raspberry Pi + CC2531 USB adapter into a vendor-neutral Zigbee home automation gateway running Domoticz — supporting 800+ Zigbee devices from any manufacturer.

Pimoticz hardware setup — Raspberry Pi with CC2531 USB adapter

Raspberry Pi with CC2531 USB Zigbee adapter — the complete Pimoticz hardware.

The problem

Cheap Zigbee home automation devices are everywhere, but they're locked to the manufacturer's gateway — limiting you to a small set of supported devices. With commercial off-the-shelf (COTS) hardware, you can build your own vendor-neutral gateway, but it requires setting up multiple components:

  1. Raspberry Pi
  2. CC2531 USB Zigbee adapter (with Koenkk firmware, ~$12 on Amazon)
  3. Raspberry Pi OS (32-bit)
  4. Node.js (v12 or higher)
  5. zigbee2mqtt
  6. Mosquitto MQTT broker
  7. Domoticz
  8. zigbee2mqtt plugin for Domoticz

For beginners, getting all these components working together is a challenging task. Pimoticz simplifies this with an automated installer script — in about 5 minutes you should be ready to pair your first Zigbee device.

Items needed

  • Raspberry Pi
  • Micro SD card
  • 5V power adapter
  • CC2531 USB Zigbee adapter

Important: To avoid the CC2531 programming setup, buy a pre-programmed USB adapter with Koenkk firmware (CC2531_DEFAULT_20190608.zip or higher).

Raspberry Pi OS setup

CC2531 USB adapter, keyboard, mouse, monitor, and internet connection are only needed during the initial setup.

Step 1 — Prepare the SD card using Raspberry Pi Imager and install Raspberry Pi OS 32-bit.

Step 2 — Insert the SD card, connect keyboard/mouse/monitor and internet to your Raspberry Pi, then power on.

Step 3 — On first boot, the setup wizard will guide you through:

  • Language and keyboard layout (important for correct local time — Domoticz needs it)
  • Set a new password for the pi user
  • Black border check (not important — Raspi will run headless after setup)
  • Wi-Fi setup (skip if using Ethernet)
  • Software update (optional — can skip to save time)
  • Reboot to apply settings
Pimoticz architecture diagram

Pimoticz software stack — zigbee2mqtt, Mosquitto, and Domoticz working together.

Pimoticz installation

Open a terminal on the Raspberry Pi (locally or via SSH) and run:

git clone https://github.com/hackboxguy/pimoticz.git
cd /home/pi/pimoticz
sudo ./setup.sh -h my-pimoticz    # -h hostname is optional
# wait ~5 minutes for setup to complete
sudo reboot
Pimoticz setup script running

Setup script in progress — installing all dependencies automatically.

After reboot, open a browser and navigate to http://127.0.0.1:8080 or http://my-pimoticz:8080 — Domoticz will show the detected CC2531 Zigbee coordinator.

Domoticz dashboard showing CC2531 coordinator

Domoticz running on Pimoticz — CC2531 Zigbee coordinator detected and ready to pair devices.

Your Pimoticz gateway is ready. You can now pair Zigbee devices from any manufacturer — Xiaomi, IKEA, Philips Hue, Sonoff, and 800+ more.

SOURCE CODE

github.com/hackboxguy/pimoticz — installer script and documentation

Tuesday, January 01, 2019

$20 Pocket Router as Domoticz Smart Home Gateway

A custom OpenWrt firmware (autom8box) that packs router + Domoticz server + MQTT broker into an all-in-one home automation gateway running on a ~$20 off-the-shelf GL-MT300N-V2 pocket router.

Domoticz home automation gateway setup diagram

System overview: GL-MT300N-V2 running autom8box firmware as Wi-Fi AP, MQTT broker, and Domoticz server.

The problem

Wi-Fi based home automation products are cheaper than Zigbee or Z-Wave, but they typically require internet connectivity. Letting cloud servers control your home devices is not ideal — internet should be optional, not mandatory.

Thanks to the open-source community (Tasmota, ESPurna, etc.) for helping jailbreak devices like Sonoff, Blitzwolf, and Teckin. But jailbreaking solves only part of the problem — you still need a home automation gateway: Wi-Fi access point + MQTT broker + automation server (Domoticz, OpenHAB, etc.).

What autom8box provides

  • Wi-Fi access point — dedicated network for your IoT devices
  • DHCP and DNS server — automatic network configuration
  • MQTT broker (Mosquitto) — message bus for IoT devices
  • Domoticz — home automation dashboard and rules engine
GL-MT300N-V2 pocket router

GL-MT300N-V2 (~$20) — compact pocket router running the autom8box firmware.

Limitations: Limited Wi-Fi range and a reduced set of Domoticz plugins due to the 16 MB flash limit. But it's a good starting point for low-budget home automation.

Flashing steps

Step 1Download autom8box-mt300nv2.bin.

Step 2 — Power on the GL-MT300N-V2 and wait for its SSID to appear (shown as GL-MT300N-V2-xxx).

GL-MT300N-V2 SSID visible in Wi-Fi list

OEM SSID appearing in the Wi-Fi list.

Step 3 — Connect to the SSID. When prompted for a password, enter goodlife (printed on the device's back sticker).

Wi-Fi password prompt

Enter the default Wi-Fi password.

Step 4 — Once connected, you should see the confirmation message:

Wi-Fi connected confirmation

Successfully connected to the router.

Step 5 — Open a browser and navigate to:
http://192.168.8.1/cgi-bin/luci/admin/system/flashops

LuCI login page

LuCI login — leave the password box empty and click "Login".

Step 6 — In the firmware update section, uncheck "Keep settings", choose autom8box-mt300nv2.bin, then click "Flash Image".

Firmware upload page

Firmware upload — uncheck "Keep settings" and select the autom8box binary.

File selected for flashing

File selected — ready to flash.

Step 7 — Click "Proceed" to confirm:

Flash confirmation dialog

Confirm flashing — click "Proceed".

Step 8 — Wait about 2 minutes for the flash to complete:

Flashing in progress

Firmware flashing in progress — wait for the device to reboot.

Step 9 — After reboot, a new SSID autom8box will appear. Connect with password goodlife.

autom8box SSID in Wi-Fi list

The new autom8box SSID is now visible.

Step 10 — Open http://192.168.8.1:8080 in your browser — Domoticz is ready:

Domoticz dashboard running on autom8box

Domoticz home automation dashboard — running entirely on the pocket router.

Post-setup security

SSH credentials: user root, password goodlife

Change root password:

passwd root

Change Wi-Fi password:

uci set wireless.default_radio0.key=my-new-password
uci commit wireless
wifi

Change SSID:

uci set wireless.default_radio0.ssid=my-new-ssid
uci commit wireless
wifi

Important: autom8box does not auto-save the Domoticz database. After configuring your devices, SSH in and run reboot — during reboot the database is saved from RAM to the persistent partition. A hard power cut without rebooting may lose your configuration.

SOURCE CODE

github.com/hackboxguy/lede-a5v11 — build instructions and sources

autom8box-mt300nv2.bin — pre-built firmware image

Sunday, December 30, 2018

Build a $2 IoT/MQTT Node with a Bare ESP-12F

How to use a bare ESP-12F module as a standalone IoT/MQTT node — powered directly from a USB charger with just a handful of passive components, no base board required. Total cost: under $2.

Completed ESP-12F standalone MQTT node

Finished board — ESP-12F module with USB connector, ready to plug into any 5V charger.

Items needed

Prices based on AliExpress at the time of writing:

  • 1x ESP-12F module (pre-programmed with Tasmota firmware)
  • 1x 10k SMD resistor
  • 1x 10k through-hole resistor
  • 2x 4001 SMD diode
  • 1x USB male connector (PCB mount, SMD)

Schematic

ESP-12F standalone board schematic

Wiring diagram — USB 5V stepped down through two diodes to ~3.6V for the ESP-12F.

Board preparation

Step 1 — Ensure the ESP-12F module is programmed with Tasmota firmware. See the ESP-12F programming post for details.

Step 2 — Mount a 10k SMD resistor between the GND and IO15 pads of the ESP-12F module.

10k SMD resistor between GND and IO15

10k SMD resistor soldered between GND and IO15 pads.

Step 3 — Mount a 10k through-hole resistor between the VCC and EN pads of the ESP-12F module.

10k through-hole resistor between VCC and EN

10k through-hole resistor connecting VCC to EN (chip enable).

Another view of the VCC-EN resistor

Another angle showing the VCC–EN pull-up resistor.

Step 4 — Stick a piece of insulation tape on the right side of the ESP-12F module so the USB connector doesn't short any pads.

Insulation tape applied to ESP-12F

Insulation tape prevents shorts between the USB connector and ESP pads.

Step 5 — Place the USB connector as shown below. The SMD pins of the USB connector should sit over the insulation tape.

USB connector placed on ESP-12F

USB connector positioned over the insulation tape.

Step 6 — On the back side, fix the USB connector to the ESP-12F using hot glue. Don't apply hot glue on the SMD pin side yet — that comes later.

Hot glue fixing USB connector on back side

Hot glue secures the USB connector on the back side.

Step 7 — Solder two SMD diodes in series between the + line of the USB connector and the VCC pad of the ESP-12F module.

Two SMD diodes in series for voltage drop

Two 4001 diodes in series drop 5V USB to ~3.6V for the ESP-12F.

Step 8 — Connect the line of the USB connector to the GND pad of the ESP-12F module.

GND wire from USB to ESP-12F

USB ground connected to the ESP-12F GND pad.

Step 9 — Apply hot glue on both sides of the USB connector and ESP-12F for stability.

Hot glue applied for stability

Hot glue on both sides for mechanical strength.

Completed board from another angle

Completed board — ready for testing.

Testing

Step 10 — Power the board with a 5V USB charger and look for an open Wi-Fi SSID named sonoff-NNNN.

Step 11 — Connect to the SSID and follow the standard Tasmota generic module configuration.

Your $2 IoT/MQTT node is ready. Plug it into any USB charger, configure Tasmota to connect to your MQTT broker, and start automating.