YunClient client = server.accept();

Hi everyone

excuse my bad english, i will try to write my question.

The function YunClient client = server.accept(); reads the Url when i call the Sketch from the Browser.

Example:

http://xxx.xxx.xx.xx/arduino/SX/1

By the first time of the Loop, all is ok, the Client is set and the Program gos on (sensordata will be wrote on a micro SD Card).

By the 2nd Loop the "Client" is empty. Why? And how can i set the Yun Client onesmore with the Calling Url to keeps runing the Programm and write Data on the Micro SD-Card.

I hope you can decipher my Question. :wink:

Peter

No apologies needed for your English, you made your point well. :slight_smile:

Please post your code. I suspect you have an issue with the way your server and client objects ore defined. Or perhaps you are not calling server.accept() each time through the loop? It’s hard to say without seeing the code.

When you post the code, please be sure to put it inside code tags, by clicking the button that looks like a scroll with blue <> angle brackets. That puts in the tags and lets you paste the code between them.

Hallo ShapeShifter

Thanks for answering. Oh, i forgott to poste the Code. :sleeping:
I think the YunClient client = server.accept(); is on the right place (Void Loop () ), but see the code by yourself.

What will i get?

I’m a Little bit confiused.

/* Dieser Code is based on Bridge-Example from http://arduino.cc/en/Tutorial/Bridge */

/*  Arduino Y�n 
	-Bridge 
	-YunServer/Client
	-Timestamp
	-Sd-Karte schreiben

	
	It will send Sensor Data every 5 Minutes to Micro SD-Karte in die Datei "/mnt/sda1/arduino/www/xxxxxx.txt" geschrieben.
	
	Columns are DATUM/ZEIT/LUFTFEUCHTIGKEIT/TEMPERATUR
	
	Possible Calls
	
	*	Call from the Browser to see the xxxx.txt file
		http://xxxxxxxxxxx/sd
	
	*	Starts reading the Sensordata and write to Micro SD-Card
		http://xxxxxxxxxxx/arduino/sd/0
		
	*       Beenden eines Laufenden Scripts
		http://xxxxxxxxxxx/arduino/stop/0
	
*/


#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
#include <Process.h>       			
#include <FileIO.h>        			
unsigned long previousMillis = 0;               //FRZAP: Merkt sich den Zeitpunkt der letzten Speicherung
unsigned long interval = 10000;                 //FRZAP: Zeitinterval   (5 Minuten)
//unsigned long interval = 300000;              //FRZAP: Zeitinterval   (5 Minuten)
//unsigned long interval = 900000               //FRZAP: Zeitinterval  (15 Minuten)
//unsigned long interval = 1800000              //FRZAP: Zeitinterval  (30 Minuten)


#include "DHT.h"           			
#define DHTTYPE DHT22      			
const int dhtPin = 8;      			
DHT dht(dhtPin, DHTTYPE);  


YunServer server;				

void setup() {
  
Serial.begin (9600);  
Serial.println("Void setup");				

  
pinMode(13, OUTPUT);	
digitalWrite(13, LOW);
Bridge.begin();									
 
server.begin();									

dht.begin();              						

FileSystem.begin();       
}

void loop() {
YunClient client = server.accept();
				

if (millis() - previousMillis > interval) {
previousMillis = millis();   

Serial.println("In 1. If"); 
  
  if (client) {

  Serial.println("In 2. If"); 
  process(client);							

  client.stop();								
  }

  delay(50); 									
}
}


void process(YunClient client) {
Serial.println("process");				
  
  String command = client.readStringUntil('/');				
  
  if (command == "sd") {						
    sdCommand(client);
  }
   if (command == "stop") {						
    stopCommand(client);
  }
}


void stopCommand(YunClient client) {
Serial.println("stopCommand");				
}

void sdCommand(YunClient client) {

Serial.println("sdCommand");				

    float h = dht.readHumidity();			
    float t = dht.readTemperature();			
    
    String rowData;								
    rowData += getTimeStamp();
    rowData += ";";
    rowData += h;
    rowData += ";";
    rowData += t;

Serial.println("Schreiben auf SD-Karte");

	File dataFile = FileSystem.open("/mnt/sda1/arduino/www/xxxxxx.txt", FILE_APPEND);


if (dataFile) {
    dataFile.println(rowData);					
    dataFile.close();						
    Serial.println(rowData);				
    
    digitalWrite(13, HIGH);					
    delay(2000);
    digitalWrite(13, LOW);
    delay(2000);
    digitalWrite(13, HIGH);
    delay(2000);
    digitalWrite(13, LOW);

  }
else {
    Serial.println("error opening datalog.txt");
} 
}

