def update_adafruitio(weather_forecast): aio_username = os.environ['AIO_USERNAME'] aio_api_key = os.environ['AIO_API_KEY'] aio_feeds = [ 'weather-station.darksky-minutely', 'weather-station.darksky-hourly' ] # Setting up the aio connection aio = Client(aio_username, aio_api_key) # Retrieving a list of all forecast data values and deleting them as we don't need that data to persist for feed in aio_feeds: d = aio.data(feed) for data in d: aio.delete(feed, data.id) # Now send the forecasts to Adafruit for i in range(0, 2): # 0 == Minutely, 1 == Hourly aio.send_data(aio_feeds[i], weather_forecast[i])
class AFIOClient: def __init__(self, aio_username, aio_api_key): self.client = Client(aio_username, aio_api_key) self.mqttClient = MQTTClient(aio_username, aio_api_key) def pubsub_feed_listen(self, onConnect, onMessage, onDisconnect): # Setup the callback functions defined above. self.mqttClient.on_connect = onConnect self.mqttClient.on_disconnect = onDisconnect self.mqttClient.on_message = onMessage self.mqttClient.connect() self.mqttClient.loop_blocking() def publish_status(self, feed_key, status): self.client.send_data(feed_key, status) # Listen in the background -- don't block the main loop def listen_background(self): self.mqttClient.loop_background()
def hello_pubsub(event, context): """Triggered from a message on a Cloud Pub/Sub topic. Args: event (dict): Event payload. context (google.cloud.functions.Context): Metadata for the event. """ print("Execution Starting") pubsub_message = json.loads(base64.b64decode(event['data']).decode('utf-8')) time_elapsed = 240 limit = float(120) print("Checking Time limit") print(time_elapsed) if time_elapsed > limit: print("Limit exceeded, connecting to twillio client") prob_list = pubsub_message['probs'] appliances = ["treadmill", "washing machine", "dishwasher", "microwave", "microwave", "kettle", " ", " "] active_appliances = [] print("Checking probabilities of appliances") for prob in prob_list: if prob > 0.2: index = prob_list.index(prob) device_name = appliances[index] active_appliances.append(device_name) print("Appliance {} detected".format(device_name)) if active_appliances: # Create an instance of the REST client. aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) devices = aio.feeds('devices') # aio.send_data(devices.key, active_appliances.index(appliance)) # for appliance in active_appliances: # aio.send_data(devices.key, appliance) # print("Sending data to adafruit feed") # print("Data Sent") str = " " data = str.join(active_appliances) aio.send_data(devices.key, data) print("Sent")
# We dont' have a GPS hooked up, but let's fake it for the example/test: # (replace this data with values from a GPS hardware module) value = 0 lat = 40.726190 lon = -74.005334 ele = 6 # elevation above sea level (meters) while True: print('\nSending Values to location feed...\n') print('\tValue: ', value) print('\tLat: ', lat) print('\tLon: ', lon) print('\tEle: ', ele) # Send location data to Adafruit IO metadata = { 'lat':lat, 'lon':lon, 'ele':ele, 'created_at':time.asctime(time.gmtime()) } aio.send_data(location.key,value,metadata) # shift all values (for test/demo purposes) value += 1 lat -= 0.01 lon += -0.02 ele += 1 # Read the location data back from IO print('\nData Received by Adafruit IO Feed:\n') data = aio.receive(location.key) print('\tValue: {0}\n\tLat: {1}\n\tLon: {2}\n\tEle: {3}' .format(data.value, data.lat, data.lon, data.ele)) # wait loop_delay seconds to avoid api throttle time.sleep(loop_delay)
# Import Adafruit IO REST client. from Adafruit_IO import Client, Feed # holds the count for the feed run_count = 0 # Set to your Adafruit IO key. # Remember, your key is a secret, # so make sure not to publish it when you publish this code! ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY' # Set to your Adafruit IO username. # (go to https://accounts.adafruit.com to find your username) ADAFRUIT_IO_USERNAME = '******' # Create an instance of the REST client. aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) # Create a new feed named 'counter' feed = Feed(name="Counter") response = aio.create_feed(feed) while True: print('sending count: ', run_count) run_count += 1 aio.send_data('counter', run_count) # Adafruit IO is rate-limited for publishing # so we'll need a delay for calls to aio.send_data() time.sleep(3)