Imp to Imp Communication

imptoimp_comm

For one reason or another, you must want to connect two Arduinos to each other when they are far apart. So far apart that they need to connect to wifi, and different wifi networks at that.

One solution is to use the Electric Imp. You can set it up in such a way that the Arduino has no idea that there is any other wizardry going on. The Arduinos communicate with each other totally transparently. Tx of Arduino1 goes to Rx of Arduino2 and Rx of Arduino1 goes to Tx of Arduino2. This is a pretty weird setup, as Electric Imps are not very cheap. In total, even if you are using Arduino Pro Minis (the cheapest) you have $55 on each end of this link. ($30 imp, $12 breakout, $10 pro mini). I wouldn’t really recommend it for a long term project, but that said, the Arduino has additional ability to work offline, and maybe you would find yourself in a situation where you want to have a system that works like this.

As a note, you don’t need to have another Arduino and Imp on the other end of the communication. You can swap a computer for the Arduino; or can use a web interface and communicate directly with one of the Imp/Arduino combos.

I based my work off of the sparkfun tutorial found at https://www.sparkfun.com/tutorials/397, but as of 12/2013, the interface has changed for Electric Imps. “Agents” are more flexible and ultimately less confusing to get working- but they are very different from the old system.

Wiring

The wiring for this is the exact same as in the Sparkfun Example (and the diagram above).  ImpPin5 > ArduinoPin1 & ImpPin7 > ArduinoPin0. Don’t forget to power your Imp and Arduino!

Arduino Code

There really isn’t any special Arduino code. Just be sure to open the serial device in your setup and then get and send data.

Serial.begin(19200);
...
if(Serial.available() > 0){
  // get the new byte (we receive one byte/character at a time)
  char inChar = (char)Serial.read();
  // do something here with your received data!
}
Serial.print("s");

 Imp Code

You have two parts for the Imp code: Agent Code, and Device Code. The Device code runs on the physical Imp, and the Agent code runs on the server.

Imp Code- Device

Just like how you load the Arduinos with the same code, you load the Imps with the same code. You can just copy and paste this code into the Imp IDE (Device Section). Before you move to the Agent section, be sure you’ve added this code to both of your Imps (just be sure to name them clearly, like ImpCommProj-1 and ImpCommProj-2.

// This is called whenever we receive data from the agent (internet)
agent.on("impInput", function(inchar) {
  hardware.uart57.write(inchar);
  server.log(format("Internet to Ard %s", inchar)); // send the character out to the server log. Optional, great for debugging
});
 
// configure the UART to call the readserial() function when it gets data
// callbacks are much better than polling!
function initUart() {
  hardware.configure(UART_57);    // Using UART on pins 5 and 7
  hardware.uart57.configure(19200, 8, PARITY_NONE, 1, NO_CTSRTS,readserial); // 19200 baud worked well, no parity, 1 stop bit, 8 data bits
}
 
// If there is data in the UART57 buffer, this function will be called and we will
// this will read as much of it as it can, and send it out of the impee's outputPort.
function readserial() {
  local byte = hardware.uart57.read();    // read the UART buffer
  while (byte != -1) { // otherwise, we keep reading until there is no data to be read.
    server.log(format("Ard to Internet %c", byte)); // send the character out to the server log. Optional, great for debugging
    agent.send("impOutput",byte);
    byte = hardware.uart57.read();  // read from the UART buffer again (not sure if it's a valid character yet)
  }   
}
 
initUart(); // Initialize the UART, called just once

Imp Code- Agent

Whenever the agent receives serial information (which would be from the local device) it sends out that information to the other imp. Each Agent has almost the same code- you just have to be sure the devices point to each other.

agent_location

Click on the Devices button (where the top left arrow is), and then select your device. Then you will see the agent URL (the bottom right arrow). Look at the URL for Imp1 and copy it to Imp2′s agent code. Similarly look at the Imp2 URL and copy it to Imp1′s agent code. Enter the following code to each Agent being sure to adjust the URL.

const OtherAgentUrl = "https://agent.electricimp.com/PASTEURLHERE";
// when we get a message from the device, send it to the other imp...
device.on("impOutput", function(c) {
    // Log the fact that we received a message
    server.log(format("Agent received %c",c));
 
    // This allows us to talk to ourselves...
    //device.send("impInput", c);
 
    // This lets us talk to the other person...
    local url = format("%s?msg=%c", OtherAgentUrl, c);
    http.get(url) .sendasync(function(resp) {
          // this line will show HTTP response information (useful for debugging)
          //server.log(format("%i: %s", resp.statuscode, resp.body));
    });
});

log

 Conclusion

Thats it! Turn on your Imps and Arduinos and you should see them communicating with each other.

Also read...

Comments are closed.