Create a gist now

Instantly share code, notes, and snippets.

@RChloe /wioMotion.py Secret
Created Dec 4, 2015

What would you like to do?
# Import libraries for parsing the JSON output by the Wio Link
# Also import the Initial State Python library so we can stream
# the Wio Link readings to a visualization
import urllib2
import json
import os
import glob
import time
from ISStreamer.Streamer import Streamer
#### Wio Link Information ####
# The access token for your Wio Link
WIO_ACCESS_TOKEN = "Wio_API_Access_Token_Here"
# The name of the attached sensor
SENSOR_NAME = "GroveIRDistanceInterrupterDigital2"
# The name of the signal the sensor produces
SIGNAL_NAME = ["approach"]
### Initial State Information ###
# The name your bucket will appear with in Initial State
BUCKET_NAME = ":seedling: Wio Python"
# The hidden bucket key that associates the data w/a particular bucket
BUCKET_KEY = "wiolink"
# Initial State access key - found under "Account"
IS_ACCESS_KEY = "Initial_State_Access_Key_Here"
# Time between sensor readings
MINUTES_BETWEEN_READS = 5
# Function to fetch the JSON from a particular Wio API URL
def get_reading(sensor,signal):
api_reading_url = "https://iot.seeed.cc/v1/node/" + sensor + "/" + signal + "?access_token=" + WIO_ACCESS_TOKEN
print api_reading_url
try:
f = urllib2.urlopen(api_reading_url)
except:
print "Failed to get " + signal
return False
json_reading = f.read()
f.close()
return json.loads(json_reading)
# Initialize the Intial State streamer
streamer = Streamer(bucket_name=BUCKET_NAME, bucket_key=BUCKET_KEY, access_key=IS_ACCESS_KEY)
# Loop through reading and streaming signal's value
while True:
try:
# Read motion value from IR Distance Interrupter
motionReading = get_reading(SENSOR_NAME,SIGNAL_NAME[0])
if (motionReading != False):
if (motionReading['status'] != 205):
motion = motionReading['msg']['approach']
if motion == 0:
print "No motion"
streamer.log("Motion?","No motion")
if motion == 1:
print "Motion detected"
streamer.log("Motion?","Motion detected")
else:
message = motionReading['msg']
print message
streamer.log("Error",str(message))
# Wait designated time between readings
time.sleep(60*MINUTES_BETWEEN_READS)
except TypeError:
print "Type Error"
pass

The return json format have changed. Maybe need fix the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment