Spike Brewing 12.5 Conical Fermenter Giveaway!

Home Brew Forums > Home Brewing Beer > DIY Projects > TeensyPi Networked Temperature Controller

Reply
 
LinkBack Thread Tools
Old 01-31-2013, 01:24 PM   #91
redbenn
Feedback Score: 0 reviews
Recipes 
 
Join Date: Dec 2009
Location: Chicago
Posts: 133
Liked 3 Times on 3 Posts

Default

This is where I diverged from the Adafruit tutorial, as I know PHP so this was easier for me...

You will need the PHP-Curl library installed to post/get from COSM. You may have to install this if it is not already on the Raspberry Pi. To check to see if it is installed, create a new PHP file named phpinfo.php in your /var/www/htdocs folder with the text:

Code:
phpinfo()
Then open the file in your web browser and go to teensypi.local/phpinfo.php. Search for "curl" in the text, if nothing comes up, then you will have to install the library.

From the command line, type in:
Code:
sudo apt-get install php5-curl
This will update some of the php5 libraries that were installed, and now install curl. Once it is finished installing, restart the webserver
Code:
sudo service apache2 restart
__________________
redbenn is offline
 
Reply With Quote Quick reply to this message
Old 01-31-2013, 01:30 PM   #92
redbenn
Feedback Score: 0 reviews
Recipes 
 
Join Date: Dec 2009
Location: Chicago
Posts: 133
Liked 3 Times on 3 Posts

Default

I added a table to the MySQL database called cosm.

Log into phpmyadmin at http://teensypi.local/phpmyadmin/. Go to the TeensyPi database.

Click on the SQL link and paste the following code:

Code:
CREATE TABLE IF NOT EXISTS `cosm` (
  `id` int(6) NOT NULL AUTO_INCREMENT,
  `chipid` varchar(50) NOT NULL,
  `cosmfeedid` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;
Click GO, this will create your cosm table. I have this to check if a datastream for a chip has already been created.
__________________
redbenn is offline
 
Reply With Quote Quick reply to this message
Old 01-31-2013, 01:33 PM   #93
redbenn
Feedback Score: 0 reviews
Recipes 
 
Join Date: Dec 2009
Location: Chicago
Posts: 133
Liked 3 Times on 3 Posts

Default

Next download the COSM (formally known as Pachube) PHP library from here: https://github.com/MunGell/PachubeAPI. Then upload the PachubeAPI.php file to your /var/www/htdocs directory.

This will create functions that make it easy to get/post data from/to COSM.

Next create a new PHP file in your /var/www/htdocs directory called cosm.php.

This will be the file we will use to post our data to COSM.

Most of the code is taken from updateStatus.php to get the data from the chips.

Here is the code for cosm.php

Code:
<?php


include('PachubeAPI.php');
$pachube = new PachubeAPI("YOUR_API_KEY"); // replace with your API KEY
      $feed = YOUR_FEED_ID;  //replace with your Feed ID
      
      $user = "USER_NAME";  //replace with your username


  include_once("makeASocket.php"); //allows us to read the chips
  include_once("accessDatabase.php"); //accesses the MySQL database
  $tempStr="";
  $switchStr="";
  $unusedStr="";
  $switchjStr="";
  

  $newSocket = makeASocket($service_port, $address);
  $in = $getAllStatus."\n";
  socket_write($newSocket, $in, strlen($in));
  $chipX = socket_read($newSocket, $socBufSize);
  socket_close($newSocket);

  $status=explode(",", $chipX);
  $count = count($status);

// Query to get chip name and id. This will create the elements $cosmid and $temp for the COSM API
  
  for($x=0,$y=0,$z=0;$x<$count;$x++)
  {   
    $query = "select * from chipNames where id='".$x."'";
    $result = mysqli_query($link, $query);
    $row = mysqli_fetch_row($result);
    mysqli_free_result($result);
    if(is_numeric($status[$x]) === TRUE)
    {
      $query2 = "SELECT * FROM cosm where chipid = '". $row[1] . "'";
      
      $result2 = mysqli_query($link,$query2);
      $row2 = mysqli_fetch_row($result2);
      mysqli_free_result($result2);
      $cosmid = $row2[2];
      

      
      $temp = $status[$x];
//debug text
      //echo "<br><br>" . $cosmid. ": "; 
      //echo $temp;
//end debug text
    
//call the $pachube function from PachubeAPI.php, and update the data stream
      $pachube->updateDatastream("csv", $feed, $cosmid, $temp);
    }
}
?>
If you would like to track your switches as well, add the following code after this line:
Code:
//call the $pachube function from PachubeAPI.php, and update the data stream
      $pachube->updateDatastream("csv", $feed, $cosmid, $temp);
    }
