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

simple requests based on the four GPIO functions available.

             DigitalWrite sets the pin to either HIGH or LOW, which in turn connects to 3.3 V or to
        GND. Previously we know that pin D7 is connected to an on-board LED on the Photon
        board. If we set this pin to HIGH, the LED turns on, and when we set it to LOW, it turns

        off. The following code is the API request sent to the Photon to accomplish this task:


        POST /v1/devices/{DEVICE_ID}/digitalwrite


        # EXAMPLE REQUEST IN TERMINAL
        # Core ID is 0123456789abcdef

        # Your access token is 123412341234
        curl https://api.spark.io/v1/devices/0123456789abcdef/digitalwrite \
        -d access_token=123412341234 -d params=D0,HIGH


        The parameters must be a pin number followed by the value, which is either HIGH or
        LOW. If the request has succeeded, it will return a value of 1 and −1 if it fails.

             AnalogWrite sets the pin value in the range from 0 to 255, where 0 is the lowest value
        (GND) and 255 is the highest value (3.3 V). As previously mentioned, we are using a

        digital system, so it would not be possible to create an analog signal, but what we can do
        is emulate an analog signal using something called pulse width modulation or PWM for
        short. A good example of using PWM, and probably the easiest when starting out, is to use
        the  analogWrite  function  to  dim  an  LED.  The  following  code  is  used  to  send  the  API

        request to the Photon:

        POST /v1/devices/{DEVICE_ID}/analogwrite



        # EXAMPLE REQUEST IN TERMINAL
        # Core ID is 0123456789abcdef

        # Your access token is 123412341234
        curl https://api.spark.io/v1/devices/0123456789abcdef/analogwrite \
        -d access_token=123412341234 -d params=A0,215


        The parameters here must be the pin number followed by the integer value ranging from 0
        to 255. As before, the return value will be 1 for success and −1 if it fails.

             DigitalRead will read the value from one of the digital pins on the board; this value
        can be either HIGH or LOW. This API request is as follows:


        POST /v1/devices/{DEVICE_ID}/digitalread



        # EXAMPLE REQUEST IN TERMINAL
        # Core ID is 0123456789abcdef
        # Your access token is 123412341234
        curl https://api.spark.io/v1/devices/0123456789abcdef/digitalread \
   35   36   37   38   39   40   41   42   43   44   45