Skip to content

Commit

Permalink
Merge pull request #46 from pimoroni/feature/influxdb-destination
Browse files Browse the repository at this point in the history
added support for influxdb
  • Loading branch information
lowfatcode authored Sep 2, 2022
2 parents 88c1692 + 881ac55 commit 1692755
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 9 deletions.
8 changes: 7 additions & 1 deletion enviro/config_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@

# adafruit ui settings
adafruit_io_username = None
adafruit_io_key = None
adafruit_io_key = None

# influxdb settings
influxdb_org = None
influxdb_url = None
influxdb_token = None
influxdb_bucket = None
53 changes: 53 additions & 0 deletions enviro/destinations/influxdb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from enviro.helpers import get_config
import urequests, time

def url_encode(t):
result = ""
for c in t:
# no encoding needed for character
if c.isalpha() or c.isdigit() or c in ["-", "_", "."]:
result += c
elif c == " ":
result += "+"
else:
result += f"%{ord(c):02X}"
return result

def upload_reading(reading):
bucket = get_config("influxdb_bucket")

payload = ""
for key, value in reading["readings"].items():
if payload != "":
payload += "\n"
timestamp = reading["timestamp"]

year = int(timestamp[0:4])
month = int(timestamp[5:7])
day = int(timestamp[8:10])
hour = int(timestamp[11:13])
minute = int(timestamp[14:16])
second = int(timestamp[17:19])
timestamp = time.mktime((year, month, day, hour, minute, second, 0, 0))

nickname = reading["nickname"]
payload += f"{key},device={nickname} value={value}"

influxdb_token = get_config("influxdb_token")
headers = {
"Authorization": f"Token {influxdb_token}"
}

url = get_config("influxdb_url")
org = get_config("influxdb_org")
url += f"/api/v2/write?precision=s&org={url_encode(org)}&bucket={url_encode(bucket)}"

try:
# post reading data to http endpoint
result = urequests.post(url, headers=headers, data=payload)
result.close()
return result.status_code == 204 # why 204? we'll never know...
except:
pass

return False
2 changes: 1 addition & 1 deletion enviro/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def get_values_from_file(filename, key = None, default = None):
with open(filename, "r") as infile:
result = {}
for line in infile.read().split("\n"):
parts = line.split("=")
parts = line.split("=", 1)
if len(parts) > 1:
read_key = parts[0].strip()
read_value = eval(parts[1].strip())
Expand Down
38 changes: 31 additions & 7 deletions enviro/html/provision-step-4-destination.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ <h2>Where are you uploading your data to?</h2>
A custom HTTP endpoint
<p class="note">We'll make a request to your supplied URL with all of the data included.</p>
</li>
<li data-destination="influx_db">
InfluxDB &ndash; coming soon!
<li data-destination="influxdb">
InfluxDB
<p class="note">The Time Series Data Platform where developers build IoT, analytics, and cloud applications.</p>
</li>
<li data-destination="mqtt">
Expand Down Expand Up @@ -50,6 +50,32 @@ <h2>Enter your username and password.</h2>
</fieldset>
</div>

<div id="destination_influxdb">
<h2>Enter your InfluxDB org.</h2>
<aside>This is usually your e-mail address.</aside>
<fieldset>
<input type="text" name="influxdb_org" value="{{influxdb_org}}" placeholder="e.g. geoff@datalover.com" autocapitalize="none" autocorrect="off" autocomplete="off" spellcheck="false" />
</fieldset>
<br>
<h2>Enter your InfluxDB URL.</h2>
<aside>You can find this under Organisation -> Settings in the InfluxDB dashboard.</aside>
<fieldset>
<input type="text" name="influxdb_url" value="{{influxdb_url}}" placeholder="e.g. https://eu-central-1-1.aws.cloud2.influxdata.com" autocapitalize="none" autocorrect="off" autocomplete="off" spellcheck="false" />
</fieldset>
<br>
<h2>Enter your InfluxDB API Token.</h2>
<aside>You can create this under Load Data -> API Tokens in the InfluxDB dashboard.</aside>
<fieldset>
<input type="text" name="influxdb_token" value="{{influxdb_token}}" placeholder="It will be a long sequence of characters ending in '=='" autocapitalize="none" autocorrect="off" autocomplete="off" spellcheck="false" />
</fieldset>
<br>
<h2>Enter your InfluxDB Bucket.</h2>
<aside>The name of the bucket you want to add your Enviro data to (must be created first).</aside>
<fieldset>
<input type="text" name="influxdb_bucket" value="{{influxdb_bucket}}" placeholder="e.g. enviro" autocapitalize="none" autocorrect="off" autocomplete="off" spellcheck="false" />
</fieldset>
</div>

<div id="destination_adafruit_io">
<h2>Enter your Adafruit IO username.</h2>
<aside>You will have created this when setting up your Adafruit IO account.</aside>
Expand Down Expand Up @@ -104,11 +130,9 @@ <h2>HTTP authentication credentials (optional).</h2>
}

document.querySelectorAll("#destinations >li").forEach((listItem) => {
if(listItem.dataset.destination != "influx_db") {
listItem.addEventListener("click", function(e) {
selectDestination(listItem.dataset.destination)
})
}
listItem.addEventListener("click", function(e) {
selectDestination(listItem.dataset.destination)
})
})

selectDestination("{{destination}}")
Expand Down
4 changes: 4 additions & 0 deletions enviro/provisioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ def provision_step_4_destination(request):
"mqtt_broker_password",
"adafruit_io_username",
"adafruit_io_key"
"influxdb_org",
"influxdb_url",
"influxdb_token",
"influxdb_bucket"
],
request.form
)
Expand Down

0 comments on commit 1692755

Please sign in to comment.