How do I control a webpage with an arduino?

时间:2015-09-01 21:33:15

标签: arduino

I have an Arduino I want to set up with a button, to put a signal through an input pin, that will turn a light on or off on an external website (Not hosted on the Ethernet shield). I have been looking through online documentation, but the only thing I can find is something about thermometer readings. Some relevant info, I have an Arduino Uno with an Ethernet shield. I have made this work to turn the Arduino on or off using the site, I am trying to do this in the other direction now. I used a php script to control Arduino from the page, I can provide my code if anyone wants.

Could anyone offer me any advice or point me in the right direction?

2 个答案:

答案 0 :(得分:0)

You've got a web page from PHP, fully loaded in your browser window. Now there is no more server-side action until something happens on the client side (form submit, refresh, click link to a new page, etc.)

What I think you should be looking into is ajax, or node.js socket transmission to display the light or whatever on the page in realtime when the device is turned on or off.

TL;DR PHP alone is not going to suit your needs in this situation. You'll need something async.

答案 1 :(得分:0)

我在2年后做了一个项目,使用arduino + ethernet监控水位。我用PHP。该项目在网页上显示水位,arduino用一些传感器测量水位,以太网模块将传感器值发送到服务器。我使用tank.php将水位(传感器值)发送到服务器上的文本文件(L.txt)。

arduino代码

/* put this in loop so that it reads level value and and send to the server every second*/
sprintf(buffer, "GET /tank.php?level=%d HTTP/1.0",level );
client.println(buffer);
client.println( "Host: localhost");

tank.php会将级别值写入L.txt。

tank.php的代码

<?php
$l = $_GET["level"]; 
$fileLocation= "L.txt";
$fl = fopen($fileLocation, 'w') or die("Something went wrong!"); 
fwrite($fl, $l);
fclose($fl); 
?> 

最后用waterlevel.php进行显示。 waterlevel.php从L.txt读取值并在网页上显示水位(我每秒都使用Meta标签进行自动刷新)。

waterlevel.php的代码

<?php
$l = file_get_contents("L.txt");
$tanklevel=$l;
$a = 2 *(100 - $tanklevel) ;
$b = 2*$tanklevel ;
echo "<html><center>";
echo "<meta http-equiv=refresh content=1>";
echo "<p align= center >";
echo "<font size= 8 >";
echo "<h1>Tank Level</h1>";
echo "<h2> $tanklevel% </h2>";
echo "</p>";
echo "<table width=200 cellspacing=0 cellpadding=0 border=1>";
echo "<tr><td bgcolor=#cccccc height= $a ></td></tr>";
echo "<tr><td bgcolor=#3333aa height= $b ></td></tr>";
echo "</table>";
echo "</center></html>";
?>