Issue with Connect to server

Hello all,

I am hoping someone can help me with my code as its not really working for me. I need to keep my Imp running as one of the features of a product we are developing is changeability using the imp as the base. The issue is that when I change the Connection Timeout policy it works but if I try a reconnect i.e. Connect (Function, Timeout) it locks up and my Imp stops running code and I have to Hard Reset.

Cheers

Harry

/*

Date : 23-10-13
Function : To allow ths mems system to transmit data and commands to the server
*/
/*********************************Below is the Variable Declaration for the Device Code **************************************************************************/
local Data = “”; // This is the data that is to be transmitted
local Status = “”; // Here we store the status of the Imp
local TimerToSend = 0; // Sending data is a issue with the AT Mega buffer overloaing so this Var hhelps by every 0.5 seconds
local impeeOutput = OutputPort(“UART In”, “string”); // set impeeOutput as a string
/*********************************Below is the Init for the ports on the Imp *************************************************************************************/
function initUart()
{
hardware.configure(UART_57); // Using UART on pins 5 and 7
hardware.uart57.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS); // 9600 baud worked well, no parity, 1 stop bit, 8 data bits
}
/*********************************Below is the CheckTimer Function its allows the device status to be sent *******************************************************/
function CheckTimer ()
{
TimerToSend += 1;
if (TimerToSend >= 500) // Check that 1/2 second has passed as we dont want to stress the AT Mega
{
hardware.uart57.write(Status); // Write t0 the AT Mega that Online \

hardware.uart57.write(’
’); // The Data is delimited by a newline so send
TimerToSend = 0;
if (Status == “Offline”)
{
Connect(GetData, 30); // Try and reconnect to the server
}
}
}
/*********************************Below is the Get Data function which is the main function ***********************************************************************/
function GetData()
{
imp.wakeup(0.001, GetData); // schedule the next poll in 100us
// The First thing we need to do is ensure the Imp is online as to send or store the data
if (server.isconnected()) // Is connected will allow us to do
{

Status = “Online”; // Declare online
local byte = hardware.uart57.read(); // read the UART buffer /
while (byte != -1) // otherwise, we keep reading until there is no data to be read.
{
Data+=format("%c", byte);
if ((format("%c", byte))=="
") // If there is a newline then we send the data to the MEMS server
{
server.log(Data); // Log the responce as to let a user see it
if (Status == “Online”) // If Online then we send it to the agent
{
impeeOutput.set(Data); // send the valid character out the impee’s outputPort
}
Data= “”; // Set the data to ""
byte = hardware.uart57.read(); // read from the UART buffer again (not sure if it’s a valid character yet)
}
else // Else add some more data to “Data”
{
byte = hardware.uart57.read(); // read from the Serial buffer again
}
}
}
else // If Offline
{
Status = “Offline”; // Set the status
}
CheckTimer(); // Check and see when the imp sent the last Status message to the ATMega
}

// This is where our program actually starts! Previous stuff was all function and variable declaration.
imp.configure(“MEMSV1.0”, [], [impeeOutput]);
initUart(); // Configure the Imps Serial Port
GetData(); // start the UART polling, this function continues to call itself
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 10);

GetData schedules itself to run again immediately. So it runs all the time (well, every 10ms). But then you also supply GetData as the connection callback – so when connection succeeds (or fails), GetData is called again, separately, so now you’ve got two lots of GetData being called all the time. If your network is persistently unavailable, you’ll get an additional chain of endless GetData’s being created every five seconds (10ms callback times 500 counts of TimerToSend).

Peter

peter,

Thanks for the advice, I did try a dummy function but to no avail. It is a case that the imp stops executing the code in the Getdata function. I will keep trying over the weekend.

Is the Connect () method working for sure or is it my code ? or is it my method of code dev ?

Again thanks

Harry

connect() definitely works. Possibly it would be clearer if you moved the connection maintenance/setup/status updating to functions separate from the getdata callback, though.

Hugo, Peter,

It was a case of the callback and the layout of my code again thank you so much.
I used it last night logging away to the SD card of my system and when I switched on my router , as expected it connected and started sending my data.

