Control an LED from the Internet with Raspberry Pi – Node.js –


In this post we will remotely toggle an LED connected to a Raspberry Pi. Remember if you can toggle an LED you can control anything.

You don’t need experience in any of these technologies to complete this tutorial. I will supply all code.

We will program the Pi in JavaScript running on Nodejs. This code code will listen for commands from our web page.

We will program our webpage in html and JavaScript running in the browser. This code will send commands to our Pi to toggle the LED on and off. We will be using Microsoft Azure to host our web page for free. Or if you have you own web space – you can use that.

We will use to stream the commands from our web page to our Raspberry Pi in what seems to be real-time – Ably is fast. We can also stream data from our Pi back to our web page. You will need to create an Ably account – It’s free for the basic stuff we will be doing and you don’t need a credit card to sign up.

We will use the node GPIO package onoff to toggle the pins on our PI thus turning our LED on and off.


I do not derive any income from these links. Find cheapest price with good reviews. Also look around for other Pi packages. Some will come with an SD card included – some even with an Operating System installed.

Raspberry Pi (2 or 3)

Micro SD Card

LED, Resistor & Breadboard

SD Reader/Writer

Installing Noobs and Nodejs on your Raspberry Pi

If you want a quick setup see below. If you want a detailed version DavaJ does a great job. If you follow Dave’s steps you can then skip  down to ‘Install onoff’ section. Make sure you use the 7x version of nodesource – Dave’s site uses 8x.

Install Raspbian

  • Install micro sd card (connectors up) into the the slot it fits best into -)
  • Hook up keyboard, mouse and monitor (and Ethernet if you have it). If not go to upper right of screen once your install is done to connect to your wireless router
  • Plug it in
  • Choose ->Raspbian -> Install (Watch for any prompts… I don’t know make a decision)
  • Update Config – Raspberry Pi Icon (upper left) -> Preferences -> Raspberry Pi Configuration -> Localization -> Set all to language of your choice. Note you can also change your computer name and password if you like under the System tab. Probably a good idea…

Update Pi Kernel and Software

Open terminal window – Icon top of screen that looks like a ‘terminal window’
Type the following commands (wait for first to finish before typing second)

  • $ sudo apt update
  • $ sudo apt full-upgrade

Install NodeJs using

Note: I have been using nodesource for ans while now and trust their service. Whether you do is up to you.

Install onoff

At your terminal window type
~$ npm install onoff


Install Ably

At your terminal window type
~$ npm install ably


Wire up the Pi

Connect the black wire to ground on your Pi and the shorter lead on your LED
Connect the red wire to GPIO 27 on your Pi and to one end of your resistor
Connect the other end of the resistor to the longer lead on your LED
Note there are plenty of tutorials out there that will show you how to calculate the proper size resistor for your LED. You can also live without the resistor for some quick tests, on and off just don’t leave the LED on very long without a resistor.


The Code

We can now write the code we need to toggle our LED. This will consist of two parts –

  •  JavaScript – running in nodejs on our Pi – which listens for commands and toggles the LED
  • Html page with JavaScript which will send the commands

Pi JavaScript (Listener)

Every new account come with a Sandbox key and channel by default. We will be using both here.

  • Choose Pi icon (upper left) -> Programming -> Geany
  • Type or copy/paste code below with your Sandbox key included
  • Save the file as listener.js in  /home/pi directory
var Ably = require("ably");
//Note make sure you change to Your Sandbox Key below
var realtime = new Ably.Realtime({ key: 'Sandbox key here' });

var GPIO = require('onoff').Gpio,
led = new GPIO(27, 'out');

var channel = realtime.channels.get("Sandbox");
//listen for messages from our web page and set
//GPIO pin 27 to 1 (on) or 0 (off).
channel.subscribe(function(msg) {
  var message = JSON.stringify(;

  if (message == "\"on\"") {
  else if (message == "\"off\"") {


Let’s do some testing before we move on to writing the Html/Javascript code to send messages.

Open a terminal window and type the following to run our listener.js file


If you don’t have any file errors your cursor will move to the next line and wait for commands. If you do have errors in the file your Pi will tell you. Fix them and try again…

Now that our listener is running we can write some commands to test

Open a second terminal window and type the following commands


Fun stuff… Your LED should have toggled on and off. If not the fun actually begins – debugging.

  • Check your LED connections. Are you using the right pins?
  • Check each line of code. Is it exact? Case counts – ‘Sandbox’ != ‘sandbox’…

Create a Web Site

    • Browse to this link to create Azure account. You do not need to be a student to create a free account.
    • Once you have an account – create a web site.
    • Html Page(Sender)

      Next we’ll create a web page to allow us to toggle our LED from anywhere we have an internet connection.

      You can write this code on your main computer or on your Pi. Notepad works just fine as an editor or if you have an Html editor you can use that.

    • Create a file in notepad called LightToggle.html
    • Add the following code and save
      <!DOCTYPE html>
      <html lang="en">
      <meta charset="UTF-8">
      <button type="button" onclick="toggleLight(this)" id="on">On</button>
      <button type="button" onclick="toggleLight(this)" id="off">Off</button>
      <!-- Include the latest Ably Library -->
      <script src=""></script>
      <script src="SandboxKey.js"></script>
      <script type="text/javascript"></script>
      /* Publish a message when the button is pressed */
       function toggleLight(elem) {
       var state;
       else if ( == "off")
       state = "off";
       channel.publish("update", { "lightstate": state });


Create JavaScript file to hide your Ably key

  • Create a file in notepad called SandboxKey.js
  • Add the following and save
    var realtime = new Ably.Realtime({ key: 'Your Sandbox Key' });
    var channel = realtime.channels.get("Sandbox");

    Upload files

  • Upload the html file to your page
    • Browse to: https://{sitename}
    • Navigate to: site/wwwroot
    • Upload the html file
  • Browse to: http://{sitename}
  • Enjoy

One thought on “Control an LED from the Internet with Raspberry Pi – Node.js –

  1. Pingback: RealTimeWeekly | RealTimeWeekly #159

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s