Two-way Control with Web and Button

Two-way Control with Web and Button

About the Application

Now let's expand the functionality of the previous app by adding a hardware button control. Pushing the button will also toggle the state of the GPIO line (LED, relay,...) and will be reflected in the LED state displayed in the browser window.

What you need

In addition to the hardware used in the previous app you will also need:

Node.js Application

Add the following code to server.js:



var button = gpio.init("S11A");

var wasButtonPressed = false;


    // If button is just released...
    if(button.getValue() === 1 && wasButtonPressed === true){
        wasButtonPressed = false;

        // ...reads the LED state...
        var ledState = led.getValue();

        //...inverses it...
        if(ledState === 1){
            ledState = 0
            ledState = 1;


        //...and submits to the web app if connected
        if(clients !== undefined){
            clients.emit('tps:state:changed', ledState);
    }else if(button.getValue() === 0){
        // If button is pressed
        wasButtonPressed = true;

Two-way Control with Web and Button