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


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");

I thank you all!!


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);

        url: 'arduino.php',
        type: 'POST',
        data: {switch : selectedValue},
        success: function() {


In your arduino.php file, make the change:


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