Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var gpio = require("gpio");
- var net = require('net');
- var host = '192.168.1.101';
- var port = 5000;
- var client = false;
- var canRing = true;
- var gpio4 = gpio.export(4, {
- direction: 'in',
- interval: 200,
- ready: function() {
- console.log("Initialized gpio pin4.")
- net.createServer(function (sock) {
- console.log('Client connected!');
- client = sock;
- if (canRing == false) {
- // should practically never happen, but whatever:
- alertClient();
- }
- }).listen(port, host);
- console.log('Server listening...');
- }
- });
- gpio4.on("change", function(val) {
- if (0 == val) {
- // pin 4 connected to ground:
- onDoorbellPress();
- }
- });
- function alertClient() {
- console.log('Alerting client...');
- client.write('RINGRING\n');
- client.on('data', function(data) {
- console.log('Received OK message. Closing connection.');
- sock.destroy();
- });
- client.on('error', function (err) {
- console.log('Error alerting client ' + client + ':' + err);
- client = false;
- });
- console.log('Message sent.');
- }
- function onDoorbellPress() {
- if (canRing) {
- canRing = false;
- console.log("RINGRING");
- // prevent multiple alerts within the same second:
- setTimeout(function onTimer() {
- canRing = true;
- }, 1000);
- if (client) {
- alertClient();
- } else {
- console.log('No client connected.');
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement