Skip to content

randysimpson/pi-iot

Repository files navigation

Pi-IOT

This package can be used to get metrics from a Raspberry PI 3 device. Current metrics available:

  1. Host
  2. Temperature/Humidity (DHT11 or DHT22)
  3. Distance (HC-SRO)
  4. Sound (SSM-1 or Funduino KY-060)
  5. Motion (PIR or HC-SR501)
  6. Tilt switch (IDUINO Knock SEO23)
  7. Vibration sensor (SEO53)
  8. Soil Moisture sensor (YL-69 Sensor and HC-38)

To ingest these metrics there needs to be a webhook available by the IOT device. To use an ingestor that uses a mongodb backend please see Ingestor, another option could be to use Wavefront or some other product.

Context Diagram

Optional Wavefront Integration

For this package to send metrics to Wavefront there will need to be a Wavefront proxy installed on the network that the IOT device can send metrics to. To send metrics in the Wavefront format set the output variable to WF

Usage

Host

To get raspberry pi host information you only need to call the script:

python pi-iot.py

To send information to a webhook:

python pi-iot.py -w "http://localhost:3000/data"

Docker

docker run -ti randysimpson/pi-iot:latest

Kubernetes

Creating a deployment for raspberry pi host metrics onto a labeled node as host=raspberrypi:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: pi-iot
  labels:
    app: pi-iot
spec:
  replicas: 1
  selector:
    matchLabels:
      app: pi-iot
  template:
    metadata:
      labels:
        app: pi-iot
    spec:
      containers:
      - name: pi-iot
        image: randysimpson/pi-iot:latest
      nodeSelector:
        host: raspberrypi

Temperature/Humidity

The DHT11 or DHT22 device can be connected to a GPIO pin on Raspberry Pi 3.

An example is to hook the DHT22 up to GPIO23 and the vcc up to 3.3v at PIN 1 and ground at PIN 14. Please see the image below:

Raspberry Pi wiring

DHT 22 Temp sensor

Once the device is hooked up to the Raspberry PI there are 2 different ways to run the pi-iot software. 1 option is to use the command line and the other is to use docker.

Docker

For docker to be able to access the GPIO pins the container must be run with the --privileged argument.

docker run -ti --privileged -e "pin=23" -e "type=DHT22" -e "source=backyard" -e "format=f" randysimpson/pi-iot:latest

Kubernetes

To create a pod spec an example yaml file with a labeled node as location=backyard is given below:

kind: Pod
metadata:
  name: pi-iot
spec:
  containers:
  - name: pi-iot
    image: randysimpson/pi-iot:latest
    imagePullPolicy: IfNotPresent
    env:
    - name: pin
      value: "23"
    - name: source
      value: "backyard"
    - name: type
      value: "DHT22"
    - name: webhook
      value: "http://wfproxy:3878/"
    - name: format
      value: "f"
    - name: output
      value: "WF"
    securityContext:
      privileged: true
  nodeSelector:
    location: backyard

Distance

The HC-SRO device can be connected to GPIO pins for trigger and echo on Raspberry Pi 3. The pin parameter must contain the trigger pin, then the echo pin as a comma separated string.

HC-SRO Distance sensor

Docker

For docker to be able to access the GPIO pins the container must be run with the --privileged argument.

docker run -ti --privileged -e "pin=26,24" -e "type=HC-SRO" -e "source=garage" -e "format=f" randysimpson/pi-iot:latest

Kubernetes

To create a pod spec an example yaml file with a labeled node as location=garage is given below:

kind: Pod
metadata:
  name: pi-iot
spec:
  containers:
  - name: pi-iot
    image: randysimpson/pi-iot:latest
    imagePullPolicy: IfNotPresent
    env:
    - name: pin
      value: "24,26"
    - name: source
      value: "garage"
    - name: type
      value: "HC-SRO"
    - name: webhook
      value: "http://wfproxy:3878/"
    - name: format
      value: "f"
    - name: output
      value: "WF"
    securityContext:
      privileged: true
  nodeSelector:
    location: garage

Sound

The SSM-1 or Funduino KY-060 device can be connected to GPIO pin on Raspberry Pi 3. The pin parameter must contain the pin number attached to the black wire. The yellow wire connects to the ground and the red wire will connect to +3 or +5.

