- Simple to create
- Useful to control discrete states of devices (e.g. turn on/off LED)
How to control sensors/devices via HTTP POST or GET
This kind of method does NOT require any code run in system loop.
For the sake of simplicity, In this tutorial present only HTTP GET.
There are some way to make HTTP GET from webpage. A simple way is hyperlink. Hyperlink can be embedded in text or image on webpage. In this kind of webpage, there is two main part of source code:
- Hyperlink code
- Code to process HTTP GET
Let's learn through an example of turning on/off an built-in LED on PHPoC. We need to:
- Put Hyperlink in webpage
- Hyperlink with text
HTML Code:
<a href='index.php?led=on'>Turn On</a> <br> <a href='index.php?led=off'>Turn Off</a>
- Hyperlink with Image
HTML Code:
<a href='index.php?led=on'><img src='on.png'></a> <br> <a href='index.php?led=off'><img src='off.png'></a>
- Process HTTP GET
No matter we use hyperlink with text or image, the prossess of HTTP request is the same. Of cource, prossess code on server is PHPoC code.
PHP Code:
if(($led = _GET("led"))) // check whether HTTP request include "led" variable or not. If not, it does not control LED, just return webpage.
{
if($led == "on")
uio_out(0, OUT_PIN, HIGH); // Turn LED on
else
uio_out(0, OUT_PIN, LOW); // Turn LED off
}
All of above code is put in the same file index.php
Full source code of index.php (with Hyperlink in text)
PHP Code:
<html>
<head>
<title>PHPoC Tutorial</title>
<style> body { text-align: center; } </style>
</head>
<body>
<h2>Control via Hyperlink in Text
<br><br>
<?php
include_once "/lib/sd_340.php";
define("OUT_PIN", 30);
uio_setup(0, OUT_PIN, "out");
if(($led = _GET("led"))) // check whether HTTP request include "led" variable or not. If not, it does not control LED, just return webpage.
{
if($led == "on")
uio_out(0, OUT_PIN, LOW); // Turn LED on
else
uio_out(0, OUT_PIN, HIGH); // Turn LED off
}
?>
<a href='index.php?led=on'>Turn On</a>
<br>
<a href='index.php?led=off'>Turn Off</a>
</h2>
</body>
</html>
With that code, we can control the LED via the webpage look like below
Full source code of index.php (with Hyperlink in image)
PHP Code:
<html>
<head>
<title>PHPoC Tutorial</title>
<style> body { text-align: center; } </style>
</head>
<body>
<h2>Control via Hyperlink in Image
<br><br>
<?php
include_once "/lib/sd_340.php";
define("OUT_PIN", 30);
uio_setup(0, OUT_PIN, "out");
if(($led = _GET("led"))) // check whether HTTP request include "led" variable or not. If not, it does not control LED, just return webpage.
{
if($led == "on")
uio_out(0, OUT_PIN, LOW); // Turn LED on
else
uio_out(0, OUT_PIN, HIGH); // Turn LED off
}
?>
<a href='index.php?led=on'><img src='image_on.png'></a>
<br>
<a href='index.php?led=off'><img src='image_off.png'></a>
</h2>
</body>
</html>
With that code, we can control the LED via the webpage look like below
For the sake of simplicity, the above webpages is made simply. You can add CSS to make the webpage look more beautiful.
See Also