Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* set-up server and socket */
- const http = require('http'); // load and initialise http pack
- const express = require('express'); // load and initialise expess pack
- const app = express(); // create express application
- const server = http.createServer(app); // create HTTP server that passes requests to express application
- /*
- var http = require('http').Server(app); // initialise app handler
- var io = require('socket.io')(http); // initialise socket.io module
- */
- const router = express.Router(); // set_up router
- console.log('test');
- app.get('/', function(req, res){ // make handler getting called at local address
- res.sendFile(__dirname + '/index.html'); // Serve this content to caller when webpage is loaded
- });
- app.get('/imagemap/', function(req, res){ // make handler getting called at local address
- res.sendFile(__dirname + '/image1.png'); // Serve this content to caller when webpage is loaded
- });
- /*
- io.on('connection', function(socket){ // do something when user connects
- socket.on('chat message', function(msg){ // receive message from input
- io.emit('chat message', msg); // distribute event to all connected systems
- });
- });
- */
- // express().use(express.static('/imagemap'));
- server.listen(3000, function(){ // Make http server listen on port 3000 for serving content
- console.log('listening on *:3000'); // write message ato console/debugger
- console.log('where the magic is happening');
- });
- /* set-up socket connection */
- const socketIo = require('socket.io'); // set up web socket connections
- const sio = socketIo(server); // Create socket.io server attatched to http server
- const InputSocket = sio.of('/InputSpace')
- .on('connection', function (socket) {
- console.log('input connection established'); // tell debugger/logger that connection is established
- socket.on('status', function (data) { // gets called when cliënt attempt a connection, function listen for events
- console.log('incomming data: ', data); // show data on debugger/logger
- InputSocket.emit ('status', data); // Transfer input data to cliënt
- });
- });
- /* ^ Create transferring space for inputs to make cliënt connect to and synchronize cliënts data with existing serverdata ^ */
- const OutputSocket = sio.of('/OutputSpace')
- .on('connection', function (socket) {
- console.log('Output connection established'); // tell debugger/logger that connection is established
- socket.on('status', function (data) { // gets called when cliënt attempt a connection, function listen for events
- console.log('Transferring data: ', data); // show data on debugger/logger
- OutputSocket.emit ('status', data); // Transfer output data to cliënt
- OutputPins(data);
- });
- });
- /* ^ Create transferring space for outputs to make cliënt connect to and synchronize cliënts data with existing serverdata ^ */
- Inputs = [];
- Outputs = [];
- /* GPIO Handler */
- const Gpio = require('pigpio').Gpio; // initialise pigpio module for further use
- const GpioInputArray = [0, 1, 2, 3, 4, 5, 6, 7]; // define input array size
- const GpioOutputArray = [9, 10, 11, 12, 13, 14, 15]; // define output array size
- var GpioInputPin = []; // create array variables
- var GpioOutputPin = []; // create array variables
- const button1 = 19; // define input pin for switch1
- GpioInputArray.map(function (value, key) { // creates new array within defined size
- GpioInputPin[1] = new Gpio(button1, { // define GpioInputPin[1] as GPIO input, port pulled down and rising and falling edge controlled
- mode: Gpio.INPUT,
- pullUpDown: Gpio.PUD_DOWN,
- edge: Gpio.EITHER_EDGE
- });
- GpioInputPin[1].on('interrupt', function (level) { // attach interrupt based event handler to GpioInputPin[1] to do routine if gpio state changes
- if (level == 0) {
- Inputs[1] = false; // make input state false
- }
- else {
- Inputs[1] = true; // make input state true
- }
- InputSocket.emit('status', inputs); // transfer changed state to socket
- });
- });
- function OutputPins(data) {
- Outputs = data;
- }
Advertisement
Add Comment
Please, Sign In to add comment