// Diese Funktion gibt einen Timestamp zur�ck
String getTimeStamp() {

  
  String Tag;
  String Monat;
  String Jahr;
  String Zeit;

  String result;
  Process time;
  // date is a command line utility to get the date and the time 
  // in different formats depending on the additional parameter 
  time.begin("date");
  time.addParameter("+%D-%T");  // parameters: D for the complete date mm/dd/yy
                                //             T for the time hh:mm:ss    
  time.run();  // run the command

  // read the output of the command
  while(time.available()>0) {
    char c = time.read();
    if(c != '\n')
      result += c;
      
      
  }

Tag = result.substring(3,5);
Monat = result.substring(0,2);
Jahr = "20" +  result.substring(6,8);
Zeit = result.substring(9,17);

//string.concat(string, string2)
result = Tag + "." + Monat + "." + Jahr + ";" + Zeit;
return result;
}

I see a couple things...

One, which may or may not be a problem, is that you don't have the call to server.listenOnLocalhost() before the server.begin(). This tells the YunServer to listen for connections from the local web server running on the AR3391 processor. (That server is the one that actually processes the incoming web request, sees the /arduino/ token at the beginning of the URL, and then passes the request to the YunServer in the sketch.

But the bigger issue is that loop() starts out by calling server.accept() at the top of the loop, which is correct. But it doesn't look to see if it actually returned a client connection unless your time interval has passed -- the "if (client)" check is inside the "if (millis() - previousMillis > interval)" code block.

With the accept outside of the loop, it will check for and accept an incoming connection through each iteration of the loop. But it will only try to process that connection when your time interval has passed, otherwise it will just throw away and ignore the connection. With the way it is now, the only time you will ever be able to process an incoming connection is if the connection is established in that microsecond of time between the call to loop() where the time interval has passed, and the previous call to loop(). The odds are VERY low that you will ever hit that window.

You need to call server.accept() in every iteration of your loop, like you do, but you also have to check for an process the client on every loop, as well.

I think the general idea you need for your loop is:

void loop()
{
   client = server.accept
   if (client)
   {
      process client
   }

   if (time interval passed)
   {
      if (logging enabled)
      {
         process and store a sample
      }
   }
}

Hallo ShapeShifter

Many thanks for your answer. I tried to replay. I wrote 30 minutes for a replaypost in my best english. :wink: But when i preview the post it was lost. :smiling_imp:

So i write you onesmore but with less Details. It seams i understand your description and i will test it on Weekend. I will Report after testing.

Many thanks
Peter

Perseus01:
But when i preview the post it was lost. :smiling_imp:

I hate it when that happens! When you rewrite the post, it never seems as good as the version that was lost. :cry:

I've had that problem a lot in the past, especially with another forum. I've gotten into the habit that when I have a long post, I select it all and copy it before hitting preview or post. That way, if there's a problem, I can just paste the copied post and not lose anything. It's a good habit to develop. :wink:

Normally i do so too. But this time i don't. :sunglasses:

Hi ShapeShifter

What have i done?

I declared a Global String Variable STATUS

String Status = "";

The void Loop() Part with your hints:

void loop() 
{
				
YunClient client = server.accept();

  if (client)
   {
    Serial.println("In if(client)"); 
     process(client);
   }

  if (millis() - previousMillis > interval) 
   {
    Serial.println("if (millis() - previousMillis > interval)"); 
    previousMillis = millis(); 
    process(client);  
   }  
}

Finaly the Part void process(YunClient client)
At the first call command is set per example "sd". Than i store "sd" in Variable Status. By the second turn of the loop command isn't set. I set the command variable with Status. Finish !!!

I think it works perfect.

Many thanks

void process(YunClient client) 
{

  Serial.println("process");				
  String command = client.readStringUntil('/');	                        
  
  if (command != "") {
    Status = command;
  }
  else {
    command = Status;
  }
  
 
  if (command == "sd") {						
    sdCommand(client);
  }
   if (command == "stop") {						
    stopCommand(client);
  }
}