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

}


             First we need to define the pins that we are going to use on the Photon board. The
        buzzer is connected to digital pin 1, and the vibration motor is connected to analog pin 4.


        #define MOTORPIN A4
        #define BUZZPIN D1


             Next in the setup function we set both the buzzer and the motor to outputs and create
        a  Spark  function  that  we  can  pass  data  though  from  IFTTT.  Remember  that  the  Spark
        function name cannot be longer than 16 characters.


        pinMode(MOTORPIN, OUTPUT);

        pinMode(BUZZPIN, OUTPUT);
        Spark.function("Twitter", twitter);

             We ignore the loop function because when the Spark function is called, it initializes

        the twitter function. The Spark function passes through a string value called command,
        and we check to see if this value is equal to “buzz,” then write the motor and buzzer to
        HIGH for one second before turning them off. If the function does not equal the value of

        “buzz,” then the function returns −1 as failed.

        int twitter(String command)

        {
            if (command == "buzz")
            {

                digitalWrite(MOTORPIN, HIGH);
                digitalWrite(BUZZPIN, HIGH);
                delay(1000);
                digitalWrite(MOTORPIN, LOW);
                digitalWrite(BUZZPIN, LOW);

                return 1;
            }
            else return -1;

        }

             Now that we have our Photon set up and running, we can now turn our efforts to the
        IFTTT Web service. Log in to your account and create a new recipe. This time we are

        going to use Twitter as a trigger rather than the Photon as before. At this point you may be
        prompted to authenticate your Twitter account and asked to give permission for IFTTT to
        use  the  Twitter  Web  service.  On  the  trigger  page  there  are  quite  a  number  of  different
        types of triggers that you can choose from—the one we will use for this experiment is
        “Twitter mentions of you.” Whenever your Twitter ID is tagged in a tweet, this will trigger

        the events in the recipe. Select New Mention Of You and proceed to the next step. Here
   166   167   168   169   170   171   172   173   174   175   176