UART inputting

I’m having trouble receiving a uart string into the imp. I’m using the RFID CatFlap example in worked code but it doesn’t seem to be correct. The RFID data is on pin 7 and the code is using uart1289 but the pin Mux table shows pin 9 as the input for uart1289. The CatFlap latch output is pin 9. In my code I have tried both uart57 and uart1289. I figure I should resolve this basic disconnect before going on to the next thing not working. Help will be appreciated.

BTW- In my setup, I have an Arduino collecting data. It sends a short ascii string once every 15 seconds at 9600 baud with the data. I want to input the string to imp pin 7 and make the string available via http. It would be cool if someone already has code that can do it. Thanks. Jay.

Good point, looks like Rob didn’t build that as it should definitely be uart57.

Can you post your code?

You should just be able to do something like this:

`
hardware.uart57.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS);

string <- “”;

function poll() {
imp.wakeup(0.1, poll);
local b=hardware.uart57.read();
while(b >= 0) {
if (b < 32) {
// Treat any control character as EOL and send string
out.set(string);
string = “”;
} else {
string += b.tochar();
}
b = hardware.uart57.read();
}
`

Ok, thanks. your code works! Now I have to figure out the http part but it looks like I need a server, I thought I wasn’t going to need a server. anyway, on to the next step.

Here is my entire code:

// Jays Well Monitor
// Instantiate the sensor
local output = OutputPort(“Monitor”, “number”);

string <- “”;

    // Schedule the next poll in 100ms

function poll() {
imp.wakeup(0.1, poll);
local b=hardware.uart57.read();
while(b >= 0) {
if (b < 32) {
// Treat any control character as EOL and send string
server.show(string);
output.set(string);
string = “”;
} else {
string += b.tochar();
}
b = hardware.uart57.read();
}
}

hardware.uart57.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS); // pin 7 in RxD

// Register with the server
imp.configure(“Jays Well Monitor”, [], [output]);

poll();

server.show(“Hello Out There”);
// End of code.

Depends on what you want to do; with the code above and an HTTP out you can have it send the string to a server.

Agents, due to enter beta soon, will let you just hit a URL on our server and get this data back, no other server required.

I’m using uart57 at 9600 baud and noticed that it drops received characters. I have strings up to several hundred chars long and it will drop a bunch in the middle. I tried adding delays in sending the chars but that didn’t help, however changing the baud rate to 4800 or below fixes the problem. Does uart57 have a known problem at 9600 baud?

My system is working pretty well now (at 4800). I have made a lot of changes, I’ll post the code and description of the project as soon as I get it cleaned up a bit.

Should be no issue with data rates like that, but the buffer is only ~80 bytes right now. How are you reading them? You need to ensure you empty the buffer every time your imp.wakeup fires.

You can fire wakeups 100 times per second so in theory (and yes, it won’t ever work this neatly) you should be able to support up to 80x100 = 8,000 bytes per second (or about 80,000 baud) with the current code. Release-12 has callbacks on uart data ready so you’re not restricted by how fast imp.wakeup’s can call you… and we’ll have settable buffer size at some point.

I’ll do some experiments. I have a theory the problem is this code:
string += b.tochar();
which adds each new char to the string. Doesn’t it have to re-allocate the string and copy it for every char? I’m going to put a gpio out around this and measure how much time it takes with the scope.

It does indeed re-allocate the string and copy it. In fact it recalculates a hash of it as well. If you’re doing this several hundred times, it may well be that the Squirrel garbage collector has to run to tidy up all your partial strings. It’d be much more efficient to use a blob pre-allocated to the maximum input size – though of course that means you do need to know the maximum input size.

Peter