Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # vim: ts=4 sw=4:expandtab:ai
- DEBUG = True
- #credentials for your spark core
- spark_api="https://api.spark.io"
- access_token="aaabbbcccdddeeefff1112223334445556667778"
- device_id="123456789abcdef123456789"
- # The MARKER_FILE exists when the Spark is unreachable
- MARKER_FILE='spark_timeout.marker'
- # Logs succesful connections and data value from Spark (which must be running Tinker)
- OUTPUT_FILE='spark.log'
- import requests
- import json
- import datetime
- import os
- fOutput = open (OUTPUT_FILE, 'a')
- # ---------------------------
- # Call the tinker "analogread" function on A0
- # ---------------------------
- payload = {'access_token': access_token}
- function = "analogread"
- arguments = {'params': 'A0'}
- response = requests.post(spark_api + '/v1/devices/' + device_id + '/' + function , params=payload, data=arguments)
- if (DEBUG):
- # Show the URL
- print (response.url)
- print (response.text)
- print (response.json())
- if 'return_value' in response.json ().keys (): # I got a valid 'return_value' from my call
- # Remove the MARKER_FILE if it exists - i.e. start logging data again
- if os.path.isfile(MARKER_FILE):
- os.remove (MARKER_FILE)
- fOutput.write ("\n--- Spark came back online --- \n")
- avalue = response.json()['return_value']
- print ("A0 = {}".format (avalue) )
- fOutput.write ("{}, {} \n".format (datetime.datetime.now().strftime("%s"), avalue) )
- else:
- if not os.path.isfile(MARKER_FILE): # i.e. This is the first failure, so log and create MARKER_FILE
- print ("Response code: {}:{} \n".format (response.status_code, response.text) )
- print ("JSON return_value: {} \n".format (response.json()['error']) )
- fOutput.write ("{}, Response Code: {} \n".format (datetime.datetime.now().strftime("%s"), response.status_code) )
- fOutput.write ("{}, error: {} \n".format (datetime.datetime.now().strftime("%s"), response.json()['error']) )
- # Create a marker file indicating I've timed out
- fError = open (MARKER_FILE, 'w')
- fError.close ()
- fOutput.close ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement