User Tools

Site Tools


oak:tutorials:particle-variable

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
oak:tutorials:particle-variable [2016/03/19 21:23]
jwhendy finished example with photocell
oak:tutorials:particle-variable [2016/03/22 22:18]
jwhendy updated format
Line 1: Line 1:
 +===== Oak: using Particle.variable() =====
 +
 So, you've got an Oak all set up and ready to go. Now it's time to put its wireless So, you've got an Oak all set up and ready to go. Now it's time to put its wireless
-abilities to work!  This lesson will introduce ''​Particle.variable()''​ a method provided by [[https://​www.particle.io/​|particle.io]] for reading data from the Oak. The basic idea is pretty simple, and we'll start with a simple push button. ​Feel free to review ​the [[oak:​tutorials:​button|tutorial on buttons]] for a refresher. In addition, please do take a look at the [[https://​docs.particle.io/​reference/​firmware/​core/#​particle-variable-|official documentation]].+abilities to work!  This lesson will introduce ''​Particle.variable()''​ a method provided by [[https://​www.particle.io/​|particle.io]] for reading data from the Oak. The basic idea is pretty simple, and we'll start with a simple push button. ​Review ​the [[oak:​tutorials:​button|tutorial on buttons]] for a refresher ​if you'd like. In addition, please do take a look at the [[https://​docs.particle.io/​reference/​firmware/​core/#​particle-variable-|official documentation]].
  
-==== Requirements ​====+===== Components used =====
  
-This lesson will use (in addition to an Oak):+^ Part       ^ Quantity ​         ^Identification^ 
 +Oak with soldered headers |1| | 
 +| Breadboard| 1| | 
 +| Photocell | 1 | | 
 +| Push button | 1 | | 
 +| Jumper wires | 3 | | 
 +| Resistor, 10k ohm |1| Brown-Black-Orange|
  
-  ​a tactile push button +**Note:** Optionally, you can install [[https://​curl.haxx.se/​download.html|curl]] to get data from the Particle API, however pasting the links into browser works just as well.
-  ​a photoresistor +
-  ​jumper wires +
-  * a breadboard +
-  ​* a resistor (10k ohms is good)+
  
-Optionally, you can install [[https://​curl.haxx.se/​download.html|curl]] to get data from the Particle API, however pasting the links into a browser works just as well.+===== Concepts =====
  
 +=== Particle.variable() ===
  
-==== Example: getting ​the state of button ====+This function creates an interface between a variable on your Oak and particle.io. Each time the variable changes, the Oak will update the value stored in the Particle cloud, which you can then access with a call to a website (produced by visiting the page in the browser, or issuing ​command using ''​curl''​).
  
-Here's the circuit we'll use, which reads the button ​(pulled up to ''​Vcc'' ​through a 10k resistor) on pin 10:+The ''​Particle.variable()'' ​function takes two parameters: the name of the variable in the Particle cloud, and the name of the local variable stored on the Oak. For example ​''​Particle.variable("​particle_name",​ oak_name)''​. The value on the Oak would be stored in the variable ''​oak_var''​. When accessing this variable via the Particle API, you would use the following: 
 + 
 +<​code>​curl https://​api.particle.io/​v1/​devices/​id_here/​particle_name?​access_token=token_here</​code>​ 
 + 
 +===== Circuit ===== 
 + 
 + 
 +=== Button === 
 +This is the circuit we will begin with, which is explained in the [[http://​digistump.com/​wiki/​oak/​tutorials/​button|tutorial on buttons]].
  
 [[http://​digistump.com/​wiki/​_media/​oak/​tutorials/​button-pullup.png|{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​button-pullup.png?​400}}]] [[http://​digistump.com/​wiki/​_media/​oak/​tutorials/​button-pullup.png|{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​button-pullup.png?​400}}]]
  
  
-The code we'​ll ​start with is as follows:+=== Photocell === 
 + 
 +Additionally,​ we will recreate the circuit from the [[http://​digistump.com/​wiki/​oak/​tutorials/​photocell|tutorial on photocells]] to read an analog sensor value. 
 + 
 +[[http://​digistump.com/​wiki/​_media/​oak/​tutorials/​photocell.png|{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​photocell.png?​400}}]] 
 + 
 +[[http://​digistump.com/​wiki/​_media/​oak/​tutorials/​photocell-wiring.jpg|{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​photocell-wiring.jpg?​400}}]] 
 + 
 + 
 +===== Code ===== 
 + 
 +=== Reading a button === 
 + 
 +Here is the code we will start with:
  
 <​code>​ <​code>​
 +// variable to store the push button digitalRead() value
 int button; int button;
  
-// the setup() loop sets pinModes, starts things like Serial, 
-// and is run once when the Oak powers on 
 void setup() void setup()
 {                ​ {                ​
Line 39: Line 64:
  
   // Particle.variable() takes two options:   // Particle.variable() takes two options:
-  // 1) the name of the varialbe ​to access in the GET request+  // 1) the name of the variable ​to access in the GET request
   // 2) the name of the local variable that holds the value to report   // 2) the name of the local variable that holds the value to report
   Particle.variable("​button",​ button);   Particle.variable("​button",​ button);
Line 61: Line 86:
 </​code>​ </​code>​
  
-The only thing we did other than reading ​button ​as usual is to attach ​''​Particle.variable()''​ to that variable and give it a name we can use through ​the web to access the value. After uploading the code above, press the button to check that the on-board LED illuminates:​+Notice that the only additional code other than reading ​the button is to attach ​a "cloud name" to the variable ​so that we can read it from the web. After uploading the code above, press the button to check that the on-board LED illuminates:​
  
 [[http://​digistump.com/​wiki/​_media/​oak/​tutorials/​button-led-off.jpg|{{oak:​tutorials:​button-led-off.jpg?​300 }}]] [[http://​digistump.com/​wiki/​_media/​oak/​tutorials/​button-led-off.jpg|{{oak:​tutorials:​button-led-off.jpg?​300 }}]]
 [[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, make sure you have your ''​device_id''​ and ''​access_token''​ as described in the [[oak:​tutorials:​particle-id-token|Particle API wiki entry]]. +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]]. If you have ''​curl''​ installed, open a command prompt/​terminal,​ and type the command below, ​replacing ''​id_here''​ and ''​token_here''​ with the appropriate values. ​Alternativelyput the URL below into a browser:
- +
-[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)Otherwisetype out the URL somewhere you can copy/paste from, and visit that page in a browser.+
  
 <​code>​ <​code>​
Line 77: Line 97:
 </​code>​ </​code>​
  
-Hopefully this is what you get in return!+This is the response:
  
 <​code>​ <​code>​
Line 115: Line 135:
 Note that the contents in ''​result''​ changed. Since the data is sent in ''​json''​ format, you can use any number of methods to query the above URL, extract the contents of ''​result'',​ and do something based on that value. Note that the contents in ''​result''​ changed. Since the data is sent in ''​json''​ format, you can use any number of methods to query the above URL, extract the contents of ''​result'',​ and do something based on that value.
  
-==== Examplegetting the brightness from a photoresistor ​====+=== Codereading ​a photoresistor ===
  
-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 informationAdafruit has a [[https://learn.adafruit.com/photocells|nice writeup]] on photoresistorsHere's how we'll wire up our circuit:+The only difference ​in the next example ​is that instead of a button, ​we will be reading the analog signal from a photoresistor, ​just as described in [[http://digistump.com/wiki/​oak/​tutorials/​photocell|this tutorial]].
  
-[[http://​digistump.com/​wiki/​_media/​oak/​tutorials/​photocell.png|{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​photocell.png?​400}}]] +Here is the code we will use:
- +
-In pictures: +
- +
-[[http://​digistump.com/​wiki/​_media/​oak/​tutorials/​photocell-wiring.jpg|{{http://​digistump.com/​wiki/​_media/​oak/​tutorials/​photocell-wiring.jpg?​400}}]] +
- +
-To read the photoresistor, ​we use this code:+
  
 <​code>​ <​code>​
Line 131: Line 145:
 int light; int light;
  
-// the setup() loop sets pinModes, starts things like Serial, 
-// and is run once when the Oak powers on 
 void setup() void setup()
 {                ​ {                ​
Line 200: Line 212:
 In addition the on-board LED turns on when the cell is covered, as the analog reading drops below 250. 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.+===== Conclusion ===== 
 + 
 +And that's it for the basics of getting data via particle.io! It's up to you how you get the data and what you do with it. The sky is the limit. 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.
oak/tutorials/particle-variable.txt · Last modified: 2016/03/22 22:18 by jwhendy