Using server.show() and Arduino

I’m following the sparkfun tutorial for connecting an arduino to electric imp https://www.sparkfun.com/tutorials/397. I only have one arduino and imp, so I’m trying to get whatever I type in the arduino serial monitor to display in the imp node using server.show().

I’ve modified one of the functions in the sparkfun code to look like this:
`function pollUart()
{
imp.wakeup(0.00001, pollUart.bindenv(this)); // schedule the next poll in 10us

local byte = hardware.uart57.read();    // read the UART buffer
// This will return -1 if there is no data to be read.
while (byte != -1)  // otherwise, we keep reading until there is no data to be read.
{
    //  server.log(format("%c", byte)); // send the character out to the server log. Optional, great for debugging
//    impeeOutput.set(byte);  // send the valid character out the impee's outputPort
    
    server.show(byte)
    byte = hardware.uart57.read();  // read from the UART buffer again (not sure if it's a valid character yet)
    toggleTxLED();  // Toggle the TX LED
}

}`

server.show(byte) is only displaying seemingly random numbers. I have an idea of why this is, I just don’t know how to fix it because I’m not that familiar with UARTs and squirrel.

local byte = hardware.uart57.read(); reads in the ascii characters from the arduino in byte form (I think), and they’re not being ‘translated’ into their ascii characters before I use server.show(byte).
How do I do this in squirrel?
Also, I think polling every 10us is the wrong way to go here. I’d like to only poll when there’s new information, but I also don’t know how to do that in squirrel. Can someone point me to an example where this happens?

Thanks!

Hugo,

Thanks for the code. I tried it but the imp keeps restarting giving an “out of memory” error.

Steve

Are you sending a “
” (ascii 10) at the end of each line from the arduino? This will collect data until it gets one of those… you’d have to send it a lot of data without a linefeed, but if you did it would run out of memory eventually.

Here’s a couple threads that might be of interest :slight_smile:

Write bytes as characters / strings: http://forums.electricimp.com/discussion/comment/7340#Comment_7340
Creating callbacks for UART (also - hooking an Arduino up to an imp): http://forums.electricimp.com/discussion/comment/7243#Comment_7243

Try this:
server.show(byte.tochar());

thanks guys, I’ll try all this tonight and report back.

server.show(byte.tochar()) isn’t working… it’s no longer throwing random numbers at me. Now I’m just seeing a little question mark in a black diamond.

The diamond with a question mark in it means that you read a byte that isn’t standard ascii. What value is it printing when you get rid of the .tochar() (ie - what is the byte you’re reading from uart when you send the ‘r’).

I am having a similar issue. I have an Arduino connected to a compass that reads out degrees and sends them via serial. I would like to read the value on the imp and post it wirelessly to the Planner. Problem is that it receives only one number at a time. A reading of 340.6 degrees reads out as:
3
4
0

Is there a way to use server.show() to display the whole 340.6 value?

Thanks,

Steve

You should be collecting the data into a string, and waiting for the newline before printing it.

eg:

`
// string we’re collecting byte by byte
s <- “”;

function newdata() {
local d=hardware.uart1289.read();
while(d >= 0) {
if (d==’
’) {
// got whole line, show it
server.show(s);
s="";
} else {
// append to string if it’s printable, otherwise ignore
if (d>=32) s = s+d.tochar();
}

d = hardware.uart1289.read();

}

// set up uart here, using newdata as the new data callback.
hardware.uart1289.configure(38400, 8, PARITY_NONE, 1, NO_CTSRTS, newdata);

`

Thought I was, but it was not. Fixed the Arduino code and it works like a charm!! Thanks for your help. I think I am starting to get the hang of this!

Best,

Steve

slibutti, would you mind posting your code that worked for you (for the imp and arduino?) Thanks!

Sure. Not “my” code as I have just made minor mods on the Arduino code from the compass tutorial on Adafruit and the imp code is all Hugo’s. Here is the arduino code:

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303.h>

/* Assign a unique ID to this sensor at the same time */
Adafruit_LSM303_Mag mag = Adafruit_LSM303_Mag(12345);

void setup()
{
Serial.begin(9600);
Serial.println(“Magnetometer Test”); Serial.println("");

/* Initialise the sensor /
if(!mag.begin())
{
/
There was a problem detecting the LSM303 … check your connections */
Serial.println(“Ooops, no LSM303 detected … Check your wiring!”);
while(1);
}
}

void loop()
{
/* Get a new sensor event */
sensors_event_t event;
mag.getEvent(&event);

float Pi = 3.14159;

// Calculate the angle of the vector y,x
float heading = (atan2(event.magnetic.y,event.magnetic.x) * 180) / Pi;

// Normalize to 0-360
if (heading < 0)
{
heading = 360 + heading;
}
Serial.print(heading);
Serial.print(’
’);
delay(500);
}

and here is the imp code

s <- “”;

function newdata() {
local d=hardware.uart57.read();
while(d >= 0) {
if (d==’
’) {
server.show(s);
server.log(s);
s="";
}else {
if (d>=32) s = s+d.tochar();
}

    d = hardware.uart57.read();
}

}

hardware.uart57.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, newdata);
imp.configure(“Compass”, [], []);

Steve

Something happened with my cut and paste on the Arduino code. The beginning should be:

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303.h>

S

Did it again!!!

#include Wire.h
#include Adafruit_Sensor.h
#include Adafruit_LSM303.h

Ah, this forum software is a bit rubbish and it “expects” people to put code inside “code” tags (the “C” button above the edit window):
#include <stdio.h>

Peter

Thanks Peter. I will replace code properly.

Steve

Arduino Code

`#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303.h>

/* Assign a unique ID to this sensor at the same time */
Adafruit_LSM303_Mag mag = Adafruit_LSM303_Mag(12345);

void setup()
{
Serial.begin(9600);
Serial.println(“Magnetometer Test”); Serial.println("");

/* Initialise the sensor /
if(!mag.begin())
{
/
There was a problem detecting the LSM303 … check your connections */
Serial.println(“Ooops, no LSM303 detected … Check your wiring!”);
while(1);
}
}

void loop()
{
/* Get a new sensor event */
sensors_event_t event;
mag.getEvent(&event);

float Pi = 3.14159;

// Calculate the angle of the vector y,x
float heading = (atan2(event.magnetic.y,event.magnetic.x) * 180) / Pi;

// Normalize to 0-360
if (heading < 0)
{
heading = 360 + heading;
}
Serial.print(heading);
Serial.print(’
’);
delay(500);
}`

imp Code

`s <- “”;

function newdata() {
local d=hardware.uart57.read();
while(d >= 0) {
if (d==’
’) {
server.show(s);
server.log(s);
s="";
}else {
if (d>=32) s = s+d.tochar();
}

    d = hardware.uart57.read();
}

}

hardware.uart57.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, newdata);
imp.configure(“Compass”, [], []);`