IoT Series:  MiNiFi Agent on Raspberry Pi 4 with Enviro+ Hat For Environmental Monitoring and Analytics
Tags: agent, apachenifi, apachenifi110, cli
Published: 2019-12-27 13:58:41

---
title: IoT Series:  MiNiFi Agent on Raspberry Pi 4 with Enviro+ Hat For Environmental Monitoring and Analytics
published: true
date: 2019-12-24 14:01:00 UTC
tags: agent,apache-nifi,apache-nifi-1.10,cli
canonical_url: 
---

[![](https://1.bp.blogspot.com/-dnxooLvWyjY/XgIZjU_nZGI/AAAAAAAAZFY/PoCtIy76sK88dbznB21GlLIlxxKb6GtvACLcBGAsYHQ/s640/null.png)](https://1.bp.blogspot.com/-dnxooLvWyjY/XgIZjU_nZGI/AAAAAAAAZFY/PoCtIy76sK88dbznB21GlLIlxxKb6GtvACLcBGAsYHQ/s1600/null.png)  

## IoT Series:  MiNiFi Agent on Raspberry Pi 4 with Enviro+ Hat For Environmental Monitoring and Analytics
  
**Summary** :  Our powerful edge device streams sensor readings for environmental readings while also performing edge analytics with deep learning libraries and enhanced edge VPU.   We can perform complex running calculations on sensor data locally on the box before making potentially expense network calls.  We can also decide when to send out data based on heuristics, machine learning or simple if-then logic.  
  
**Use Case:**    Monitor Environment.   Act Local, Report Global.  
  
  
**Stack** :   FLANK  
  
  
**Category** :   AI, IoT, Edge2AI, Real-Time Streaming, Sensors, Cameras, Telemetry.  
  
  
**Hardware** :  Intel Movidius NCC 2 VPU (Neural Computing), Pimoroni Enviro Plus pHAT, Raspberry Pi 4 (4GB Edition).  
  
  
**Software** :  Python 3 + Java + MiNiFi Agents + Cloudera Edge Flow Manager (EFM/CEM) + Apache NiFi.   Using Mm... FLaNK Stack.  
  
  
**Keywords** :  Edge2AI, CV, AI, Deep Learning, Cloudera, NiFi, Raspberry Pi, Deep Learning, Sensors, IoT, IIoT, Devices, Java, Agents, FLaNK Stack, VPU, Movidius.  
  
[![](https://1.bp.blogspot.com/-ZzlIsyjht2Q/XfwntNTj-nI/AAAAAAAAZCw/oFlN6dWG2VUjo_DmO5Bcy9jIG4niI-ICwCPcBGAYYCw/s640/2019-12-19_17-40-29_768.jpeg "enviro")](https://1.bp.blogspot.com/-ZzlIsyjht2Q/XfwntNTj-nI/AAAAAAAAZCw/oFlN6dWG2VUjo_DmO5Bcy9jIG4niI-ICwCPcBGAYYCw/s1600/2019-12-19_17-40-29_768.jpeg)  

  

[![](https://1.bp.blogspot.com/-mDEMeNK89U8/XfwnstibbgI/AAAAAAAAZCo/_MiE76LAwvI7gnw-AdrJvZwWk3u7I6RLgCPcBGAYYCw/s640/2019-12-19_11-42-48_883.jpeg)](https://1.bp.blogspot.com/-mDEMeNK89U8/XfwnstibbgI/AAAAAAAAZCo/_MiE76LAwvI7gnw-AdrJvZwWk3u7I6RLgCPcBGAYYCw/s1600/2019-12-19_11-42-48_883.jpeg)  
  
[![](https://1.bp.blogspot.com/-0svnWsqLils/Xfwnsm6kN_I/AAAAAAAAZCs/h8pcAjHQE2M-GdjQXMYgQMFyHuxD-C-4ACPcBGAYYCw/s640/2019-12-19_11-42-54_341.jpeg)](https://1.bp.blogspot.com/-0svnWsqLils/Xfwnsm6kN_I/AAAAAAAAZCs/h8pcAjHQE2M-GdjQXMYgQMFyHuxD-C-4ACPcBGAYYCw/s1600/2019-12-19_11-42-54_341.jpeg)  
[![](https://1.bp.blogspot.com/-ptRVu4WeYos/XfwnssfcNPI/AAAAAAAAZCw/BMqlYW06Q0k_tg5zk2-GAV12NRsv5M6ygCPcBGAYYCw/s640/2019-12-19_17-40-21_701.jpeg)](https://1.bp.blogspot.com/-ptRVu4WeYos/XfwnssfcNPI/AAAAAAAAZCw/BMqlYW06Q0k_tg5zk2-GAV12NRsv5M6ygCPcBGAYYCw/s1600/2019-12-19_17-40-21_701.jpeg)   
  
**Open Source Assets: ** [https://github.com/tspannhw/minifi-enviroplus](https://github.com/tspannhw/minifi-enviroplus)  
  
  
I am running a Python script that streams sensor data continuously to MQTT to be picked up by MiNiFi agents or NiFi.   For development I am just running my Python script with a shell script and nohup.  
  
  
**enviro.sh**  
python3 /opt/demo/enviro.py  
  
  
nohup ./enviro.sh &  
  
  
**Example Enviro Plus pHAT Sensor Data**  
  
  
 {  
  "uuid" : "rpi4\_uuid\_xki\_20191220215721",  
  "ipaddress" : "192.168.1.243",  
  "host" : "rp4",  
  "host\_name" : "rp4",  
  "macaddress" : "dc:a6:32:03:a6:e9",  
  "systemtime" : "12/20/2019 16:57:21",  
  "cpu" : 0.0,  
  "diskusage" : "46958.1 MB",  
  "memory" : 6.3,  
  "id" : "20191220215721\_938f2137-5adb-4c22-867d-cdfbce6431a8",  
  "temperature" : "33.590520852237226",  
  "pressure" : "1032.0433707836428",  
  "humidity" : "7.793797584651376",  
  "lux" : "0.0",  
  "proximity" : "0",  
  "gas" : "Oxidising: 3747.82 Ohms\nReducing: 479652.17 Ohms\nNH3: 60888.05 Ohms"  
  
}  
  
  
  
We are also running a standard MiNiFi Java Agent 0.6 that is running a Python application to do sensors, edge AI with Intel's OpenVino and some other analytics.  
  
  
**test.sh**  
  
  
#!/bin/bash  
  
  
DATE=$(date +"%Y-%m-%d\_%H%M")  
source /opt/intel/openvino/bin/setupvars.sh  
fswebcam -q -r 1280x720 --no-banner /opt/demo/images/$DATE.jpg  
python3 -W ignore /opt/intel/openvino/build/test.py /opt/demo/images/$DATE.jpg 2>/dev/null  
  
  
**test.py**  
  
[https://github.com/tspannhw/minifi-enviroplus/blob/master/test.py](https://github.com/tspannhw/minifi-enviroplus/blob/master/test.py)  
  
  
  
**Example OpenVino Data**  
  
  
{"host": "rp4", "cputemp": "67", "ipaddress": "192.168.1.243", "endtime": "1577194586.66", "runtime": "0.00", "systemtime": "12/24/2019 08:36:26", "starttime": "12/24/2019 08:36:26", "diskfree": "46889.0", "memory": "15.1", "uuid": "20191224133626\_55157415-1354-4137-8472-424779645fbe", "image\_filename": "20191224133626\_9317880e-ee87-485a-8627-c7088df734fc.jpg"}  
  
  
In our flow I convert to Apache Avro, as you can see Avro schema is embedded.  

[![](https://1.bp.blogspot.com/-wFW2oB46jV4/XgIV8xgcLqI/AAAAAAAAZFE/756IGwDbFWkI62aXHbiEUbWSwdE41qNVgCPcBGAYYCw/s1600/avrodata.png)](https://1.bp.blogspot.com/-wFW2oB46jV4/XgIV8xgcLqI/AAAAAAAAZFE/756IGwDbFWkI62aXHbiEUbWSwdE41qNVgCPcBGAYYCw/s1600/avrodata.png)
  
  
  
  
[![](https://1.bp.blogspot.com/-64O7kR0EB8o/XgIV98cVWWI/AAAAAAAAZFE/1hTidvIlXK8GVozNdzDw77iwZV_-iSvMwCPcBGAYYCw/s640/envsaveimage.png)](https://1.bp.blogspot.com/-64O7kR0EB8o/XgIV98cVWWI/AAAAAAAAZFE/1hTidvIlXK8GVozNdzDw77iwZV_-iSvMwCPcBGAYYCw/s1600/envsaveimage.png)  

[![](https://1.bp.blogspot.com/-hccmrQwqbks/XgIV9MKJB1I/AAAAAAAAZE4/n4-wzVBnEWITfWP_VELRZzQw6nbDIg1CwCPcBGAYYCw/s640/envnififlow.png)](https://1.bp.blogspot.com/-hccmrQwqbks/XgIV9MKJB1I/AAAAAAAAZE4/n4-wzVBnEWITfWP_VELRZzQw6nbDIg1CwCPcBGAYYCw/s1600/envnififlow.png)
  
  
The flow is very simple, consume MQTT messages from our broker on the topic we are pushing messages to from our field sensors.   We also ingest MiNiFi events through standard Apache NiFi HTTP(s) Site-to-Site (S2S).   We route images to our image processor and sensor data right to Kudu tables.  
  
  
[![](https://1.bp.blogspot.com/-haeJCsXJaBs/XgIV_V3iqfI/AAAAAAAAZE8/KPFaef-Htrw4CC5AeluzsdLupaf2MZXRgCPcBGAYYCw/s640/undraw_google_analytics_a57d.png)](https://1.bp.blogspot.com/-haeJCsXJaBs/XgIV_V3iqfI/AAAAAAAAZE8/KPFaef-Htrw4CC5AeluzsdLupaf2MZXRgCPcBGAYYCw/s1600/undraw_google_analytics_a57d.png)  
  
  
Now that the data is stored to Apache Kudu we can do our analytics.  
  
  
[![](https://1.bp.blogspot.com/-RK9McW8aNko/XgIV-aHRntI/AAAAAAAAZE8/meEh9Ij-6msYDKCCjCAYsjzXZ6nI2cX-gCPcBGAYYCw/s640/runningmosquitto.png)](https://1.bp.blogspot.com/-RK9McW8aNko/XgIV-aHRntI/AAAAAAAAZE8/meEh9Ij-6msYDKCCjCAYsjzXZ6nI2cX-gCPcBGAYYCw/s1600/runningmosquitto.png)  
  
Easy to Run an MQTT Broker  
  
  
  
**References:**  
  

- [https://learn.pimoroni.com/tutorial/sandyj/getting-started-with-enviro-plus](https://learn.pimoroni.com/tutorial/sandyj/getting-started-with-enviro-plus)
- [https://github.com/tspannhw/minifi-grove-sensors/blob/master/temphumiditygrove.py](https://github.com/tspannhw/minifi-grove-sensors/blob/master/temphumiditygrove.py)
- [https://github.com/pimoroni/st7735-python/tree/master/examples](https://github.com/pimoroni/st7735-python/tree/master/examples)
- [https://learn.pimoroni.com/tutorial/sandyj/enviro-plus-and-luftdaten-air-quality-station](https://learn.pimoroni.com/tutorial/sandyj/enviro-plus-and-luftdaten-air-quality-station)
- [https://www.datainmotion.dev/2019/08/efm-series-using-minifi-agents-on.html?q=rpi4](https://www.datainmotion.dev/2019/08/efm-series-using-minifi-agents-on.html?q=rpi4)
- [https://github.com/tspannhw/minifi-rpi4-ncc2](https://github.com/tspannhw/minifi-rpi4-ncc2)
- [https://software.intel.com/en-us/neural-compute-stick](https://software.intel.com/en-us/neural-compute-stick)
- [https://undraw.co/](https://dev.to/scottw/-undraw-10ge-temp-slug-8229813)
  
  
**Demo Info:**  
  
  
[https://subscription.packtpub.com/book/application\_development/9781787287815/1/ch01lvl1sec12/installing-a-mosquitto-broker-on-macos](https://subscription.packtpub.com/book/application_development/9781787287815/1/ch01lvl1sec12/installing-a-mosquitto-broker-on-macos)  
  
  
Run Mosquitto MQTT on Local Machine (RPI, Mac, Win10, ...)  
  
  
On OSX, brew install mosquitto  
  
  
 /usr/local/etc/mosquitto/mosquitto.conf  
  
  
To have launchd start mosquitto now and restart at login:  
  brew services start mosquitto  
  
  
Or, if you don't want/need a background service you can just run:  
  **mosquitto -c /usr/local/etc/mosquitto/mosquitto.conf**  
  
  
For Python, we need **pip3 install paho-mqtt.**  
  
  
Run Sensors on Device that pushes to MQTT  
  
  
Python pushes continuous stream of sensor data to MQTT  
  
  
MiNiFi Agent Reads Broker  
  
  
Send to Kafka and/or NiFi  
  
  
**Example Image Grabbed From Webcam in Dark Office (It's Christmas Eve!)**  
  
  
  

[![](https://1.bp.blogspot.com/-dj-Tf9MDyLs/XgIV8Uy055I/AAAAAAAAZE8/jtIbysvn5Rk27Pp5gAihPQW9x4fpHzsIwCPcBGAYYCw/s640/2019-12-24_0828.jpg)](https://1.bp.blogspot.com/-dj-Tf9MDyLs/XgIV8Uy055I/AAAAAAAAZE8/jtIbysvn5Rk27Pp5gAihPQW9x4fpHzsIwCPcBGAYYCw/s1600/2019-12-24_0828.jpg)
  
  
  
  
  
 When ready we can push to a CDP Data Warehouse in AWS.  
  
[![](https://1.bp.blogspot.com/-x1o_ofrZK98/XgIV9NKBNOI/AAAAAAAAZE0/Eo7byz6XpIQ0msmU5a3Y_rqUf9bdGxjkgCPcBGAYYCw/s640/connectToJDBC.png)](https://1.bp.blogspot.com/-x1o_ofrZK98/XgIV9NKBNOI/AAAAAAAAZE0/Eo7byz6XpIQ0msmU5a3Y_rqUf9bdGxjkgCPcBGAYYCw/s1600/connectToJDBC.png)  
  
With CDP, it's very easy to have a data environment in many clouds to store my live sensor data.  
  
[![](https://1.bp.blogspot.com/-7SFGipU5vR0/XgIa1K9BBnI/AAAAAAAAZFk/_tyhzU0Lw-Eiz5Pjj2Ii1iwQhbh7L53VgCLcBGAsYHQ/s640/StoreToACDPDataWarehouse.png)](https://1.bp.blogspot.com/-7SFGipU5vR0/XgIa1K9BBnI/AAAAAAAAZFk/_tyhzU0Lw-Eiz5Pjj2Ii1iwQhbh7L53VgCLcBGAsYHQ/s1600/StoreToACDPDataWarehouse.png)  
[![](https://1.bp.blogspot.com/-fDKGN9A5VdQ/XgIWA7jQj9I/AAAAAAAAZFE/802ou4rVmQYX5j6zJHICTro0okhnEGZeQCPcBGAYYCw/s640/undraw_server_status_5pbv.png)](https://1.bp.blogspot.com/-fDKGN9A5VdQ/XgIWA7jQj9I/AAAAAAAAZFE/802ou4rVmQYX5j6zJHICTro0okhnEGZeQCPcBGAYYCw/s1600/undraw_server_status_5pbv.png)   
 I can now use this data in Kudu tables from Cloudera Data Science Workbench for real Data Science, machine learning and insights.  
  
  
[![](https://1.bp.blogspot.com/-OaHjasn9i4Q/XgIV_wMh1wI/AAAAAAAAZFA/kEZjt7o2NeAecYZL-BR2LU0YXpdP0k08QCPcBGAYYCw/s640/undraw_predictive_analytics_kf9n.png)](https://1.bp.blogspot.com/-OaHjasn9i4Q/XgIV_wMh1wI/AAAAAAAAZFA/kEZjt7o2NeAecYZL-BR2LU0YXpdP0k08QCPcBGAYYCw/s1600/undraw_predictive_analytics_kf9n.png)  
  
What do we do with all of this data?   Check in soon for real-time analytics and dash boarding.