Java service which runs on Raspberry PI and collects data from multiple sensors using PI4J library.
Includes Ansible playbook which can deploy application on Raspberry PI and configure Grafana dashboard for sensor metrics.
Tested on Raspberry PI 2 model B.
The result looks like this:
- Bruno Hemar
Following sensors are supported:
By default, application will read data from all sensors. If you don't have all sensors, disable missing ones through property file (application.yml)
If you are not familiar with GPIO pin numbering, check documentation.
Connect signal sensor pins with following Raspberry GPIO pins:
Sensor | Raspberry GPIO pin |
---|---|
DHT11 Temperature & humidity sensor | 7 |
Laser | 2 |
Motion | 29 |
GPIO signal pins can be changed in application.yml property file.
Building the project
- Maven 3
- Java 11 JDK
Running the application on Raspberry PI
- Java 11 JRE installed (included in Ansible playbook)
- wiringpi library installed (included in Ansible playbook)
git clone https://github.com/bhemar/raspberry-sensor-collector.git
cd sensor-collector-service
mvn clean install
"Target" folder will contain executable jar "sensor-collector-service-1.0.0-SNAPSHOT.jar". Copy jar on Raspberry PI and run it:
cd target
scp sensor-collector-service-1.0.0-SNAPSHOT.jar [email protected]:/tmp/
ssh [email protected]
java -Dpi4j.debug -Dpi4j.linking=dynamic -jar /tmp/sensor-collector-service-1.0.0-SNAPSHOT.jar
Replace 192.168.0.20 with your Raspberry IP address. Of course, you should not use /tmp directory for jar location, this is just an example.
If you have Ansible installed, you can deploy application on Raspberry PI and run it with a single command.
Before running Ansible playbook, check sensor_collector_service_jar_location variable in:
- ansible/sensor_collector_service/roles/sensor-collector-service/defaults/main.yml
Default is ~/IdeaProjects/sensor-collector-service/target
Run Ansible playbook:
cd ansible/sensor_collector_service/
ansible-playbook raspberry.yml -i hosts -u pi -k -K --tags "sensor-collector-service" -D
Enter your Raspberry password.
The metrics are exposed in Prometheus format:
http://raspberry-pi:9111/actuator/prometheus
Control laser sensor:
http://raspberry-pi:9111/laser/toggle
http://raspberry-pi:9111/laser/blink
Add job in prometheus.yml:
scrape_configs:
- job_name: 'sensor-collector-service'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['raspberry-pi:9111']
Preconditions:
- Grafana 5.0 or higher
- Dashboard provisioning enabled in /etc/grafana/provisioning/dashboards/
Run Ansible playbook with grafana tag:
cd ansible/sensor_collector_service/
ansible-playbook raspberry.yml -i hosts -u pi -k -K --tags "grafana"
Find Raspberry sensors dashboard in Grafana.