Arduino UART

I’m working on a project where I need instructions that are sent through an agent to be passed on to an arduino. I’m getting an error that says “ERROR: bad parameters to uart.write(int/string/blob)” My code is below. What am I doing wrong?

`
// electric imp device code
server.log(“Device Started”);

///////////
// Setup //
///////////

arduino <- hardware.uart57;

local Instructions = {
angle = 0,
distance = 0
}

function dataUpdate()
{
local sensorData = arduino.read();

// Once the table is constructed, send it out to the agent with "sensorData"
// as the identifier.
agent.send("sensorData", sensorData);

}

function sendInst(Instructions)
{
server.log(“Sending Instructions to Arduino.”);
arduino.write(Instructions);
}

arduino.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, dataUpdate);
agent.on(“Instructions”, sendInst)
`

What’s the agent code that calls the line device.send(“instructions” …)? Seems like some object is being sent which can’t be passed directly to a serial write call.

Here’s my agent code. “Instructions” is a set of two inputs sent in a query to the agents url, that are then incorporated into a local array and forwarded to the device.

`
//////////////////////
// Global Variables //
//////////////////////
steeringAngle <- “”;
encoderDist <- “”;
measurement1 <- “”;
measurement2 <- “”;
measurement3 <- “”;
measurement4 <- “”;
state <- 0;

local Instructions = {
angle = 0,
distance = 0
}

//////////////////////////
// Function Definitions //
//////////////////////////

// respondDataValues is called whenever an http request is received.
// This function will construct a JSON table containing our most recently
// received imp pin values, then send that out to the requester.
function respondDataValues(request,response){

if("angle" in request.query)
{
    server.log("Instructions Recieved.");
    Instructions.angle = request.query["angle"].tointeger();
    Instructions.distance = request.query["distance"].tointeger();
    device.send("Instructions", Instructions);
}

if(request.query["state"]!=state){
// First, construct a JSON table with our received pin values.
    local dataTable = {
        "steeringAngle": ""+steeringAngle+"",    // e.g.: "pin1" : "1"
        "encoderDist": ""+encoderDist+"",
        "measurement1": ""+measurement1+"",    // e.g.: "pin5" : "48491"
        "measurement2": ""+measurement2+"",
        "measurement3": ""+measurement3+"",
        "measurement4": ""+measurement4+"",
        "state": ""+state+""
    }

    // the http.jsonencode(object) function takes a squirrel variable and returns a
    // standardized JSON string. - https://electricimp.com/docs/api/http/jsonencode/
    local jvars = http.jsonencode(dataTable);

    // Attach a header to our response.
    // "Access-Control-Allow-Origin: *" allows cross-origin resource sharing
    // https://electricimp.com/docs/api/httpresponse/header/
    response.header("Access-Control-Allow-Origin", "*");

    // Send out our response. 
    // 200 is the "OK" http status code
    // jvars is our response string. The JSON table we constructed earlier.
    // https://electricimp.com/docs/api/httpresponse/send/
    response.send(200,jvars);
}

}

///////////
// Setup //
///////////

// device.on(“sensorData”) will be called whenever an “sensorData” request is sent
// from the device side. This simple function simply fills up our global variables
// with the equivalent vars received from the imp.
device.on(“sensorData”, function(iv) {
steeringAngle = iv.steeringAngle;
encoderDist = iv.encoderDist;
measurement1 = iv.measurement1;
measurement2 = iv.measurement2;
measurement3 = iv.measurement3;
measurement4 = iv.measurement4;
state = 1-state;
});

http.onrequest(respondDataValues);
server.log(“Data updated.”);
`

Any ideas on this anyone? I haven’t done a lot of serial communication between devices before, so I might be missing something simple.

Thanks in advance!

Yeah, you’re passing a table (Instructions) to serial.write. A table means nothing to a serial port, you need to send a string.

Try changing this:

function sendInst(Instructions) { server.log("Sending Instructions to Arduino."); arduino.write(Instructions); }

to this:

function sendInst(Instructions) { server.log("Sending Instructions to Arduino."); arduino.write(format("angle=%d, distance=%d\\r\ ", Instructions.angle, Instructions.distance)); }

…then you’ll need to write code on the arduino to parse these lines and extract the information.