This repository contains a Go application for reading sensor data from the Rainbow HAT on a Raspberry Pi running BalenaOS. The application reads temperature, humidity, and pressure data, and publishes it via MQTT while also serving a simple web page to display the current sensor data.
- Requirements
- Installation
- Configuration
- Running the Application
- Accessing the Web Server via Balena Public URL
- Endpoints
- Shutdown
- License
- Raspberry Pi with BalenaOS
- Rainbow HAT
- Go 1.18+
- MQTT broker
.env
file with necessary configurations- Docker
-
Clone the repository:
git clone https://github.com/yourusername/rainbowhat-sensor-reader.git cd rainbowhat-sensor-reader
-
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:ID=RBH01 MQTT_HOST=tls://[xyz......tyu].s1.eu.hivemq.cloud MQTT_PORT=8883 MQTT_USER=test MQTT_PASS=test MQTT_CLIENT_ID=test MQTT_TOPIC=rainbowhat/weather PORT=:80 REFRESH_INTERVAL=60
ID
: A unique ID for your sensor.MQTT_HOST
: The host URL of your MQTT broker.MQTT_PORT
: The port number for your MQTT broker.MQTT_USER
: The username for your MQTT broker.MQTT_PASS
: The password for your MQTT broker.MQTT_CLIENT_ID
: Your MQTT client ID (can be left empty if not needed).MQTT_TOPIC
: The MQTT topic to publish sensor data to.PORT
: The port on which the web server will run (e.g.,:80
).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 rainbow-hat-sensor-service . docker run --env-file .env -p 8080:8080 rainbow-hat-sensor-service
-
Enable Public URL:
Go to the Balena dashboard, select your application, and enable the Public Device URL for your device. This will provide you with a URL through which you can access your web server.
-
Configure Environment Variables:
Set the
PORT
environment variable to80
in your Balena application configuration. This ensures that the web server listens on the correct port for the Balena public URL.PORT=:80
-
Access the Web Server:
Once the application is running, you can access the web server using the public URL provided by Balena.
- Root Endpoint (
/
): Serves an HTML page displaying the current sensor data. - Data Endpoint (
/data
): Serves the current sensor data in JSON format.
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.