File tree Expand file tree Collapse file tree 3 files changed +86
-0
lines changed Expand file tree Collapse file tree 3 files changed +86
-0
lines changed Original file line number Diff line number Diff line change
1
+ var http = require ( 'http' ) ;
2
+ var fs = require ( 'fs' ) ;
3
+ var index = fs . readFileSync ( 'index.html' ) ;
4
+
5
+ var SerialPort = require ( 'serialport' ) ;
6
+ const parsers = SerialPort . parsers ;
7
+
8
+ const parser = new parsers . Readline ( {
9
+ delimiter : '\r\n'
10
+ } ) ;
11
+
12
+ var port = new SerialPort ( '/dev/tty.usbmodem1421' , {
13
+ baudRate : 9600 ,
14
+ dataBits : 8 ,
15
+ parity : 'none' ,
16
+ stopBits : 1 ,
17
+ flowControl : false
18
+ } ) ;
19
+
20
+ port . pipe ( parser ) ;
21
+
22
+ var app = http . createServer ( function ( req , res ) {
23
+ res . writeHead ( 200 , { 'Content-Type' : 'text/html' } ) ;
24
+ res . end ( index ) ;
25
+ } ) ;
26
+
27
+ var io = require ( 'socket.io' ) . listen ( app ) ;
28
+
29
+ io . on ( 'connection' , function ( socket ) {
30
+
31
+ socket . on ( 'lights' , function ( data ) {
32
+
33
+ console . log ( data ) ;
34
+
35
+ port . write ( data . status ) ;
36
+
37
+ } ) ;
38
+
39
+ } ) ;
40
+
41
+ app . listen ( 3000 ) ;
Original file line number Diff line number Diff line change
1
+ <!doctype html>
2
+ < html >
3
+ < head >
4
+
5
+ < script src ='https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js '> </ script >
6
+ < script src ="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js "> </ script >
7
+
8
+ < script >
9
+
10
+ $ ( document ) . ready ( function ( ) {
11
+
12
+ var socket = io ( ) ;
13
+
14
+ $ ( '#lightOn' ) . click ( function ( ) {
15
+
16
+ socket . emit ( 'lights' , { "status" :"1" } ) ;
17
+
18
+ } ) ;
19
+
20
+ $ ( '#lightOff' ) . click ( function ( ) {
21
+
22
+ socket . emit ( 'lights' , { "status" :"0" } ) ;
23
+
24
+ } ) ;
25
+
26
+ } ) ;
27
+
28
+ </ script >
29
+ </ head >
30
+ < body >
31
+
32
+ < button id ="lightOn "> Turn Light On</ button >
33
+ < button id ="lightOff "> Turn Light Off</ button >
34
+
35
+ </ body >
36
+ </ html >
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " basic-socket" ,
3
+ "version" : " 0.0.1" ,
4
+ "description" : " Sample communication from an HTML document to a NodeJS using socket.io." ,
5
+ "dependencies" : {
6
+ "socket.io" : " ^2.0.4" ,
7
+ "serialport" : " ^6.0.5"
8
+ }
9
+ }
You can’t perform that action at this time.
0 commit comments