Visit Jeremy's Blog.
Go Back > Blogs > Dmitry_Slepov
User Name


Rate this Entry

LED Control From the Web Browser

Posted 01-21-2017 at 07:37 AM by Dmitry_Slepov
Tags arm, linux, nodejs, tibbo

About the Application
  • This is a simple application which enables the control of a GPIO line via a web browser. To toggle the line HIGH/LOW, click on a rectangle. This app can be used to control a relay Tibbit, a LED Tibbit, or some other "output" Tibbit.
  • The app utilizes a popular library to facilitate a connection without interruption between the TPS and the browser, as well as the AngularJS V1.x.x front-end framework that makes the development of single-page applications simple.

What you need
*Feel free to replace the Tibbits with other output ones

Onboard Software
Node.js V6.x.x (pre-installed during production)

GitHub Repository
Name: tps-gpio-tutorials
Repository page:
Clone URL:
Updated At: Mon Oct 10 2016

Node.js Application
  • and Express and are used to support the web interface functionality
  • The code for web interface connectivity, LED manipulation, and the HTTP server providing web client files is located in the server.js file
  • Web client files are served from the /public folder

Installation and Configuration
  • Define the configuration in the LTPS Web Interface
  • Login to the LTPP3 board from the SSH client
  • Install NPM and other tools as required
  • Install the app:

git clone
cd tps-gpio-tutorials
npm install .
cd one-led
  • Launch the app:
node server
Comments in the code explain how it works
// Requires HTTP as WebSocket server modules
const express = require("express");
const app = express();
const http = require('http').Server(app);
const io = require('')(http);
const gpio = require("@tibbo-tps/gpio");

// Serves static assets from the 'public' folder
app.use("/", express.static('public'));

const led = gpio.init("S15A");

if(led.getDirection() === "input"){

// Listens to the incoming WebSocket connection
var clients = io.on('connection', function(socket){
    // When the connection is established
    console.log('USER CONNECTED');

    // Reads I/O line state..
    // ..and broadcasts it to all the connected clients
    var value = led.getValue();

    clients.emit('tps:state:changed', value);

    // When any of the connected clients require a change of the line state
    socket.on('web:state:changed', function(value){
        // Changes the line state...

        //.. and broadcasts it to all the clients
        clients.emit('tps:state:changed', value);

    socket.on('disconnect', function(){
        console.log('USER DISCONNECTED');

// Runs HTTP server on :3000 port
Web client


Comments in the code explain how it works:

<html lang="en">
        <meta charset="UTF-8">
        <script type="text/javascript" src=""></script>
        <script type="text/javascript" src=""></script>
        <script type="text/javascript" src="client.js"></script>
        <link href="main.css" rel="stylesheet" type="text/css"/>
    <body ng-app="leds"> <!-- The ng-app directive bootstraps your Angular application -->

    <!-- The ng-controller directive attaches controller to a view -->
    <!-- The ng-hide directive hides DOM element depending on the 'locked' varibale -->
    <svg version="1.1" xmlns="" x="0px" y="0px" width="110px" height="110px" xml:space="preserve"

                <!-- The ng-class directive changes class of the DOM element depending on the 'state' variable -->
        <!-- The ng-click directive evokes the function on click by DOM element -->
                <g transform="translate(5,5)" class="led-icon">
                    <rect width="100" height="100" class="frame"></rect>
                    <rect x="10" y="10" width="80" height="80"
                        ng-class="(state ? 'on' : 'off')"

Comments in the code explain how it works:
angular.module('leds', [])
    .controller('ledsController', function($scope) {
        var socket = io(); //

        $scope.locked = true; // Disables the view by default

        socket.on('connect', function () { // On connection established
            $scope.locked = false; // Enables the view
            $scope.$apply(); // Re-renders the view

        socket.on('disconnect', function () { // Hides everything on disconnect
            $scope.locked = true;

        socket.on('tps:state:changed', function (value) { // Catches the 'tps:state:changed' event
            $scope.state = value == 0;

        $scope.switch = function() { // Sends the inversed value of the 'state' variable
            console.log($scope.state ? 1 : 0);
            socket.emit('web:state:changed', $scope.state ? 1 : 0);
Posted in Uncategorized
Views 904 Comments 0
« Prev     Main     Next »
Total Comments 0




All times are GMT -5. The time now is 06:19 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration