real time data in php help


19 posts
by sim_tcr » Wed Dec 19, 2012 11:16 am
Hello,

Is there any way we can display real time data in php?
For eg, I am using one of Adafruits' python script to display date, time and CPU scaling frequency on my 2x16 LCD display. It refresh every one second.
Can I call the same script from php and display the output (and refresh every second without the whole page being refreshed) in my webpage?

Here is Adafruits' script,

Code: Select all
#!/usr/bin/python

from Adafruit_CharLCD import Adafruit_CharLCD
from subprocess import *
from time import sleep, strftime
from datetime import datetime

lcd = Adafruit_CharLCD()

cmd = "/opt/vc/bin/vcgencmd measure_temp"

lcd.begin(16,1)

def run_cmd(cmd):
        p = Popen(cmd, shell=True, stdout=PIPE)
        output = p.communicate()[0]
        return output

while 1:
        lcd.clear()
        ipaddr = run_cmd(cmd)
        lcd.message(datetime.now().strftime('%b %d  %H:%M:%S\n'))
        lcd.message('%s' % ( ipaddr ) )
        sleep(1)
http://raspisimon.no-ip.org
Raspberry Pi Model B x 2, Transcend 16GB Class 10, Transcend 8GB Class 4, Nokia Travel Charger ACP-12E 5.7V 800mA, Netgear WNA1000M Wifi Dongle/Ethernet
Posts: 311
Joined: Tue Nov 06, 2012 1:01 pm
Location: Bangalore
by elatllat » Wed Dec 19, 2012 12:00 pm
there are 3 options that I know of:
1) ajax/jason the update
2) same as 1 but use a html5 socket
3) make the page load in bits, with each flush() push new javascript that updates the page.


date and time you don't need to send from the server often you can use javascript to keep that counting and just adjust it every so often from the server. (if your server and computer are both using ntp then you don't really need anything from the server for date/time)
Posts: 891
Joined: Sat Dec 17, 2011 5:05 pm
by sim_tcr » Wed Dec 19, 2012 12:16 pm
elatllat wrote:there are 3 options that I know of:
1) ajax/jason the update
2) same as 1 but use a html5 socket
3) make the page load in bits, with each flush() push new javascript that updates the page.


date and time you don't need to send from the server often you can use javascript to keep that counting and just adjust it every so often from the server. (if your server and computer are both using ntp then you don't really need anything from the server for date/time)


Thanks for replying elatllat.

Its not date or time i actually want to display. I was using that as an example for real time data.
I wanted to display my CPU temp and CPU scaling frequency.

Can you explain your solution by posting some code examples?
http://raspisimon.no-ip.org
Raspberry Pi Model B x 2, Transcend 16GB Class 10, Transcend 8GB Class 4, Nokia Travel Charger ACP-12E 5.7V 800mA, Netgear WNA1000M Wifi Dongle/Ethernet
Posts: 311
Joined: Tue Nov 06, 2012 1:01 pm
Location: Bangalore
by sim_tcr » Wed Dec 19, 2012 12:57 pm
Check out this website http://pi.gadgetoid.co.uk/ . He is doing exactly what I have asked help here for.
He says, his script is in ruby. So I downloaded the ruby script provided in his website (.rb file).
I installed ruby by sudo apt-get install ruby.
Now how do I run this file from php and display in my webpage?

Note: I have sent a pm to the website owner asking more information.
http://raspisimon.no-ip.org
Raspberry Pi Model B x 2, Transcend 16GB Class 10, Transcend 8GB Class 4, Nokia Travel Charger ACP-12E 5.7V 800mA, Netgear WNA1000M Wifi Dongle/Ethernet
Posts: 311
Joined: Tue Nov 06, 2012 1:01 pm
Location: Bangalore
by thedudemabry » Fri Dec 21, 2012 6:03 am
Now how do I run this file from php and display in my webpage?

Hi sim_tcr,

