Page 126 - Programming the Photon Getting Started With the Internet of Things
P. 126

The program starts with a basic variable, which will be used to return a value when the
        webpage requests the function on the Photon board. The other variable is used to identify
        the analog pin that we are going to use to read from a sensor or similar device, in this case

        analog  pin  A0.  In  the  setup  function  we  link  the  two  variables  together  using
        spark.variable. The first parameter is always the name given to the variable (analog),
        and  the  second  parameter  identifies  the  variable  that  the  sensor  is  connected  to  on  the
        Photon board (getvalue), and finally the last parameter defines the type of value, which

        is an integer value.

             Upload the program to the Photon board to test it out. You can do this using the Web
        browser, because unlike functions, variables use HTTP GET requests, which can you can

        easily issue using the uniform resource locator (URL) in your browser. So with this in
        mind open up a browser and type the following URL into it:


        https://api.spark.io/v1/devices/<device ID>/analog?access_token=<Access tok
        en>

             The request page should look like something Figure 6.7.































        Figure 6.7 URL request in browser.


             You can reload the page a few times, which will resend the URL request to the Photon,

        and  you  should  see  a  different  value.  Currently,  as  there  is  nothing  connecting  to  the
        analog pin, the value is floating.




        Reading a Light Sensor


        In this experiment we are going to be using a simple photoresistor to measure light levels
        and display that information on a webpage with a nice interface that should look similar to
        Figure 6.8.
   121   122   123   124   125   126   127   128   129   130   131