This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision Last revision Both sides next revision | ||
|
oak:tutorials:particle-variable [2016/03/19 14:40] jwhendy |
oak:tutorials:particle-variable [2016/03/19 21:24] jwhendy [Example: getting the state of a button] |
||
|---|---|---|---|
| Line 17: | Line 17: | ||
| ==== Example: getting the state of a button ==== | ==== Example: getting the state of a button ==== | ||
| - | Here's the circuit we'll use: | + | Here's the circuit we'll use, which reads the button (pulled up to ''Vcc'' through a 10k resistor) on pin 10: |
| + | |||
| + | [[http://digistump.com/wiki/_media/oak/tutorials/button-pullup.png|{{http://digistump.com/wiki/_media/oak/tutorials/button-pullup.png?400}}]] | ||
| - | [fritzing here] | ||
| The code we'll start with is as follows: | The code we'll start with is as follows: | ||
| Line 35: | Line 36: | ||
| | | ||
| // initialize the digital pin as an input | // initialize the digital pin as an input | ||
| - | pinMode(10); | + | pinMode(10, INPUT); |
| // Particle.variable() takes two options: | // Particle.variable() takes two options: | ||
| Line 65: | Line 66: | ||
| [[http://digistump.com/wiki/_media/oak/tutorials/button-led-on.jpg|{{ oak:tutorials:button-led-on.jpg?310 }}]] | [[http://digistump.com/wiki/_media/oak/tutorials/button-led-on.jpg|{{ oak:tutorials:button-led-on.jpg?310 }}]] | ||
| - | This confirms that the variable ''button'' is being read correctly. Next, you'll need your ''device_id'' and ''access_token'' to issue a ''GET'' request via the particle.io API. You can find both of these at [[https://build.particle.io/|build.particle.io]]: | + | This confirms that the variable ''button'' is being read correctly. Next, make sure you have your ''device_id'' and ''access_token'' as described in the [[oak:tutorials:particle-id-token|Particle API wiki entry]]. |
| - | + | ||
| - | [pic of device id] | + | |
| - | [pic of access token] | + | |
| If you have ''curl'' installed, open a command prompt/terminal, and type the following (replacing ''id_here'' and ''token_here'' with the appropriate values). Otherwise, type out the URL somewhere you can copy/paste from, and visit that page in a browser. | If you have ''curl'' installed, open a command prompt/terminal, and type the following (replacing ''id_here'' and ''token_here'' with the appropriate values). Otherwise, type out the URL somewhere you can copy/paste from, and visit that page in a browser. | ||
| <code> | <code> | ||
| - | curl https://api.particle.io/v1/devices/id_here/button?access_token=token_here | + | $ curl https://api.particle.io/v1/devices/id_here/button?access_token=token_here |
| </code> | </code> | ||
| Line 79: | Line 77: | ||
| <code> | <code> | ||
| - | $ curl curl https://api.particle.io/v1/devices/id_here/button?access_token=token_here | + | $ curl https://api.particle.io/v1/devices/id_here/button?access_token=token_here |
| { | { | ||
| "cmd": "VarReturn", | "cmd": "VarReturn", | ||
| Line 97: | Line 95: | ||
| <code> | <code> | ||
| - | $ curl curl https://api.particle.io/v1/devices/id_here/button?access_token=token_here | + | $ curl https://api.particle.io/v1/devices/id_here/button?access_token=token_here |
| { | { | ||
| "cmd": "VarReturn", | "cmd": "VarReturn", | ||
| Line 118: | Line 116: | ||
| From a code standpoint, this example is nearly identical to the above. The difference is that we will be reading the analog signal from a photoresistor. For some great background information, Adafruit has a [[https://learn.adafruit.com/photocells|nice writeup]] on photoresistors. Here's how we'll wire up our circuit: | From a code standpoint, this example is nearly identical to the above. The difference is that we will be reading the analog signal from a photoresistor. For some great background information, Adafruit has a [[https://learn.adafruit.com/photocells|nice writeup]] on photoresistors. Here's how we'll wire up our circuit: | ||
| - | [fritzing] | + | [[http://digistump.com/wiki/_media/oak/tutorials/photocell.png|{{http://digistump.com/wiki/_media/oak/tutorials/photocell.png?400}}]] |
| In pictures: | In pictures: | ||
| - | [pic] | + | [[http://digistump.com/wiki/_media/oak/tutorials/photocell-wiring.jpg|{{http://digistump.com/wiki/_media/oak/tutorials/photocell-wiring.jpg?400}}]] |
| - | **TODO...*** | + | To read the photoresistor, we use this code: |
| + | |||
| + | <code> | ||
| + | // variable to store our reading | ||
| + | int light; | ||
| + | |||
| + | // the setup() loop sets pinModes, starts things like Serial, | ||
| + | // and is run once when the Oak powers on | ||
| + | void setup() | ||
| + | { | ||
| + | |||
| + | // use the on-board LED to illuminate when the button is pressed | ||
| + | pinMode(1, OUTPUT); | ||
| + | |||
| + | // initialize the analog pin as an input | ||
| + | pinMode(A0, INPUT); | ||
| + | |||
| + | // Particle.variable() to access this from the cloud | ||
| + | Particle.variable("light", light); | ||
| + | |||
| + | } | ||
| + | |||
| + | |||
| + | // the loop() routine runs over and over again | ||
| + | void loop() | ||
| + | { | ||
| + | |||
| + | // take a reading | ||
| + | light = analogRead(A0); | ||
| + | |||
| + | // if the photocell is covered (dark, resistance is low) | ||
| + | // turn the on-board LED light on. Otherwise, turn it off. | ||
| + | if(light < 250) { digitalWrite(1, HIGH); } | ||
| + | if(light > 250) { digitalWrite(1, LOW); } | ||
| + | delay(25); | ||
| + | |||
| + | } | ||
| + | </code> | ||
| + | |||
| + | Now we can call the URL | ||
| + | |||
| + | <code> | ||
| + | |||
| + | $ curl https://api.particle.io/v1/devices/id_here/light?access_token=token_here | ||
| + | { | ||
| + | "cmd": "VarReturn", | ||
| + | "name": "light", | ||
| + | "result": 785, | ||
| + | "coreInfo": { | ||
| + | "last_app": "", | ||
| + | "last_heard": "2016-03-20T04:13:23.389Z", | ||
| + | "connected": true, | ||
| + | "last_handshake_at": "2016-03-20T04:13:15.370Z", | ||
| + | "deviceID": "12345678", | ||
| + | "product_id": 82 | ||
| + | } | ||
| + | |||
| + | $ curl https://api.particle.io/v1/devices/id_here/light?access_token=token_here | ||
| + | { | ||
| + | "cmd": "VarReturn", | ||
| + | "name": "light", | ||
| + | "result": 193, | ||
| + | "coreInfo": { | ||
| + | "last_app": "", | ||
| + | "last_heard": "2016-03-20T04:13:27.820Z", | ||
| + | "connected": true, | ||
| + | "last_handshake_at": "2016-03-20T04:13:15.370Z", | ||
| + | "deviceID": "12345678", | ||
| + | "product_id": 82 | ||
| + | } | ||
| + | } | ||
| + | </code> | ||
| + | In addition the on-board LED turns on when the cell is covered, as the analog reading drops below 250. | ||
| + | And that's it for the basics of getting data via particle.io! It's up to you how you get the data (''curl'' as above, ''javascript'' in a web page, or some other means) and what you do with it (say, a sensor that can notify you when it's dark somewhere), so the sky is the limit for what else you come up with. Keep in mind that the lessons above are transferable to //any// sensor. If you can read the data, you can make it available in the cloud for access. | ||