CSV to PHP, works but is butt ugly.

It is working properly on my server. What version of PHP are you running? What server are you on?

What exactly is the code in new.php?

http://galaxyinternet.us/jackbarnes/

http://galaxyinternet.us/jackbarnes/read_csv_reverse_advanced.php?rows=30

http://galaxyinternet.us/jackbarnes/read_csv_reverse_advanced.php?rows=13

http://galaxyinternet.us/jackbarnes/read_csv_reverse_advanced.php

I am using a Rasberry Pi as the Linux box. It has http://bammo.info/phpinfo.php diagnostic available.

the quick answer is PHP Version 5.4.4-14+deb7u7

System Linux raspberrypi 3.10.25+ #622 PREEMPT Fri Jan 3 18:41:00 GMT 2014 armv6l

your working example of Jims code is exactly what I was hoping for.

Let me see your new.php exactly as you have it.

Here is a C&P of it. [php]<?php

function readLog($fileName, $rows) {
if (!file_exists($fileName)) {
return false;
}
$fl = fopen($fileName, ‘r’);
for ($x_pos = 0, $ln = 0, $output = array(); fseek($fl, $x_pos, SEEK_END) !== -1; $x_pos–) {
$char = fgetc($fl);
if ($char === “\n”) {
if (++$ln === $rows) {
break;
}
continue;
}
$output[$ln] = $char . ((array_key_exists($ln, $output)) ? $output[$ln] : ‘’);
}
fclose($fl);

return $output;
}

$rows = !empty($_GET[‘rows’]) ? (int) $_GET[‘rows’] : 10;
$data = readLog(‘widgets.csv’, $rows);

$i = 1;
foreach ($data as $line) {
echo $i . ': ’ . $line . ‘
’;
$i++;
}

?>[/php]

Nothing wrong with what you posted. Problem is on your server. Set permissions on the csv file to 777 and see if that changes anything.

Thank you for the efforts. I have no clue why this isnt displaying. I have changed the settings for CSV and for the new.php too. Both read -rwxrwxrwx 1 root root

No luck tonight. I guess its time to sit back and ponder why. I am logged in as root so its not a permission issues from the admin point of view. Your code parses the CSV.

Thank you for getting the original upgrade to work, and for the continued effort tonight. I may try a different linux arm chip / flavor install down the road if I dont get around this issue with this install. This code enhancement is what I need to work. The next question is what solution to take.

I want to thank Jim and Benanamen for their help. The code was correct, it was throwing an error for a hidden \xc2 issue. Once I sterilized the C&P and reloaded the new file, it worked perfectly.

Its results are as follow:

1:
2: Soil; 814
3: Soil; 814
4: Soil; 815
5: Soil; 815
6: Soil; 815
7: Soil; 815
8: Soil; 814
9: Soil; 814
10: Soil; 813

Any thoughts on how to snip the first one from showing up blank? ie start with #2 for the display?

Thank you again guys for a major upgrade to the project, in how it displays its results.

I have moved the new.php to the index page now for the RasPi. www.bammo.info with it displaying 2 lines. 1st blank, and 2 with latest reported result.

Well, you could do this

[php]<?php

function readLog($fileName, $rows) {
if (!file_exists($fileName)) {
return false;
}
$fl = fopen($fileName, ‘r’);
for ($x_pos = 0, $ln = 0, $output = array(); fseek($fl, $x_pos, SEEK_END) !== -1; $x_pos–) {
$char = fgetc($fl);
if ($char === “\n”) {
if (++$ln === $rows+1) { // ugly hack to remove first empty row
break;
}
continue;
}
$output[$ln] = $char . ((array_key_exists($ln, $output)) ? $output[$ln] : ‘’);
}
fclose($fl);
array_shift($output); // ugly hack to remove first empty row

return $output;
}

$rows = !empty($_GET[‘rows’]) ? (int) $_GET[‘rows’] : 10;
$data = readLog(‘widgets.csv’, $rows);

$i = 1;
foreach ($data as $line) {
echo $i . ': ’ . $line . ‘
’;
$i++;
}[/php]

But as said in the comments it’s an ugly hack. It will always remove the first entry no matter what it containts.

WIll the empty line always be there? Is it possible to have multiple empty lines?

oh and a ps: the csv data you have isn’t really csv… :stuck_out_tongue:

Make sure you dont have any line breaks after the last record. That will get rid of the blank line. I simply backspaced the last empty row from your data.

See here:http://galaxyinternet.us/jackbarnes/read_csv_reverse_advanced.php?rows=6

As Jim commented, this isn’t really CSV, its a txt log file saved as a CSV. It appears that the single space is always in the file, as the putty serial monitor updates the logs from the Arduino serial data. I added Jims Hack to index.php for bammo.info and it works perfectly.

In the future, I will be adding more sensors to the output display. They will be reported as a single row of info. The historical results are for diagnostic work, if the sensors report a significant change in results.

