Espruino and IfThisThenThat...

Posted on
  • It turns out IFTTT have a Maker Channel now, which means you can trigger loads of random stuff (tweeting, emails, etc) to happen on the net really easily (thanks for letting me know @Ollie!).

    To use it:

    • Connect Espruino to an ESP8266 WiFi module
    • Go to IFTTT and make an account
    • Go to https://ifttt.com/maker, enable it, and copy your secret key.
    • Then copy the code at the bottom into the Web IDE, and paste your key into the IFTTTKEY variable. Also set up your WiFi access point name and password, and then write the code to Espruino.
    • Create a recipe, click this and choose Maker, then Receive a web request
    • Put button_pressed as the event name
    • Click that and choose whatever you want to happen
    • Make sure you click all the way through the options to enable your recipe
    • Press the button on your Espruino :)

    This code actually resets your ESP8266 and connects to the wireless network each time you press the button. It takes a bit longer, but it's much more reliable as the ESP8266 will start from the same state each time.

    Going back the other way (using Espruino as the that) is a bit more painful at the moment, because IFTTT expects to send a request to a web server that's available from the net. Does anyone have any good solutions to that?


    var APNAME = "...";
    var APKEY  = "...";
    var IFTTTEVENT = "button_pressed";
    var IFTTTKEY = "...";
    
    var working = false;
    var wifi;
    
    Serial2.setup(9600, { rx: A3, tx : A2 });
    
    function buttonPressed() {
      if (working) {
        console.log("Busy...");
        return;
      }
      working = true;
      console.log("Resetting ESP8266");
      wifi.reset(function(err) {
        if (err) {
          working = false;
          throw err;
        }
        console.log("Connecting to WiFi");
        wifi.connect(APNAME,APKEY, function(err) {
          if (err) {
            working = false;
            throw err;
          }
          console.log("Connected");
          // Now finally
          require("http").get("http://maker.ifttt.­com/trigger/"+IFTTTEVENT+"/with/key/"+IF­TTTKEY, function(res) {
            working = false;
            console.log("Response: ",res);
            var data = "";
            res.on('data', function(d) { data += d; });
            res.on('close', function(d) {
              console.log("--->"+data);
            });
          });
        });
      });
    }
    
    function onInit() {
      wifi = require("ESP8266WiFi").connect(Serial2, function(err) {
        if (err) throw err;
        console.log("ESP8266 connected");
      });
    }
    
    setWatch(buttonPressed, BTN, { debounce:100, repeat:true, edge:"rising" });
    
  • Well, if you don't like security, you could just use Espruino as server and poke a hole in your router's firewall and set up port forwarding.

    I'm planning to implement a 'magic mirror' in the cloud (smoke and mirrors?) for my own use, with a couple of PHP scripts that stores (non-persistently) the state of sensors in my network (being updated by an Espruino periodically calling a page iwth appropriate arguments), and allowing me to set flags that will be seen when the device within the network polls the magic mirror.

  • @Gordon I did wonder if it might be worthy of a more prominent announcement. Glad to help!

  • For receiving webhooks without messing with firewalls etc use a public webserver to receive the request then poll that server for the data? My example is PHP - maybe be better in Go or Node.js, depending on how realtime you needed the thing to feel?

    <?php 
    const PRIVATE_KEY = "my_private_key";
    // Authenticate
    if(!@$_REQUEST["key"] || $_REQUEST["key"] !== PRIVATE_KEY) {
    	header('HTTP/1.0 401 Unauthorized');
    	echo 'HTTP/1.0 401 Unauthorized';
    	die();
    }	
    // Handle request
    if($_POST) {
    	// Update data and write to a file - validation maybe - but it should be data you expect
    	file_put_contents('data.espruino', json_encode($_POST));	
    } else {	
    	// Get data from file, set headers and display
    	header('Content-Type: application/json');
    	echo file_get_contents('data.espruino');
    }
    
  • Yeah, I was thinking that a lot of people might not have a webserver. Wasn't sure if there was some other service (like pastebin?) that could be used..

  • Dweet.io if people can't host something themselves? Not used but looks the part.

  • You can get a free webserver for a year from AWS, your own VM. you can use it to build Espruino firmware too ;-)

    I think working apache and php is like half a dozen console commands, and you can copy-paste them from amazon's docs.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Espruino and IfThisThenThat...

Posted by Avatar for Gordon @Gordon

Actions