Over a million developers have joined DZone.

Two-Way Control With Web and Buttons

DZone's Guide to

Two-Way Control With Web and Buttons

Why have one way of controlling your hardware when you could have two? Building upon a web interface, you can add a button to your hardware to toggle your control.

· IoT Zone ·
Free Resource

You can expand the functionality of the app we previously covered if you add a hardware button control. Pressing the button will also toggle the state of the GPIO line (LED, relay,...) which is reflected in the LED state displayed in the browser window.

What You Need

Besides the hardware used in the previous app, you will also need:

Image title

GitHub Repository

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;


And that's it! You've added a hardware button control to your app, which lets you toggle the state of the GPIO line. Having built upon what we accomplished in the last article, you now have two different ways of controlling your hardware.

nodejs ,linux ,arm ,iot ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}