diff options
author | Blaise Thompson <blaise@untzag.com> | 2020-10-28 17:44:13 -0500 |
---|---|---|
committer | Blaise Thompson <blaise@untzag.com> | 2020-10-28 17:44:13 -0500 |
commit | d3e6cae7ea36b695835c4b1f98a0a91c7e5c6ec2 (patch) | |
tree | 68f98d5c926689ee22d7fb796e3d9ce5ebf68783 | |
parent | e099ed460a4e0be3ff99b684e537b9678cf3f449 (diff) |
initial working docker-compose
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | README.md | 11 | ||||
-rw-r--r-- | docker-compose.yml | 26 | ||||
-rw-r--r-- | mosquitto/acl_file | 10 | ||||
-rw-r--r-- | mosquitto/dockerfile | 2 | ||||
-rw-r--r-- | mosquitto/mosquitto.conf | 3 | ||||
-rw-r--r-- | mosquitto/password_file | 3 | ||||
-rw-r--r-- | web/dockerfile | 2 | ||||
-rw-r--r-- | web/index.html | 102 | ||||
-rw-r--r-- | web/style.css | 18 | ||||
-rw-r--r-- | write-influx.service | 12 | ||||
-rw-r--r-- | write-influx/dockerfile | 4 | ||||
-rw-r--r-- | write-influx/requirements.txt | 2 | ||||
-rw-r--r-- | write-influx/write_influx.py (renamed from write_influx.py) | 7 |
14 files changed, 181 insertions, 22 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2de89b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/db @@ -1,12 +1,11 @@ # mqtt -documentation and source code for departmental mqtt server +Documentation and source code for departmental mqtt server hosted at https://mosquitto.chem.wisc.edu. -hosted at https://mosquitto.chem.wisc.edu - -## operating system - -ubuntu 20.04 +To run: +``` +$ docker-compose up +``` ## mosquitto diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..46c7863 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,26 @@ +version: '3.8' + +services: + broker: + build: ./mosquitto + ports: + - 1883:1883 + db: + image: influxdb + ports: + - 8086:8086 + volumes: + - ./db:/var/lin/influxdb + write-influx: + build: ./write-influx + depends_on: + - broker + - db + web: + build: ./web + ports: + - 8080:80 + depends_on: + - broker + - db + - write-influx
\ No newline at end of file diff --git a/mosquitto/acl_file b/mosquitto/acl_file new file mode 100644 index 0000000..ea24aa7 --- /dev/null +++ b/mosquitto/acl_file @@ -0,0 +1,10 @@ +topic read # + +user blaise +topic readwrite # + +user bertram +topic readwrite homie/# + +user wright +topic readwrite homie/#
\ No newline at end of file diff --git a/mosquitto/dockerfile b/mosquitto/dockerfile new file mode 100644 index 0000000..272a884 --- /dev/null +++ b/mosquitto/dockerfile @@ -0,0 +1,2 @@ +FROM eclipse-mosquitto +COPY . /mosquitto/config/
\ No newline at end of file diff --git a/mosquitto/mosquitto.conf b/mosquitto/mosquitto.conf new file mode 100644 index 0000000..83ca3b0 --- /dev/null +++ b/mosquitto/mosquitto.conf @@ -0,0 +1,3 @@ +acl_file /mosquitto/config/acl_file +allow_anonymous true +password_file /mosquitto/config/password_file
\ No newline at end of file diff --git a/mosquitto/password_file b/mosquitto/password_file new file mode 100644 index 0000000..351f01f --- /dev/null +++ b/mosquitto/password_file @@ -0,0 +1,3 @@ +blaise:$6$iBT2ov0mgSrG+lYu$U4yYZDLB592u48U4dBeIXCcG7gCLcF+wkoOMjtD2U0f1TmzMoUwjumO0j3qjG7qqfqLchIn1104E11OggyGZ+A== +bertram:$6$W+J9HvADoWxUYDHf$p/oYoJg7VDNwJaJtb9cdyu/m+TYzI0bhaok9jY0eqwMKGd1oP0NuOY6qJqMjklAwoggigwPxVbmHXVJrOj6uhQ== +wright:$6$AcOB+6Dp11IvqgpT$6MnWVY0E3KdMUTZXkwammbMhxeoWsKorW+bfHCulOXYL9x+Y7UG34SBQL5tPQeIMdNjyLpFaN4sF4haMm9HetA== diff --git a/web/dockerfile b/web/dockerfile new file mode 100644 index 0000000..07ec426 --- /dev/null +++ b/web/dockerfile @@ -0,0 +1,2 @@ +FROM nginx:mainline +COPY . /usr/share/nginx/html
\ No newline at end of file diff --git a/web/index.html b/web/index.html new file mode 100644 index 0000000..78d52a6 --- /dev/null +++ b/web/index.html @@ -0,0 +1,102 @@ +<!DOCTYPE html> +<html> + +<head> + <meta charset="utf-8" name="viewport" content="width=80ch"> + <title>mosquitto</title> + <link rel="stylesheet" href="style.css"> +</head> + +<body> + +<h1>mosquitto.chem.wisc.edu</h1> + +<hr> + +<p> +This server hosts an <a href="https://en.wikipedia.org/wiki/MQTT">MQTT</a> broker for "internet of things" applications within the <a href="https://chem.wisc.edu">Department of Chemistry</a> at the <a href="https://wisc.edu">University of Wisconsin-Madison</a>. <br> +In particular, this server hosts: +</p> + +<ul> + <li><a href="https://mosquitto.org/">mosquitto</a>, an MQTT broker</li> + <li><a href="https://www.influxdata.com/">influxdb</a>, a time series database</li> +</ul> + +<p> +This server is purposefully isolated to the campus network. <br> +If you are reading this, congrats---you're on the network. +</p> + +<p> +This server is maintained by Blaise Thompson and Alan Silver. <br> +Please contact them with any questions or concerns. +</p> + +<h2>mosquitto</h2> + +<p> +This server runs <a href="https://mosquitto.org/">mosquitto</a>, an MQTT broker. +Briefly, MQTT is a lightweight publish-subscribe protocol that allows many devices to communicate. +The broker provides structured topics, e.g. "sensor42/temperature" and "instrument9/opa2/wavelength". +Clients can publish topic updates. +Other clients can subscribe to be notified each time a topic is updated. +Refer to <a href="https://mqtt.org">mqtt.org</a> for more information. +</p> + +<p> +Anyone on the campus network can subscribe to the topics served here. +There are lots of great MQTT clients you can use. +We recommend: +</p> + +<ul> + <li><a href="https://mosquitto.org/man/mosquitto_sub-1.html">mosquitto_sub</a> (command line interface)</li> + <li><a href="https://github.com/eerimoq/mqttools">mqttools</a> (python)</li> + <li><a href="https://www.eclipse.org/paho/">paho</a> (many languages)</a></li> +</ul> + +<p> +You'll need a password to publish to this broker. +Talk to Blaise Thompson or Alan Silver if you want to start publishing. +</p> + +<h2>influxdb</h2> + +<p> +The Chemistry Department has standardized on the <a href="https://homieiot.github.io/">homie convention</a>. +This convention imposes a well-defined structure on top of the basic MQTT communication layer. +This structure introduces a "device" topology. + +<p> +This server stores all homie device history to an <a href="https://www.influxdata.com/">influx</a> time-series database. +Anyone on the campus network can query this database. +Importantly, only homie-convention compliant MQTT topics are stored---other published topics are not recorded in any permanent way. +</p> + +<p> +While we make every attempt to prevent data loss, please do not treat this database as an archive. +You are responsible for saving data that you care about in a more permanent way. +Device history older than two years will automatically be deleted. +</p> + +<p> +EXAMPLE CURL +</p> + +<p> +EXAMPLE PYTHON +</p> + +<p> +RECOMMEND GRAFANA +</p> + +<hr> + +<p> +<a href="https://creativecommons.org/publicdomain/zero/1.0/">CC0</a>: no copyright +</p> + +</body> +</html> diff --git a/web/style.css b/web/style.css new file mode 100644 index 0000000..4e33ceb --- /dev/null +++ b/web/style.css @@ -0,0 +1,18 @@ +a { + text-decoration: none; + color: #4271ae; +} + +body { + font-family: 'DejaVu Sans Mono', monospace; + margin: 40px auto; + min-width: 80ch; + max-width: 80ch; + line-height: 1.5; + font-size: 16px; + background-color: #ffffff; + color: #4d4d4c; + padding: 0 10px; + text-align: left; + overflow-y: scroll; +} diff --git a/write-influx.service b/write-influx.service deleted file mode 100644 index aaed2b6..0000000 --- a/write-influx.service +++ /dev/null @@ -1,12 +0,0 @@ - -[Unit] -Description=write-influx - -[Service] -Type=simple -User=blaise -ExecStart=<path-to-python-file> -ExecReload=/bin/kill -HUP $MAINPID - -[Install] -WantedBy=multi-user.target diff --git a/write-influx/dockerfile b/write-influx/dockerfile new file mode 100644 index 0000000..fa509f8 --- /dev/null +++ b/write-influx/dockerfile @@ -0,0 +1,4 @@ +FROM python:3.8 +COPY . ./ +RUN pip install --no-cache-dir -r requirements.txt +CMD ["python", "-u", "write_influx.py"] diff --git a/write-influx/requirements.txt b/write-influx/requirements.txt new file mode 100644 index 0000000..84d08b3 --- /dev/null +++ b/write-influx/requirements.txt @@ -0,0 +1,2 @@ +influxdb +paho-mqtt diff --git a/write_influx.py b/write-influx/write_influx.py index 240b10b..17b4ce7 100644 --- a/write_influx.py +++ b/write-influx/write_influx.py @@ -53,15 +53,14 @@ def on_message(client, userdata, msg): measurement = topics[3] tags["node"] = topics[2] datatype = homie[topics[1]][topics[2]][topics[3]]["$datatype"]["__value__"] - print(datatype, datatype=="float") if datatype == "float": fields = {"value": float(payload)} else: - raise KeyError + return write_point(measurement, tags, fields) -influx_client = InfluxDBClient('localhost', 8086, 'root', 'root', "homie") +influx_client = InfluxDBClient("db", 8086, 'root', 'root', "homie") influx_client.create_database("homie") @@ -82,7 +81,7 @@ client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message -client.connect("mosquitto.chem.wisc.edu", 1883, 60) +client.connect("broker", 1883, 60) # Blocking call that processes network traffic, dispatches callbacks and # handles reconnecting. |