Datalogger (SD) combine with process(BridgeClient client)


I’m saving the values of the analog ports with time stamp on the SD card; it works great.
But my index.html will not update anymore with the values as soon I write the values on the sd card. Each part for itself (updating the website - datalogger) works.
Maybe it is a wrong approach, since I’m new to Arduino Yun. Any advice/help/comment is welcome, thx.

HTML code:

<!DOCTYPE html>
<title>Optimization System</title>

<style type="text/css">
html, body {

body {

#background {

#scrollbereich {

#inhalt {
  padding:20px 100px 30px 200px;

#footer {
    bottom: 0;
    width: 100%;

        <script type="text/javascript" src="zepto.min.js"></script>
          <script type="text/javascript">
                   function refresh() {

<img id="background" src="background.jpg" alt="" title="" />

<div id="scrollbereich">
<h1>Temperature Optimization System</h1>

<div id="inhalt">

<p>Eine mögliche Variante und Fenstergröße im Browser varrieren! Hintergrund austauschbar</p>

<table width="100%" border="2" bordercolor="#0080C0" cellpadding="3" cellspacing="3">
  <td><span id="temperatureSMU"></span></td>
  <td><span id="humiditySMU"></span></td>
  <td><span id="temperatureDatarecorder"></td>
  <td><span id="humidityDatarecorder"></td>
  <td><span id="temperatureLaser"></td>
  <td><span id="humidityLaser"></td>

<div id="footer">
<font size="-1">Administrator Panel</font>
<a href="http://incas.local/luci/webpanel/homepage"><img src="on.png" align="middle" border="0" width="" height=""></a>


        <body onload="setInterval(refresh, 500);">

Arduino Code:

#include <Bridge.h>
#include <BridgeServer.h>
#include <BridgeClient.h>
#include <FileIO.h>

// Listen to the default port 5555, the Yún webserver
// will forward there all the HTTP requests you send
BridgeServer server;

void setup() {
  // Bridge startup
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  digitalWrite(13, HIGH); //on-board LED as a status light to know when Bridge has started
  while (!SerialUSB); // wait for Serial port to connect.
  SerialUSB.println("Filesystem datalogger\n");

void loop() {

  // make a string that start with a timestamp for assembling the data to log:
  String dataString;
  dataString += getTimeStamp();
  dataString += " = ";

  // read analog sensors and append to the string:
  for (int analogPin = 0; analogPin < 6; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 5) {
      dataString += ",";  // separate the values with a comma

  File dataFile ="/mnt/sd/datalog.txt", FILE_APPEND);

  // if the file is available, write to it:
  if (dataFile) {
    // print to the serial port too:
  // if the file isn't open, pop up an error:
  else {
    SerialUSB.println("error opening datalog.txt");


  // Get clients coming from server
  BridgeClient client = server.accept();

  // There is a new client?
  if (client) {
    // Process request

    // Close connection and free resources.

  delay(50); // Poll every 50ms

void process(BridgeClient client) {
  // read the command
  String command = client.readStringUntil('/');
    if (command == "temperatureSMU") {
      int val = analogRead(A0);
    if (command == "humiditySMU") {
      int humiditySMU = analogRead(A1);
    if (command == "temperatureDatarecorder") {
      int temperatureDatarecorder = analogRead(A2);
    if (command == "humidityDatarecorder") {
      int humidityDatarecorder = analogRead(A3);
    if (command == "temperatureLaser") {
      int temperatureLaser = analogRead(A4);
    if (command == "humidityLaser") {
      int humidityLaser = analogRead(A5);

// This function return a string with the time stamp
String getTimeStamp() {
  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.addParameter("+%D-%T");  // parameters: D for the complete date mm/dd/yy
  //             T for the time hh:mm:ss;  // run the command

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

  return result;

I think your problem is the use of the delay() function. As is so often the case, this function generally does more harm than good. For a simple "blink an LED and do nothing else" type of sketch, delay() is fine, but in most other cases it does nothing but cause problems.

Because of the delay() calls, every pass through loop() will take a minimum of 2050 milliseconds, plus the time it takes to collect the analog samples and write to the disk file. During that pass through loop(), it will accept and process at most one web data request. One call to refresh() on your web page makes six web requests, so it will take a minimum of 12.3 seconds for one refresh operation.

If you want this sketch to perform reasonably well, you need to eliminate all calls to delay(). The loop() function must do what it needs to do and return as soon as possible. Since it only handles one connection request per call to loop(), it will take at least six iterations of loop() each time your web page's refresh() function is called. Note that it is NOT a good idea to keep the two second delay in place and simply call the check for a client connection multiple times.

Take a look at the Blink Without Delay example sketch, and understand how it works. This will be the key to getting it to work. Use that logic to make your data collection/disk update code run once every two seconds. Then keep the one call to check for a connection, and get rid of the 50 ms delay() call. What you will have is a loop() function that gets called frequently and returns quickly (which is the way it should be.) That will allow it to quickly handle multiple web requests, while the blink without delay code will still limit the data collection to once every two seconds.

What you have now in loop():

collect analog data and write data to disk

wait 2000 ms

if there is an incoming connection
   process connection

wait 50 ms

This always takes at least 2050 ms.

What you should have in loop():

if two seconds has passed since the last update
   collect analog data and write data to disk

if there is an incoming connection
   process connection

This will usually take almost no time. Every two seconds it will take a little longer to collect and store the data, and every time a web request comes in it will take a little time to process it. But generally, there will be little interaction between the two operations and the response time will be fast.