low.js Tutorial: Control LED via Website

Letting the user control the LED through a website the microcontroller serves is easy. It is just basic Node.JS programming.

Before you try this tutorial, please do the Blink LED tutorial first.

Enter the following code into /index.js:

Is the example not working as expected?

Try checking the status with lowsync status.

To start or restart the program call lowsync start.

You can also check for any (error-) output by calling lowsync monitor and then fix any errors you find in your editor.

Remember to call lowsync sync after any changes you make.

let http = require('http');
let fs = require('fs');
let gpio = require('gpio');

let state = 0;

fs.readFile('/client.html', 'utf8', (err, page) => {
    if(err) {

    http.createServer((req, res) => {
        if (req.url == '/Toggle') {
            state = !state;
        } else if (req.url == '/') {
            res.writeHead(200, { 'Content-Type': 'text/html' });
        } else {

Enter the following code into /client.html. This file will be sent to the web browser.

        <script type="text/javascript">
            function toggle() {
                var request = new XMLHttpRequest();
                request.open('GET', '/Toggle', true);
        <p>The example page, served by low.js</p>
        <p><input type="button" value="Toggle LED" onclick="javascript: toggle()"></p>

Sync and restart program. You should see the website when pointing your web browser to the microcontroller's IP.

Next tutorial: Control LED via Serial