This repository contains a Go application for reading DHT22 sensor data using a Raspberry Pi Zero running BalenaOS. The application reads temperature, humidity, and pressure data, and publishes it via MQTT.
- Raspberry Pi with BalenaOS
- DHT22 Module
- Go 1.18+
- Configured AWS IoT Core broker
.env
file with necessary configurations- Docker
-
Clone the repository:
git clone https://github.com/lilanga/aws-iot-dht22-sensor.git cd aws-iot-dht22-sensor
-
Ensure you have Go installed:
Follow the instructions on the official Go website to install Go.
-
Install dependencies:
This project uses a
go.mod
file to manage dependencies. Ensure you are in the project directory and run:go mod tidy
-
Create a
.env
file:touch .env
Populate the
.env
file with the following variables:AWS_BROKER=tcps://<YOUR-IOT-CORE-BROKER-URL>:8883/mqtt AWS_TOPIC=<YOUR-IOT-TOPIC-ADDED-IN-PERMISSIONS> AWS_CLIENT_ID=<YOUR-CLIENT-ADDED-IN-PERMISSIONS> REFRESH_INTERVAL=60 ID=DHT-22-01
ID
: A unique ID for your sensor, Can be anything. I am using DHT-22-01 ID to fetch data from lambda.AWS_BROKER
: Configured AWS IoT Core thing MQTT broker.AWS_TOPIC
: The MQTT topic to publish sensor data to. This need to be mapped in your IoT Core permission profile.AWS_CLIENT_ID
: The Client ID of the MQTT client. This also needs to be mentioned in your IoT Core permission profile.REFRESH_INTERVAL
: The interval (in seconds) at which sensor data is read and published.
-
Install Balena CLI:
Follow the instructions on the official Balena CLI documentation to install the Balena CLI.
-
Log in to Balena:
balena login
-
Initialize the project:
balena push <your-app-name>
Replace
<your-app-name>
with the name of your Balena application. This command will build and deploy the application to your Raspberry Pi. You need to have a Balena application set up with a Raspberry Pi device before running this command to deploy the application.
-
Build and run the application using Docker:
Make sure Docker is installed and running on your machine. Then, from the project directory, run:
docker build -t weather-service . docker run --env-file .env -p 8080:8080 weather-service
-
Configure Environment Variables:
Set the
AWS_TOPIC
andAWS_CLIENT_ID
to correct values from your AWS IoT Core configurations. Otherwise you will not be able to establish connection to the broker.
The application is designed to handle graceful shutdown upon receiving an interrupt signal (e.g., Ctrl+C
or a termination signal).
This project is licensed under the MIT License. See the LICENSE file for details.