Attached is my code and again lads thanks so much,

Cheers

Harry

/*

Date : 25-10-13
Function : To allow ths mems system to transmit data and commands to the server
*/
/*********************************Below is the Variable Declaration for the Device Code **************************************************************************/
local Data = “”; // This is the data that is to be transmitted
local Status = “”; //Here we store the status of the Imp
local TimerToSend = 0; // Sending data is a issue with the AT Mega buffer overloaing so this Var hhelps by every 0.5 seconds
local impeeOutput = OutputPort(“UART In”, “string”); // set impeeOutput as a string
local OfflineCounter = 0; // Ok so we need to keep track of the offline Count
/*********************************Below is the Init for the ports on the Imp *************************************************************************************/
function SendToMEMs (Status)
{
hardware.uart57.write(Status); // Write t0 the AT Mega that Trying as to alert User
hardware.uart57.write(’
’); // This is to get it to Terminate
}
function initUart()
{
hardware.configure(UART_57); // Using UART on pins 5 and 7
hardware.uart57.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS); // 9600 baud worked well, no parity, 1 stop bit, 8 data bits
}
/*********************************Below is the CheckTimer Function its allows the device status to be sent *******************************************************/
function CheckTimer ()
{
TimerToSend += 1; // Check the Timer
if (TimerToSend >= 500) // Check that 1/2 second has passed as we dont want to stress the AT Mega
{
SendToMEMs(Status); // Send Server Status
TimerToSend = 0; // Reset the Timer
}
}
/*********************************Below is the Reconnect Code for the Imp ****************************************************************************************/
function onConnectedCallback(state)
{
// if we’re connected
if (state == SERVER_CONNECTED)
{
SendToMEMs(Status); // Send Server Status
}
else
{
Status = “Connection Failed”; // Set Status as onffine
SendToMEMs (Status); // Send Server Status

}
}

function Connect(callback, timeout)
{
// check if we’re connected before calling server.connect()
// to avoid race condition
if (server.isconnected())
{
callback(SERVER_CONNECTED); // if we’re already connected execute the callback
}
else
{
server.connect(callback, timeout); // If not then try and pass fail to callback
}
}
/*********************************Below is the Get Data function which is the main function ***********************************************************************/
function GetData()
{
imp.wakeup(0.001, GetData); // schedule the next poll in 100us
// The First thing we need to do is ensure the Imp is online as to send or store the data
if (server.isconnected()) // Is connected will allow us to do
{
OfflineCounter = 0; // As we are online
Status = “Online”; // Declare online
local byte = hardware.uart57.read(); // read the UART buffer /
while (byte != -1) // otherwise, we keep reading until there is no data to be read.
{
Data+=format("%c", byte); // Add the Next Byte
if ((format("%c", byte))=="
") // If there is a newline then we send the data to the MEMS server
{
server.log(Data); // Log the responce as to let a user see it
if (Status == “Online”) // If Online then we send it to the agent
{
impeeOutput.set(Data); // send the valid character out the impee’s outputPort
server.log(Data); // Display the Data On the Server
}
Data= “”; // Set the data to ""
byte = hardware.uart57.read(); // read from the UART buffer again (not sure if it’s a valid character yet)
}
else // Else add some more data to “Data”
{
byte = hardware.uart57.read(); // read from the Serial buffer again
}
}
}
else // If Offline
{
Status = “Offline”; // Set Status as Offline
OfflineCounter += 1; // Set the Offline Counter to inc
if (OfflineCounter >= 1100) // If the Offline Counter Excedes 1100 i.e. 11s Try and connect
{
OfflineCounter = 0; // Reset the Counter
Connect(onConnectedCallback, 30); // This is the Callback for the Connect and What does the Work
}
}
CheckTimer(); // Check and see when the imp sent the last Status message to the ATMega
}
/*********************************Below This is where our program actually starts! *********************************************************************************/
imp.configure(“MEMSV1.0”, [], [impeeOutput]);
initUart(); // Configure the Imps Serial Port
GetData(); // start the UART polling, this function continues to call itself
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 30); // Set the Call Back and connection policy as to keep Imp Alive