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);
}
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.
//////////////////////////
// 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;
});