This unit is for my wifes garden, but I have professional farmer friends, following this build who are interested in building these units for near their giant water irrigation well circles, where they could see what the condition is of the moisture of the soil remotely (4-6 inches below soil top), before turning on the 750 gallon per minute beasts.

Thank you again both Jim & Benanamen.

If you are to log more data then I would consider changing the way the pi works

You could build this as a SaaS app, where other farmers can buy∕build your device, set up wifi and just let it do its magic. Then it will post data to your API which the users again can log in to. By getting data systematically instead of reading a huge text-file you’re able to do all sorts of fun stuff. Like building graphs which let them compare with last week/month/year, etc etc.

Jim,

Agree completely, this is the direction that this project is headed. First was to prove I could get the Ardu to Pi reporting in the field (garage in a giant pot at moment) wirelessly to the web. Once the next batch of sensors arrive, I will be redoing the coding and looking towards developing a solution like yours.

Any suggestions on must read books for idiots with stupid ideas in their garage? :smiley:

You will most likely see me here again in a few weeks looking lost and confused.

Best,

Jack

I’d reconsider using the Pi at all, afaik the Arduino can (with a wifi/eth shield) post data over the net on its own.

I would probably do something like this:

Generate an UUID for each arduino (you can find php functions for this). This can be used both as a identification and a licensing number.

At spesific times I would check the sensor and post data to the API

post data with the UUID:
[php]{
“id”: “8fd0cb6b-1716-4c09-9122-505cb84bf869”,
“data”: [
{
“time”: “1391969828”,
“someData”: “value”
}
]
}[/php]
if you get no response or an error from server side then store the data to a local store, so that you can post it next time you post data, it may now look something like this:
[php]{
“id”: “8fd0cb6b-1716-4c09-9122-505cb84bf869”,
“data”: [
{
“time”: “1391969828”,
“someData”: “value”
},
{
“time”: “1391944628”,
“otherData”: “value”
}
]
}[/php]

[hr]

On the server side you use json_decode to convert the json to an array of objects, and then store the data to your database.

Jim,

I have ordered a wifi shield for the Ardu, and hope that I can spend next weekend on freeing the Ardu. The irony is that a Pi with Wifi Doggle is cheaper than the standard wifi shield alone is for the Ardu. So, while I waited on the wifi shield I ordered from China to arrive, I used the Pi sitting on my work bench to prove the first concept.

There is currently a deal where you could put a GSM cell on an Ardu with free 1000 SMS per month for a year for the same cost as the Pi and or the Wifi shield. I am pondering Ardu SMS to Cloud for the end solution. It free’s the units of needing Wifi at the well site.

That sounds like a plan, less things that can (will) go wrong is better :slight_smile:

Then you will probably get a unique number pr gsm cell, and you can just send a csv with data (real comma separated)

like this
1391969828:815
or
1391969828:815,1391944628:816

How will you catch the sms messages though?

Jim,

One of the cool features about the Pi, and why its part of this build beside on my workbench, is that I bought the IR Camera module with the near blue spectrum.

This allows the box to take photos at night, to monitor the health of the photosynthesis in the plants. The cam adds $35 but, single pictures at Dawn, Noon, Sunset and Midnight, would capture the health of the plants as they grow remotely.

http://missionscience.nasa.gov/ems/08_nearinfraredwaves.html

And that is how I sneaked Nasa into my geek garden box. lol

An added value feature for farmers willing to pay for an upgraded SaaS account. I am pondering Ardu with Cell shield with USB to Pi, with Pi controlling the Ardu and Cell via serial as needed. For Two way telcom.

The SMS is to the farmers phones. The capture of SMS data is to be decided.

A simple work around solution is to use www.freedompop.com and plug a small portable Mifi with 500 megs of free data per month of upload capacity into each box for $50ish. Then we have the Cell to Wifi handled by a free service provider and back to Ardu or Ardu with Pi Cam for upgraded monitoring.

So, now that this is stable and flowing smoothly, I was wondering how would I produce a chart of the data?

Thoughts on http://pchart.sourceforge.net/documentation.php?topic=advexemple22 should I even ponder chasing down the install parts necessary to make this work? What would the guru’s of making the data clean use to display this data if they were doing this?

I like this script
http://www.chartjs.org/docs/

Looks good and easy to use

Jim,

My friend who helps/mocks is a .js guy loved your idea, I have node-0.25 running, but I am so blind on .js, I figured a php solution must be easier. If you have any solutions for my install please share.

best,

Jack

a PHP solution will definitly not be easier.

If you include this and jquery all you have to do is write a php script that fetch the data you want to display and return a proper json response with labels and data. Then use a simple jquery ajax request to fetch data from PHP and insert it into the chart.

I’m sure your js-friend can help out with the js-part, if not we can do it here. Except the queries for the data all this (php+js) is possible to accomplish in like 10 lines of code.

Sponsor our Newsletter | Privacy Policy | Terms of Service