File tree Expand file tree Collapse file tree 2 files changed +65
-0
lines changed
Expand file tree Collapse file tree 2 files changed +65
-0
lines changed Original file line number Diff line number Diff line change 1+ var app = require ( 'http' ) . createServer ( handler ) ,
2+ io = require ( 'socket.io' ) . listen ( app ) ,
3+ fs = require ( 'fs' )
4+
5+
6+
7+
8+
9+
10+ app . listen ( 8500 ) ;
11+
12+
13+ function handler ( req , res ) {
14+ console . log ( 'Connection from %j' , req . connection . remoteAddress ) ;
15+ fs . readFile ( __dirname + '/index.html' ,
16+ function ( err , data ) {
17+ if ( err ) {
18+ res . writeHead ( 500 ) ;
19+ return res . end ( 'Error loading index.html' ) ;
20+ }
21+ res . writeHead ( 200 ) ;
22+ res . end ( data ) ;
23+ } ) ;
24+ }
25+
26+
27+ io . sockets . on ( 'connection' , function ( socket ) {
28+
29+ var mqtt = require ( 'mqtt' ) ;
30+ var client = mqtt . createClient ( 1883 , 'localhost' , function ( err , client ) {
31+ keepalive: 10000
32+ } ) ;
33+
34+ client . on ( 'connect' , function ( ) {
35+ client . subscribe ( 'sensors/power/0' ) ;
36+
37+ client . on ( 'message' , function ( topic , message ) {
38+ console . log ( 'topic: ' + topic + ' payload: ' + message ) ;
39+ socket . emit ( 'data' , { channel : 0 , value : message } ) ;
40+ } ) ;
41+ } ) ;
42+
43+ } ) ;
44+
Original file line number Diff line number Diff line change 1+ < html >
2+ < head >
3+ < title >
4+ Power used today
5+ </ title >
6+ </ head >
7+ < body style ="font-family:Verdana; ">
8+
9+ < div id =power > </ div >
10+
11+ < script type ="text/javascript " src ="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js "> </ script >
12+ < script src ="http://www.trease.eu:8500/socket.io/socket.io.js "> </ script >
13+ < script >
14+ var socket = io . connect ( "http://192.168.1.103:8500" ) ;
15+ socket . on ( 'data' , function ( data ) {
16+ console . log ( "channel : %s, value : %s" , data . channel , data . value ) ;
17+ document . getElementById ( "power" ) . innerHTML = data . value ;
18+ } ) ;
19+ </ script >
20+ </ body >
21+ </ html >
You can’t perform that action at this time.
0 commit comments