To use the script you've linked to, you'll need 3 separate components:
  1. Your PHP server application to serve HTML and Javascript
  2. A Javascript script running in the web browser to connect to the Ruby Websocket server and display the data it receives
  3. The Ruby Websocket server (the script at http://pi.gadgetoid.co.uk/code/Socket.rb) to periodically push data to the web browser

It sounds like the Ruby portion is holding you up right now, so this is how to get it running:
  1. First, you'll need to install a few Ruby libraries, called Gems, that the script requires. Running the command
    Code: Select all
    gem install em-websocket
    will download and install everything automatically. I haven't tried this yet on the Pi, so it may complain about missing system libraries. If so, post the error message on this board and we can help you out.
  2. Second, you'll start the Ruby Websocket server running with
    Code: Select all
    ruby Socket.rb
    It will periodically inspect the system stats and send a message with the results to any clients connected to it.
  3. Third, you'll need to add Javascript to a PHP page to connect the browser to the Websocket server. The website you linked to uses a script found at http://pi.gadgetoid.co.uk/bootstrap/js/custom.min.js. A simpler example can be found here: http://martinsikora.com/nodejs-and-websocket-simple-chat-tutorial#frontend-template. The Ruby script will listen on the Pi's network interface on port 8070, so your Javascript will need to connect to whatever IP address your Pi is on but with port 8070 specified.

This solution uses a lot of different technologies, so let us know if you encounter any issues and someone will probably know how to sort it out. Good luck!

- Nick
Posts: 1
Joined: Fri Dec 21, 2012 5:30 am
by sim_tcr » Fri Dec 21, 2012 7:16 am
Thanks for your reply Nick.

I was able to do step 1and 2 successfully. I have installed Gems by running the command you provided.
When I do
Code: Select all
pi@raspiabita ~ $ ruby Socket.rb
starting
threading
is what I see and I am never back to prompt. That looks like web socket is running. Is that how the prompt will behave?

thedudemabry wrote:Third, you'll need to add Javascript to a PHP page to connect the browser to the Websocket server.

This part I am stuck with. I am a beginner to HTML and PHP. How do I add a java script to my php page? should I first copy paste the code from http://martinsikora.com/nodejs-and-webs ... d-template to anywhere between <HTML> and </HTML> and then do modification in it?
Could you post an example php page source with the script Incorporated for websocket.?
http://raspisimon.no-ip.org
Raspberry Pi Model B x 2, Transcend 16GB Class 10, Transcend 8GB Class 4, Nokia Travel Charger ACP-12E 5.7V 800mA, Netgear WNA1000M Wifi Dongle/Ethernet
Posts: 311
Joined: Tue Nov 06, 2012 1:01 pm
Location: Bangalore
by poing » Fri Dec 21, 2012 9:10 am
I have no experience with Ruby, but you could follow this Ajax tutorial: http://www.w3schools.com/ajax/default.asp.
Posts: 1090
Joined: Thu Mar 08, 2012 3:32 pm
by toysareforboys » Sat Dec 22, 2012 6:05 am
sim_tcr wrote:I was able to do step 1and 2 successfully. I have installed Gems by running the command you provided.
Just so you know, I was not able to get that persons status page working. It relies on something called "eventmachine" which I was not able to get working on the Pi. If you watch the Ruby logs you'll see something like "illegal call: eventmachine" :( I was not able to get it working and switched to PHP. It gets the data perfect, but I have not done any "realtime" updating yet. If you click the refresh link on the page you can pretend real time :)

http://tafb.yi.org

The source code is available at the bottom of that page. I'm running it on Wheezy, Pancake and PHP5. It's very inefficient.

Eventually I'm going to follow this guide to push the updates seamlessly to that status page: http://www.developerdrive.com/2012/03/p ... nt-events/

Have fun with your project.

-Jamie M.
Seagate GoFlex Home, 1.2GHz ARM (kirkwood), 128MB RAM, Gigabit Ethernet, SATA2. Sandisk Extreme 120GB SSD running Arch ARM Linux 3.6.11-0. nginx + php-fpm = LIVE STATUS hosted right on the SGFH!! http://tafb.yi.org
User avatar
Posts: 136
Joined: Thu Dec 06, 2012 11:01 pm
by sim_tcr » Sat Dec 22, 2012 7:06 am
toysareforboys wrote:
sim_tcr wrote:I was able to do step 1and 2 successfully. I have installed Gems by running the command you provided.
Just so you know, I was not able to get that persons status page working. It relies on something called "eventmachine" which I was not able to get working on the Pi. If you watch the Ruby logs you'll see something like "illegal call: eventmachine" :( I was not able to get it working and switched to PHP. It gets the data perfect, but I have not done any "realtime" updating yet. If you click the refresh link on the page you can pretend real time :)

http://tafb.yi.org

The source code is available at the bottom of that page. I'm running it on Wheezy, Pancake and PHP5. It's very inefficient.

Eventually I'm going to follow this guide to push the updates seamlessly to that status page: http://www.developerdrive.com/2012/03/p ... nt-events/

Have fun with your project.

-Jamie M.

I have come across another solution and I had posted it here earlier. And you said due to the heavy number of calls php is going to make to system it may unstable the system. I decided to give it a try, I will leave it on for couple of days and see how stable it can run. Issue is I don't know if my website will get hits from outsides much.
Any way here is it http://raspisimon.no-ip.org/status.php
Note: Live status page works properly on Firefox and Chrome and not one IE. Don't know the reason yet.

I still wanted try out that web socket setup.
http://raspisimon.no-ip.org
Raspberry Pi Model B x 2, Transcend 16GB Class 10, Transcend 8GB Class 4, Nokia Travel Charger ACP-12E 5.7V 800mA, Netgear WNA1000M Wifi Dongle/Ethernet
Posts: 311
Joined: Tue Nov 06, 2012 1:01 pm
Location: Bangalore
by Alesfatalis » Mon Dec 31, 2012 4:49 pm
You can execute the ruby script with php and display the result on your webpage with this:
Code: Select all
<html>
<head>
</head>
<body>
<?php
shell_exec('shell commando to run the ruby script here', $output);
echo '<pre>'.$output.'</pre>';
?>
<meta http-equiv="refresh" content="1">
</body>
</html>


This should display the result of the ruby script on your website and it should automatic refresh itself every 1 second. Save the file as .php. Thats the easiest method i know.

You don't need to install anything for it it is built in php. Hope this helps ;)
Posts: 3
Joined: Mon Dec 31, 2012 1:09 am
Location: Germany
by toysareforboys » Mon Dec 31, 2012 4:55 pm
sim_tcr wrote:I have come across another solution and I had posted it here earlier. And you said due to the heavy number of calls php is going to make to system it may unstable the system. I decided to give it a try, I will leave it on for couple of days and see how stable it can run. Issue is I don't know if my website will get hits from outsides much.
Any way here is it http://raspisimon.no-ip.org/status.php
Note: Live status page works properly on Firefox and Chrome and not one IE. Don't know the reason yet.
Looks great! Nice work!

I got flawless live updating working on my page too (with jquery) but also, it doesn't appear to work in IE :(

http://tafb.yi.org

Works great on everything else though :) Just got my arch kernel upgraded, converted to EXT4 and tweaked my fstab, runnin like a champ :D

