board.on(webduino.BoardEvent.READY, function () { led = new webduino.module.Led(board, board.getDigitalPin(10)); led.blink(50); setTimeout(function () { board.close(); }, 5000); });
function createWebArduino() { var option = { device: config.boardId, server: config.mqttBroker }; var board = new webduino.WebArduino( option ); var router = express.Router(); router.get('/', function(req, res) { res.json({ Status: status }); }); app.use('/', router); board.on(webduino.BoardEvent.READY, onReady); board.on(webduino.BoardEvent.DISCONNECT, onDisconnect); board.on(webduino.BoardEvent.ERROR, onError); board.on(webduino.BoardEvent.BEFOREDISCONNECT, onBeforeDisconnect); //////////////// function onError(err) { log(err); board.disconnect(); writeData({value: -1}); } function onBeforeDisconnect() { log('before disconnect'); } function onDisconnect() { log('disconnect'); board.disconnect(); writeData({value: -1}); var data = { chat_id: devGroupChatId, text: '我 GG 惹 ╰( ゜ω゜)っ✂╰ひ╯' } request.post( { url:'https://bot.moli.rocks/messages', headers: { Authorization: token }, json: true, body: data }, function optionalCallback(err, httpResponse, body) { if (err) { log(err); } else { log('message success send!'); } } ); getCameraSnapshot(devGroupChatId); createWebArduino(); } function onReady() { var text; status = -2; board.samplingInterval = 20; button = new webduino.module.Button(board, board.getDigitalPin( config.boardPin )); log('Ready'); var data = { chat_id: devGroupChatId, text: '我開始監控了喔 ^.<' } request.post( { url:'https://bot.moli.rocks/messages', headers: { Authorization: token }, json: true, body: data }, function optionalCallback(err, httpResponse, body) { if (err) { log(err); } else { log('message success send!'); } } ); onToggle(); button.on('pressed', onToggle); button.on('released', onToggle); //////////////// function onToggle() { if (timer) { clearTimeout(timer); } log('onToggle Status: ' + status); var chatId = groupChatId; if (status >= 0) { timer = setTimeout(toggle, 2000); } else if (status === -2) { chatId = devGroupChatId; timer = setTimeout(toggle, 2000); } else { toggle(); } function toggle() { console.log(chatId); var boardValue = board.getDigitalPin( config.boardPin ).value; if (status != boardValue) { log('status: ' + status); if (boardValue == 1) { text = 'MOLi 關門'; } else if (boardValue == 0) { text = 'MOLi 開門'; } if (status == -2) { text = text.concat('中'); } log('Send "' + text + '" to ' + chatId); var data = { chat_id: chatId, text: text } request.post( { url:'https://bot.moli.rocks/messages', headers: { Authorization: token }, json: true, body: data }, function optionalCallback(err, httpResponse, body) { if (err) { log(err); } else { log('message success send!'); } } ); getCameraSnapshot(chatId); writeData({value: boardValue}); } else { log('重複喔'); } log('boardValue: ' + boardValue); } } } }
board.on(webduino.BoardEvent.DISCONNECT, function () { console.log('disconnect'); // test: should not emit 'disconnect' again board.disconnect(); });
setTimeout(function () { board.close(); }, 5000);
'use strict'; var webduino = require('webduino-js'), board, led; board = new webduino.WebArduino('yJL0'); // board = new webduino.Arduino({ // 'transport': 'serial', // 'path': '/dev/cu.usbmodem1421' // }); // board = new webduino.Arduino({ // 'transport': 'bluetooth', // 'address': '30:14:09:30:15:67' // }); board.on(webduino.BoardEvent.READY, function () { led = new webduino.module.Led(board, board.getDigitalPin(10)); led.blink(50); setTimeout(function () { board.close(); }, 5000); }); board.on(webduino.BoardEvent.ERROR, function (err) { console.log(err); });