Skip to content

Instantly share code, notes, and snippets.

@catarak
Last active March 6, 2016 17:46
Show Gist options
  • Save catarak/45c13a58df2d34d9cb19 to your computer and use it in GitHub Desktop.
Save catarak/45c13a58df2d34d9cb19 to your computer and use it in GitHub Desktop.
Partial Server-Side code for water level meter
from flask import Flask, jsonify
import threading
import time
import requests
import json
app = Flask(__name__)
max_num_entries = 25
current_entry = 0
#this array will store the last 25 values of the water meter
#initialize to length 25
water_level_entries = [None] * max_num_entries
#if you run this and then go to http://localhost:5000/waterlevel, it will show you the value of water_level_entries
@app.route("/waterlevel")
def return_water_level():
#this returns your dictionary as JSON, which is a standard data format for HTTP.
return json.dumps(water_level_entries)
def record_water_level():
global current_entry
global water_level_entries
#TODO fetch water meter value from particle url and parse value
#let's say it will look something like this
entry = {'value': 0.1234, 'timestamp': 'Sun Mar 06 2016 12:13:42 GMT-0500 (EST)'}
#save the current value
water_level_entries[current_entry] = entry
#move the current index to store the next value
current_entry = current_entry + 1
#we only want to store the last 25 entries, so reset current_index to overrite last value
if current_entry >= max_num_entries:
current_entry = 0
#make the timer value whatever, you want, this would fetch the level once a minute
threading.Timer(60, record_water_level).start()
record_water_level()
if __name__ == "__main__":
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment