Weather Station Agent Code

I would appreciate help on the agent code for a SparkFun Weather Station. Many people seem to have got this code running without mentioning the difficulty I have experienced. (I am a nebie in both Cplusplus and and Squirrel).
The code is the latest version from Codebender:

I get the following errors:
[Agent] ERROR: the index ‘16’ does not exist
[Agent] ERROR: at unknown:247

and here is line 247 of the agent code:
server.log(format(“Error: incorrect frame received (%s, %s)”, a[0], a[16]));

The block of code generating the error is (lines 236 – 269):
// When we hear something from the device, split it apart and post it
device.on(“postToInternet”, function(dataString) {

//server.log("Incoming: " + dataString);

//Break the incoming string into pieces by comma
a <- mysplit(dataString,',');

if(a[0] != “$” || a[16] != “#”)
server.log(format(“Error: incorrect frame received (%s, %s)”, a[0], a[16]));
server.log(format(“Received: %s)”, dataString));
//Pull the various bits from the blob
//a[0] is $ note a[0] commented out
local winddir = a[1];
local windspeedmph = a[2];
local windgustmph = a[3];
local windgustdir = a[4];
local windspdmph_avg2m = a[5];
local winddir_avg2m = a[6];
local windgustmph_10m = a[7];
local windgustdir_10m = a[8];
local humidity = a[9];
local tempf = a[10];
local rainin = a[11];
local dailyrainin = a[12];
local pressure = a[13].tofloat();
local batt_lvl = a[14];
local light_lvl = a[15];
//a[16] is # note a[16] commented out
Any help would be appreciated.

The data in dataString is likely not in the correct format. The format checking after mysplit is a bit too lightweight. It should check the length of the array before checking its content. Uncomment the statement: //server.log("Incoming: "+ dataString); to see what you’re getting.

Thanks coverdriven. Uncommented as suggested but the new errors where “the index ‘a’ does not exist” and "at unknown:241 - which is a blank line.

what does the console in the device IDE tell you? What was the data output that began with "Incoming: "?

Regarding the error, I haven’t seen your edited code, but it sounds to me like the line that declares “a <- …” has been deleted.

This is the Device IDE output:
[Status] Device connected
[Device] Device started, impee_id 233dacab5ba5ceee and mac = 0c2a69046533
[Device] Gathering new weather data
[Device] We heard:
[Device] Arduino read complete
[Device] Nothing to do, going to sleep for 60 seconds
[Device] sleeping until 1456728503000
[Agent] ERROR: the index ‘a’ does not exist
[Agent] ERROR: at unknown:241 ***241 is a blank line.
***243 is a<- mysplit(dataString,’,’);

but this is the device code:
server.log(“We heard: " + format(”%s", incomingStream)); // Display in log window
server.log(“Arduino read complete”);

*** Does this mean in the message above [Device] We heard: , that the incomingStream is missing?

 // Send info to agent, that will in turn push to internet
agent.send("postToInternet", incomingStream);

The incomingStream is formed by:
// Collect bytes
local incomingStream = “”;
while (result != ‘
’) // Keep reading until we see a newline
counter = 0;
while(result == NOCHAR)
result =;

        if(result == NOCHAR)
            if(counter++ > 20) //Wait no more than 20ms for another character
                server.log("Serial timeout error");
                return(0); //Bail after 20ms max wait 
    //server.log("Test: " + format("%c", result)); // Display in log window

***”Test” is missing from the Device output.
incomingStream += format("%c", result);
toggleTxLED(); // Toggle the TX LED
result =; //Grab the next character in the que

*** Quite a few people have had trouble with serial timeout errors.
*** I cannot find where the a [ ] matrix is formed.

The variable a is allocated in your device.on() statement. Have you edited that handler?

Regarding incomingStream, it appears to be empty. That’s why you were getting the index 16 error in the Agent. You need to change your agent code so that it can cope with a possibly incomplete/corrupt value being returned from the device. One you can do that, you also need to look at why incomingStream does not have the data you require.

Thanks for your advice. No, the device.on() handler has not been edited.
I will be away for about one week and hope to catch up with you when I return.
I very much welcome your comments.

Make sure to check your wiring. It could be that there’s nothing in the data stream because you have a bad connection somewhere.

Any update on this issue? bone stock code with exception of adding my ID and Password to the agent. In serial monitor it’s populating as it should and string is as it should be. but agent is giving same error as top post.

Think your problem is similar to “IMP serial timeout error” post. I described the timeout problem which I have seen with the Sparkfun code, which is that the incoming message was sent before the Imp was ready (a race condition). I solved the problem by adding a 100 ms delay before sending the message to the Imp. See if that works. If you look at the Sparkfun Arduino Code which communicates with the Imp, their Arduino code has a lot of calculations to do before sending their weather data to the Imp. So the Sparkfun Imp code is written to expect the data message after all of the calculations were completed, if your (Arduino or other) code is prepared to send the data immediately, the Sparkfun Imp code will not be ready.

I have the same problem.

Error log
2016-12-12 20:58:01 UTC-2 [Status] Device connected
2016-12-12 20:58:01 UTC-2 [Device] Device started, impee_id 2335154cead3dbee and mac = 0c2a69001ee8
2016-12-12 07:58:01 PST [Device] Gathering new weather data
2016-12-12 20:58:02 UTC-2 [Status] Device disconnected
2016-12-12 20:58:02 UTC-2 [Agent] ERROR: the index ‘16’ does not exist
2016-12-12 20:58:02 UTC-2 [Agent] ERROR: in unknown agent_code: 246
2016-12-12 20:58:02 UTC-2 [Device] We heard:
2016-12-12 20:58:02 UTC-2 [Device] Arduino read complete
2016-12-12 20:58:02 UTC-2 [Device] Nothing to do, going to sleep for 60 seconds
2016-12-12 20:58:02 UTC-2 [Device] Sleeping until 2016-12-12 22: 59: 01Z

Was the problem solved?

You should probably look at line 246 of your agent code and work out what it’s trying to do?

If you attach the code then someone may be able to look.

This agent code is the same as the one on the page.

The weather station code is an example of “fair weather” code, which means it only works when conditions are ideal. Your problem lies in the same place as for the others who have tried it and struck similar errors. The device is not getting valid information from the weather station. Your agent just doesn’t cope with this well. Have a look at why your device is not reading any data from the weather station.

The station is reading the information in the arduino serial. I did not understand.

Does the error happen occasionally or all the time? I don’t think you’ve done anything wrong. Looking through the source code in the device.nut, it is not particularly robust. If there is any error in the data, you will get a run-time error in the agent, like: “ERROR: the index ‘16’ does not exist.”

Maybe there is someone out there who can fix the code to make it more robust.

In the meantime, I recommend that you change this:

`if(a[0] != "$" || a[16] != "#")
        server.log(format("Error: incorrect frame received (%s, %s)", a[0], a[16]));
        server.log(format("Received: %s)", dataString));

to this:

`if(a.len()<16 && (a[0] != "$" || a[16] != "#"))
        server.log("Error: incorrect frame received. Length="+a.len());

The previous mistake happens all the time.
Now this error appears.

2016-12-14 03:22:48 UTC-2 [Device] Gathering new weather data
2016-12-14 03:22:48 UTC-2 [Status] Device disconnected
2016-12-14 03:22:48 UTC-2 [Agent] Error: incorrect frame received. Length=6
2016-12-14 03:22:48 UTC-2 [Device] binary: 57 65 20 68 65 61 72 64 3a 20 a2 72 82 62 92 85 a5 b9 a5 b9 f5 30 2e 30 30 2c 64 61 69 6c 79 72 61 69 6e 69 6e 3d 30 2e 30 30 2c 70 72 65 73 73 75 72 65 3d 31 30 31 36 34 36 2e 35 30 2c 62 61 74 74 5f 6c 76 6c 3d 34 2e 30 31 2c 6c 69 67 68 74 5f 6c 76 6c 3d 30 2e 31 36 2c 23 0d
2016-12-14 03:22:48 UTC-2 [Device] Arduino read complete
2016-12-14 03:22:48 UTC-2 [Device] Nothing to do, going to sleep for 60 seconds
2016-12-14 03:22:48 UTC-2 [Device] Sleeping until 2016-12-14 05:23:48Z
2016-12-14 03:23:54 UTC-2 [Status] Device connected
2016-12-14 03:23:54 UTC-2 [Device] Device started, impee_id 2335154cead3dbee and mac = 0c2a69001ee8

Can it be a hardware problem?
Thank you, Your attention.

The data you are receiving from the weatherstation is not entirely in the format that the code expects. The data received is translates as

The code expects data like this: "$,winddir=270,windspeedmph=0.0,windgustmph=0.0,windgustdir=0,windspdmph_avg2m=0.0,winddir_avg2m=12,windgustmph_10m=0.0,windgustdir_10m=0,humidity=998.0,tempf=-1766.2,rainin=0.00,dailyrainin=0.00,-999.00,batt_lvl=16.11,light_lvl=3.32,#,"

The last part of the message is certainly correct.
The first part of the message appears to be corrupted. This could be caused by a hardware problem.