0

Example php script for the info widget
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Example php script for the info widget

by tvdbon on Mon Jul 28, 2008 5:23 pm

I've got some temperature sensors around the home and the value of each gets dumped into a mysql database. I wrote a little php script which gets the current/min/maximum values for two of the zones and returns in to the ybox2.

I'm still struggling to figure out the color codes and the clear screen code doesn't work for me, but apart from that it works nicely. Here is the script in case anybody is interested. Obviously you need to change the queries to your own database, but its a starting point.

If anybody can get the clear screen code to work or figured out how to do the colours then let me know.

Code: Select all | TOGGLE FULL SIZE
<?
    require_once( "lib/database.php" );

    connect_sql();

    echo chr(0x1);  // goto home
   
    echo chr(0x0);  // this is not working so we do our own clear
   
    for ( $c = 0; $c < 12; $c++ )
    {
        for ( $d = 0; $d < 10; $d++ )
            echo chr(0x09);
    }

    echo chr(0x1);  // goto home
   
    echo "            GeekHouse Dashboard         ";
    echo chr(0xD);
    echo "Temperatures:";
    echo chr(0xD);
    echo chr(0x09).chr(0x09)."Cur".chr(0x09)."Min".chr(0x09)."Max".chr(0x0D);
   
    // temp sensor outside
    $sql = "SELECT  value FROM `sensor_readings` where sensor_id='109D4EF800080050' ORDER BY `timestamp`  DESC limit 0,1";
    $result = mysql_query( $sql );
    $data = mysql_fetch_row( $result );
   
    echo "Outside: ".chr(0x09).$data[0]." C".chr(0x09);
   
    $sql = "SELECT  value FROM `sensor_readings` where sensor_id='109D4EF800080050'  order by value  ASC ";
    $result = mysql_query( $sql );
    $data = mysql_fetch_row( $result );
   
    echo "".$data[0]." C".chr(0x09);

    $sql = "SELECT  value FROM `sensor_readings` where sensor_id='109D4EF800080050'  order by value  DESC limit 0,1";
    $result = mysql_query( $sql );
    $data = mysql_fetch_row( $result );
   
    echo "".$data[0]." C".chr(0x09);
       
   
    // temp sensor bedroom
    $sql = "SELECT  value FROM `sensor_readings` where sensor_id='1023983F00080039' ORDER BY `timestamp`  DESC limit 0,1";
    $result = mysql_query( $sql );
    $data = mysql_fetch_row( $result );
   
    echo "Bedroom: ".chr(0x09).$data[0]." C".chr(0x09);
   
    $sql = "SELECT  value FROM `sensor_readings` where sensor_id='1023983F00080039'  order by value  ASC limit 0,1";
    $result = mysql_query( $sql );
    $data = mysql_fetch_row( $result );
   
    echo "".$data[0]." C".chr(0x09);

    $sql = "SELECT  value FROM `sensor_readings` where sensor_id='1023983F00080039'  order by value  DESC limit 0,1";
    $result = mysql_query( $sql );
    $data = mysql_fetch_row( $result );
   
    echo "".$data[0]." C".chr(0x09);
?>


I'll post a photo of what it looks like as soon as my camera is charged.
tvdbon
 
Posts: 14
Joined: Fri Jun 06, 2008 2:51 pm

by tvdbon on Tue Jul 29, 2008 3:10 pm

this is what it looks like so far :

Image
tvdbon
 
Posts: 14
Joined: Fri Jun 06, 2008 2:51 pm

by adafruit on Tue Jul 29, 2008 5:35 pm

too cool! (or hot, depending :) )

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

by tvdbon on Wed Jul 30, 2008 1:55 am

thanks ladyada, unfortunately I'm still struggling to get the colours right and the clear screen doesn't work for me at all.

But this is experiment #1, so I'll see how it goes. How difficult would it be to change the ethernet implementation to accept a connection instead of retrieving data from a remote server?
tvdbon
 
Posts: 14
Joined: Fri Jun 06, 2008 2:51 pm

by harrison on Wed Jul 30, 2008 2:24 am

the clear screen doesn't work for me at all

There was a small bug in the original infowidget that broke the clear screen control byte ($00). Darco fixed it a few weeks ago in the svn repository.

Specifically, the problem was this line:
Code: Select all | TOGGLE FULL SIZE
if (in := \tel.rxcheck) > 0

The > 0 condition dropped all 0's before they made it to the tv_text driver. You can also work around it by just sending a screen worth of spaces to push off any unwanted stuff.

How difficult would it be to change the ethernet implementation to accept a connection instead of retrieving data from a remote server?

The tcp/ip stack supports both client and server mode. Take a look at the .listen(...) method in api_telnet_serial.spin / driver_socket.spin. The webserver in infowidget.spin would probably be a good reference on how to wait for connections.
harrison
 
Posts: 8
Joined: Mon Jun 09, 2008 3:55 am
Location: Texas

by tvdbon on Wed Jul 30, 2008 4:40 am

There was a small bug in the original infowidget that broke the clear screen control byte ($00). Darco fixed it a few weeks ago in the svn repository.


Cool, I'll get the new widget then and reprogram it. Thats anyways on my todo list

(x) Compile and download new widget onto the ybox2

You can also work around it by just sending a screen worth of spaces to push off any unwanted stuff.


Yeah, thats what I did with the script, instead I sent a bunch of tabs.

The tcp/ip stack supports both client and server mode. Take a look at the .listen(...) method in api_telnet_serial.spin / driver_socket.spin. The webserver in infowidget.spin would probably be a good reference on how to wait for connections.


Ofcourse, I never even thought about the webserver, it also accepts incoming connections. This is starting to sound like my original Home Automation idea will work great.

Another question: I've only worked on AVR's before, no parallax or even basic stamps before, so what I want to try and do is write some small test programs as I learn to use the propeller and get a handle on spin. Will my own programs influence the ethernet bootloader ? I don't have propplug (planning on getting one when the money is available) so I don't want to lose functionality of uploading new programs (widgets).
tvdbon
 
Posts: 14
Joined: Fri Jun 06, 2008 2:51 pm

by darco on Wed Jul 30, 2008 8:38 pm

I've only worked on AVR's before, no parallax or even basic stamps before, so what I want to try and do is write some small test programs as I learn to use the propeller and get a handle on spin. Will my own programs influence the ethernet bootloader ? I don't have propplug (planning on getting one when the money is available) so I don't want to lose functionality of uploading new programs (widgets).


It is somewhat hard to overwrite the bootloader on accident. If you need to store information persistently in the EEPROM, just use my settings object and you will be absolutely safe. Other than that you shouldn't have to worry about it.

Using the prop-plug on the other hand will overwrite the bootloader. This isn't really considered to be much of a problem however, because if you have a prop-plug then you can easily write the bootloader back.



[/quote]
darco
 
Posts: 118
Joined: Wed Jun 04, 2008 7:54 pm
Location: Campbell, CA

Please be positive and constructive with your questions and comments.