I’m trying to do a simple serial connection between an Arduino Mega board and the elecric imp. The Arduino is sending a string that I am trying to post to either the log or the planner. The Imp seems to be dropping many of the bytes in the string, even at lower baud rates. My imp and arduino code is based of the Sparkfun Serial connection example. Is there a better way to read in a string? I eventually want to be able to log the data to a google docs spreadsheet ideally.
Take not, I’m a fairly rank beginner, so I’m sure I’m making fairly basic mistakes.
//Transmit Serial Data from Arduino
// impeeIn will override the InputPort class.
// Whenever data is received to the impee, we’ll jump into the set© function defined within
class impeeIn extends InputPort
{
name = “UART Out”;
type = “string”;
// This function takes whatever character was sent to the impee
// and sends it out over the UART5/7. We'll also toggle the txLed
function set(c)
{
//hardware.uart57.write(c);
}
}
local impeeInput = impeeIn(); // assign impeeIn class to the impeeInput
local impeeOutput = OutputPort(“UART In”, “string”); // set impeeOutput as a string
function initUart()
{
hardware.configure(UART_57); // Using UART on pins 5 and 7
hardware.uart57.configure(2400, 8, PARITY_NONE, 1, NO_CTSRTS); // 19200 baud worked well, no parity, 1 stop bit, 8 data bits
}
// This is our UART polling function. We’ll call it once at the beginning of the program,
// then it calls itself every 10us. If there is data in the UART57 buffer, this will read
// as much of it as it can, and send it out of the impee’s outputPort.
function pollUart()
{
imp.wakeup(0.0001, pollUart.bindenv(this)); // schedule the next poll in 10us
local byte = hardware.uart57.read(); // read the UART buffer
// This will return -1 if there is no data to be read.
while (byte != -1) // otherwise, we keep reading until there is no data to be read.
{
server.log(format("%c", byte)); // send the character out to the server log. Optional, great for debugging
server.show(byte);
impeeOutput.set(byte); // send the valid character out the impee's outputPort
byte = hardware.uart57.read(); // read from the UART buffer again (not sure if it's a valid character yet)
}
// This is where our program actually starts! Previous stuff was all function and variable declaration.
// This’ll configure our impee. It’s name is “ArduinoSerial”, and it has both an input and output to be connected:
imp.configure(“ArduinoSerial”, [], [impeeOutput]);
initUart(); // Initialize the UART, called just once
pollUart(); // start the UART polling, this function continues to call itself
// From here, two main functions are at play:
// 1. We’ll be calling pollUart every 10us. If data is sent from the UART, we’ll send out out of the impee.
// 2. If data is sent into the impee, we’ll jump into the set function in the InputPort.
//
// The end