This is so cool that I made it into it's own topic.
Say you wanted a PHP file or database to crunch some numbers for your photon and send the information back to the photon. Example: You need to check an employees number from a fingerprint scanner or RFID tag against a database. (Note: This tutorial does not use any mySQL database stuff since that is another level of confusion, for here I just what to prove that the Photon can communicate with IFTTT and a php webpage without using any AJAX )
Also, if anyone has setup a LAMP computer (Linux, Apache, MySQL and PHP) you might really appreciate cloud9 at https://c9.io You can make a php site in about 60 seconds. Make your file, preview the page and your up and running for free to test things out.
Note: You have to set up the maker channel on IFTTT at https://maker.ifttt.com
The photon setup is a connection between 3V3 and D0 shown here, but could be done in several ways.
The .ino file to flash to the photon.
//PUT YOUR GLOBAL VARIABLES HERE
volatile bool myFlag1 = false;
int myCount = 0;
Timer myTimerD7(7000, myD7Function);
// slow down the number of published events sent to D7
// presently set to one event every 7 seconds
// after myTimerD7.reset() waits 7 seconds to activate myD7Function
void myD7Function(){
myFlag1 = true;
digitalWrite(D7, 0); // D7 Off
}
// Any general setup stuff goes here
void setup(){
pinMode(D7, OUTPUT);
pinMode(D0, INPUT_PULLDOWN);
// INPUT_PULLDOWN resets D0 to LOW if no power applied to it
myTimerD7.start();
// activate the timer for lamp D7
// Will need another timer for each lamp you activate
Particle.subscribe("my-lamp-on", myLampFunction); // public
// Particle.subscribe("my-lamp-on3456", myLampFunction, MY_DEVICES);
// ", MYDEVICES" is only for your photons, think of it as private
// change the number for uniqueness. Change 3456 to some other number
// Can be used with IFTTT website
}
void myLampFunction(const char *event, const char *data){
String myOptional = String(data);
int myNumberLoops = myOptional.toInt();
myTimerD7.reset(); // reset timer to start fresh
data = "0"; // reset data
for (int i=0; i <= myNumberLoops; i++){
digitalWrite(D7, 1); // flash D7 High
delay(50); //wait ms
digitalWrite(D7, 0); // D7 low
delay(50); //wait ms
}
}
void loop(){
// your looping stuff goes here
if (digitalRead(D0) == 1 && myFlag1){
Particle.publish("ifttt-php-post", "56478", 60, PUBLIC);
myTimerD7.reset(); // reset timer to start fresh
myFlag1 = false; // reset timer loop variable
}
}
Then the php file
<?php
$myPublished = $_POST['value1']; // grab the posted value 56478 sent by the photon
// Now you can do all the wonderful post-processing database stuff that
// PHP is really good at. I have not confused this tutorial with database stuff for simplicity
// I have just changed the particle data 56478 to the number 20 to send back to Flash the D7 LED 20 times.
if ($myPublished == "56478"){
$mySend = "20"; // change this number to test if it is working.
}
$url = 'https://maker.ifttt.com/trigger/ifttt-php-reply/with/key/jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj';
// get the key from the Maker channel on IFTTT or this link https://maker.ifttt.com to replace all the jjjjjjjjjjj's
// Don't bash your head against the computer for an hour with the wrong key like I did.
$data = array('value1' => $mySend);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
?>
And then the two IFTTT receipes. The first takes the Photons published event and sends the data to the php file
Then an IFTTT receipe to take the output from the php file and send it back to the photon (or send it to a different photon)
All kinds of places for this to messup. My IFTTT version of a serial print is to append commands to a google drive log file. Then I can check if each step of the receipes work. Here is an example google drive receipe that checks the values sent from the php file.
Extra Note: IFTTT can handle three variables but they must be called value1, value2, value3 but the photon only sends or recieves one variable. If you need the photon to send more complex information you need to look into writing the info in JSON sturcture {"value1":"20","value2":"40","value3":"100"}
I hope this is useful to someone. By the way, this code takes between about 3-9 seconds to complete which is not too bad considering the amount of sites that are being used. (And don't forget the seven second delay between button pushes, that always makes me think it is not working :>)
The original discussion was at
Disclaimer: Use this code at your own risk