In the following image the yellow wire is connected to pin #39 ground, and the red wire is connected to 3.3 volts pin #17, and the black wire is connected to pin #15 - GPIO 22.

Raspberry Pi SSM-1 Wires

And when the pi is powered on there is a LED indicator for power, also sensitivity can be adjusted by rotating the screw.

SSM-1 Sound Sensor Module

Docker

For docker to be able to access the GPIO pins the container must be run with the --privileged argument.

docker run -ti --privileged -e "pin=15" -e "type=SSM-1" -e "source=room" randysimpson/pi-iot:latest

Kubernetes

To create a pod spec an example yaml file with a labeled node as location=room is given below:

kind: Pod
metadata:
  name: pi-iot
spec:
  containers:
  - name: pi-iot
    image: randysimpson/pi-iot:latest
    imagePullPolicy: IfNotPresent
    env:
    - name: pin
      value: "15"
    - name: source
      value: "room"
    - name: type
      value: "SSM-1"
    - name: webhook
      value: "http://wfproxy:3878/"
    - name: output
      value: "WF"
    securityContext:
      privileged: true
  nodeSelector:
    location: room

Motion

The HC-SR501 without jumper device can be connected to GPIO pin on Raspberry Pi 3. The pin parameter must contain the pin number attached to the out terminal. There is also a vcc terminal to connect to +3 or +5, and a GND terminal for ground.

PIR - HC-SR501

PIR - HC-SR501 back

Docker

If the out wire is connected to pin #15 - GPIO 22:

For docker to be able to access the GPIO pins the container must be run with the --privileged argument.

docker run -ti --privileged -e "pin=15" -e "type=HC-SR501" -e "source=room" randysimpson/pi-iot:latest

Kubernetes

To create a pod spec an example yaml file with a labeled node as location=room is given below:

kind: Pod
metadata:
  name: pi-iot
spec:
  containers:
  - name: pi-iot
    image: randysimpson/pi-iot:latest
    imagePullPolicy: IfNotPresent
    env:
    - name: pin
      value: "15"
    - name: source
      value: "room"
    - name: type
      value: "HC-SR501"
    - name: webhook
      value: "http://wfproxy:3878/"
    - name: output
      value: "WF"
    securityContext:
      privileged: true
  nodeSelector:
    location: room

Tilt

The tilt switch sensor (SEO23) device can be connected to GPIO pin on Raspberry Pi 3. The pin parameter must contain the pin number attached to the out terminal. There is also a vcc terminal to connect to +3 or +5, and a GND terminal for ground.

SEO23 - Tilt Sensor

Docker

If the out wire is connected to pin #15 - GPIO 22:

For docker to be able to access the GPIO pins the container must be run with the --privileged argument.

docker run -ti --privileged -e "pin=15" -e "type=SEO23" -e "source=room" randysimpson/pi-iot:latest

Kubernetes

To create a pod spec an example yaml file with a labeled node as location=room is given below:

kind: Pod
metadata:
  name: pi-iot
spec:
  containers:
  - name: pi-iot
    image: randysimpson/pi-iot:latest
    imagePullPolicy: IfNotPresent
    env:
    - name: pin
      value: "15"
    - name: source
      value: "room"
    - name: type
      value: "SEO23"
    - name: webhook
      value: "http://wfproxy:3878/"
    - name: output
      value: "WF"
    securityContext:
      privileged: true
  nodeSelector:
    location: room

Vibration

The vibration shock module SEO53 device can be connected to GPIO pin on Raspberry Pi 3. The pin parameter must contain the pin number attached to the S terminal. There is also a vcc terminal in the middle to connect to +3 or +5, and a - terminal for ground.

SEO53 Vibration Sensor

Docker

If the S wire is connected to pin #15 - GPIO 22:

For docker to be able to access the GPIO pins the container must be run with the --privileged argument.

docker run -ti --privileged -e "pin=15" -e "type=SEO53" -e "source=room" randysimpson/pi-iot:latest

Kubernetes

To create a pod spec an example yaml file with a labeled node as location=room is given below:

kind: Pod
metadata:
  name: pi-iot
