Leinoset.org Web development and Mobile development, Front - and Backend, Android applications & games, design

Front- and Backend development, Web and Graphical design, Mobile application development
Front- and Backend development, Web and Graphical design, Mobile application development

Python Temper-poll and temperature to website part 2

09 October 2015

This will continue Python Temper-poll and temperature to website tutorial.

Since it would be nice to record temperature data somewhre, part 2 will focus on that. Using a SQL database would be one solution but I think it's too much, and we create our database to .csv file. We will work now on Debian 7 environment, but it should work in other systems also. You'll need to have temperature monitoring working , like in first tutorial. There is now better solution for getting measurement from usb-stick , since I noticed that for some reason it was failing to get temperature quite often.

First We will need empty file temp.csv, you can create it for example,

touch temp.csv

Then we use same helper functions as before but bring something extra, we need to create php file for our script , let's call it temp.php.

function tempParts($temp, $index) {
    $parts = explode('.', number_format($temp, 1));
    return $parts[$index];
  Same as before we take temperature using python temper-poll 
$command = "temper-poll -cq"; // measurement in  Celsius

echo "measuring temperature...";

// initialize variables
$output = array();
$return_var = -1; // get return if our command is success or not.
$counter = 0;

while ($return_var != 0 && $counter<10) {
    // continue until we get temperature
    exec($command, $output, $return_var);
        if ($return_var != 0) {
            echo "<br>connection to usb stick failed.. please wait, trying again<br>";
            $counter++; // ten tries only.
            sleep (5); // give usb-stick 5 seconds to "wakeup"

if ($return_var != 0) {
   echo "Measuring failed..";
}else {
 foreach($output as $key => $value)
    $temp = $value;
    echo "temp value =".$temp."<br>";
$path ="/var/www/temp/"; // whole path to your cron job were to save temp.csv

/* You'll use path that suits for your system. I'm using Debian , of course :)
   Cronjob could actually have different path variables so therefore we give 
   full path to csv file.

$date = date('Y-m-d H:i');  //get current date and time for measurement.

$got_lock = false; // we don't have a lock yet for file we are writing.

while (!$got_lock) {  //do as long as got_lock is false

    if (flock($f, LOCK_EX)) {
        // we got lock and we can start writing csv file.
        $csvdata = $date.",".tempParts($temp,0).".".tempParts($temp,1);;

        flock($f, LOCK_UN); // release the csv file
        $got_lock = true; // set true that we have a success!
    } else {
        echo "Could not lock !";
                    // we could not get lock to file so,
        sleep (15); //we will sleep for 15s, and try again


echo "script runs perfectly :)";
exit(0);  // remember to end script

This and when you run script from your browser, it will return. measuring temperature...script runs perfectly :)

There we go , now it's working and you can check from temp.csv file that it should have new line in following format:

2015-10-09 19:04,24.1
2015-10-09 19:05,24.1

For part 3, we'll make cronjob to run this file at time's we would like to record temperature. If you have any suggestions or comments, any feedback is highly appreciated.


17 July 2015

Yesterday I finalized first release for testing of my food diary website Ruokapaivakirja.com . It first started out as school project and I did Android application for Flask REST service but hosting Flask application with zero budget is challenge. It could be actually one article about all the challenges I faced when trying to get Flask running in reasonable way. I decided to throw Flask application away and made it from the scratch as responsive website, which is mobile friendly and focus was to make it easy to use in mobile phone.
You can check website from: