Electric Imp Random Number demo
This is an illustrative demo of interfacing the Electric Imp with a website.
When executed correctly, the following happens:
- The device - Imp + Arduino - sends a message the Imp Agent every three seconds, asking for a random number.
- The Imp Agent, on receiving that message, makes a GET request to a public webserver, and receives JSON in return.
- The Agent sends a message to the Device, with the JSON bundled up as a Table object
- On receiving that message, the Device spits the number out to its screen.
The functionality is not the point, given Squirrel's built in
rand() function. The point is: we're making a query from a device to the internet and back again, using sensible protocols (HTTP, JSON, and a simple structure for the serial message). It can then be extended into other examples.
It's assumed you know how to BlinkUp your Imp and get code across to it.
In terms of software, we use the Adafruit libraries for their OLED, and Steve Cogswell's ArduinoSerialCommand library to process commands. (We use this because it offers a simple, straightforward and extensible protocol, rather than reading bytes by hand.)
The website is a small PHP page that serves up JSON - again, it could be a string, but this is more representative of a real-world usage (where there might be more data being transmitted.)
Put the PHP file somewhere it can be accessed over the internet - either on a server with
mod_php installed that you own, or executing on a local machine tunnelled to the internet via something like ngrok. Put the contents of
device.nut into the appropriate panels in your Electric Imp IDE and build them - be sure to replace
example.com with a webserver with your device in.
In terms of hardware, the SPI OLED is wired as in Adafruit's own instructions.
"Ready" should appear on the screen. Once data starts flowing in, the screen will begin updating.
Room for improvement
This assumes the http data is always correct; a try/catch loop in the
http methods in the Squirrel code would make this much more robust. Similarly, the Serial code just hurls data down UART without necessarily considering if the Arduino's ready to receive.