ERROR: the index 'gauge_type' does not exist


#1

Hi,

I am trying to build on the Web UI Server example which displays temperature and humidity sensor readings on a web page. That works well.
I have added some uart code to device to talk to a serial gauge and can read variables and display them on the device and agent logs.

But when adding more variables to the savedData array I get:
ERROR: the index ‘gauge_type’ does not exist (line 241).

If I try to display the serial data on the temp and humid variables on the web page, it works correctly.

Any ideas why there is such a difference in the array?
What could I be missing on?

Thanks in advance.

Some parts of code below:

function postReading(reading) {
// Save the data
if (“temp” in reading) {
server.log(“Nothing here in temp”);
//savedData.temp = format("%.2f", reading.temp);
savedData.temp = reading.gauge_type;
} else {
savedData.temp = “0.0”;
}

if ("humid" in reading) {
    //savedData.humid = format("%.2f", reading.humid);
    savedData.humid = reading.gauge_pressure;
} else {
    savedData.humid = "0.0";
}

if ("gauge_type" in reading) {
    server.log("From agent gauge type is: " + reading.gauge_type);
    savedData.temp = reading.gauge_type;
    //savedData.gauge_type = reading.gauge_type;
    server.log("After savedData.gauge_type.");
    //savedData.gauge_type = format("%.2f", reading.humid);
} else {
    savedData.gauge_type = "0.0";
}
/*
if ("gauge_node" in reading) {
    savedData.gauge_node = reading.gauge_node;
} else {
    savedData.gauge_node = "0.0";
}

if ("gauge_pressure" in reading) {
    savedData.gauge_pressure = reading.gauge_pressure;
} else {
    savedData.gauge_pressure = "0.0";
}

if ("gauge_status" in reading) {
    savedData.gauge_status = reading.gauge_status;
} else {
    savedData.gauge_status = "0.0";
}

if ("gauge_hi_trip" in reading) {
    savedData.gauge_hi_trip = reading.gauge_hi_trip;
} else {
    savedData.gauge_hi_trip = "0.0";
}

if ("gauge_lo_trip" in reading) {
    savedData.gauge_lo_trip = reading.gauge_lo_trip;
} else {
    savedData.gauge_lo_trip = "0.0";
}

if ("gauge_internal_temp" in reading) {
    savedData.gauge_internal_temp = reading.gauge_internal_temp;
} else {
    savedData.gauge_internal_temp = "0.0";
}

if ("gauge_run_hours" in reading) {
    savedData.gauge_run_hours = reading.gauge_run_hours;
} else {
    savedData.gauge_run_hours = "0.0";
}

if ("gauge_exposure_thresh" in reading) {
    savedData.gauge_exposure_thresh = reading.gauge_exposure_thresh;
} else {
    savedData.gauge_exposure_thresh = "0.0";
}

if ("gauge_serial_no" in reading) {
    savedData.gauge_serial_no = reading.gauge_serial_no;
} else {
    savedData.gauge_serial_no = "0.0";
}
*/
local result = server.save(savedData);
if (result != 0) server.error("Could not back up data");

}

api.get("/state", function(context) {
// Request for data from /state endpoint
context.send(200, { “temp” : savedData.temp,
“humid” : savedData.humid,
“gauge_type” : savedData.gauge_type,
“locale” : savedData.locale });

//Backup data
savedData.temp <- “TBD”;
savedData.humid <- “TBD”;
savedData.gauge_type <- “TBD”;


#2

This error looks like ‘gauge_type’ slot doesn’t yet exist in your savedData table. When adding a slot to a table in squirrel you must use the slot operator not just an equal sign.

// Create an empty table
savedData <- {};

// Add slots to table
savedData.temp <- 24.3;
savedData.humid <- 34.6;
savedData.gauge_type <- 0.0;

Your code checks that slots exist in the ‘reading’ table, but not if they exist in savedData. The following code from your sample will throw an error if savedData doesn’t have a slot ‘gauge_node’.

// This code will be ok if savedData.gauge_node exists
if ("gauge_node" in reading) {
    savedData.gauge_node = reading.gauge_node;
} else {
    savedData.gauge_node = "0.0";
}

It would be better to write the following code if you might be adding a slot to your data.

// This code will work if savedData.gauge_node is not yet a slot, but you'd like to add it
if ("gauge_node" in reading) {
    savedData.gauge_node <- reading.gauge_node;
} else {
    savedData.gauge_node <- "0.0";
}

Also if you are on the agent you can print the contents of a table or array by using the http.jsonencode method. This could be helpful in debugging. Here is how to print the savedData table:

server.log(http.jsonencode(savedData));