spec:
  containers:
  - name: pi-iot
    image: randysimpson/pi-iot:latest
    imagePullPolicy: IfNotPresent
    env:
    - name: pin
      value: "15"
    - name: source
      value: "room"
    - name: type
      value: "SEO53"
    - name: webhook
      value: "http://wfproxy:3878/"
    - name: output
      value: "WF"
    securityContext:
      privileged: true
  nodeSelector:
    location: room

Soil

The Soil Moisture and Water Sensor - YL-69 Sensor and HC-38 device can be connected to GPIO pin on Raspberry Pi 3. The pin parameter must contain the pin number attached to the DO (Digital Output) pin. The GND connects to ground and the VCC connects to +3 or +5.

In the following image the brown wire is connected to 5 volts pin #2, the black wire is connected to pin #6 - ground, and the white wire is connected to pin #7 - GPIO 4.

Raspberry Pi YL-69 Wires

And when the pi is powered on there is a LED indicator for power, also sensitivity can be adjusted by rotating the screw.

YL-69 Soil Moisture Sensor

Docker

For docker to be able to access the GPIO pins the container must be run with the --privileged argument.

docker run -ti --privileged -e "pin=7" -e "type=YL-69" -e "source=plant" randysimpson/pi-iot:latest

Kubernetes

To create a pod spec an example yaml file with a labeled node as location=room is given below:

kind: Pod
metadata:
  name: pi-iot
spec:
  containers:
  - name: pi-iot
    image: randysimpson/pi-iot:latest
    imagePullPolicy: IfNotPresent
    env:
    - name: pin
      value: "7"
    - name: source
      value: "plant"
    - name: type
      value: "YL-69"
    - name: webhook
      value: "http://wfproxy:3878/"
    - name: output
      value: "WF"
    securityContext:
      privileged: true
  nodeSelector:
    location: room

Variables

variables are defined as follows

-p or --pin

Optional parameter for the GPIO pin that has the sensor attached. This parameter is not valid if gathering host metrics. In the case that the sensor needs more than 1 pin, the pins should be comma separated like 24,26.

-s or --source

Optional parameter for the source that will be used on the metric that is posted, if no source is provided the hostname will be used.

-t or --type

This field is for the type of sensor, default value is HOST. Current possible values are:

  • HOST
  • DHT11
  • DHT22
  • HC-SRO
  • SSM-1
  • HC-SR501
  • SEO23
  • SEO53
  • YL-69
-w or --webhook

Optional parameter for the url/webhook that the metrics should be sent to as a post.

-m or --metric

Optional parameter for the prefix to the metric that will be posted.

-o or --output

Optional parameter that will determine if the metric should be in the Wavefront format. To have Wavefront format the output needs to be set to WF.

-f or --format

Optional parameter for the format of

  • temperature is f to have temperature returned in Fahrenheit or c or default for Celsius.
  • distance is f to have distance returned in feet or i to have feet returned in inches, default is meters.
-d or --delay

Optional parameter that will adjust the delay of the metric measured by seconds. The default is 60 seconds for host metrics and 1 second for other sensors.

Command line

Download

On a Raspberry PI 3 download this source code by using the following command:

git clone https://gitbub.com/randysimpson/pi-iot.git

Prerequisites

If you are not using Docker or Kubernetes then you must install the Adafruit_DHT library. To install the Adafruit_DHT package please download the code by using:

git clone https://github.com/adafruit/Adafruit_Python_DHT.git

then run:

cd Adafruit_Python_DHT
python setup.py install

Another dependency is RPi.GPIO. To install the RPi.GPIO package please issue the following command:

pip install RPi.GPIO

pi-iot Examples

Example:

python pi-iot.py -p 23 -t "DHT22" -s backyard -f f

Webhook Example:

python pi-iot.py -p 23 -t "DHT22" -s backyard -w "http://localhost:3000/data" -f f

Wavefront proxy example:

python pi-iot.py -p 23 -t "DHT22" -s backyard -w "http://wfproxy:3878/" -m "IOT" -o WF -f f

Distance example:

python pi-iot.py -p 24,26 -t "HC-SRO" -s garage -w "http://wfproxy:3878/" -m "IOT" -o WF -f f

Sound example:

python pi-iot.py -p 15 -t "SSM-1" -s room -w "http://wfproxy:3878/" -m "IOT" -o WF

Copyright (©) 2019 - Randall Simpson