roomba-iot – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | #!/usr/bin/env nodejs |
2 | /////////////////////////////////////////////////////////////////////////// |
||
3 | // Copyright (C) Wizardry and Steamworks 2018 - License: GNU GPLv3 // |
||
4 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
||
5 | // rights of fair usage, the disclaimer and warranty conditions. // |
||
6 | /////////////////////////////////////////////////////////////////////////// |
||
7 | |||
8 | const Gpio = require('onoff').Gpio |
||
9 | const mqtt = require('mqtt') |
||
10 | const YAML = require('yamljs') |
||
11 | const winston = require('winston') |
||
12 | |||
13 | var serialport = require("serialport") |
||
14 | var SerialPort = serialport.SerialPort |
||
15 | |||
16 | var SerialPort = require('serialport'); |
||
17 | var port = new SerialPort('/dev/serial0', { |
||
18 | baudRate: 115200 |
||
19 | }); |
||
20 | |||
21 | // Load configuration file. |
||
22 | const config = YAML.load('config.yml') |
||
23 | |||
24 | // Set up logger. |
||
25 | winston.add(winston.transports.File, {filename: config.log}) |
||
26 | |||
27 | // Initiate connection to MQTT. |
||
28 | const client = mqtt.connect(config.mqtt.url, {queueQoSZero: false}) |
||
29 | |||
30 | // Set up GPIO wake pin. |
||
31 | const wakeGPIO = new Gpio(config.wake, 'out') |
||
32 | |||
33 | client.on('connect', function () { |
||
34 | winston.info('Connected to MQTT server') |
||
35 | client.subscribe(config.mqtt.topic) |
||
36 | }) |
||
37 | |||
38 | client.on('message', function (topic, message) { |
||
39 | if(message.length === 0) |
||
40 | return; |
||
41 | |||
42 | // Remove any retained message. |
||
43 | client.publish(topic, "", {retain: true}) |
||
44 | |||
45 | message = message.toString() |
||
46 | |||
47 | winston.debug('Received message: ' + message) |
||
48 | |||
49 | if(!(message in config.ROOMBA)) { |
||
50 | winston.warn('Unknown Roomba command: ' + message) |
||
51 | return; |
||
52 | } |
||
53 | |||
54 | winston.info('Executing Roomba command: ' + message) |
||
55 | |||
56 | // Wake up Roomba. |
||
57 | RoombaWake(wakeGPIO) |
||
58 | |||
59 | // Execute Roomba command. |
||
60 | RoombaExecute(message) |
||
61 | }) |
||
62 | |||
63 | /* |
||
64 | * Sequence: |
||
65 | * 1. Set DD to HIGH |
||
66 | * 2. Wait 100ms |
||
67 | * 3. Set DD to LOW |
||
68 | * 4. Wait 500ms |
||
69 | * 5. Set DD to HIGH |
||
70 | */ |
||
71 | function RoombaWake(gpio) { |
||
72 | gpio.write(1, err => { |
||
73 | if(err) { |
||
74 | winston.error('Could not wake Roomba!') |
||
75 | return; |
||
76 | } |
||
77 | |||
78 | setTimeout(function() { |
||
79 | gpio.write(0, function(err) { |
||
80 | if(err) { |
||
81 | winston.error('Could not wake Roomba!') |
||
82 | return; |
||
83 | } |
||
84 | setTimeout(function() { |
||
85 | gpio.write(1, function(err) { |
||
86 | if(err) { |
||
87 | winston.error('Could not wake Roomba!') |
||
88 | return; |
||
89 | } |
||
90 | }) |
||
91 | }, 500) |
||
92 | }) |
||
93 | }, 100) |
||
94 | }) |
||
95 | } |
||
96 | |||
97 | function RoombaExecute(message) { |
||
98 | setTimeout(function () { |
||
99 | port.write(new Buffer(config.ROOMBA['start'])); |
||
100 | setTimeout(function () { |
||
101 | port.write(new Buffer(config.ROOMBA['safe'])); |
||
102 | setTimeout(function () { |
||
103 | port.write(new Buffer(config.ROOMBA[message])); |
||
104 | }, 500) |
||
105 | }, 30) |
||
106 | }, 30) |
||
107 | } |
||
108 |