-Jamie M.
Seagate GoFlex Home, 1.2GHz ARM (kirkwood), 128MB RAM, Gigabit Ethernet, SATA2. Sandisk Extreme 120GB SSD running Arch ARM Linux 3.6.11-0. nginx + php-fpm = LIVE STATUS hosted right on the SGFH!! http://tafb.yi.org
User avatar
Posts: 136
Joined: Thu Dec 06, 2012 11:01 pm
by sim_tcr » Tue Jan 01, 2013 5:21 am
toysareforboys wrote:
I got flawless live updating working on my page too (with jquery) but also, it doesn't appear to work in IE :(

http://tafb.yi.org?

what is the refresh interval set?
When I look at Uptime in your page, seconds are jumped incremental of 3 or 5 seconds seconds.COuld it be because of my internet connection speed?
http://raspisimon.no-ip.org
Raspberry Pi Model B x 2, Transcend 16GB Class 10, Transcend 8GB Class 4, Nokia Travel Charger ACP-12E 5.7V 800mA, Netgear WNA1000M Wifi Dongle/Ethernet
Posts: 311
Joined: Tue Nov 06, 2012 1:01 pm
Location: Bangalore
by toysareforboys » Tue Jan 01, 2013 5:32 am
sim_tcr wrote:what is the refresh interval set?
When I look at Uptime in your page, seconds are jumped incremental of 3 or 5 seconds seconds.COuld it be because of my internet connection speed?
Probably your internet connection :) I've had people report from the other side of the pond (UK, AUS, NZ), they have a real slow connection to me (I'm in Canada).

The refresh interval is set to one second AFTER the last SUCCESSFUL data update received (to make sure the requests don't pile up on slow/flaky connections). So if your internet connection takes 2 seconds to pull down the data update, then your page won't refresh for 3 seconds in total. I may set it so that if more than one second has elapsed since the page update REQUEST, then to just request a refresh immediately upon receiving the refreshed data. I didn't do it like that originally because that means it'll just be hammering your internet connection 100% with back to back to back requests. The 1 second delay should give things a nice rest.

And, if I get rid of that stupid phpChart gauge on the page, that'll cut a massive amount off the data it has to pull each time to refresh the data.

Let me know where you're located. If I keep getting reports like this from people I may try a different internet provider and see if they have a stronger link for my overseas friends.

-Jamie M.
Seagate GoFlex Home, 1.2GHz ARM (kirkwood), 128MB RAM, Gigabit Ethernet, SATA2. Sandisk Extreme 120GB SSD running Arch ARM Linux 3.6.11-0. nginx + php-fpm = LIVE STATUS hosted right on the SGFH!! http://tafb.yi.org
User avatar
Posts: 136
Joined: Thu Dec 06, 2012 11:01 pm
by sim_tcr » Tue Jan 01, 2013 6:04 am
toysareforboys wrote:Let me know where you're located. If I keep getting reports like this from people I may try a different internet provider and see if they have a stronger link for my overseas friends.

I am located in India and have a 2mbps connection (I am not exactly sure what is the upload and download speed). Can you try to access my page and http://raspisimon.no-ip.org/status.php and see if you see any delay in refresh? You may have to check the uptime counter as it should refresh every second.
http://raspisimon.no-ip.org
Raspberry Pi Model B x 2, Transcend 16GB Class 10, Transcend 8GB Class 4, Nokia Travel Charger ACP-12E 5.7V 800mA, Netgear WNA1000M Wifi Dongle/Ethernet
Posts: 311
Joined: Tue Nov 06, 2012 1:01 pm
Location: Bangalore
by toysareforboys » Tue Jan 01, 2013 6:09 am
sim_tcr wrote:I am located in India and have a 2mbps connection (I am not exactly sure what is the upload and download speed). Can you try to access my page and http://raspisimon.no-ip.org/status.php and see if you see any delay in refresh? You may have to check the uptime counter as it should refresh every second.
It refreshes every 2 seconds, sometimes 3 :) So yep, it's our connection to each other for sure :)

