Announcement

Collapse
No announcement yet.

How to Control Devices via HTTP Request

Collapse
X
Collapse
  •  

  • How to Control Devices via HTTP Request

    Advantage of controlling sensors/devices via HTTP request (POST or GET)
    • 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(0OUT_PINHIGH); // Turn LED on
            
    else
                
    uio_out(0OUT_PINLOW); // 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(0OUT_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(0OUT_PINLOW); // Turn LED on
        
    else
            
    uio_out(0OUT_PINHIGH); // 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(0OUT_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(0OUT_PINLOW); // Turn LED on
        
    else
            
    uio_out(0OUT_PINHIGH); // 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
    Last edited by support; 02-08-2018, 06:23 PM.
      Posting comments is disabled.

    Categories

    Collapse

    Latest Articles

    Collapse

    • Arduino - RS-485 Expansion Board
      by support
      PES-2607 is an easy-to-use RS422/RS485 Expansion Board for Arduino Uno and Mega, which allows Arduino to exchange data with serial device via RS422 or RS485.
      Especially, Arduino does NOT use UART pins to communicate with RS422/RS485 expansion board. Therefore, users can use Arduino UART pins for other purposes.
      Moreover, A single Arduino Uno/Mega can communicate with multiple RS422/RS485 expansion boards (up to 14) without using Arduino UART pins.

      Library and examples for...
      11-13-2018, 03:45 PM
    • Arduino - RS-422 Expansion Board
      by support
      PES-2607 is an easy-to-use RS422/RS485 Expansion Board for Arduino Uno and Mega, which allows Arduino to exchange data with serial device via RS422 or RS485.
      Especially, Arduino does NOT use UART pins to communicate with RS422/RS485 expansion board. Therefore, users can use Arduino UART pins for other purposes.
      Moreover, A single Arduino Uno/Mega can communicate with multiple RS422/RS485 expansion boards (up to 14) without using Arduino UART pins.

      Library and examples for...
      11-13-2018, 03:44 PM
    • Arduino - RS-232 Expansion Board
      by support
      PES-2606 is an easy-to-use RS-232 Expansion Board for Arduino Uno and Mega, which allows Arduino to exchange data with serial device via RS-232.
      Especially, Arduino does NOT use UART pins to communicate with RS-232 expansion board. Therefore, users can use Arduino UART pins for other purposes.
      Moreover, A single Arduino Uno/Mega can communicate with multiple RS-232 expansion boards (up to 14) without using Arduino UART pins.

      Library and example for the RS-232 expansion board...
      11-13-2018, 03:43 PM
    • Arduino - Stepper Motor Controller
      by support
      PES-2605 is an easy-to-use stepper motor controller for Arduino Uno and Mega, which uses micro-stepping method to precisely control stepper motor.
      Library and example for the stepper motor controller are part of of PhpocExpansion library for Arduino. The library reference is available here.

      This tutorial shows how to use the step motor controller with an example of PhpocExpansion library for Arduino.


      Hardware Required...
      11-13-2018, 03:41 PM
    • Arduino - DC Motor Controller
      by support
      PES-2604 is an easy-to-use DC motor controller for Arduino Uno and Mega.
      Library and example for the DC motor controller are part of of PhpocExpansion library for Arduino. The library reference is available here.

      This tutorial shows how to use the DC motor controller with an example of PhpocExpansion library for Arduino.


      Hardware Required...
      11-13-2018, 03:40 PM
    • Arduino - Digital Input Board
      by support
      PES-2602 is an easy-to-use 4-port Input Expansion Board for Arduino Uno and Mega, which allows Arduino to monitor state of DC electric device. In addition, it can monitor NPN, PNP and dry contact(relay).
      Library and example for the 4-port input expansion board are part of of PhpocExpansion library for Arduino. The library reference is available here.

      This tutorial shows how to use 4-port input expansion board with an example of PhpocExpansion library for Arduino.

      ...
      11-13-2018, 03:39 PM
    Working...
    X