Delegate a bunch of logic to the ESP

This commit is contained in:
Jan Tuomi
2017-09-24 13:16:01 +03:00
parent 1c4be69964
commit 8b98dcf1ce
2 changed files with 29 additions and 75 deletions
+25 -52
View File
@@ -3,74 +3,47 @@ import logging
import datetime
from collections import deque
MQTT_HOST = "mqtt.sik.party"
MQTT_HOST = 'mqtt.sik.party'
TOPIC_TEMP = "sik/kiltahuone/kahvivaaka/temperature"
TOPIC_WEIGHT = "sik/kiltahuone/kahvivaaka/weight"
TOPIC_BREW_TIME = 'sik/kiltahuone/kahvivaaka/brewtime'
TOPIC_WEIGHT = 'sik/kiltahuone/kahvivaaka/weight'
TOPIC_BREWING = 'sik/kiltahuone/kahvivaaka/brewing'
TOPIC_CUPS = 'sik/kiltahuone/kahvivaaka/cups'
BREWING_DIFFERENTIAL_MIN = 60
# setting down the pan creates at least 800 g of weight
BREWING_DIFFERENTIAL_MAX = 180
WEIGHT_DEQUE_LENGTH = 20
weight_deque = deque(maxlen=WEIGHT_DEQUE_LENGTH)
latest = {
'last_brew': datetime.datetime.now()
}
def calc_averaged_weight():
if len(weight_deque) > 2:
half = []
for i in range(0, int(len(weight_deque) / 2)):
half.append(weight_deque[i])
return sum(half) / len(half)
else:
return 0
latest = {}
def on_connect(client, userdata, flags, rc):
logging.info('Subscribing to all topics on mqtt.sik.party.')
client.subscribe("#")
logging.info('Connected successfully to MQTT.')
logging.info('Subscribing to all topics on {}.'.format(MQTT_HOST))
client.subscribe('sik/kiltahuone/kahvivaaka/#')
def on_message(client, userdata, msg):
if msg.topic == TOPIC_TEMP:
latest['temp'] = float(msg.payload.decode('utf-8'))
elif msg.topic == TOPIC_WEIGHT:
weight = float(msg.payload.decode('utf-8'))
# avoid showing erroneous data from an empty scale
if weight > 50: # g
weight_deque.appendleft(weight)
payload = msg.payload.decode('utf-8')
if msg.topic == TOPIC_WEIGHT:
weight = float(payload)
latest['weight'] = weight
elif msg.topic == TOPIC_CUPS:
cups = float(payload)
latest['cups'] = cups
elif msg.topic == TOPIC_BREWING:
brewing = bool(int(payload))
latest['brewing'] = brewing
elif msg.topic == TOPIC_BREW_TIME:
brew_time = datetime.datetime.fromtimestamp(float(payload))
latest['brew_time'] = brew_time
def on_disconnect(client, userdata, rc):
if rc != 0:
logging.warning("MQTT unexpectedly disconnected.")
logging.warning('MQTT unexpectedly disconnected.')
else:
client.loop_stop(force=False)
logging.warning("MQTT disconnected.")
logging.warning('MQTT disconnected.')
def get_latest():
if len(weight_deque) > 2:
first = weight_deque[0]
last = weight_deque[len(weight_deque) - 1]
diff = first - last # reverse order
if len(weight_deque) < WEIGHT_DEQUE_LENGTH:
brewing = False
else:
if BREWING_DIFFERENTIAL_MIN < diff < BREWING_DIFFERENTIAL_MAX:
brewing = True
latest['last_brew'] = datetime.datetime.now()
else:
brewing = False
latest['brewing'] = brewing
latest['weight'] = calc_averaged_weight()
return latest
@@ -82,8 +55,8 @@ client.on_disconnect = on_disconnect
try:
logging.info('Connecting to MQTT at {}...'.format(MQTT_HOST))
logging.info('If there is no confirmation, the MQTT connection has probably failed.')
client.connect_async(MQTT_HOST, 1883, 60)
logging.info('Connected successfully to MQTT.')
except Exception as ex:
logging.error(ex)
logging.error('Failed to connect to MQTT at {}'.format(MQTT_HOST))
+4 -23
View File
@@ -9,24 +9,6 @@ import logging
from django.conf import settings
def get_cups_from_weight(weight):
if not weight:
return 0
EMPTY = 820 # grams
FULL = 2000 # grams
cups = 10 * (weight - EMPTY) / (FULL - EMPTY)
cups = round(cups, 1)
if cups < 0.5:
cups = 0
if cups > 10:
cups = 10
# logging.debug("Coffee cups: {}, weight: {}".format(cups, weight))
return cups
def coffee_view(request):
logging.info('User navigated to coffee page!')
return render(request, 'coffee.html')
@@ -35,12 +17,11 @@ def coffee_view(request):
def cups_view(request):
now = datetime.datetime.now()
latest = get_latest()
cups = get_cups_from_weight(latest.get('weight'))
data = {
'date': now,
'cups': cups,
'temp': latest.get('temp'),
'last_brew': latest.get('last_brew'),
'brewing': latest.get('brewing')
'cups': latest.get('cups'),
'last_brew': latest.get('brew_time'),
'brewing': latest.get('brewing'),
'weight': latest.get('weight')
}
return JsonResponse(data)