I have 100mbps download and 2mbps upload:

Image

I wish they offered faster upload :(

-Jamie M.
Seagate GoFlex Home, 1.2GHz ARM (kirkwood), 128MB RAM, Gigabit Ethernet, SATA2. Sandisk Extreme 120GB SSD running Arch ARM Linux 3.6.11-0. nginx + php-fpm = LIVE STATUS hosted right on the SGFH!! http://tafb.yi.org
User avatar
Posts: 136
Joined: Thu Dec 06, 2012 11:01 pm
by sim_tcr » Tue Jan 01, 2013 6:20 am
so that was it. I have a slow speed and that was causing delays to refresh your page at my end. And also refresh my page at your end takes time because again my upload speed is very low.
Image
http://raspisimon.no-ip.org
Raspberry Pi Model B x 2, Transcend 16GB Class 10, Transcend 8GB Class 4, Nokia Travel Charger ACP-12E 5.7V 800mA, Netgear WNA1000M Wifi Dongle/Ethernet
Posts: 311
Joined: Tue Nov 06, 2012 1:01 pm
Location: Bangalore
by toysareforboys » Tue Jan 01, 2013 6:24 am
sim_tcr wrote:so that was it. I have a slow speed and that was causing delays to refresh your page at my end. And also refresh my page at your end takes time because again my upload speed is very low.
Image
Wow, that's really terrible! That ping time especially, need a fast one for quick server response :(

-Jamie M.
Seagate GoFlex Home, 1.2GHz ARM (kirkwood), 128MB RAM, Gigabit Ethernet, SATA2. Sandisk Extreme 120GB SSD running Arch ARM Linux 3.6.11-0. nginx + php-fpm = LIVE STATUS hosted right on the SGFH!! http://tafb.yi.org
User avatar
Posts: 136
Joined: Thu Dec 06, 2012 11:01 pm
by sim_tcr » Tue Jan 01, 2013 6:28 am
toysareforboys wrote:
sim_tcr wrote:so that was it. I have a slow speed and that was causing delays to refresh your page at my end. And also refresh my page at your end takes time because again my upload speed is very low.
Image
Wow, that's really terrible! That ping time especially, need a fast one for quick server response :(

-Jamie M.


But I see that you pay around 200$ (from Rogers website) per month for your high speed internet connection. For me it is 1000INR (Nearly 20$) per month. :D
http://raspisimon.no-ip.org
Raspberry Pi Model B x 2, Transcend 16GB Class 10, Transcend 8GB Class 4, Nokia Travel Charger ACP-12E 5.7V 800mA, Netgear WNA1000M Wifi Dongle/Ethernet
Posts: 311
Joined: Tue Nov 06, 2012 1:01 pm
Location: Bangalore
by toysareforboys » Tue Jan 01, 2013 6:34 am
sim_tcr wrote:But I see that you pay around 200$ (from Rogers website) per month for your high speed internet connection. For me it is 1000INR (Nearly 20$) per month. :D
I would normally pay $100USD per month for the internet package I have (Ultimate plan), but I get it a little cheaper because they provide my phone and television service as well :)

Looks like the cheapest ISP around here is $30USD per month for 5mbps down and 1mbps up :)

-Jamie M.
Seagate GoFlex Home, 1.2GHz ARM (kirkwood), 128MB RAM, Gigabit Ethernet, SATA2. Sandisk Extreme 120GB SSD running Arch ARM Linux 3.6.11-0. nginx + php-fpm = LIVE STATUS hosted right on the SGFH!! http://tafb.yi.org
User avatar
Posts: 136
Joined: Thu Dec 06, 2012 11:01 pm