Reading cloudBit state in IFTTT

I have a project in mind where I don’t want to send or receive a discrete event from the cloudBit; I simply want to read the state of its input.

As far as I can tell, the way to do this is via the cloudBit API with an HTTP query. It can be done using, say, cURL, or a web page if you want to craft one. Am I understanding correctly so far?

Here’s the rub: I want to connect this to If This Then That, so that when an event on a IFTTT channel triggers (say, my Android device disconnects from my work WiFi), it runs a script that queries the state of the cloudBit input and then performs an action based on that state (e.g. sends me a text if the cloudBit is receiving input).

Right now I don’t see a way to do this. The cloudBit channel in IFTTT only receives or sends a discrete event; there is no query. I also don’t see a way in IFTTT to run an arbitrary script somewhere (cURL, python, etc.) that would could query the state with the cloudBit API and then do what I want–maybe I’m missing that.

Any thoughts on how to make something like this work?

The cloud bit only makes outgoing http(s) requests. So there is nothing that you can connect to. The internal http server is only available while it is being setup.

I found this on github. Lesson 2 says “Read from cloudBit”, so I’m confused. I mean, the cloudcontrol page receives and shows the cloudBit input, so there must be some way to get it via script.

I haven’t worked in this kind of development environment before, so I’m also not sure how the heroku app site figures into it either.

Maybe outlining my intended usage would help. I have a drawer I want to check whether it’s open or closed, so I use the roller switch as input to the cloudBit. At some point in time later, I want to check the state coming in to the cloudBit to remotely check if the drawer is open or closed.

Can something like this be done with the cloudBit API? The github page seems to imply that it can.

I have been playing with the cloudbit by patching so the sshd is enabled (You can use the solder pads to get console access) I mounted the MicroSD on a linux machine and patched it that way. There are test programs that can be used to read the input/ write the output and change the LED color (not sure why they call cyan - teal). These take about 2 - 5 seconds to run which seems slow. So I assume that the cloud bit polls the littlebits cloud every few seconds.

@jeffriv for your first question:

it runs a script that queries the state of the cloudBit input and then performs an action based on that state (e.g. sends me a text if the cloudBit is receiving input).

I believe you need to setup two recipes, possibly. You can’t pipe triggers yet, like sending one trigger to another, but you can probably chain two recipes, not 100% sure since it’s more of an iffft thing.

But for your second question :

I have a drawer I want to check whether it’s open or closed, so I use the roller switch as input to the cloudBit. At some point in time later, I want to check the state coming in to the cloudBit to remotely check if the drawer is open or closed.

You can totally do this with an iffft trigger on their site. Here’s a screenshot:


and some recipes here

Though I believe the new iffft mobile app only does a do at the moment.

@codewizard58 You can totally get input from our v3 api endpoint, our undocumented v3 API can stream data over this end point:

curl -i -XGET -H "Authorization: Bearer [token]" https://api-http.littlebitscloud.cc/v3/devices/[device_id]/input

checkout lesson 2: https://github.com/littlebits/cloud-api-lessons

And about the input interval:

These take about 2 - 5 seconds to run which seems slow.

It’s delayed by 750ms. And can be sped up to 200ms via an undocumented feature that we’re planning on releasing really soon. Basically after we’ve banged on it internally.

Hope this helps!

  • Syed // littleBits

2 Likes

Oh and btw i’m writing a new driver foriffft, which will unlock some really neat features. Hoping to deploy it in the near future!

3 Likes

That looks great. I will have to spend some time learning this development environment but it looks promising. Here’s my “big picture” for the project I have in mind:

I have a switch detecting something (e.g. drawer is open, or key is not hanging from a hook) that is hooked up to the cloudBit. When I leave the building, my phone disconnects from the work WiFi. That is a IFTTT trigger that fires off a script that checks to see the state of the switch, via the cloudBit. Given that state, the script uses IFTTT to send me a text (“Hey, dummy, you left the drawer open!”) so I’m notified before I actually drive off.

I’m still not quite sure how the IFTTT trigger will work since it’s based on a discrete event. In my scenario, the drawer may have been open all day, or been opened and closed multiple times, so the switch “triggered” long ago or multiple times during the day. I don’t see a way to make this work unless you can get IFTTT to have a channel for some site that hosts arbitrary scripts.

So I think this is really a IFTTT issue as much as a cloudBit API question at this point.

Hi @jeffriv, I’m looking to create a switch that detects when the drawer is left open. What trigger bit are you using for this? Appreciate the advice!

The roller switch seems to be the best option. It was easy to place it (using tape for trial–you could use a mounting foot for something more permanent) just inside the drawer in a spot where the switch would close contact when the drawer did. It has the advantage of being a discrete state (e.g. 1 if the drawer is open or 0 if closed) rather than a “momentary” contact like the button.

