Experimenting with tulip.co when stuck at home

One thing I wanted to experiment with was pick to light. Since I didn’t happen to have this sitting on my desk at home

I wanted some little wireless device I could play with, turning lights on and off. Enter in these little wireless tags. http://wirelesstag.net/

Wireless Sensor Tag (with Temperature)

Someone had so kindly written a Simple python wrapper over the wirelesstags REST API

https://pypi.org/project/wirelesstagpy/

But, it didn’t have support for turning the lights on and off.

I added a few lines of code to enable turning the lights on and off.

The easiest way (and fastest response) I found was to create an Amazon API Gateway.

First things first create your lambda, I called it SetTagLight

Choose the upload .zip (so you can import wirelesstagpy)

The code proceeds as follows –

get the tag uuid and light_state that will be passed as query strings to the API. Get the email address and password from Parameter store. If the light state is a 1, turn the light on, otherwise turn it off.

import wirelesstagpy
import boto3

def lambda_handler(event, context):

	# get which wireless tag to work with
	tag_uuid = event['params']['querystring']['tag_uuid']
    # get whether to turn the light on or off
	# 0 off 1 on
	light_state = event['params']['querystring']['set_light_state']

	#tag_uuid = '1'
	#light_state = '0'
	# get user name and password from parameter store
	ssm = boto3.client('ssm')
	email_address = ssm.get_parameter(Name='tag_email_address', WithDecryption=True)
	password = ssm.get_parameter(Name='tag_password', WithDecryption=True)

	# log into wireless tag API
	api = wirelesstagpy.WirelessTags(username=email_address['Parameter']['Value'], password=password['Parameter']['Value'])
	
	if light_state == '1':
		sensor = api.light_on(tag_uuid)
	
	else:
		sensor = api.light_off(tag_uuid)
	
	message = 'Done'
	
	return {
        'statusCode': 200,
        'body': message
    }

Next, we want to utilize this lambda from API gateway.

So, create an API

Create a resource, and a POST method, and choose the lambda function you created above

Under integration request, set a mapping template for application/json and generate the method request passthrough (this passes the query strings through)

To test, click the test button

Then type tag_uuid=1&set_light_state=1

Then hit test. It threw an error for me because the lambda didn’t have access to SSM, so find the lambda role and add permissions to allow it to read the SSM

Ok, now when you test it, it should succeed and the light should turn on!

Ok, let’s access this API from tulip

Ok, deploy the API.

Head over to tulip and create a connector

Click here to configure it (this is a little unintuitive)

Get the invoke URL from the published API, use this in the connector but strip off everything before and after the URL

Next click create function

Paste the rest of your invoke URL to the end at the top of the function, like this

Next, we want to pass in two parameters: the light uuid and the on/off state.

Add those as inputs (and give them some default values)

Then on the right side add them as Query Parameters. The trick here is to wrap a dollar sign around the input you want to reference, i.e. $tag_uuid$ when you do so you will notice it changes into a black box with white font.

Ok, if you press ‘Test’, it should light up your light!

How do we use this in Tulip? Pretty easily.

Create a new app, create a button (or favorite trigger of choice)

Call the connector passing it the ID and state that you want the wireless tag to be in, bingo.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s