Reading Serial data from Arduino, Device disconnecting due to looping, I'm guessing..?

I’m working on coding an application to pass data from the imp cloud to arduino, though at the moment I’m working the other way to verify the serial connection between the two. The arduino is sending data with start (‘W’) and stop (‘X’) characters and the imp’s job is output.

I’ll include the code in a moment, but what’s happening is that I’m seeing arduino.Data1 output in the “We’re getting some data” output, and then either the device disconnects or the log fills up with an increasing # of repeated 'W’s.

The original code was Hugo’s - anyone have any thoughts as to why it appears to be ‘stuck’, and/or how to write the loop better to handle the disconnects? I added a boolean in the Arduino code to only send the data once, and then I re-upload to the Arduino once the imp is connected, but I have yet to see a “W,0,1X” output string

This is a disconnect example:

2014-06-02 09:25:19 UTC-4 [Device] Restarting imp shield processing... 2014-06-02 09:25:19 UTC-4 [Device] attempting serial read... 2014-06-02 09:25:19 UTC-4 [Device] we're getting some data ... (0) 2014-06-02 09:25:44 UTC-4 [Status] Device disconnected; 193 bytes sent, 0 received, 193 total

Here is the imp device code (no server code yet; that comes after the hardware):
server.log(“Restarting imp shield processing…”);
// string we’re collecting from the arduino
s <- “”;
local bWrite = true;

// process(0 takes the string passed to it and writes it to the log)
function process(newline) {

function arduinoData() {
// Read the data from the arduino
local b =;
server.log(“attempting serial read…”)
// While b has a value; while we’re receiving data
while(b!=-1) {
if(bWrite) {
server.log(“we’re getting some data … (” + b + “)”);
bWrite = false;

if (s == "") {
  // We look for a W to start the string, otherwise ignore
  if (b == 'w') s = "W";
} else {
  // Append to string
  // Let's see what we got
  // If we saw an X, we got the end of the string
  if (b == 'x') {
    // process string
    // and reset it
    s = "";

b =;

// Init uart. It will callback
hardware.uart57.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, arduinoData);

This is the arduino code:

`#include <SoftwareSerial.h>
SoftwareSerial impSerial(8, 9); // RX on 8, TX on 9

String Data1 = “0”;
String Data2 = “1”;
boolean sendData = true;
void setup() {
void loop() {
// Output to Imp
// Here we should output some information to the arduino log as well, so that we can see
// what’s going on…
while(sendData == true) {
impSerial.print(“W,”); //Start Character: There is never a W character in the data.


impSerial.print(“X”); //Stop character: There is never an X character in the data.
sendData = false; // send once, in other words

(I added the sendData to prevent my imp String variable from filling up with 'W’s and running out of memory)

Ah – I think I see a problem (“W,” vs. “W”) in my arduino sketch – testing…