I did try the pressure sensor for a door-closed sensor for another project, and it worked but took some tinkering (and a heavy door!).

Another choice, depending on your application, might be the bend sensor, although I don’t have one of those to try out.

The disadvantage of those two is the “continuous” output, which would change depending on the pressure or bend, so if you used one of those you might need to add a threshold bit to do what you want.

I would use a regular magnetic switch that is used for security systems. A proto bit can easily be used to connect it to the cloudbit. Using a regular switch and as much wire as you need makes this sort of project something that works well and lets you put the cloud bit somewhere convenient. To wire, remove the center jumper from the protobit to seperate the input and output. Connect one wire from the switch to 0 Volt and the other to the center screw terminal on the ‘output’ side. A resistor of around 1k ohm Brown, black, Red should be connected from the center screw where the wire is, to the +5 Volt side to provide a ‘pullup’.

The switches can be purchased as normally open (Off) or normally closed. Using the same circuit you can also use PIR detectors to detect motion instead.

1 Like

@jeffriv Is there some sort of timing switch? i.e. if it was left open for more than 2-minute, it would send the alert? Would the delay bit work?

Hello, @jeffriv :smile: Do you have any logic Bits? I’m imagining for you a circuit that has a double AND before the cloudBit. Going into one AND input would be USB power + roller switch ; the other AND input would be a wire looping around from the cloudBit output. With this circuit, the cloudBit would only receive an input if you left the office AND the drawer was open at the same time. What do you think of that?

You might be able to use the timeout bit in off-on mode. When the drawer opens, the switch triggers, but the timeout does not activate the rest of the circuit for two minutes. I’d need that bit to experiment: once the trigger hits the timeout bit, does the timeout continue until it fires, or does the timer reset if the switch is closed again? I am thinking that if you open and close the drawer quickly, the timeout would still trigger even though the door is now closed.

Of course, using the Arduino bit could handle all of that. Your program loop could just poll the switch state and handle the timing itself before triggering the cloudBit.

@JackANDJude, I drew that out but am having trouble visualizing how it would work. How does feeding back the cloudBit output detect the “I left the office” state? If the cloudBit output starts at 0, the AND would never output 1.

(Aside: why is it called “double AND/OR”? AND and OR operators always take two states as input?)

This idea also has me thinking about event-driven vs. state-driven machines. littleBit, and IFTTT, both work primarily on an event-driven basis. But lots of things in the real world are dependent on state, reading it and storing it. Outside of the Arduino, saving state seems difficult to do in this sort of environment.

@nickweinberg, there is a second timer bit, the timeout bit with " period of time (between 1 second and 5 minutes). "

I thought this might meet your need after our discussion this morning.

I was thinking you can make a recipe for when your phone leaves your office. IF Android Location THEN littleBits. The cloudBit only gets an input when your phone leaves the office AND the roller switch is not depressed. So now you can make another recipe IF littleBits THEN SMS text your phone.

Okay, I see. I forgot the cloudBit input and output are, for practical purposes, separated, so it wouldn’t necessarily be a feedback loop. Very clever, thanks!

1 Like

@syedBits, nice to see you on the community call today, and can’t wait for the “secret project”. Not sure if this should get it’s own thread, but… today I wanted to try the cloudBit API out, hoping to have something cool to share in today’s hangout. The goal: turn on the cloudBit with my own web page (basically recreate the cloud control button).

I got cURL installed, and successfully used the command line as above to send & receive to/from the cloudBit. So far so good. Then it bogged down. I made a web page with a simple form that POSTed to the littlebitscloud. That failed due to authorization issues. Okay, maybe I can use php and libcurl to send the request. Well, probably, but you can’t invoke php from a form since it’s server-side.

Long story short, me not being an experienced web app programmer, it’s not easy or obvious how to connect the cloudBit to something like a web page. Maybe you could point me to the best way to go about something like that?

Hi @jeffriv !

That failed due to authorization issues

So you’ll need to set the headers on your form to include your auth token (it’s security token that makes sure that you have permission to post to your bit) which looks like Authorization: Bearer ACCESS_TOKEN in addition to the other headers info required https://github.com/littlebits/cloud-api-lessons

That being said you can’t alter the default headers on a plain old HTML form. What you can do is use jquery http://stackoverflow.com/questions/10093053/add-header-in-ajax-request-with-jquery

Or like you said, some sort of server-side language like nodejs, php, or python I’d suggest totally checking out this link https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/Sending_and_retrieving_form_data

or the MDN in general is an amazing resource: https://developer.mozilla.org/en-US/

Let me know if that helps at all!

Best,
Syed

Thanks for the pointers @syedBits , I will go play! I’ve done lots of straight-up javascript but haven’t used jquery, so I guess it’s time to open up that box.