This article is more than 1 year old
Little devil: Electric Imp is an Internet of Things Wi-Fi PC-ON-AN-SD-CARD
The card micro that makes connecting projects to the cloud easy
The phone’s favoured wireless network is already listed for the Imp to use, but you can enter others, along with their passwords. I had to do this: my main WLAN, presented by the app, sits in the 5GHz band, a region the Imp’s 2.4GHz radio can’t access. The Imp currently remembers only one wireless network, but Electric Imp developer relations chief, Matt Haines, tells me upcoming ImpOS upgrades may allow multiple networks to be recorded and add the ability to hop onto free hotspots out in the wild.
Whether you use the network the phone already knows about or key in the details of another one, you next hold the phone’s screen up against a tiny window on the end of the Imp, and the SSID and associated WPA key are literally flashed across. Audio tones tell you when to put screen to card and take it away again. If all’s well, the card will now connect to the internet and Electric Imp’s servers, download and apply any firmware updates.
Now it’s ready to use.
Electric Imp’s app: choose or enter your WLAN’s details (left) then prepare to flash the data to the Imp (right)
Programming the Imp is carried out in an online IDE. Imp uses an object-oriented scripting language called Squirrel to code two event-driven programs that run in parallel: one in the Imp, the other on Electric Imp’s servers. The latter program is called an “Agent” and it provides a web-based (HTTPS) mechanism for routing control commands and data over the internet to your Imp. Electric Imp manages the security and handles accessing Imps across routers’ NAT tables and firewalls, but of course you’ll need to sign up for a free account to make use of it.
Each Agent is assigned a unique web address which can then be embedded in a local HTML page presented by a browser or coded into a mobile app and accessed using Android and iOS stock URL handlers. You can pass named parameters tacked onto the end of the URL just as you would if you were scripting basic browser-server interaction, and code the Agent to parse those parameters when it’s passed an inbound request.
One response the Agent can make is to fire off a message to the device. The second program, running on the Imp, in turn reacts to this message by calling functions that can examine data sent by the Agent and perform actions accordingly, typically by sending signals out through the GPIO ports, the three-channel UART, two-channel I2C and two-channel SPI buses, either to control another device or to read data from it.
The Imp code can then send a message back to the Agent, to which it can respond, perhaps by informing the browser or app in front of the user, or to send further messages back to the device. You might, for instance, want to perform particularly weighty calculations on the back end to help keep the device’s power consumption as low as possible. But the Imp has processing resources to apply logic to the data coming in off through the GPIO.
It’s not hard to imagine a typical usage scenario. A mobile app asks for, receives and displays the temperature in your living room, from the Imp, then allows you to turn the central heating on if it’s getting a bit nippy. A more advanced version might detect that you’ve left work for home, that it’s the middle of winter and so interrogate an online weather service to see if it had better get the boiler fired up on your behalf.
The big question, of course, is whether the intelligence to handle this goes in the device - via the Imp, say - or resides in the app. By giving the Imp compute capability, it can also run code that doesn’t need user interaction.
Electric Imp’s IDE presents Agent code side by side with the Device code so it’s easy to look back and forth between them to ensure, say, you’ve got your message parameters applied consistently. You can have the IDE error-check the code without beaming it to the Imp, or upload and run Agent code and device code immediately.
Together Agent code and device code form what Electric Imp calls a “Model”. Only one Model can be applied to an Imp at once, and Electric Imp could improve how you work on multiple Models in parallel. As it stands, a Model that’s not currently associated with an Imp can’t be edited. Multiple Imps can be assigned the same Model.
Breadboarded, powered and connected
The IDE doesn’t compile code: ImpOS incorporates a Squirrel interpreter. There’s clearly a small memory penalty involved in storing tokenised source code on the device rather than machine code, admits Haines, but the quid pro quo is that it makes it almost impossible to crash the hardware: the interpreter should be able to catch and stop any code before it takes out the system. On a lightweight embedded device in particular, compiled bare-metal code may find it has the freedom to cause more havoc.
The Squirrel language itself is not too hard to get to grips with if you’ve programmed C or Python before, and you’re doing relatively simple stuff. More complex code has a steeper learning curve. Being interpreted, it has one or two weak spots, bit-level maths operations in particular. The language has been used in a number of video games as a scripting language, but its use isn’t widespread. The available documentation is aimed more at the computer scientist than at the amateur programmer.
Squirrel is available under the open source MIT licence, but at this stage only its creator, Alberto Demichelis, is working on its future development and expansion.