Simple Imp to Arduino communication (serial?)

Hi All,
I’m looking for a simple way (example code) to pass information between an Imp and Arduino.
Overall, the process I’m trying to create is:
Imp: Gets JSON data from web site, stores in a table
Arduino: Requests data from the table (by referencing the key:value)
Imp: Communicates data
Arduino: Displays the data on a LED display

Current example code being run is (thanks to jwehr for this code!!!)

AGENT CODE:
function getTicker() { local reqURL = "https://btc-e.com/api/2/btc_usd/ticker"; server.log(format("Sending request to %s", reqURL)); local req = http.get(reqURL); // send the request synchronously (blocking). Returns an HttpMessage object. local res = req.sendsync(); // check the status code on the response to verify that it's what we actually wanted. server.log(format("Response returned with status %d", res.statuscode)); if (res.statuscode != 200) { server.log("Request for Ticker data failed."); return; } //Decode JSON from the body to a variable local response = http.jsondecode(res.body); //send it to the device. device.send("ticker", response); //refresh the data every 30 seconds. imp.wakeup(30, getTicker); } getTicker();

DEVICE CODE:
agent.on("ticker", function(data) { //Do something with your ticker data server.log(data.ticker.high); });

-I would love to just use the Imp and not worry about the Arduino however there is no library code for running a 8 character LED display from the IMP - specifically the JY-LKM1638 (if you know how I could do this that would be amazing!)

This describes it through a simple serial interface (see discussion here too). This isn’t to say that interacting with other uCs from an impee isn’t also interesting (see example here)…

from the Sparkfun example I borrowed the rxLEDToggle and txLEDToggle functions for troubleshooting in the Imp code and for the Arduino code I use the AltSoftSerial library for communication with the Imp, so you can use the Serial Monitor on the hardware serial (and upload the sketch without having to disconnect the Imp).
Imp Device code:
`// electric imp device code
server.log(“Device Started”);

arduino <- hardware.uart57;
//Port 5=TX , Port 7=RX
//remember: Arduino TX = Imp RX

function arduinoData() {
local b = arduino.read();
while(b != -1) {
local state = “Unknown”;
if (b == 0x10) state = “Off”;
if (b == 0x11) state = "On"
server.log("LED: " + state);
b = arduino.read();
toggleRxLED();
}
}

arduino.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, arduinoData);
//****************
local rxLEDToggle = 1; //Yellow LED receiving pin8
local txLEDToggle = 1; //Red LED trnsmitting pin9
function initLEDs(){
// LEDs are on pins 8 and 9 on the imp Shield
// They’re both active low, so writing the pin a 1 will turn the LED off
hardware.pin8.configure(DIGITAL_OUT_OD_PULLUP);
hardware.pin9.configure(DIGITAL_OUT_OD_PULLUP);
hardware.pin8.write(1);
hardware.pin9.write(1);
}
function toggleTxLED(){
// This function turns an LED on/off quickly on pin 9.
// It first turns the LED on, then calls itself again in 50ms to turn the LED off
txLEDToggle = txLEDToggle?0:1; // toggle the txLEDtoggle variable
if (!txLEDToggle)
{
imp.wakeup(0.01, toggleTxLED.bindenv(this)); // if we’re turning the LED on, set a timer to call this function again (to turn the LED off)
}
hardware.pin9.write(txLEDToggle); // TX LED is on pin 8 (active-low)
}
function toggleRxLED(){
// This function turns an LED on/off quickly on pin 8.
// It first turns the LED on, then calls itself again in 50ms to turn the LED off
rxLEDToggle = rxLEDToggle?0:1; // toggle the rxLEDtoggle variable
if (!rxLEDToggle)
{
imp.wakeup(0.01, toggleRxLED.bindenv(this)); // if we’re turning the LED on, set a timer to call this function again (to turn the LED off)
}
hardware.pin8.write(rxLEDToggle); // RX LED is on pin 8 (active-low)
}
initLEDs();
//****************

function blink(state) {
server.log("Setting LED to: " + state);
arduino.write(state);
toggleTxLED();
imp.wakeup(1.0, function() { blink(1-state); });
} blink(1);`

Arduino sketch:
`#include <AltSoftSerial.h>
AltSoftSerial impSerial(8, 9); // RX on 8, TX on 9
// remember: Arduino TX = Imp RX!
int led = 13; // led pin number

void setup() {
//pins 0 (RX) and 1 (TX)
Serial.begin(9600); // configure serial
// set the data rate for the SoftwareSerial port
impSerial.begin(9600);
pinMode(led, OUTPUT); // configure LED pin
digitalWrite(led, 0); // turn LED off
}

void loop() {
int b = 0;
// if there’s data available
if (impSerial.available () > 0) {
// read a byte
b = impSerial.read();
if (b == 0x00) {
digitalWrite(led, LOW);
impSerial.write(0x10);
} else if (b == 0x01) {
digitalWrite(led, HIGH);
impSerial.write(0x11);
}
}
}`

We have a very (very) basic example of imp <–> Arduino communication as our example code for the UART documentation

http://electricimp.com/docs/api/hardware/uart/

It’s meant to be a starting point to do more complex things :slight_smile:

My modification is based on your example… :wink:

@DolfTraanberg - Huzzah! People are using sample code.

Dirk, Dolf and Matt thanks so much for your assistance!!!

Thanks for the re-write of the code Dolf, I’ve got it running, flicking LED status (and thank you for the comments in the code, I found them very helpful!!!)

I think you are right Dirk, it’s interesting to link the Arduino and Imp together however I’m coming across to a lot of hurdles (how to communicate switch presses ect will make this project far to complicated). Do you have any starting points on what you think the best way to write the code for the imp to display would be?

Thanks again everyone!