title: Ajax add value to JSON to be graphed every second
body: <p>I have an Arduino via an Ethernet Shield printing out a value every second via AJAX. I would like to graph this value in <a href="http://flotcharts.org" rel="nofollow">Flot</a>. This requires the value to be put in a JSON format like this</p>
  
  <pre><code>[[epochtimestamp, value], [epochtimestamp, value]]
  </code></pre>
  
  <p>So I was wondering if you could help me use JavaScript/AJAX (or PHP if you think it is appropriate) to every second get this value and add it to the JSON inside the .txt file (used to store the previous values), so Flot can read all the values and create a time-based graph of these values but update the graph every second through AJAX.</p>
  
  <p>Here is the basic process which needs to happen every second.</p>
  
  <ol>
  <li>Arduino Updates Value (I've done that bit)</li>
  <li>Javascript or PHP file which adds this value in to the JSON with a timestamp </li>
  <li>Flot updates and graphs the new value.</li>
  </ol>
  
  <p>Here is some code I have started but it would have to be called by AJAX every second and wouldn't do the entire job.</p>
  
  <pre><code>&lt;?php
  $file    = 'data.txt';
  $webpage = 'test.txt';
  $t       = time(); // Open the file to get existing content 
  $current = file_get_contents($file);
  $data    = file_get_contents($webpage);
  // Append a new person to the file 
  if ($current &lt; 1) {
      $current .= "[[" + $t + "," + $data + "]";
  } else {
      $current .= ", " + "[" + $t + "," + $data + "]";
  } // Write the contents back to the file 
  file_put_contents($file, $current);
  echo $current;
  ?&gt;
  </code></pre>
  
  <p>I'm not sure if it would be easier to do with Javascript/AJAX?</p>