Code:
else if((($trimStr = trim($status[$x])) === "N") || (($trimStr = trim($status[$x])) === "F") ){
$query2 = "SELECT * FROM cosm where chipid = '". $row[1] . "'";

$result2 = mysqli_query($link,$query2);
$row2 = mysqli_fetch_row($result2);
mysqli_free_result($result2);
$cosmid = $row2[2];

if ($status[$x]=="N") {$temp = 1;} else {$temp = 0;}


//echo "<br><br>" . $cosmid. ": ";
//echo $temp;
//$pachube->updateDatastream("csv", $feed, $cosmid, $temp);
$pachube->updateDatastream("csv", $feed, $cosmid, $temp);
}
__________________
redbenn is offline
 
Reply With Quote Quick reply to this message
Old 01-31-2013, 01:51 PM   #94
redbenn
Feedback Score: 0 reviews
Recipes 
 
Join Date: Dec 2009
Location: Chicago
Posts: 133
Liked 3 Times on 3 Posts

Default

You will also have to modify updateNames.php. This will add the ability to create COSM datafeeds when a new chip is named. I set it up to tie the chip address to the feed as opposed to the chip ID since they will move around IDs, but the address will stay the same.

First add references to the PachubeAPI, insert this code in between the php tags near the:

Code:
  <?php 
      include_once("makeASocket.php");
      include_once("accessDatabase.php");
      include_once("header.html");
      $h2Header = "<font color=\"blue\">Update Names</font>";
      $tempAddrStr = "";
      $switchAddrStr = ""; 
      

     //INSERT PACHUBE CODE HERE
    ?>
Code:
      include("PachubeAPI.php"); 
      $pachube = new PachubeAPI("YOUR_API_KEY");
      $feed = YOUR_FEED_ID;
      $user = "YOUR_USERNAME";

Next add a query that will check to see if the chip is a DS18B20. Then it checks if there is already a COSM feed create, if not it will create the feed.

Insert the code after this block of code updating the chip names:
Code:
 $chipAddress = $_POST["address$updateCnt"];
                $result = mysqli_query($link,$query);
                $escapedName = mysqli_real_escape_string ($link , $_POST["name$updateCnt"]);
                $query = "update chipNames set address='".$chipAddress."', name='".$escapedName."' where id='".$updateCnt."'";
                $result = mysqli_query($link,$query);
