File tree Expand file tree Collapse file tree 3 files changed +94
-0
lines changed Expand file tree Collapse file tree 3 files changed +94
-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
+ console . log ( 'Node is listening to port' ) ;
32
+
33
+ } ) ;
34
+
35
+ parser . on ( 'data' , function ( data ) {
36
+
37
+ console . log ( 'Received data from port: ' + data ) ;
38
+
39
+ io . emit ( 'data' , data ) ;
40
+
41
+ } ) ;
42
+
43
+ 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
+ socket . on ( 'data' , function ( data ) {
15
+
16
+ console . log ( data ) ;
17
+
18
+ $ ( '#sample' ) . css ( 'opacity' , data / 100 ) ;
19
+
20
+ } ) ;
21
+
22
+ } ) ;
23
+
24
+ </ script >
25
+
26
+ < style >
27
+
28
+ # sample {
29
+ background-color : red;
30
+ width : 300px ;
31
+ height : 300px ;
32
+ }
33
+
34
+ </ style >
35
+
36
+ </ head >
37
+ < body >
38
+
39
+ < div id ="sample "> </ div >
40
+
41
+ </ body >
42
+ </ 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