User avatar
richiepp
Posts: 140
Joined: Wed Dec 19, 2012 4:56 pm

AJAX to show live temp sensor data?

Mon May 27, 2013 12:57 am

I'm looking for a way to display real time data from my temp sensor to a web page. The idea is every time there is a change in temp by .1 degrees only that section of the webpage would update. I've never used AJAX but from what I've read about it it seems like it could be a solution.

Anyone have experience with this kind of thing?

Thanks
Rich

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: AJAX to show live temp sensor data?

Mon May 27, 2013 9:17 am

WebIOPi appears to do something similar to what you describe. Perhaps you might find something useful from the way it is implemented?

https://code.google.com/p/webiopi/
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

jpc
Posts: 18
Joined: Tue Mar 26, 2013 3:32 am

Re: AJAX to show live temp sensor data?

Wed May 29, 2013 3:54 am

You may want to look into using WebSockets, which allow for event-based TCP sockets within a web page using JavaScript.

Heater
Posts: 15949
Joined: Tue Jul 17, 2012 3:02 pm

Re: AJAX to show live temp sensor data?

Thu Jun 20, 2013 5:50 pm

I second the use of websockets. Very easy to use.

If you use node.js on your server to serve up websockets there is the socket.io module that makes life really easy and you end up using the same language, JavaScript, in the browser and the server which is a bonus.
http://nodejs.org/
http://socket.io/

Node.js installs easily on the Pi
http://nodejs.org/dist/v0.11.2/node-v0. ... -pi.tar.gz
Memory in C++ is a leaky abstraction .

NewPi
Posts: 66
Joined: Sat Aug 18, 2012 2:52 pm

Re: AJAX to show live temp sensor data?

Mon Jul 15, 2013 2:38 pm

The easier way to do that would be to send date to "internet of things" aggregator like https://xively.com/ and get feeds from there.
Raspberry Pi Howto, Tips, Tricks and Tools -> http://bit.ly/RPiTricks

evgkib
Posts: 1
Joined: Sun Aug 11, 2013 1:53 am

Re: AJAX to show live temp sensor data?

Sun Aug 11, 2013 1:56 am

Here's something I wrote to display temperature in browser real time using node.js and socket.io https://github.com/evgkib/RaspberryPi-temp. My next goal is add more weather sensors, and create a live dashboard displaying information

User avatar
jbeale
Posts: 3675
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: AJAX to show live temp sensor data?

Sat Oct 05, 2013 12:40 am

evgkib wrote:Here's something I wrote to display temperature in browser real time using node.js and socket.io https://github.com/evgkib/RaspberryPi-temp.
Wondering if anyone can help me get node.js and socket.io working on my R-Pi. I have them installed but they don't work as I expect. Should the example below (from Scott Blaine's Blog ) work from the R-Pi's own browser (Midori) or does it require Firefox or something else? Here is the simple example I can't get to work, it is two files connectionCounter.js :

Code: Select all

var express = require('express');
var app = express();
var socket = require('socket.io');
var server = app.listen(4000);
var io = socket.listen(server);

app.get('/', function(request, response){
  response.sendfile(__dirname + "/index.html");
});

var activeClients = 0;

io.sockets.on('connection', function(socket){clientConnect(socket)});

function clientConnect(socket){
  activeClients +=1;
  io.sockets.emit('message', {clients:activeClients});
  socket.on('disconnect', function(){clientDisconnect()});
}

function clientDisconnect(){
  activeClients -=1;
  io.sockets.emit('message', {clients:activeClients});
and here is the second file index.html :

Code: Select all

<!DOCTYPE html>
<html>
  <head>
    <title>Connection Counter</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script>
      function msgReceived(msg){
        $clientCounter.html(msg.clients);
      }

      $(document).ready(function () {
        $clientCounter = $("#client_count")

        var socket = io.connect('http://localhost:4000');
        socket.on('message', function(msg){msgReceived(msg)});
      });
    </script>
  </head>
  <body>
    <p><span id="client_count">0</span> connected clients</p>
</body>
</html>
I can connect to http://127.0.0.1:4000 from the R-Pi using Midori, from the R-Pi using links2 (text mode browser) and from my Android phone (Opera Mobile) via wifi. In every case I get the static page "0 connected clients" but I never see it increment, no matter how many clients connect. I do see each connection add a line of text on the console output:

Code: Select all

pi@raspberrypi:~/nodeJS$ sudo node connectionCounter.js
   info  - socket.io started
   debug - served static content /socket.io.js
   debug - served static content /socket.io.js
   debug - served static content /socket.io.js
   debug - served static content /socket.io.js
...etc.
it also works the same way, that is to say not correctly, if I don't use 'sudo'.

User avatar
jbeale
Posts: 3675
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: AJAX to show live temp sensor data?

Sat Oct 05, 2013 12:59 am

Well, the below demo does work (slowly) in the R-Pi's midori browser, and the no-ip.org site is itself apparently served by a R-Pi. So I ought to be able to do something much simpler on my own Pi- although I don't understand quite how node.js and socket.io is used in this example.
http://chriscavanagh.wordpress.com/2013 ... pberry-pi/
http://bobstrogg.no-ip.org:8080/demo.html

UPDATE: I got Scott's simple socket.io demo to work, yay: http://www.raspberrypi.org/phpBB3/viewt ... 08#p434008

Return to “Other programming languages”