Photon publish to IFTTT to PHP to IFTTT and back to the Photon

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

@bko @Moors7 @peekay123 @ScruffR @clarissa

Anyone think this might be useful?

The companies that may still use php include:
MCI/Worldcom, Dialpad, Google, Mitsubishi, Redhat, MP3.com, Lycos, Ericsson, Volvo, NASA, the W3C, SourceForge, Honda, Xoom, WinAmp, Sony Music, Cap Gemini Ernst & Young, the US Army, UPI, the New York Yankees, Southwestern Bell, the San Diego Zoo, the Oakland Raiders, Audi, Subaru, VA Linux, Winamp, Duke University, Quicken, The Village Voice, Undernet, Access Micro

Being able to connect the Photon easily with these companies php websites or your own php web server may be very powerful.

I do not have a lot of experience with PHP but I know that @wgbartley does–maybe he can chime in here.

It certainly looks like an interesting mash-up. I think web hooks or IFTTT would work in this case too.

You dont really need IFTTT to connect to a PHP web server, its just like calling any other url, you can do it with webhook if you need ssl or a simple httpclient if you dont.

Calling back can happen with either cloudFunctions or events that the photon subscribes to.

IFTTT is a neat tool to interact with various services without having to lean their API, but it has its limits too…

1 Like

@MORA

Thanks Mora, I missed the information about webhook options at https://docs.particle.io/guide/tools-and-features/webhooks/#webhook-options. Never knew Particle had the ability to respond to a webhook with a .json file.

If anyone knows about working examples this would be a good place to comment, especially grabbing information from webservers and using the information back on the photon using if statements.

This might be helpful:

Its a bit harder to use at the moment than it should be, hopefully in the future there will be a nice web interface to handle them.

You need to install the particle CLI.
Then create a json file that defines your webhook
A simple one would look something like this

{
    "eventName": "getTime",
    "url": "http://myserver.dk/time.php",
    "requestType": "GET",
    "query": {
        "timezone": "GMT"
    },
    "mydevices": true
}

Then create that hook with "particle webhook create myhook.json"
Then you send a publish with the same name to trigger it, and get a reply back at hook-response/getTime

void setup() {
    Spark.subscribe("hook-response/getTime", gotHookReply, MY_DEVICES);
	Particle.publish("getTime");
}

void gotHookReply(const char *name, const char *data)
{
    Particle.publish("log", data, 60, PRIVATE);
}
1 Like

Any chance the new dashboard will allow uploading the .json file? Been about 12 months since I played with and did not enjoy the CLI. I am curious about webhooks, but do not think I will be able to use them in the classroom due to having to load the CLI.

P.S. This Topic is about IFTTT shouldn't we move this discussion to Tutorial: Webhooks and Responses with Parsing (JSON, Mustache, Tokens)

:grinning: :grinning: :grinning: found this link which means I may be able to teach webhooks. Particle and Cloud9

Works great.

So I made my own version of the cloud9 for particle at

and have also included my own version of webhooks. Spent a while trying to find a website that sent reliable JSON data. I could make my own using php but that might not be helpful to others. Finally decided to use the particle site which sends a standard error message if you try to link to

https://api.spark.io/v1/devices/

The error you in get JSON format is

{
  "error": "invalid_request",
  "error_description": "The access token was not found"
}

So I used that to try to capture the “error_description” response and turn on D7 if the response was

"The access token was not found"

Anyway, Thanks @MORA for mentioning this, not only do I have it working but I also now have a way to use the CLI with students in a school setting.