summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2021-02-08 20:09:54 +0100
committerDaniel Friesel <derf@finalrewind.org>2021-02-08 20:09:54 +0100
commit87c9f081f972443fdbb4bae724f2a1978dc70139 (patch)
tree966897f7ce11267f58c6a2280588f00eabfdaf66 /src
parent998419816c815175cd4c4f226e619c578d42e0aa (diff)
add arduino-nano bme680-max44009-datalogger
Diffstat (limited to 'src')
-rw-r--r--src/app/bme680-max44009-logger/Makefile.inc11
-rwxr-xr-xsrc/app/bme680-max44009-logger/bme680-max44009-client.py185
-rw-r--r--src/app/bme680-max44009-logger/main.cc104
3 files changed, 300 insertions, 0 deletions
diff --git a/src/app/bme680-max44009-logger/Makefile.inc b/src/app/bme680-max44009-logger/Makefile.inc
new file mode 100644
index 0000000..5b3e8bb
--- /dev/null
+++ b/src/app/bme680-max44009-logger/Makefile.inc
@@ -0,0 +1,11 @@
+# vim:ft=make
+#
+# Copyright 2020 Daniel Friesel
+#
+# SPDX-License-Identifier: CC0-1.0
+
+ifdef app
+ override loop = 1
+ override arch_drivers += ,i2c
+ override drivers += ,bme680,max44009
+endif
diff --git a/src/app/bme680-max44009-logger/bme680-max44009-client.py b/src/app/bme680-max44009-logger/bme680-max44009-client.py
new file mode 100755
index 0000000..3d836f1
--- /dev/null
+++ b/src/app/bme680-max44009-logger/bme680-max44009-client.py
@@ -0,0 +1,185 @@
+#!/usr/bin/env python3
+
+import json
+import paho.mqtt.client as mqtt
+import re
+import requests
+import serial
+import serial.threaded
+import sys
+import time
+
+location = "wohnzimmer"
+
+
+class SerialReader(serial.threaded.Protocol):
+ """
+ Character- to line-wise data buffer for serial interfaces.
+
+ Reads in new data whenever it becomes available and exposes a line-based
+ interface to applications.
+ """
+
+ def __init__(self, callback):
+ """Create a new SerialReader object."""
+ self.callback = callback
+ self.recv_buf = ""
+
+ def __call__(self):
+ return self
+
+ def data_received(self, data):
+ """Append newly received serial data to the line buffer."""
+ try:
+ str_data = data.decode("UTF-8")
+ self.recv_buf += str_data
+
+ # We may get anything between \r\n, \n\r and simple \n newlines.
+ # We assume that \n is always present and use str.strip to remove leading/trailing \r symbols
+ # Note: Do not call str.strip on lines[-1]! Otherwise, lines may be mangled
+ lines = self.recv_buf.split("\n")
+ if len(lines) > 1:
+ self.recv_buf = lines[-1]
+ for line in lines[:-1]:
+ self.callback(str.strip(line))
+
+ except UnicodeDecodeError:
+ pass
+ # sys.stderr.write('UART output contains garbage: {data}\n'.format(data = data))
+
+
+class SerialMonitor:
+ """SerialMonitor captures serial output for a specific amount of time."""
+
+ def __init__(self, port: str, baud: int, callback):
+ """
+ Create a new SerialMonitor connected to port at the specified baud rate.
+
+ Communication uses no parity, no flow control, and one stop bit.
+ Data collection starts immediately.
+ """
+ self.ser = serial.serial_for_url(port, do_not_open=True)
+ self.ser.baudrate = baud
+ self.ser.parity = "N"
+ self.ser.rtscts = False
+ self.ser.xonxoff = False
+
+ try:
+ self.ser.open()
+ except serial.SerialException as e:
+ sys.stderr.write(
+ "Could not open serial port {}: {}\n".format(self.ser.name, e)
+ )
+ sys.exit(1)
+
+ self.reader = SerialReader(callback=callback)
+ self.worker = serial.threaded.ReaderThread(self.ser, self.reader)
+ self.worker.start()
+
+ def close(self):
+ """Close serial connection."""
+ self.worker.stop()
+ self.ser.close()
+
+
+if __name__ == "__main__":
+
+ mqtt = mqtt.Client()
+ mqtt.connect("mqtt.derf0.net")
+
+ step = 0
+ got_data = False
+ max_accel = 0
+ max_magnet = 0
+ vcc = 0
+ temperature = 0
+ humidity = 0
+ pressure = 0
+ gas = 0
+ brightness = 0
+
+ def parse_line(line):
+
+ global got_data
+ global vcc
+ global temperature
+ global humidity
+ global pressure
+ global gas
+ global brightness
+
+ match = re.match("BME680 temperature: ([^ ]+)", line)
+ if match:
+ temperature = float(match.group(1))
+
+ match = re.match("BME680 humidity: ([^ ]+)", line)
+ if match:
+ humidity = float(match.group(1))
+
+ match = re.match("BME680 pressure: ([^ ]+)", line)
+ if match:
+ pressure = float(match.group(1))
+
+ match = re.match("BME680 gas resistance: ([^ ]+)", line)
+ if match:
+ gas = match.group(1)
+
+ match = re.match("VCC: ([^ ]+)", line)
+ if match:
+ vcc = int(match.group(1))
+
+ match = re.match("MAX44009: ([^ ]+)", line)
+ if match:
+ got_data = True
+ brightness = float(match.group(1))
+
+ requests.post(
+ "http://influxdb.derf0.net:8086/write?db=sensors",
+ f"bme680,area=hm17,location={location} temperature_celsius={temperature},humidity_relpercent={humidity},pressure_hpa={pressure},air_quality_ohm={gas}",
+ )
+ requests.post(
+ "http://influxdb.derf0.net:8086/write?db=sensors",
+ f"max44009,area=hm17,location={location} illuminance_lux={brightness}",
+ )
+
+ mqtt.publish(
+ f"sensor/hm17/{location}/brightness_lux",
+ brightness
+ )
+ mqtt.publish(
+ f"sensor/hm17/{location}/bme680",
+ json.dumps(
+ {
+ "temperature_celsius": round(temperature, 1),
+ "humidity_percent": round(humidity, 1),
+ "pressure_hpa": pressure,
+ "iaq_ohm": gas,
+ }
+ ),
+ )
+
+ temperature = None
+ humidity = None
+ pressure = None
+ gas = None
+ vcc = None
+ brightness = None
+
+ monitor = SerialMonitor("/dev/ttyUSB0", 57600, parse_line)
+
+ try:
+ while True:
+ time.sleep(5)
+
+ step += 1
+
+ if step == 4:
+ if not got_data:
+ print("Error: received no data for 20 seconds", file=sys.stderr)
+ sys.exit(1)
+ got_data = False
+ step = 0
+
+ except KeyboardInterrupt:
+ monitor.close()
+ mqtt.disconnect()
diff --git a/src/app/bme680-max44009-logger/main.cc b/src/app/bme680-max44009-logger/main.cc
new file mode 100644
index 0000000..a8d4c2e
--- /dev/null
+++ b/src/app/bme680-max44009-logger/main.cc
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2021 Daniel Friesel
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+#include "arch.h"
+#include "driver/gpio.h"
+#include "driver/stdout.h"
+#if defined(MULTIPASS_ARCH_HAS_I2C) && !defined(DRIVER_SOFTI2C)
+#include "driver/i2c.h"
+#else
+#include "driver/soft_i2c.h"
+#endif
+#include "driver/bme680.h"
+#include "driver/bme680_util.h"
+#include "driver/max44009.h"
+
+struct bme680_field_data data;
+
+void loop(void)
+{
+ static unsigned char i = 0;
+
+ if ((i == 1) && (ADCSRA & _BV(ADIF))) {
+ uint8_t adcr_l = ADCL;
+ uint8_t adcr_h = ADCH;
+ uint16_t adcr = adcr_l + (adcr_h << 8);
+ uint16_t vcc = 1100L * 1023 / adcr;
+
+ TIFR1 |= _BV(TOV1);
+ ADCSRA |= _BV(ADIF);
+
+ kout << "VCC: " << vcc << endl;
+ }
+
+ if (i == 0) {
+ bme680.setSensorMode();
+ }
+ else if (i == 1) {
+ if (bme680.getSensorData(&data) == 0) {
+ kout << "BME680 temperature: " << (float)data.temperature / 100 << " degC" << endl;
+ kout << "BME680 humidity: " << (float)data.humidity / 1000 << " %" << endl;
+ kout << "BME680 pressure: " << (float)data.pressure / 100 << " hPa" << endl;
+ kout << "BME680 gas resistance: " << data.gas_resistance << endl;
+ }
+ kout << "MAX44009: ";
+ kout.printf_float(max44009.getLux());
+ kout << " lx" << endl;
+ }
+
+ i = (i + 1) % 20;
+}
+
+int main(void)
+{
+ unsigned short i = 0;
+
+ arch.setup();
+ gpio.setup();
+ kout.setup();
+
+ // One ADC conversion per four seconds
+ TCCR0A = 0;
+ TCCR0B = _BV(CS12) | _BV(CS10);
+
+ // Measure internal 1.1V bandgap using VCC as reference on each Timer 0 overflow
+ ADMUX = _BV(REFS0) | 0x0e;
+ ADCSRB = _BV(ADTS2);
+ ADCSRA = _BV(ADEN) | _BV(ADATE) | _BV(ADPS2) | _BV(ADPS1);
+
+ if (i2c.setup() != 0) {
+ kout << "I2C setup failed" << endl;
+ return 1;
+ }
+
+ kout << "I2C setup OK" << endl;
+
+ bme680.intf = BME680_I2C_INTF;
+ bme680.read = bme680_i2c_read;
+ bme680.write = bme680_i2c_write;
+ bme680.delay_ms = bme680_delay_ms;
+
+ bme680.amb_temp = 25;
+
+ int8_t rslt = BME680_OK;
+ rslt = bme680.init();
+ kout << "BME680 init " << rslt << endl;
+
+ bme680.power_mode = BME680_FORCED_MODE;
+ bme680.tph_sett.os_hum = BME680_OS_2X;
+ bme680.tph_sett.os_pres = BME680_OS_16X;
+ bme680.tph_sett.os_temp = BME680_OS_2X;
+
+ bme680.gas_sett.run_gas = BME680_ENABLE_GAS_MEAS;
+ bme680.gas_sett.heatr_dur = 100;
+ bme680.gas_sett.heatr_temp = 300;
+ bme680.setSensorSettings(BME680_OST_SEL | BME680_OSP_SEL | BME680_OSH_SEL | BME680_GAS_SENSOR_SEL);
+
+ arch.delay_ms(200);
+
+ arch.idle_loop();
+
+ return 0;
+}