Created
January 25, 2017 07:26
-
-
Save raiscui/a13c001a6834e5e80f22a94a8fe2bc00 to your computer and use it in GitHub Desktop.
pi2 接收
This file contains 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 Gpio = require('pigpio').Gpio, | |
led = new Gpio(17, { mode: Gpio.OUTPUT }), | |
led2 = new Gpio(18, { mode: Gpio.OUTPUT }); | |
led.pwmFrequency(50) | |
led2.pwmFrequency(50) | |
led.pwmWrite(0); | |
const PWRUN = 110; | |
const PWSTOP = 90; | |
led2.pwmWrite(PWSTOP); | |
var blinkTimeoutHandle1 = null; | |
var blinkTimeoutHandle2 = null; | |
var blinkStop = false; | |
var blink = function (led, t = 500, t2 = 0, x = 0, cb = null, frun = null) { | |
// x= times | |
if (frun) frun(); | |
if (blinkStop) return; | |
led.pwmWrite(0); | |
led.pwmWrite(255); | |
console.log('t', t, 'x', x) | |
clearTimeout(blinkTimeoutHandle1); | |
clearTimeout(blinkTimeoutHandle2); | |
blinkTimeoutHandle1 = setTimeout(function () { | |
led.pwmWrite(0); | |
blinkTimeoutHandle2 = setTimeout(function () { | |
if (x > 0) { | |
console.log('x', x) | |
return blink(led, t, t2, --x, cb); | |
} else if (x < 0) { | |
return blink(led, t, t2, x, cb); | |
} | |
if (cb) { | |
console.log('cb') | |
return cb(); | |
} | |
}, t2); | |
}, t); | |
}; | |
function ledBlinkInv(t = 500, t2 = 0) { | |
blinkStop = true; | |
clearTimeout(blinkTimeoutHandle1); | |
clearTimeout(blinkTimeoutHandle2); | |
setTimeout(function () { | |
ledBlink(t, t2, -1, null, () => { | |
blinkStop = false; | |
}); | |
}, 100); | |
} | |
var ledBlink = blink.bind(this, led); | |
var boot = setInterval(() => { | |
ledBlink(100); | |
}, 1000) | |
var mqtt = require('mqtt') | |
var client = mqtt.connect('mqtt://10.1.0.14:1883', { | |
clientId: 'pi2' | |
}) | |
var conts = [0, 0]; | |
var connected; | |
function ledshowConnect(cont) { | |
if (cont[0] + cont[1] === 1) { | |
connected = setInterval(() => { | |
ledBlink(400); | |
}, 800) | |
return | |
} | |
if (cont[0] + cont[1] === 2) { | |
connected = setInterval(() => { | |
ledBlink(800); | |
}, 1600) | |
} | |
} | |
client.on('connect', function () { | |
console.log('connect'); | |
client.subscribe('car'); | |
client.subscribe('pi2'); | |
clearImmediate(boot); | |
conts[0] = 1; | |
ledshowConnect(conts); | |
}); | |
client.on('reconnect', () => { | |
conts = [0, 0]; | |
clearImmediate(connected) | |
console.log('reconnect'); | |
ledBlink(20, 800); | |
}); | |
client.on('offline', () => { | |
conts = [0, 0]; | |
clearImmediate(connected) | |
console.log('offline') | |
ledBlinkInv(20, 5000); | |
}); | |
client.on('message', (topic, message) => { | |
// message is Buffer | |
let msg = message.toString(); | |
console.log('message:', message); | |
// run | |
if (message[0] === 1) { | |
led2.pwmWrite(PWRUN); | |
console.log('run'); | |
setTimeout(function () { | |
clearImmediate(connected); | |
ledBlink(30, 30, 3, () => ledshowConnect(conts)); | |
}, 0); | |
return; | |
} | |
// stop | |
if (message[0] === 0) { | |
led2.pwmWrite(PWSTOP); | |
console.log('stop'); | |
setTimeout(function () { | |
clearImmediate(connected); | |
ledBlink(30, 30, 10, () => ledshowConnect(conts)); | |
}, 0); | |
return; | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment