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?
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.
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?
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);
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:
/* 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();
}
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>
/* 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();
}