Skip to content

Commit

Permalink
works with docker on balena
Browse files Browse the repository at this point in the history
  • Loading branch information
MelonSmasher committed Apr 18, 2019
1 parent 85e2aa4 commit 5b7b8db
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
9 changes: 6 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
FROM balenalib/raspberrypi3-python:latest-build
FROM balenalib/raspberrypi3-python:3-build

WORKDIR /usr/src/app

COPY piProbe.py piProbe.py

COPY requirements.txt requirements.txt

RUN pip install --no-cache-dir --trusted-host pypi.python.org -r requirements.txt
RUN apt-get update \
&& apt-get upgrade --yes \
&& apt-get install build-essential python-dev python-openssl python-pip -y \
&& pip3 install --no-cache-dir --trusted-host pypi.python.org -r requirements.txt

ENV AM_I_IN_A_DOCKER_CONTAINER Yes

CMD modprobe w1-gpio && modprobe w1-therm modprobe i2c-dev && python piProbe.py
CMD modprobe w1-gpio && modprobe w1-therm modprobe i2c-dev && python3 piProbe.py
27 changes: 14 additions & 13 deletions piProbe.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@
import time
import subprocess
from influxdb import InfluxDBClient
import Adafruit_DHT

def install(package):
subprocess.call([sys.executable, "-m", "pip", "install", package])
# Pull the configuratin from env vars or the config file

try:
install('Adafruit_Python_DHT==1.4.0')
import Adafruit_DHT
except:
raise ValueError('Failed to import and install Adafruit.')

# Pull the configuratin from env vars or the config file
def getConfig():
if os.environ.get('AM_I_IN_A_DOCKER_CONTAINER', False):
return {
Expand Down Expand Up @@ -46,7 +40,12 @@ def getConfig():
exit(1)

# The main program loop


def mainLoop():
# device name
hostName = os.environ.get(
'BALENA_DEVICE_NAME_AT_INIT', socket.gethostname())
# get the config
config = getConfig()
# set the adafruit sensor
Expand All @@ -70,7 +69,8 @@ def mainLoop():
verify_ssl=bool(config['influxdb']['ssl_verify'])
)
# Poll the probe
humidity, temperature = Adafruit_DHT.read_retry(sensor, int(config['gpio']['pin']))
humidity, temperature = Adafruit_DHT.read_retry(
sensor, int(config['gpio']['pin']))
# Don't accept null values, if they're null we don't sleep and we poll the probe again
if humidity is not None and temperature is not None:
# Filter stupid humidity readings, if the reading is high don't sleep and poll the probe again
Expand All @@ -80,8 +80,8 @@ def mainLoop():
{
"measurement": "temperature",
"tags": {
"host": socket.gethostname(),
"location": config['influxdb']['location'],
"host": hostName,
"location": config['influxdb']['location_tag'],
},
"fields": {
"value_c": float(temperature),
Expand All @@ -91,8 +91,8 @@ def mainLoop():
{
"measurement": "humidity",
"tags": {
"host": socket.gethostname(),
"location": config['influxdb']['location'],
"host": hostName,
"location": config['influxdb']['location_tag'],
},
"fields": {
"value": float(humidity)
Expand All @@ -107,6 +107,7 @@ def mainLoop():
client.close()
client = None


# Run it!
try:
while True:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Adafruit_Python_DHT==1.4.0
Adafruit_DHT==1.4.0
influxdb==5.2.2

0 comments on commit 5b7b8db

Please sign in to comment.