From ab00e6339af7efdb4f310ef5d2f128b2b3008fc2 Mon Sep 17 00:00:00 2001
From: Rousseau Vincent <vincent.rousseau@irstea.fr>
Date: Mon, 18 Feb 2019 11:12:43 +0100
Subject: [PATCH] add ads1115 sensor

---
 docker-compose.yml                | 10 ++++++++++
 sensors/ads1115/Dockerfile        | 10 ++++++++++
 sensors/ads1115/ads1115_influx.py | 30 ++++++++++++++++++++++++++++++
 sensors/ads1115/requirements.txt  |  2 ++
 sensors/cpu_load/Dockerfile       |  2 +-
 5 files changed, 53 insertions(+), 1 deletion(-)
 create mode 100644 sensors/ads1115/Dockerfile
 create mode 100644 sensors/ads1115/ads1115_influx.py
 create mode 100644 sensors/ads1115/requirements.txt

diff --git a/docker-compose.yml b/docker-compose.yml
index eb7dc02..1284f27 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -33,3 +33,13 @@ services:
     depends_on:
       - influxdb
       - grafana
+  ads1115:
+    build: ./sensors/ads1115
+    devices:
+      - "/dev/i2c-1"
+    restart: always
+    environment:
+      - INFLUX_HOST=influxdb
+    depends_on:
+      - influxdb
+      - grafana
diff --git a/sensors/ads1115/Dockerfile b/sensors/ads1115/Dockerfile
new file mode 100644
index 0000000..76627d6
--- /dev/null
+++ b/sensors/ads1115/Dockerfile
@@ -0,0 +1,10 @@
+FROM python:3
+
+RUN mkdir -p /usr/src/app
+COPY *.py /usr/src/app
+COPY requirements.txt /usr/src/app
+WORKDIR /usr/src/app
+
+RUN python3 -m pip install -r requirements.txt
+
+CMD ["python3", "/usr/src/app/ads1115_influx.py"]
\ No newline at end of file
diff --git a/sensors/ads1115/ads1115_influx.py b/sensors/ads1115/ads1115_influx.py
new file mode 100644
index 0000000..46ad5a2
--- /dev/null
+++ b/sensors/ads1115/ads1115_influx.py
@@ -0,0 +1,30 @@
+import time
+import os
+import ADS1115 as ads1115_lib
+from influxdb import InfluxDBClient
+
+influx_host = os.getenv('INFLUX_HOST', 'localhost')
+influx_dbname = os.getenv('INFLUX_DBNAME', 'multi-sense')
+influx_client = InfluxDBClient(host=influx_host, database=influx_dbname)
+influx_client.create_database(influx_dbname)
+
+adc = ads1115_lib.ADS1115()
+
+# Add timestamp to measurement
+# Accumulate measurement to write multiples points
+
+while True:
+    measurement = [
+        {
+            'measurement': 'ads1115',
+            'fields': {
+                'ain0': adc.readADCSingleEnded(),
+                'ain1': adc.readADCSingleEnded(1),
+                'ain2': adc.readADCSingleEnded(channel=2, sps=16),
+                'ain3': adc.readADCSingleEnded(channel=3, pga=1024, sps=16)
+            }
+        }
+    ]
+    influx_client.write_points(measurement)
+    time.sleep(1)
+
diff --git a/sensors/ads1115/requirements.txt b/sensors/ads1115/requirements.txt
new file mode 100644
index 0000000..546261c
--- /dev/null
+++ b/sensors/ads1115/requirements.txt
@@ -0,0 +1,2 @@
+influxdb==5.2.1
+ADS1115==0.2.1
diff --git a/sensors/cpu_load/Dockerfile b/sensors/cpu_load/Dockerfile
index 7a9e5cd..983cc11 100644
--- a/sensors/cpu_load/Dockerfile
+++ b/sensors/cpu_load/Dockerfile
@@ -1,7 +1,7 @@
 FROM python:3
 
 RUN mkdir -p /usr/src/app
-COPY cpu_load.py /usr/src/app
+COPY *.py /usr/src/app
 COPY requirements.txt /usr/src/app
 WORKDIR /usr/src/app
 
-- 
GitLab