/*
                if($result === FALSE)
                {
                  echo "query failed";
                }else{
                  echo "query success";
                }
                echo "<br />result = ".$result."<br />";
*/
Insert this code:
Code:
if(substr($chipAddress,0,4)=='0x28') {
                
                  $query2 = "SELECT * FROM cosm where chipid = '" . $chipAddress . "'";
                  $result2 = mysqli_query($link,$query2);
                  if(mysqli_num_rows($result2)==0){
                    $data = $escapedName . "," . (int)hexdec(substr($chipAddress,-4));
                     $feedname =  preg_replace('/\s+/', '', $escapedName);
                    $data = preg_replace('/\s+/', '', $escapedName) . ",0";
                    $pachube->createDatastream("csv", $feed, $data);
                    
                    $query3 = "INSERT INTO cosm (id, chipid, cosmfeedid) VALUES (NULL, '" . $chipAddress . "', '" . $feedname . "')";
                    $result3 = mysqli_query($link,$query3);

                    $h3Header= $h3Header . "<br><br>COSM datastream created for ". $data;
                    
                    } else
                    {
                      $h3Header= $h3Header . "<br>COSM datastream already exists for " . $escapedName;
                    }
If you would like to track your switches as well, delete the first line:
Code:
if(substr($chipAddress,0,4)=='0x28') {
and also the last } in this section to get this code:
Code:
$query2 = "SELECT * FROM cosm where chipid = '" . $chipAddress . "'";
                  $result2 = mysqli_query($link,$query2);
                  if(mysqli_num_rows($result2)==0){
                    $data = $escapedName . "," . (int)hexdec(substr($chipAddress,-4));
                     $feedname =  preg_replace('/\s+/', '', $escapedName);
                    $data = preg_replace('/\s+/', '', $escapedName) . ",0";
                    $pachube->createDatastream("csv", $feed, $data);
                    
                    $query3 = "INSERT INTO cosm (id, chipid, cosmfeedid) VALUES (NULL, '" . $chipAddress . "', '" . $feedname . "')";
                    $result3 = mysqli_query($link,$query3);

                    $h3Header= $h3Header . "<br><br>COSM datastream created for ". $data;
                    
                    } else
                    {
                      $h3Header= $h3Header . "<br>COSM datastream already exists for " . $escapedName;
I also added the $h3Header to show the status was updated.

In the next block of code here, you need to add the $h3Header below the $h2Header

Code:
<tr>
              <td align=\"center\" colspan=\"2\" border=\"2\">
                <h2>".$h2Header."</h2>
                <h3>".$h3Header."</h3>
              </td>
              </tr>

That should be it, next up we will create the cronjob to run every 5 minutes to update the COSM feeds.
__________________
redbenn is offline
 
Reply With Quote Quick reply to this message
Old 01-31-2013, 02:35 PM   #95
jimmayhugh
Turgid Member
HBT_LIFETIMESUPPORTER.png
Feedback Score: 0 reviews
 
jimmayhugh's Avatar
Recipes 
 
Join Date: Feb 2011
Location: Las Vegas, NV
Posts: 577
Liked 46 Times on 41 Posts
Likes Given: 2

Default

Quote:
Originally Posted by redbenn View Post
One of the changes I did was install VSFTPD so I could FTP into the TeensyPi. This allowed me to access the htdocs easier.

I did this as root@teensypi.local, as that is the owner ove the /var/www folder. I got the instructions from here.

Code:
Type in "sudo apt-get install vsftpd" and hit Return / Enter
Now type in "sudo nano /etc/vsftpd.conf" and hit Return / Enter

Search through the file and change the following lines:
anonymous_enable=YES Change To anonymous_enable=NO
#local_enable=YES Change To local_enable=YES
#write_enable=YES Change To write_enable=YES

Also, add a line to the bottom of the file:
force_dot_files=YES

Save the file.

Now restart the FTP server with "sudo servce vsftpd restart"
Pretty sure that sftp is already ensbled in the RPi, as I use Filezilla to upload and download files to/from the TeensyPi all the time.

My development machine is running CentOS 6.3, and I can connect directly to files on the TeensyPi with the "Connect To Server" option on the "Places" menu.
jimmayhugh is offline
 
Reply With Quote Quick reply to this message
Old 01-31-2013, 02:38 PM   #96
redbenn
Feedback Score: 0 reviews
Recipes 
 
Join Date: Dec 2009
Location: Chicago
Posts: 133
Liked 3 Times on 3 Posts

Default

Quote:
Originally Posted by jimmayhugh View Post
Pretty sure that sftp is already ensbled in the RPi, as I use Filezilla to upload and download files to/from the TeensyPi all the time.

My development machine is running CentOS 6.3, and I can connect directly to files on the TeensyPi with the "Connect To Server" option on the "Places" menu.
Ahh, could have been because I was trying to connect via FTP instead of directly mounting the TeensyPi. I use Transmit on OS X Mountain Lion.
__________________
redbenn is offline
 
Reply With Quote Quick reply to this message
Old 01-31-2013, 03:05 PM   #97
jimmayhugh
Turgid Member
HBT_LIFETIMESUPPORTER.png
Feedback Score: 0 reviews
 
jimmayhugh's Avatar
Recipes 
 
Join Date: Feb 2011
Location: Las Vegas, NV
Posts: 577
Liked 46 Times on 41 Posts
Likes Given: 2

Default

Quote:
Originally Posted by redbenn View Post
Ahh, could have been because I was trying to connect via FTP instead of directly mounting the TeensyPi. I use Transmit on OS X Mountain Lion.
OK. I've got an old 17" MacBook Pro running Snow Leopard, but haven't used it lately. Might be time to fire it up and see what works.
jimmayhugh is offline
 
Reply With Quote Quick reply to this message
Old 01-31-2013, 03:17 PM   #98
redbenn
Feedback Score: 0 reviews
Recipes 
 
Join Date: Dec 2009
Location: Chicago
Posts: 133
Liked 3 Times on 3 Posts

Default

To create the cronjob to update the cosm feed, we need to update crontab with the timing to run the job, the location of PHP, and the location of our cosm.php file we want to run.

For me PHP was located at /usr/bin/php. You can verify yours by typing find php from the command line.

To access crontab, from the command line type crontab -e

Scroll all the way to the bottom and enter:

Code:
*/5 * * * * /usr/bin/php /var/www/htdocs/cosm.php
Then exit and save.

The */5 is telling the cronjob to run every 5 minutes. You could change that to something else if you wanted it to run more/less frequently.

If you click on your feed in COSM, then click the gear and go to debug, you can monitor the API calls to see if your cronjob is running successfully.



This shows my requests every 5 minutes

__________________
redbenn is offline
 
Reply With Quote Quick reply to this message
Old 01-31-2013, 04:35 PM   #99
redbenn
Feedback Score: 0 reviews
Recipes 
 
Join Date: Dec 2009
Location: Chicago
Posts: 133
Liked 3 Times on 3 Posts

Default

Added the Connecting the TeensyPi to Cosm.com to log data guide to TeensyPi.com here:
http://www.teensypi.com/cosm/

__________________
redbenn is offline
 
Reply With Quote Quick reply to this message
Old 01-31-2013, 05:37 PM   #100
jimmayhugh
Turgid Member
HBT_LIFETIMESUPPORTER.png
Feedback Score: 0 reviews
 
jimmayhugh's Avatar
Recipes 
 
Join Date: Feb 2011
Location: Las Vegas, NV
Posts: 577
Liked 46 Times on 41 Posts
Likes Given: 2

Default

I noticed that you're monitoring just the temp sensors. Any reason not to have the option to monitor switch status as well? I'm assuming that you would just have a simple on/off chart.

__________________
TeensyNet Networked Controller
ButtCrack Brewery
And This Is Why We Drink...
jimmayhugh is offline
 
Reply With Quote Quick reply to this message
Reply


Quick Reply
Message:
Options
Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
Arduino - Networked Temperature Monitor jimmayhugh DIY Projects 81 01-27-2013 12:22 AM
Temperature Controller sy2088 Equipment/Sanitation 3 10-25-2012 06:28 PM
Lux Temperature Controller scoots DIY Projects 9 07-06-2012 01:38 PM
temperature controller from deas fridge - use as external controller on live fridge? BrewingChemist DIY Projects 1 05-22-2012 10:21 PM
Johnson Analog Temperature Controller Temperature Profile Coastarine Equipment/Sanitation 4 10-11-2010 03:58 PM