George Kartsonas
Published

InteliLight

Society is moving forward technologically at lightning speed, time for light bulbs to catch up.

BeginnerFull instructions provided1 hour705

Things used in this project

Hardware components

SparkFun Inventor's Kit for Intel® Edison
SparkFun Inventor's Kit for Intel® Edison
×1
LED (generic)
LED (generic)
×1
Resistor 100 ohm
Resistor 100 ohm
×1
Resistor 10k ohm
Resistor 10k ohm
×1
General Purpose Transistor NPN
General Purpose Transistor NPN
×1

Software apps and online services

Intel XDK

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Intel Edison LED Connection

Code

main.js Intel XDK IOT Edition

Java
var mraa = require('mraa');
var http = require('http');

// Set up a digital output on MRAA pin 20 (GP12) for the LED
var ledPin = new mraa.Gpio(20);
ledPin.dir(mraa.DIR_OUT);
ledPin.write(0);

// Global LED variable to know if the LED should be on or off
var led = 0;

// Which port we should connect to
var port = 4242;

// Create a web server that serves a simple web page with a button
var server = http.createServer(function(req, res) {
    res.writeHead(200);
    res.write(" <!DOCTYPE html>                                             \
                <html>                                                      \
                <head>                                                      \
                    <title>LED Controller</title>                           \
                    <script src='/socket.io/socket.io.js'></script>         \
                </head>                                                     \
                <body>                                                      \
                    <p><button onclick='toggle()'>TOGGLE</button></p>       \
                    <script>                                                \
                        var socket = io.connect('http://" + 
                            req.socket.address().address + ":" + 
                            port + "');                                     \
                        function toggle() {                                 \
                            socket.emit('toggle');                          \
                        }                                                   \
                    </script>                                               \
                </body>                                                     \
                </html>");
    res.end();
});

// Listen for a socket connection
var io = require('socket.io').listen(server);

// Wait for a client to connect
io.on('connection', function(socket) {
    console.log('A client is connected!');

    // Look for the "toggle" message from the client, and toggle the LED
    socket.on('toggle', function() {
        led = led ? 0 : 1;
        ledPin.write(led);
    });
});

// Run the server on a particular port
server.listen(port, function() {
    console.log('Server listening on port ' + port);
});

Credits

George Kartsonas

George Kartsonas

19 projects • 46 followers

Comments