Imagine two Arduinos, half-a-globe across from each other, talking and sharing sensor information simply with access to an internet enabled WiFi network. When I stuck the Electric Imp on an Arduino shield, this is just the beginning of what I was envisioning.
In this super-quick/to-the-point tutorial, I'd like to provide some example code to explain how you could set up one or more Arduinos to talk to each other via Electric Imps. Specifically, I'm going to explain how to set up serial communication between an Arduino and an Electric Imp.
We'll explore the code, both the Arduino code that you may be familiar with, and Electric Imp code which may look a bit different. There'll also be a little hardware setup, and finally some setup within the Electric Imp planner UI.
Of course, you'll need an Electric Imp (two if you want to make Arduinos talk to eachother). You'll also need an Electric Imp account, which you can get by visiting the Electric Imp Planner. I should also mention you'll need a Android phone or iPhone with the Electric Imp app, in order to flash WiFi settings onto the Imp.
To use any Electric Imp, you'll need an Impee - the host board or socket that is designed to interface with the Imp. For this tutorial, my Impees are going to be the Imp Shield, but you could also make do with the Imp Breakout, or an Impee of your own design.
Finally, you'll need an Arduino. Just about any Arduino should do, all you really need is a UART, which they should all have. If Arduino isn't your thing, feel free to adapt the Arduino code and hardware to your favorite microcontroller platform.
Among the many features packed into the tiny Electric Imp development platform are three UARTs (in 6 pins no-less), which handle asynchronous serial communication. Serial communication just happens to be one of the more popular Arduino-to-machine communication methods. So, the goal is to setup a serial-based portal of communication between the imp and Arduino.
Using the Electric Imp planner, Imps can be configured to both pass and receive data to/from other Imps. This data can be in the form of 32-bit integers, 48-bit floats, strings, colors, images, or arrays. In this example we'll be passing strings - actually single characters - but you could modify the Imp code to pass any of the other valid data types.
We'll need to write some squirrel code that'll go on the Imps which simply passes data received from other Imps out the UART, and data received from the UART out to other Imps.
There'll also be an Arduino sketch to process received data and send data out over serial. This is the more open-ended part of the tutorial. In the example code, I'm going to make the Arduino(s) pass any data received from the Imp-connected UART out to the Serial Monitor, and vice-versa. We'll end up with something of a terminal chat program.
The code that'll live on the Imps shouldn't be too crazy. These are our tasks:
Here's the code I came up with. Fair warning: like most of you, I've had pretty limited experience coding with Imps. This code may be kludgy, but it works!
Copy/paste from above, or you can download the code from here (you can use the Insert Source tool in the Imp Code Planner to upload the code).
Check out the comments for more of a line by line explanation. Here's the gist of what's going on:
Go ahead and copy that code and save it in your Imp Planner.
While we're on a coding tear, let's write up the Arduino sketch real quick.
As I mentioned earlier, I'm going to set up something of a terminal chat program. Any data sent to the Arduino from the Serial Monitor, will be read by the Arduino and sent out a second, software, serial port to the Imp. Serial data sent from the Imp to the Arduino is read and sent out to the Serial Monitor.
Here’s what I came up with:
The software serial is set to use pins 8 (RX) and 9 (TX), which are the pins used on the Imp Shield. If you have a Leonardo, you could use hardware serial to replace any calls to impSerial with Serial1. In my case, using an Arduino Uno, I have to use SoftwareSerial because the hardware serial port is connected to the USB-to-Serial chip, which we need for the Serial Monitor.
I'm using the Imp Shield for this tutorial, not that you have to though. If you do have a couple Imp shields, and two compatible Arduinos, setup is as simple as soldering on some headers, plugging the shield in, and plugging the Imps in.
If you don't have the shield, here's what needs to be connected:
There's some wiggle room with which pins you can use. On the Imp, you could use different pins to control the LEDs (or skip the LEDs), or even use one of the other two UARTs (UART12 or UART1289) for communication. On the Arduino, you can use different pins for the SoftwareSerial port (double-check to make sure they'll work with the library though!).
A few things to keep in mind: the Imp is not a 5V tolerant device, so you'll need to level shift any signals above 3.3V down. The Imp can be a power-hungry little devil - pulling up to 400mA. I've been powering each of the Arduinos/Imp combos off purely USB, and it's worked just fine. Just make sure to keep the power requirements in mind!
I'd really encourage you to branch out on this design. The Imp's got at least 2 more pins for you to make use of. There's an I2C port available on pins 1 and 2, as well as DACs, ADCs, PWM pins...you name it. And of course, there's tons of room left on the Arduino. Add some sensors, LCDs, LEDs...whatever your project needs.
The hardware and the code are done. What's left? We need to hook everything up in the Electric Imp planner!
If you haven't already, you'll need to whip out your or your friend's smart phone to commission your imp.
Once you plug the Imp in, and you're getting the happy, blinky green LED. It should pop up in your Planner. If you've got two fresh Imps powered up, it should look something like this:
We'll need to upload our code to each of them. Click on that slider-looking button, and scroll down to the “UartCrossAir” code you saved earlier. Select that, and It should copy right over to the Imp!
Then we need to tell the Impees to talk to eachother. Click on the + icon on each of the impees and terminate the arrow on the other Imp. You should end up with something like this:
Now all that's left is testing the chat system out. Open up terminals for each of the Arduinos. You may need to use a terminal program (like HyperTerminal, TeraTerm, etc.) in addition to the Serial Monitor, as I don't think you can open up Serial Monitors on multiple ports.
Try sending something to your Arduino. Pretty instantaneously you should see the TX then RX LEDs blink on the shields, and whatever you typed should end up in the other terminal window!
Okay, I'll admit this is a little silly. Just think about the journey that “Hello, World” had to take just to get from one serial port to the other. It went through the Imp, out your WiFi, into a magical cloud on a server far far away, then it completed the round trip back to your WiFi and into the other Imp. We've come a long way from null-modem serial cables (our data's probably gone even further :).
But, just imagine what this could be! All you need is an internet-connected WiFi. There's nothing that says these Imps have to be on the same WiFi network, or even in the same country. You could have an Arduino/Imp combo connected to a coffee shop WiFi in Seoul, seamlessly passing data to an Arduino/Imp combo sitting here connected to the SparkFun WiFi. Even chatting with an Arduino 10 miles away at my house seems pretty darn cool.
So, how are you going to use the Imps? Will you use them with an Arduino or another co-microcontroller? They're certainly powerful enough to work on their own, but it is nice to be able to do some of the heavy-lifting sensor/data processing in the comfy confines of your go-to microcontroller.
We're really excited to see what our customers can make with the Electric Imps. Please let us know about anything cool you build!
Thanks for reading! If there's anything I can help clarify, or errors that need correcting, please let me know in the comments below.