php – How to create a single button to turn on and off lamp for arduino in html?

Question:

Good afternoon people!! I'm new to everything in the programming universe, but I'm venturing with arduino, php and html!! I would like to know how to pass two values ​​in a single button in html. Type on click once call a .php file and turn on the lamp and on click again call another .php file and turn off the lamp. Can anybody help me???

Here is the php code to light the lamp:

   $port = fopen("COM1", "w");
            fwrite($port, "1");
            header ("Location:../index.html");
            fclose($port);  

I thank you all!!

Answer:

Using Bootstrap in conjunction with Bootstrap Toggle , you can customize and make it look like a switch.

For functionality, I'll use AJAX to make the PHP call:

$(document).on('change', '#switch', function() {

    selectedValue = (this.checked ? 1 : 0);

    $.ajax({
        url: 'arduino.php',
        type: 'POST',
        data: {switch : selectedValue},
        success: function() {
            alert("Ok!")
        }
    });

});

In your arduino.php file, make the change:

<?php 

if(isset($_POST['switch'])) {
    $port = fopen("COM1", "w");
    fwrite($port, $_POST['switch']);
    fclose($port);  
}
Scroll to Top
AllEscort