YunClient client.print not working as I'd like it to work

Hello world,

I'm trying to create my own commands via YunClient using the same principle as the many examples found on the internets (analogcommand, digital, dac, etc...)

In fact my command works almost fine, it's just that i'd like it to print on the browser that it is working.

A piece of code will explain better:

void digitolCommand(YunClient client) {
  client.print("Je ventile");
  analogWrite(5, 255);

All the previous steps are ok as I said before, I'm testing it with a fan that indeed runs 10secs and then stops.

But the same way that when I'm using blabla.local/arduino/digital/4/1 my browser displays "digital,4,1" I'd like that when I'm using blabla.local/arduino/digitol/(whatever) my browser displays "Je ventile"

Any reasons why it doesn't work?


My guess would be that the output is buffered and not sent immediately, and/or it doesn't like those long delays. Those long delays are also likely to cause problems with the loop accepting new connections.

Since you're not sending anything after the delays are done, I would print the message, set a flag that that the command has been received, and then exit the function do that the Client object can be closed and the response flushed out. Then, I would use the techniques in the BlinkWithoutDelay example to make a simple state machine to control the output when it sees the flag indicating the command was received.

The delay command can be problematical, especially when you are dealing with 20 seconds of delay. The loop() function should not take so long to run.

Hmm I don't understand much of your answer... But I try to test it with the millis() delaying technique. Thanks!

To make it more efficient, the network saves up data until it has enough to make it worthwhile to send the data. It doesn't send the data as each character is printed. So, your "Je ventile" message isn't enough to cause it to send the data over the network right away, the network is waiting until there is more data. It's like taking a bunch of kids to a movie: as each kid is ready, you're not going to drive them one at a time to the movie, you're going to wait until they are all ready and in the car, then you will go to the movie theater in one trip.

You may be able to tell the Client object to flush the little bit of data it has right away without waiting for more, but I don't think that will be enough. Your browser is also likely to wait until it has a lot of data or the connection is closed before it bothers to redraw the screen: to prevent the screen from jittering and bouncing around, the browser typically won't redraw the screen as each little bit of data comes in.

So, the simplest solution is to finish the transaction by sending the data and closing the Client object at the bottom of loop(). Once the Client is closed, it will know that no more data is coming, and it will send the waiting data immediately. Similarly, the browser will know that no more data is coming, and it will draw the page immediately.