Created
January 25, 2017 04:42
-
-
Save sidwarkd/9420cb6f00ec3b920992d6b585164dd3 to your computer and use it in GitHub Desktop.
Help for Sameer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var config = require("./config.js"); | |
var socket = require("socket.io-client")(config.server_url); | |
var gpio = require("rpi-gpio"); | |
var sensorLib = require('node-dht-sensor'); | |
var data=0; | |
process.on("SIGINT", function(){ | |
gpio.write(config.led, 1, function(){ | |
gpio.destroy(function(){ | |
process.exit(); | |
}); | |
}); | |
}); | |
gpio.setup(config.led, gpio.DIR_OUT, function(){ | |
gpio.write(config.led, 1); // turns led off | |
}); | |
var dht_sensor = { | |
initialize: function () { | |
return sensorLib.initialize(11, 4); | |
}, | |
read: function () { | |
var readout = sensorLib.read(); | |
console.log('Temperature: ' + readout.temperature.toFixed(2) + 'C, ' + | |
'humidity: ' + readout.humidity.toFixed(2) + '%'); | |
return readout; | |
} | |
}; | |
if (dht_sensor.initialize()) { | |
console.log("DHT Sensor initialized!"); | |
} else { | |
console.warn('Failed to initialize sensor'); | |
} | |
socket.on("connect", function(){ | |
console.log("Connected to server"); | |
socket.on("updateState", function(state){ | |
console.log("The new state is: " + state); | |
//gpio.write(config.led, !state); | |
}); | |
// send Temperature data every 5 seconds | |
setInterval(function(){ | |
socket.emit("dh11Reading", dht_sensor.read()); | |
}, 5000); | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>Control My Stuff | DailyIoT</title> | |
<!-- Latest compiled and minified CSS --> | |
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> | |
<!-- Optional theme --> | |
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css"> | |
<!-- Custom styles for this template --> | |
<link href="/stylesheets/style.css" rel="stylesheet"> | |
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries --> | |
<!--[if lt IE 9]> | |
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script> | |
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script> | |
<![endif]--> | |
</head> | |
<body> | |
<div class="temp"></div> | |
<div class="site-wrapper"> | |
<div class="site-wrapper-inner"> | |
<div class="cover-container"> | |
<div class="inner cover"> | |
<h1 class="cover-heading">Control My Stuff</h1> | |
<div class="onoffswitch" style="margin:0px auto;"> | |
<div class="switch demo3"> | |
<input type="checkbox" id="myonoffswitch"> | |
<label><i></i></label> | |
</div> | |
</div> | |
</div> | |
<div id="readings"> | |
<h2>READINGS:</h2> | |
</div> | |
</div> | |
</div> | |
</div> | |
<!-- Bootstrap core JavaScript | |
================================================== --> | |
<!-- Placed at the end of the document so the pages load faster --> | |
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script> | |
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script> | |
<script src="https://cdn.socket.io/socket.io-1.0.4.js"></script> | |
<script type="text/javascript"> | |
var socket = io.connect('/'); | |
$("#myonoffswitch").change(function(){ | |
socket.emit("stateChanged", this.checked); | |
console.log("led state emitted"); | |
}); | |
socket.on('connect',function(){ | |
console.log("connected to the pi"); | |
socket.on('newDH11Reading',function(reading){ | |
console.log(reading); | |
document.getElementById("readings").innerHTML = "<h1>" +reading.temperature.toFixed(2) + "</h1>"; | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var debug = require('debug')('server'); | |
var app = require('../app'); | |
var packet=0; | |
app.set('port', process.env.PORT || 3000); | |
var server = app.listen(app.get('port'), function() { | |
debug('Express server listening on port ' + server.address().port); | |
}); | |
var io = require("socket.io").listen(server); | |
io.on("connection", function(socket){ | |
console.log("Client Connected"); | |
socket.on("dh11Reading", function(reading){ | |
// Received a new reading from the Pi. Send it along to the webpage | |
console.log("DH11 Reading: " + reading) // This prints out to the Heroku server log | |
io.emit("newDH11Reading", reading); // This sends it along to all of the other clients connected. | |
}) | |
socket.on("stateChanged", function(state){ | |
console.log("State Changed: " + state); | |
io.emit("updateState", state); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment