自动更新网页内容

I am working on a system that has a bunch of sensors communicating with a linux machine. I need to make a web interface for this to display status of sensors and automatically update them when the status of any of the sensors changes(or just automatically update them every few seconds).

The code for reading sensors is all in C code. What are the options for pushing the sensor data to the web browser automatically or even by continuously requesting the data?

I have considered the following scheme: Allocate shared memory in the C program for the sensor data, which can be read by a server side script like php. Then have some ajax or something requesting the data on a loop or timer event.

Is it possible to push the data to the client rather than pull?

I really do not have any experience with php/ajax, so any tips on how to implement this would be great.

Some key elements I do not know:

-How to do timing in javascript

-How to get some sort of structured data in javascript on the fly from a request to the server(eg php page reading the sensor data)

  1. You cannot generally push data using HTTP. You will have to poll the server. (There are alternatives that can push, if you are interested. See Comet)
  2. You can use Ajax to dynamically send request to the server. See this tutorial for details. It is pretty simple.
  3. You have to send this Ajax periodically. You can use setInterval for this.

setTimeout("yourAjaxRequestFunction",delay);

As you say , to getting data , you have two solutions :

A) Pulling data with ajax

You easily can do some periodical task with setInterval :

setInterval(callbackserver, PERIOD_MILLIS);

If you use a framework like jquery, there is plenty of plugin for periodical updating.

B) Pushing Data with a js server

Data can be push to the browser instead of pulling it. For that you need a server based on protocol like Comet. Check this project for more information

How to get some sort of structured data in javascript on the fly from a request to the server

json seems to be a perfect solution for structured data accross langage.

I have considered the following scheme: Allocate shared memory in the C program for the sensor data, which can be read by a server side script like php

Why not simply make a deamon in C and get data from it via PHP with a exec("mydeamon -data") command for exemple

  • Ajax would be a good choice to pull the data periodically. It's completely client-side and very easy to start with. check some examples on the internet.
  • And for the server-side, you need something that takes the data from C. Sharing memory between C and PHP would be tedious. You can simply create or update a text file by C. And place this file in the home folder of the web server (apache perhaps)

In your case, if you/your company can afford it, I'd recommend Wt.
If your application is written in C then C++ would likely be more familiar than PHP or Javascript.
It can also handle pushing data to a web page using either ajax pull or web sockets.