5

I am working on small web application that am using to turn a lamp on and off through internet. In case the lamp is on it shows on Lamp image, else it shows off lamp image. I am thinking in this procedure to implement this application: 1- I create an html file on server called lamp.html 2- Every 5 sec the lamp controller circuit send http data to a php server application which takes the data and opens the lamp.html file and rewrites the html file with the new data recieved from the lamp controller. Example: If the lamp is on, server application writes to lamp.html file an image tag with an image of ON lamp, and so on. 3- I request the lamp.html file from the browser . ex: http:mysite.com/lamp.html the file has auto refresh every 5 sec.

Is it good procedure to implement? Is there another method that i can use to make remote control using http request?

1
  • Can you directly request php from browser instead of HTML? Commented Apr 27, 2012 at 8:10

3 Answers 3

2

The prinicpal idea you have sounds good, but it has room for improvements. But first let's look on the general design:

enter image description here

To simplify your system it's actually enough if the server stores the last value transmitted by the lamp into a file. You don't need to render a full HTML file on every change, just modify a single byte inside a file.

The browser on the other hand won't need to refresh the full page as well every 5 seconds. Instead implement something like Stream Updates with Server-Sent Events so that the webserver will actually tell the browser when the lamp changed. The browser then can change the image URL and other stuff via javascript which makes it look much better.

Sign up to request clarification or add additional context in comments.

6 Comments

That sounds great i will read the link you shared and then send a reply...Thanks!!
This is a solid answer. Good idea, and nice diagram!
It seems that SSE is not supported by all browsers, i want something that is supported by any browser on pc or mobiles. The second issue is i want to take the problem one step further, so many users will use there own page on browser to make monitoring remotely, so what i didn't understand is how will server know that this data should be sent to this user and not to the other? The third issue, if i want to make bidirectional communication so i can control the lamp and monitor its state so i need something like websockets which is also not supported by all browsers, so what do you advice
@Hisham: SSE was just exemplary, you can do ajax requests every X seconds as well from the browser and change the URL of the image then which has a similar effect. The main thing is that you don't need to reload the whole page if you use javascript.
@hakre : Ok i understand your point, but let me share this link with you, its an application i found while am searching on the web..You can change the way you view the application from html5, silver light to html only. So i put HTMl. I am wondering about the method that this application use to display the data in this way using html. This is the link: mobile.componentart.com/devices.html?dashboard=executive
|
0

I would have every page request to look via a PHP-script if the lamp is on or off, and display the image depending on the result.

<img src="/<?php echo (lamp_is_on() ? 'on' : 'off'); ?>.png">

1 Comment

So you have to save the state of the lamp in some file or data base, and each time the user( on browser) request the state of the lamp you have to go to data base and get the state and send it back to the user. I suggest to write the data of the lamp in html file, in this case any number of users can get directly to data by just sending the link and get html page.
0

If I were to make a webpage that displays the state of a lamp...

  1. lamp.html
    Should have have two divs. One with the lamp on image and one with the lamp off image.

  2. getLampInfo.php
    Should connect to your lamp controller and echo a data of 0 or 1

  3. Write an ajax script in lamp.html that calls getLampInfo.php every 5 seconds and toggles the divs based on the response.

I wouldn't deal with file overwriting and stuff. That all seems unnecessary. Your approach will work though and there's absolutely nothing wrong with it.

Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.