Yunclient write() only works once


hello, first time poster.

my objective have yun connect tcp service on home automation system (crestron), periodically request parameters (relating thermostat), , store them locally on yun. use tembo write them google spreadsheet, first step me in process of putting whole thing together.

so far, works great... once. sketch connects crestron succesfully, write() "poll" string crestron, receive data crestron, , parse data on yun side.

the trouble is, works once. subsequent iterations of write() function return 0. (the first iteration returns 6). can see on crestron debugger socket still open, no data received on socket.

does have fact yunclient invoked every iteration through loop? doesn't make sense, because invoked, "poll" command sent, , after 2nd call of yunclient when return data parsed.

thank help.

code: [select]

#include <console.h>
#include <yunclient.h>
ipaddress cp3(192,168,76,21);

string command, heatcall, coolcall;
float setpoint, currtemp;
unsigned long time;
unsigned long polllast;
const unsigned long polldelay = 4000;

void setup()
{
  pinmode(13, output);
  digitalwrite(13, low);
  bridge.begin();
  console.begin();
  digitalwrite(13, high);
 
  while (!console); // wait serial connection
  console.println("hello kevin");

}

void loop()
{
  yunclient crestron;
  crestron.connect(cp3,7654);
  time = millis();
 
  while (crestron.available())
  {
    char c = crestron.read();
    if ( c == '\n')
    {
      console.print("command ");
      console.println(command);
      parsecommand(command);
      command = "";
    }
    else
    {
      command += c;
    }
  }

  if ( time - polllast > polldelay )
  {
    int bytessent = crestron.println("poll");  //this works first time
    polllast = time;
    console.println(bytessent);
  }
 
  delay(1000); // shouldn't need this, right?
}


by putting related yunclient connection in loop(), making , breaking connection on every iteration. i'm surprised works first time, since appears saying response first poll isn't received until second iteration?

i think have more luck if make yunclient global, connect in setup(), reads , writes in loop(). should proof of concept requiring minimal changes.

if works, want make more robust (but don't add complication until basic comm working.) issue if connection lost (network gets unplugged, crestron closes socket reason, or crestron reboots) way communications reset yun.

to make more reliable, have flag says wether connection open or closed. set closed in setup(). @ top of loop, if flag says closed, open connection , set flag open. in body of loop, check , track errors. @ bottom of loop, if there errors, close connection , set flag closed.

this way, connection opened in 1 place @ top of loop, if necessary. , if there problem, connection automatically closed , reopened.

the alternative make connection @ top of loop(), send command , wait until response or timeout, close connection. way connection opened/closed every time, may lot of unnecessary overhead.


Arduino Forum > Products > Arduino Yún (Moderator: fabioc84) > Yunclient write() only works once


arduino

Comments

Popular posts from this blog

invalid use of void expresion in FlexiTimer2 library

error: a function-definition is not allowed here before '{' token

LED Strip Code