roomba-iot – Diff between revs 3 and 4
?pathlinks?
Rev 3 | Rev 4 | |||
---|---|---|---|---|
Line 1... | Line 1... | |||
1 | #!/usr/bin/env nodejs |
1 | #!/usr/bin/env nodejs |
|
2 | /////////////////////////////////////////////////////////////////////////// |
2 | /////////////////////////////////////////////////////////////////////////// |
|
3 | // Copyright (C) Wizardry and Steamworks 2018 - License: GNU GPLv3 // |
3 | // Copyright (C) Wizardry and Steamworks 2018 - License: GNU GPLv3 // |
|
4 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
4 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
|
5 | // rights of fair usage, the disclaimer and warranty conditions. // |
5 | // rights of fair usage, the disclaimer and warranty conditions. // |
|
6 | /////////////////////////////////////////////////////////////////////////// |
6 | /////////////////////////////////////////////////////////////////////////// |
|
Line 17... | Line 17... | |||
17 | |
17 | |
|
18 | // Load configuration file. |
18 | // Load configuration file. |
|
Line 19... | Line 19... | |||
19 | const config = YAML.load('config.yml') |
19 | const config = YAML.load('config.yml') |
|
- | 20 | |
||
- | 21 | // Set up logger. |
||
20 | |
22 | const logger = winston.createLogger({ |
|
21 | // Set up logger. |
23 | transports: [ |
|
- | 24 | new winston.transports.Console(), |
||
22 | winston.add(winston.transports.File, { |
25 | new winston.transports.File({ filename: config.log }) |
|
Line 23... | Line 26... | |||
23 | filename: config.log |
26 | ] |
|
24 | }) |
27 | }); |
|
Line 25... | Line 28... | |||
25 | |
28 | |
|
Line 36... | Line 39... | |||
36 | stopBits: 1, |
39 | stopBits: 1, |
|
37 | flowControl: false |
40 | flowControl: false |
|
38 | }) |
41 | }) |
|
Line 39... | Line 42... | |||
39 | |
42 | |
|
40 | // Set up sensor packet assembler. |
43 | // Set up sensor packet assembler. |
|
Line 41... | Line 44... | |||
41 | port.on('data', AssembleSensorPacket); |
44 | port.on('data', PublishSensorPacket); |
|
42 | |
45 | |
|
43 | // Set up GPIO wake pin. |
46 | // Set up GPIO wake pin. |
|
44 | const wakeGPIO = new Gpio(config.wake, 'out') |
47 | const wakeGPIO = new Gpio(config.wake, 'out') |
|
45 | // Wake up Roomba. |
48 | // Wake up Roomba. |
|
46 | RoombaWake(wakeGPIO, function () { |
49 | RoombaWake(wakeGPIO, function () { |
|
47 | // Switch to safe mode. |
50 | // Switch to safe mode. |
|
48 | RoombaSafe(function () { |
51 | RoombaSafe(function () { |
|
49 | // Poll sensors periodically. |
52 | // Poll sensors periodically. |
|
50 | setInterval(function () { |
53 | setInterval(function () { |
|
51 | port.write(new Buffer(COMMAND_SEQUENCES['query_all_sensors'])) |
54 | port.write(new Buffer.from(COMMAND_SEQUENCES['query_all_sensors'])) |
|
52 | }, config.sensor_poll_interval) |
55 | }, config.sensor_poll_interval) |
|
Line 53... | Line 56... | |||
53 | }) |
56 | }) |
|
54 | }) |
57 | }) |
|
55 | |
58 | |
|
56 | // Subscribe to configured topic when client connects and start polling sensors. |
59 | // Subscribe to configured topic when client connects and start polling sensors. |
|
57 | mqttClient.on('connect', function () { |
60 | mqttClient.on('connect', function () { |
|
58 | winston.info('Connected to MQTT server') |
61 | logger.info('Connected to MQTT server') |
|
59 | mqttClient.subscribe(config.mqtt.topic, function (error) { |
62 | mqttClient.subscribe(config.mqtt.topic, function (error) { |
|
60 | if (error) { |
63 | if (error) { |
|
61 | winston.error('Unable to subscribe to MQTT topic') |
64 | logger.error('Unable to subscribe to MQTT topic') |
|
62 | } |
65 | } |
|
Line 63... | Line 66... | |||
63 | winston.info("Subscribed to MQTT topic " + config.mqtt.topic) |
66 | logger.info("Subscribed to MQTT topic " + config.mqtt.topic) |
|
64 | }) |
67 | }) |
|
65 | }) |
68 | }) |
|
66 | |
69 | |
|
Line 67... | Line 70... | |||
67 | // Execute command when message is received on configured topic. |
70 | // Execute command when message is received on configured topic. |
|
Line 68... | Line 71... | |||
68 | mqttClient.on('message', function (topic, message) { |
71 | mqttClient.on('message', function (topic, message) { |
|
Line 69... | Line 72... | |||
69 | if (message.length === 0) |
72 | if (message.length === 0) |
|
70 | return |
73 | return |
|
71 | |
74 | |
|
Line 72... | Line 75... | |||
72 | message = message.toString() |
75 | message = message.toString() |
|
Line 73... | Line 76... | |||
73 | |
76 | |
|
74 | winston.debug('Received message: ' + message) |
77 | logger.debug('Received message: ' + message) |
|
75 | |
78 | |
|
76 | // Skip commands that do not figure in the command sequence table. |
79 | // Skip commands that do not figure in the command sequence table. |
|
77 | if (!(message in COMMAND_SEQUENCES)) |
80 | if (!(message in COMMAND_SEQUENCES)) |
|
Line -... | Line 81... | |||
- | 81 | return |
||
- | 82 | |
||
- | 83 | logger.info('Executing Roomba command: ' + message) |
||
- | 84 | |
||
78 | return |
85 | // Execute Roomba command in safe mode. |
|
79 | |
86 | RoombaSafe(function () { |
|
80 | winston.info('Executing Roomba command: ' + message) |
87 | port.write(new Buffer.from(COMMAND_SEQUENCES[message])) |
|
81 | |
88 | }) |
|
82 | // Execute Roomba command in safe mode. |
89 | }) |
|
Line 95... | Line 102... | |||
95 | * 5. Set DD to HIGH |
102 | * 5. Set DD to HIGH |
|
96 | */ |
103 | */ |
|
97 | function RoombaWake(gpio, callback) { |
104 | function RoombaWake(gpio, callback) { |
|
98 | gpio.write(1, err => { |
105 | gpio.write(1, err => { |
|
99 | if (err) { |
106 | if (err) { |
|
100 | winston.error('Could not wake Roomba!') |
107 | logger.error('Could not wake Roomba!') |
|
101 | return |
108 | return |
|
102 | } |
109 | } |
|
Line 103... | Line 110... | |||
103 | |
110 | |
|
104 | setTimeout(function () { |
111 | setTimeout(function () { |
|
105 | gpio.write(0, function (err) { |
112 | gpio.write(0, function (err) { |
|
106 | if (err) { |
113 | if (err) { |
|
107 | winston.error('Could not wake Roomba!') |
114 | logger.error('Could not wake Roomba!') |
|
108 | return |
115 | return |
|
109 | } |
116 | } |
|
110 | setTimeout(function () { |
117 | setTimeout(function () { |
|
111 | gpio.write(1, function (err) { |
118 | gpio.write(1, function (err) { |
|
112 | if (err) { |
119 | if (err) { |
|
113 | winston.error('Could not wake Roomba!') |
120 | logger.error('Could not wake Roomba!') |
|
114 | return |
121 | return |
|
115 | } |
122 | } |
|
116 | callback() |
123 | callback() |
|
117 | }) |
124 | }) |
|
Line 124... | Line 131... | |||
124 | /* |
131 | /* |
|
125 | * Put Roomba in safe mode. |
132 | * Put Roomba in safe mode. |
|
126 | */ |
133 | */ |
|
127 | function RoombaSafe(callback) { |
134 | function RoombaSafe(callback) { |
|
128 | setTimeout(function () { |
135 | setTimeout(function () { |
|
129 | port.write(new Buffer(COMMAND_SEQUENCES['start'])) |
136 | port.write(new Buffer.from(COMMAND_SEQUENCES['start'])) |
|
130 | setTimeout(function () { |
137 | setTimeout(function () { |
|
131 | port.write(new Buffer(COMMAND_SEQUENCES['safe'])) |
138 | port.write(new Buffer.from(COMMAND_SEQUENCES['safe'])) |
|
132 | setTimeout(callback, 500) |
139 | setTimeout(callback, 500) |
|
133 | }, 100) |
140 | }, 100) |
|
134 | }, 100) |
141 | }, 100) |
|
135 | } |
142 | } |
|
Line 136... | Line 143... | |||
136 | |
143 | |
|
137 | /* |
144 | /* |
|
138 | * Build the sensor buffer as the data is read from the serial port. |
145 | * Build the sensor buffer as the data is read from the serial port. |
|
139 | */ |
146 | */ |
|
Line 140... | Line 147... | |||
140 | var packetBuffer = null |
147 | var packetBuffer = null |
|
141 | |
148 | |
|
142 | function AssembleSensorPacket(data) { |
149 | function PublishSensorPacket(data) { |
|
143 | if (packetBuffer) { |
150 | if (packetBuffer) { |
|
144 | data = Buffer.concat( |
151 | data = Buffer.concat( |
|
Line 145... | Line 152... | |||
145 | [packetBuffer, data], packetBuffer.length + data.length) |
152 | [packetBuffer, data], packetBuffer.length + data.length) |
|
146 | } |
153 | } |
|
Line 147... | Line 154... | |||
147 | |
154 | |
|
148 | if (data.length >= decoder.SENSOR_PACKET_SIZE) { |
155 | if (data.length >= decoder.SENSOR_PACKET_SIZE) { |
|
149 | var packet = data.slice(0, decoder.SENSOR_PACKET_SIZE) |
156 | var packet = data.slice(0, decoder.SENSOR_PACKET_SIZE) |
|
150 | |
157 | |
|
151 | decoder.decode_all_sensors(packet, function (result, position, error) { |
158 | decoder.decode_all_sensors(packet, function (result, position, error) { |
|
Line 152... | Line 159... | |||
152 | if (error) { |
159 | if (error) { |
|
153 | winston.error('Packet assembly failed with error:' + error) |
160 | logger.error('Packet assembly failed with error:' + error) |