HomeCommunityEmbedded and Microcontrollers blog
January 19, 2017

Two-way Control with Web and Button

Let's expand the functionality by adding a hardware button control.

By Dmitry Slepov

Share
Reading time 1 minute

About the Application

You can expand the functionality of the prev app 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 prev app you will also need:

 

GitHub Repository

Name: tps-gpio-tutorials

Repository page: https://github.com/tibbotech/tps-gpio-tutorials

Clone URL: https://github.com/tibbotech/tps-gpio-tutorials.git

Updated At: Mon Oct 10 2016

 

Node.js Application

Add the following code to server.js:

server.js

...

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

var wasButtonPressed = false;

button.setDirection('input');

setInterval(function(){
    // 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
        }else{
            ledState = 1;
        }

        //...writes...
        led.setValue(ledState);

        //...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;
    }
},100);

...
 

Log in to like this post
Share

Article text

Re-use is only permitted for informational and non-commercial or personal use only.

placeholder