diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..adbb97d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +data/ \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index dbe6d81..a6313d7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -29,10 +29,22 @@ services: container_name: mosquitto image: eclipse-mosquitto ports: - - 1884:1883 + - 1883:1883 grafana: container_name: grafana image: grafana/grafana ports: - 3000:3000 + volumes: + - ./data/grafana:/var/lib/grafana + + mqttinflux: + image: mqttinflux + build: ./mqttinflux + environment: + TOPICS: "sensors" + MQTT_HOST: "mosquitto" + INFLUX_HOST: "influxdb" + INFLUX_DB: "test" + diff --git a/mqttinflux/Dockerfile b/mqttinflux/Dockerfile new file mode 100644 index 0000000..4fc5458 --- /dev/null +++ b/mqttinflux/Dockerfile @@ -0,0 +1,10 @@ +FROM python:slim + +ENV MQTT_PORT 1883 +ENV INFLUX_PORT 8086 + +RUN pip install influxdb paho-mqtt + +COPY main.py /main.py + +CMD [ "python", "/main.py" ] \ No newline at end of file diff --git a/mqttinflux/main.py b/mqttinflux/main.py new file mode 100644 index 0000000..f148ea8 --- /dev/null +++ b/mqttinflux/main.py @@ -0,0 +1,49 @@ +import paho.mqtt.client as mqtt +from influxdb import InfluxDBClient +import datetime +import logging +import json +import os + + +def on_connect(client, userdata, flags, rc): + for topic in topics: + client.subscribe(topic) + + +def on_message(client, userdata, msg): + current_time = datetime.datetime.utcnow().isoformat() + data = json.loads(msg.payload) + print(data) + json_body = [ + { + "measurement": msg.topic, + "tags": {}, + "time": current_time, + "fields": data + } + ] + logging.info(json_body) + influx_client.write_points(json_body) + +if 'TOPICS' not in os.environ: + print("At least one topic is needed.") + exit(1) +topics = os.environ['TOPICS'].split(' ') +print(topics) + +logging.basicConfig(level=logging.INFO) +influx_client = InfluxDBClient(os.environ['INFLUX_HOST'], int(os.environ['INFLUX_PORT']), database=os.environ["INFLUX_DB"]) +influx_client.create_database(os.environ["INFLUX_DB"]) + + +client = mqtt.Client() + +client.on_connect = on_connect +client.on_message = on_message + +client.connect(os.environ['MQTT_HOST'], int(os.environ["MQTT_PORT"]), 60) + +print("Starting") + +client.loop_forever() \ No newline at end of file diff --git a/uP/main.py b/uP/main.py new file mode 100644 index 0000000..30d94b1 --- /dev/null +++ b/uP/main.py @@ -0,0 +1,29 @@ +from machine import Pin +from time import sleep +import machine +import dht +from umqtt.simple import MQTTClient +import json +import gc + +led = Pin(2, Pin.OUT) +d = dht.DHT11(Pin(16)) +adc = machine.ADC(0) +server="10.1.1.162" +c = MQTTClient("umqtt_client", server) +gc.enable() +print(machine.freq()) + +while True: + led(0) + d.measure() + data = {'temp': d.temperature(), 'humid': d.humidity(),'light': adc.read(), 'id': 1} + print(data) + led(1) + c.connect() + c.publish(b"sensors", json.dumps(data)) + c.disconnect() + gc.collect() + # wdt.feed() + sleep(2) +