Created
July 24, 2018 20:26
-
-
Save awade/c9b3f5be4fcd495f57c679db0ffed2dc to your computer and use it in GitHub Desktop.
Script for updating data streem of LIGOX coffee machine to adafruit IO. Tokens have been blanked out.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Adafruit_IO import Client | |
import Adafruit_ADS1x15 | |
import requests | |
import numpy as np | |
import time | |
# setup client with token issued by adafruit IO | |
aio = Client(‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’) | |
# Create an ADS1115 ADC (16-bit) instance. | |
adc = Adafruit_ADS1x15.ADS1115() | |
# Choose gains 2^n where n=0,1,2,3,4 | |
GAIN_strain = 8 | |
GAIN_temp = 1 | |
# Set update time step | |
timeStep = 5.0 | |
triggerLevel = 50.0 # Set temp at which a tweet will be sent to adoring fans | |
def triggerIFTTT(triggerName, token): | |
''' Function for triggering events on IFTTT.com | |
Users must provide a name of the event and a token. | |
''' | |
# Make sure strings are used as input | |
assert isinstance(triggerName, str), "var triggerName should be a string" | |
assert isinstance(token, str), "var token should be a string" | |
# Generate url to trigger event | |
urlToReqt = "https://maker.ifttt.com/trigger/" + \ | |
triggerName + "/with/key/" + token | |
# Make request that will trigger IFTTT.com | |
requests.post(urlToReqt) | |
triggerCounter = 0 # Trigger counter, when goes over a value IFTTT is triggered | |
while True: # Loop indefinitely until stopped | |
timeNow = time.time() | |
strain_count = adc.read_adc_difference(3, gain=GAIN_strain) | |
temperature_count = adc.read_adc(1, gain=GAIN_temp) | |
# Convert to volts | |
strain_volt = strain_count * (4.096 / GAIN_strain) / (2.0**16 / 2) | |
temperature_volt = temperature_count * (4.096 / GAIN_temp) / (2**16 / 2) | |
strain_cal = -strain_volt * 2000. * 17.75 - 0 | |
temperature_cal = 100. * temperature_volt - 50. | |
print('{0},{1},{2}'.format(timeNow, strain_cal, temperature_cal)) | |
# publish to the feed on io.adafruit.com | |
aio.send('tempSens', temperature_cal) | |
aio.send('strainSens_volt', strain_volt) | |
aio.send('strainSens_cal', strain_cal) | |
# Now check if over threshold and count to 10 before trig'ing tweet + email | |
if temperature_cal >= triggerLevel: | |
triggerCounter = triggerCounter + 1 | |
else: | |
triggerCounter = 0 | |
if triggerCounter == 10: | |
triggerIFTTT("coffee_trigger", “yyyyyyyyyyyyyyyyyyyyyyyyyyyyy”) | |
print("Trigger send to IFTTT") | |
time.sleep(timeStep) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment