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

Spark.function("led",ledToggle);


             This  is  the  code  that  sets  up  the  function,  giving  it  the  name  “led.”  The  second
        parameter is the name of the function in the out program to run when the led function is
        called over the Internet. Before we actually start turning the LED on or off, we need to

        make sure that the LED is first in an off state, using the following command to write the
        status to the LED:


        digitalWrite(led1, LOW);

             We  keep  the  loop  function  part  of  the  program  empty  because  we  have  created  a

        separate function to catch the led command and we do not need to keep listening for it in
        the  loop  function.  When  the  ledToggle  function  is  called  as  a  result  of  receiving  a
        message from the Internet, it receives a string as a parameter. In the HTTP request that we

        will send to the Photon board, we will make sure that we either turn the LED on or off
        using the correct parameter. If the function received a value that is either on or off, it will
        return 1 to indicate a successful command received. If the command sent is unsuccessful,
        then it will return a value of −1, indicating it has failed the checking process.


             Something to consider also is the security process of actually sending commands to the
        Photon  board.  This  is  an  important  process,  as  you  do  not  want  your  device  open  to
        anyone on the Internet to start sending ghost commands to your Photon board. Certain
        measures can take place when you send your commands to the Photon board. The Photon

        board requires up to two tokens to help secure your device.

             The first token is your device’s unique identifier—each of your devices will have this
        unique token number to help identify your device, especially when you are using several
        boards at the same time. You can find your device’s ID by checking your device in the

        Particle  build  integrated  development  environment  (IDE)  and  selecting  your  Particle
        device, as shown in Figure 6.3.
   115   116   117   118   119   120   121   122   123   124   125