How do you guys handle control over the internet?

Posted on
  • The question is how to pass commands to the Espruino, and receive info from it, over the internet... without exposing the Espruino to incoming connections.

    Here's what I've done:
    Espruino receives commands by http requests over the LAN - these can either return data or do things.

    For getting data, there are a number of php scripts (one for each data-getting-request on the Espruino) which, if the request comes from the correct IP address, will take a base64-encoded string (the response from the Espruino), decode it, and store it with apc_store(). And there are an equal number of php scripts that return that data (in the same format as the Espruino returns it, so I can use the same page inside the LAN and outside, by using different URLs depending on window.location.hostname).

    For sending commands, there's one php script that stores a command (this would be the url that would be requested from within the LAN to affect the desired result - again to allow the same page to easily work both inside and outside the LAN) via apc, and another which gets and clears this list.

    Finally, a raspberry pi within the lan has cronjobs that call python scripts, which get data from the Espruino, base64 encode it, and pass it to the php scripts on the outside webserver, and one that grabs the command list, and if not empty, makes the requests that are specified.

    It appears to work, as long as I avoid making two requests to the Espruino simultaneously....

  • I think a lot of people are using MQTT now? You can then run an MQTT server on the Pi, and could communicate with from webpages using MQTT over websockets - it avoids having to have all the state storing and constant polling, as the MQTT server handles pushing messages as and when it's required.

    Are you able to expose the Pi itself to the outside world? Even if you're not you could put an MQTT server on your web server and work that way or maybe use a third party server like Adafruit's one?

  • I can confirm MQTT, amount of data for MQTT communication is just a fraction of http, easy to check with wire shark.

    It appears to work, as long as I avoid making two requests to the Espruino simultaneously....

    no problem for MQTT

  • I use an HTTPS gateway in the cloud which provides a tunnel which is established from within my LAN using secure WebSocket. On LAN I run the MQTT server for the pub/sub IoT stuff and also other HTTP-based services.

  • Wow. It seems like everyone's using MQTT. It looks absurdly complicated -'s documentation looks nigh impenetrable. Where do you guys go to get an understanding of the most relevant parts of it for IoT and how to implement it?

  • Good way to try out is using one of the free online brokers and a browser client- I use for the broker and the chrome app MQTTBox. You can bring Espruino into it and establish two way communication in a just a few minutes without going into the guts of the protocol itself

  • In reality it's pretty simple for the main use cases, it's just that the spec's trying to cram lots of stuff about the binary format and random quality of service stuff in (that doesn't really apply over a normal TCP/IP connection anyway).

    The super quick intro is:

    • You have the idea of 'topics', which are just a path, separated with /
    • You can 'subscribe' to a topic, in which case you get told whenever that topic changes. You can use wildcards to subscribe to certain types of topic as well - so my/topic/#
    • You can 'publish' a topic, which tells everyone that subscribed that the topic has changed
    • The MQTT broker does all the smart stuff, and the clients pretty much just connect, issue subscribe/publish messages, and receive data when a topic changes

    There's also­als-part-1-introducing-mqtt/ which I seem to remember was pretty good. You can gloss over most of it as it's a bit much in places, but later on (part 5?) there's more useful info about publish/subscribe

    If you want a slightly less scary intro, look at @Ollie's tinyMQTT:­.js

    It's not crazy complex, it's just shifting a few bytes around.

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

How do you guys handle control over the internet?

Posted by Avatar for DrAzzy @DrAzzy