AllWize Library
AllWize.cpp
Go to the documentation of this file.
1 /*
2 
3 AllWize Library
4 
5 Copyright (C) 2018-2021 by AllWize <github@allwize.io>
6 
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 
20 */
21 
27 #include "AllWize.h"
28 #include <assert.h>
29 
30 // -----------------------------------------------------------------------------
31 // Init
32 // -----------------------------------------------------------------------------
33 
40 AllWize::AllWize(HardwareSerial *serial, uint8_t reset_gpio, uint8_t config_gpio) : _stream(serial), _hw_serial(serial), _reset_gpio(reset_gpio), _config_gpio(config_gpio) {
41  _init();
42 }
43 
44 #if not defined(ARDUINO_ARCH_SAMD) && not defined(ARDUINO_ARCH_ESP32)
45 
51 AllWize::AllWize(SoftwareSerial *serial, uint8_t reset_gpio, uint8_t config_gpio) : _stream(serial), _sw_serial(serial), _reset_gpio(reset_gpio), _config_gpio(config_gpio) {
52  _init();
53 }
54 #endif
55 
63 AllWize::AllWize(uint8_t rx, uint8_t tx, uint8_t reset_gpio, uint8_t config_gpio) : _rx(rx), _tx(tx), _reset_gpio(reset_gpio), _config_gpio(config_gpio) {
64 #if defined(ARDUINO_ARCH_SAMD)
65  // Software serial not implemented for SAMD
66  assert(false);
67 #elif defined(ARDUINO_ARCH_ESP32)
68  _stream = _hw_serial = new HardwareSerial(HARDWARE_SERIAL_PORT);
69 #else
70  _stream = _sw_serial = new SoftwareSerial(_rx, _tx);
71 #endif
72  _init();
73 }
74 
76  if (GPIO_NONE != _reset_gpio) {
77  pinMode(_reset_gpio, OUTPUT);
78  digitalWrite(_reset_gpio, HIGH);
79  }
80  if (GPIO_NONE != _config_gpio) {
81  pinMode(_config_gpio, OUTPUT);
82  digitalWrite(_config_gpio, LOW);
83  }
84  randomSeed(analogRead(0));
85  _access_number = random(0, 256);
86 }
87 
91 void AllWize::begin(uint8_t baudrate) {
92 
93  _baudrate = BAUDRATES[baudrate-1];
94  reset();
95  _niceDelay(200);
96 
97  // Figure out module type
98  _readModel();
99  String part_number = getPartNumber();
100  if (part_number.equals("RC1701HP-MBUS4")) {
102  } else if (part_number.equals("RC1701HP-OSP")) {
104  } else if (part_number.equals("RC1701HP-WIZE")) {
106  _ci = CI_WIZE;
107  } else {
109  }
110 
114 
115 }
116 
121 
122  if (_hw_serial) {
123 
124  _hw_serial->end();
125 #if defined(ARDUINO_ARCH_ESP32)
126  if ((_rx != -1) && (_tx != -1)) {
127  pinMode(_rx, FUNCTION_4);
128  pinMode(_tx, FUNCTION_4);
129  _hw_serial->begin(_baudrate, SERIAL_8N1, _rx, _tx);
130  } else {
131  _hw_serial->begin(_baudrate);
132  }
133 #else
134  _hw_serial->begin(_baudrate);
135 #endif
136 
137  } else {
138 
139 #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_SAMD)
140  // It should never hit this block
141  assert(false);
142 #else
143  _sw_serial->end();
144  _sw_serial->begin(_baudrate);
145 #endif
146 
147  }
148 
149  _flush();
150 
151  // Cache memory
152  #if USE_MEMORY_CACHE
154  #endif
155 
156 }
157 
163  if (GPIO_NONE == _reset_gpio) {
164  _resetSerial();
165  _niceDelay(100);
167  if (_setConfig(true)) {
168  _send('@');
169  _send('R');
170  _send('R');
171  _niceDelay(100);
172  if (GPIO_NONE != _config_gpio) {
173  digitalWrite(_config_gpio, LOW);
174  }
175  _config = false;
176  _resetSerial();
177  return true;
178  }
179  } else {
180  digitalWrite(_reset_gpio, LOW);
181  _niceDelay(1);
182  digitalWrite(_reset_gpio, HIGH);
183  _niceDelay(100);
184  if (GPIO_NONE != _config_gpio) {
185  digitalWrite(_config_gpio, LOW);
186  }
187  _config = false;
188  _resetSerial();
189  return true;
190  }
191  return false;
192 }
193 
198  if (_setConfig(true)) _setConfig(false);
199  /*
200  if (_send(CMD_ENTER_CONFIG) == 1) {
201  _flush();
202  _send(CMD_EXIT_CONFIG);
203  }
204  _niceDelay(10);
205  */
206 }
207 
213  _resetSerial();
214  _niceDelay(100);
216  if (_setConfig(true)) {
217  _send('@');
218  _send('R');
219  _send('C');
220  _niceDelay(100);
221  if (GPIO_NONE != _config_gpio) {
222  digitalWrite(_config_gpio, LOW);
223  }
224  _config = false;
225  _resetSerial();
226  return true;
227  }
228  return false;
229 }
230 
235  setMode(DEFAULT_MBUS_MODE, true);
242  setAppendRSSI(true);
244 }
245 
250  setMode(DEFAULT_MBUS_MODE, true);
255 }
256 
261  setMode(DEFAULT_MBUS_MODE, true);
263 }
264 
269  if (!_setConfig(true)) return;
270  _send(CMD_SLEEP);
271 }
272 
277  _send(CMD_AWAKE);
278  _niceDelay(5);
279  ready();
280 }
281 
286  bool response = _setConfig(true);
287  if (response) _setConfig(false);
288  return response;
289 }
290 
294 bool AllWize::waitForReady(uint32_t timeout) {
295  uint32_t start = millis();
296  while (millis() - start < timeout) {
297  if (ready()) return true;
298  _niceDelay(100);
299  }
300  return false;
301 }
302 
307 void AllWize::dump(Stream &debug) {
308 
309  #if not USE_MEMORY_CACHE
310  uint8_t _memory[0x100] = {0xFF};
311  bool _ready = _cacheMemory(_memory);
312  #endif
313 
314  if (!_ready) {
315  debug.println("Error doing memory dump...");
316  return;
317  }
318 
319  char ch[10];
320  char ascii[17] = {0};
321  uint8_t address = 0;
322  ascii[16] = 0;
323 
324  debug.println();
325  debug.print(" ");
326  for (address = 0; address <= 0x0F; address++) {
327  snprintf(ch, sizeof(ch), "%02X ", address);
328  debug.print(ch);
329  }
330  debug.println();
331  debug.print("------------------------------------------------------");
332 
333  address = 0;
334  while (true) {
335 
336  if ((address % 16) == 0) {
337  if (address > 0)
338  debug.print(ascii);
339  snprintf(ch, sizeof(ch), "\n0x%02X: ", address);
340  debug.print(ch);
341  }
342  if ((31 < _memory[address]) && (_memory[address] < 127)) {
343  ascii[address % 16] = (char)_memory[address];
344  } else {
345  ascii[address % 16] = ' ';
346  }
347  snprintf(ch, sizeof(ch), "%02X ", (uint8_t)_memory[address]);
348  debug.print(ch);
349 
350  if (0xFF == address) break;
351  address++;
352 
353  }
354 
355  debug.println();
356  debug.println();
357 
358 }
359 
366 bool AllWize::send(uint8_t *buffer, uint8_t len) {
367 
368  // Check we are in IDLE mode
369  if (_config) return false;
370 
371  // Clean line
372  softReset();
373 
374  // Send no response message if len is 0
375  if (0 == len) return (1 == _send(0xFE));
376 
377  // Wize transport layer
378  bool send_wize_transport_layer = (MODULE_WIZE == _module) && (CI_WIZE == _ci);
379 
380  // message length is payload length + 1 (CI) + 2 (for timestamp if wize) + 5 (wize transport layer if wize)
381  uint8_t message_len = len + 1;
382  if (MODULE_WIZE == _module) message_len += 2;
383  if (send_wize_transport_layer) message_len += 5;
384 
385  // max payload size is 0xF6 bytes
386  if (message_len > 0xF6) return false;
387 
388  // length
389  if (1 != _send(message_len)) return false;
390 
391  // control information field
392  if (1 != _send(_ci)) return false;
393 
394  // transport layer
395  if (send_wize_transport_layer) {
396  _send(_wize_control & 0xFF); // Wize Control
397  _send(_wize_network_id & 0xFF); // Network ID HIGH
398  _send((_counter >> 8) & 0xFF); // Frame counter HIGH
399  _send((_counter >> 0) & 0xFF); // Frame counter LOW
400  _send(_wize_application); // Wize app indicator
401  }
402 
403  // application payload
404  if (len != _send(buffer, len)) return false;
405 
406  // timestamp, TODO: add option to provide a proper timestamp
407  if (MODULE_WIZE == _module) {
408  unsigned long ts = millis() / 1000;
409  _send((ts >> 8) & 0xFF);
410  _send((ts >> 0) & 0xFF);
411  }
412 
413  _access_number++;
414  _counter++;
415  return true;
416 
417 }
418 
424 bool AllWize::send(const char *buffer) {
425  return send((uint8_t *)buffer, strlen(buffer));
426 }
427 
432 bool AllWize::ack() {
433  if (_config) return false;
435  return send("ACK");
436 }
437 
443 bool AllWize::enableRX(bool enable) {
444  if (_config) return false;
445  if (enable) {
447  } else {
449  }
450  return true;
451 }
452 
459 
460  bool response = false;
461 
462  static uint32_t when = millis();
463 
464  while (_stream->available() && _pointer < RX_BUFFER_SIZE) {
465 
466  uint8_t ch = _stream->read();
467 
468  #if defined(ALLWIZE_DEBUG_PORT)
469  {
470  char buffer[10];
471  snprintf(buffer, sizeof(buffer), "r %02X '%c'", ch, (32 <= ch && ch <= 126) ? ch : 32);
472  ALLWIZE_DEBUG_PRINTLN(buffer);
473  }
474  #endif
475 
476  _buffer[_pointer++] = ch;
477  when = millis();
478 
479  #if defined(ARDUINO_ARCH_ESP8266)
480  yield();
481  #endif
482 
483  }
484 
485  // Check if message finished and decode it
486  if ((_pointer > 0) && (millis() - when > 100)) {
487 
488  response = _decode();
489  _length = _pointer;
490  _pointer = 0;
491 
492  // If we don't soft-reset the line the RX channel gets stalled
493  softReset();
494 
495  }
496 
497  return response;
498 
499 }
500 
506  return _message;
507 }
508 
514 uint8_t * AllWize::getBuffer() {
515  return _buffer;
516 }
517 
523  return _length;
524 }
525 
531 bool AllWize::setWizeControl(uint8_t wize_control) {
532  if (wize_control > 14) return false;
533  _wize_control = wize_control;
534  return true;
535 }
536 
541 void AllWize::setWizeOperatorId(uint8_t wize_network_id) {
542  setWizeNetworkId(wize_network_id);
543 }
544 
549 void AllWize::setWizeNetworkId(uint8_t wize_network_id) {
550  _wize_network_id = wize_network_id;
551 }
552 
557 void AllWize::setWizeApplication(uint8_t wize_application) {
558  _wize_application = wize_application;
559 }
560 
565 void AllWize::setCounter(uint16_t counter) {
566  _counter = counter;
567 }
568 
573 uint16_t AllWize::getCounter() {
574  return _counter;
575 }
576 
577 // -----------------------------------------------------------------------------
578 // Configuration
579 // -----------------------------------------------------------------------------
580 
586  _ci = ci;
587 }
588 
594  return _ci;
595 }
596 
602 void AllWize::setChannel(uint8_t channel, bool persist) {
603  if (channel > 41) return;
604  if (persist) {
605  _setSlot(MEM_CHANNEL, channel);
606  if (MODULE_WIZE == _module) {
607  _setSlot(MEM_CHANNEL_RX, channel);
608  }
609  }
610  _sendCommand(CMD_CHANNEL, channel);
611 }
612 
618  return _getSlot(MEM_CHANNEL);
619 }
620 
626 void AllWize::setPower(uint8_t power, bool persist) {
627  if (0 < power && power < 6) {
628  if (persist) {
629  _setSlot(MEM_RF_POWER, power);
630  }
631  _sendCommand(CMD_RF_POWER, power);
632  }
633 }
634 
639 uint8_t AllWize::getPower() {
640  return _getSlot(MEM_RF_POWER);
641 }
642 
647 void AllWize::setDataRate(uint8_t dr) {
648 
649  if (_module == MODULE_MBUS4) return;
650  if (dr < 1) return;
651  if (_module == MODULE_OSP) {
652  if (DATARATE_6400bps == dr) {
654  }
655  if (dr > 5) return;
656  }
657  if (_module == MODULE_WIZE) {
658  if (dr > 3) return;
659  }
660 
661  _setSlot(MEM_DATA_RATE, dr);
662  if (MODULE_WIZE == _module) {
664  }
665 
666 }
667 
673  return _getSlot(MEM_DATA_RATE);
674 }
675 
681 void AllWize::setMode(uint8_t mode, bool persist) {
682 
683  // Wize FW accepts only modes 0x10 and 0x11
684  if (MODULE_WIZE == _module) {
685  if ((MBUS_MODE_N1 != mode) && (MBUS_MODE_N2 != mode)) return;
686  }
687 
688  // Only OSP FW accepts mode 0x12
689  if ((MBUS_MODE_OSP == mode) && (MODULE_OSP != _module)) return;
690 
691  if (persist) {
692  _setSlot(MEM_MBUS_MODE, mode);
693  }
695  _mbus_mode = mode;
696 
697 }
698 
703 uint8_t AllWize::getMode() {
704  return _mbus_mode;
705 }
706 
711 void AllWize::setSleepMode(uint8_t mode) {
712  _setSlot(MEM_SLEEP_MODE, mode);
713 }
714 
720  return _getSlot(MEM_SLEEP_MODE);
721 }
722 
727 void AllWize::setAppendRSSI(bool value) {
728  if (value == 1) {
730  } else {
732  }
733  _append_rssi = value;
734 }
735 
741  return _append_rssi;
742 }
743 
748 void AllWize::setPreamble(uint8_t preamble) {
749  if (PREAMBLE_FORMAT_A == preamble || PREAMBLE_FORMAT_B == preamble) {
750  _setSlot(MEM_PREAMBLE_LENGTH, preamble);
751  }
752 }
753 
760 }
761 
766 void AllWize::setTimeout(uint16_t ms) {
767  if (ms > 4080) return;
768  uint8_t timeout = (ms / 16) - 1;
769  _setSlot(MEM_TIMEOUT, timeout);
770 }
771 
777  uint8_t timeout = _getSlot(MEM_TIMEOUT);
778  return 16 * (uint16_t) (timeout + 1);
779 }
780 
785 void AllWize::setNetworkRole(uint8_t role) {
786  _setSlot(MEM_NETWORK_ROLE, role);
787 }
788 
794  return _getSlot(MEM_NETWORK_ROLE);
795 }
796 
801 void AllWize::setLEDControl(uint8_t value) {
802  if (value > 3) return;
803  _setSlot(MEM_LED_CONTROL, value);
804 }
805 
811  return _getSlot(MEM_LED_CONTROL);
812 }
813 
818 void AllWize::setDataInterface(uint8_t value) {
819  if (value <= 0x0C) {
821  _data_interface = value;
822  }
823 }
824 
830  return _data_interface;
831 }
832 
838 void AllWize::setControlField(uint8_t value, bool persist) {
839  if (persist) {
840  _setSlot(MEM_CONTROL_FIELD, value);
841  }
843 }
844 
850  return _getSlot(MEM_CONTROL_FIELD);
851 }
852 
858 void AllWize::setInstallMode(uint8_t mode, bool persist) {
859  if (mode <= 2) {
860  if (persist) {
861  _setSlot(MEM_INSTALL_MODE, mode);
862  }
864  }
865 }
866 
872  return _getSlot(MEM_INSTALL_MODE);
873 }
874 
879 void AllWize::setMAC2CheckOnlyFlag(uint8_t flag) {
880  if (0 == flag || 1 == flag) {
882  }
883 }
884 
891 }
892 
897 void AllWize::setEncryptFlag(uint8_t flag) {
898  if (0 == flag || 1 == flag || 3 == flag) {
899  _setSlot(MEM_ENCRYPT_FLAG, flag);
900  }
901 }
902 
908  return _getSlot(MEM_ENCRYPT_FLAG);
909 }
910 
915 void AllWize::setDecryptFlag(uint8_t flag) {
916  _setSlot(MEM_DECRYPT_FLAG, flag);
917 }
918 
924  return _getSlot(MEM_DECRYPT_FLAG);
925 }
926 
932 void AllWize::setKey(uint8_t reg, const uint8_t *key) {
933  if (reg > 128) return;
934  uint8_t data[17];
935  data[0] = reg;
936  memcpy(&data[1], key, 16);
937  _sendCommand(CMD_KEY_REGISTER, data, 17);
938 }
939 
944 void AllWize::setDefaultKey(const uint8_t *key) {
945  _setSlot(MEM_DEFAULT_KEY, (uint8_t *)key, 16);
946 }
947 
952 void AllWize::getDefaultKey(uint8_t *key) {
953  _getSlot(MEM_DEFAULT_KEY, key, 16);
954 }
955 
960 void AllWize::setAccessNumber(uint8_t value) {
962 }
963 
968 void AllWize::setBaudRate(uint8_t value) {
969  if ((0 < value) & (value < 12)) {
970  if (ready()) _setSlot(MEM_UART_BAUD_RATE, value);
971  _baudrate = BAUDRATES[value-1];
972  }
973 }
974 
981 }
982 
988 uint32_t AllWize::getBaudRateSpeed(uint8_t value) {
989  if ((0 < value) & (value < 12)) {
990  return BAUDRATES[value-1];
991  }
992  return 0;
993 }
994 
995 // -----------------------------------------------------------------------------
996 
1003  uint8_t response = _sendCommand(CMD_RSSI);
1004  if (response > 0) return -0.5 * (float) _buffer[0];
1005  return 0;
1006 }
1007 
1013  uint8_t response = _sendCommand(CMD_TEMPERATURE);
1014  uint8_t ret_val = 0;
1015 
1016  if (response > 0) {
1017  ret_val = _buffer[0] - 128;
1018  } else {
1019  ret_val = 0;
1020  }
1021 
1022  return ret_val;
1023 }
1024 
1030  uint8_t response = _sendCommand(CMD_VOLTAGE);
1031  uint16_t ret_val;
1032  if (response > 0) {
1033  ret_val = 30 * _buffer[0];
1034  } else {
1035  ret_val = 0;
1036  }
1037 
1038  return ret_val;
1039 }
1040 
1047 }
1048 
1053 bool AllWize::setMID(uint16_t mid) {
1054  uint8_t buffer[2];
1055  buffer[0] = (mid >> 8) & 0xFF;
1056  buffer[1] = (mid >> 0) & 0xFF;
1057  return _setSlot(MEM_MANUFACTURER_ID, buffer, 2);
1058 }
1059 
1066 }
1067 
1072 bool AllWize::setUID(uint32_t uid) {
1073  uint8_t buffer[4];
1074  buffer[0] = (uid >> 24) & 0xFF;
1075  buffer[1] = (uid >> 16) & 0xFF;
1076  buffer[2] = (uid >> 8) & 0xFF;
1077  buffer[3] = (uid >> 0) & 0xFF;
1078  return _setSlot(MEM_UNIQUE_ID, buffer, 4);
1079 }
1080 
1086  return _getSlot(MEM_VERSION);
1087 }
1088 
1093 void AllWize::setVersion(uint8_t version) {
1094  _setSlot(MEM_VERSION, version);
1095 }
1096 
1102  return _getSlot(MEM_DEVICE);
1103 }
1104 
1109 void AllWize::setDevice(uint8_t type) {
1110  _setSlot(MEM_DEVICE, type);
1111 }
1112 
1118  return _model;
1119 }
1120 
1126  return _hw;
1127 }
1128 
1134  return _fw;
1135 }
1136 
1143 }
1144 
1150  return _module;
1151 }
1152 
1158  switch (_module) {
1159  case MODULE_MBUS4: return String("MBUS4");
1160  case MODULE_OSP: return String("OSP");
1161  case MODULE_WIZE: return String("WIZE");
1162  }
1163  return String("Unknown");
1164 }
1165 
1166 
1172 double AllWize::getFrequency(uint8_t channel) {
1173  if (channel < 7) {
1174  return 169.40625 + 0.0125 * (channel - 1);
1175  } else if (channel == 7) {
1176  return 169.41250;
1177  } else if (channel == 8) {
1178  return 169.43750;
1179  } else if (channel == 9) {
1180  return 169.46250;
1181  } else if (channel == 10) {
1182  return 169.43750;
1183  } else if (channel < 38) {
1184  return 169.48125 + 0.0125 * (channel - 11);
1185  } else if (channel < 42) {
1186  return 169.62500 + 0.0500 * (channel - 38);
1187  } else {
1188  return 0;
1189  }
1190 }
1191 
1197 uint16_t AllWize::getDataRateSpeed(uint8_t dr) {
1198  if (dr == DATARATE_6400bps_OSP) dr = DATARATE_6400bps;
1199  if ((0 < dr) && (dr < 5)) {
1200  return DATARATES[dr-1];
1201  }
1202  return 0;
1203 }
1204 
1205 // -----------------------------------------------------------------------------
1206 // Protected
1207 // -----------------------------------------------------------------------------
1208 
1215 bool AllWize::_setConfig(bool value) {
1216  if (value != _config) {
1217  _flush();
1218  if (value) {
1219  if (GPIO_NONE != _config_gpio) {
1220  digitalWrite(_config_gpio, HIGH);
1221  _config = true;
1222  } else {
1224  }
1225  } else {
1226  if (GPIO_NONE != _config_gpio) {
1227  digitalWrite(_config_gpio, LOW);
1228  }
1230  _niceDelay(5);
1231  _config = false;
1232  }
1233  }
1234  return _config;
1235 }
1236 
1244 int8_t AllWize::_sendCommand(uint8_t command, uint8_t *data, uint8_t len) {
1245  int8_t response = -1;
1246  if (!_setConfig(true)) return response;
1247  if (_sendAndReceive(command) != -1) {
1248  response = _sendAndReceive(data, len);
1249  }
1250  _setConfig(false);
1251  return response;
1252 }
1253 
1261 int8_t AllWize::_sendCommand(uint8_t command, uint8_t data) {
1262  int8_t response = -1;
1263  if (!_setConfig(true)) return response;
1264  if (_sendAndReceive(command) != -1) {
1265  response = _sendAndReceive(data);
1266  }
1267  _setConfig(false);
1268  return response;
1269 }
1270 
1277 int8_t AllWize::_sendCommand(uint8_t command) {
1278  int8_t response = -1;
1279  if (!_setConfig(true)) return response;
1280  response = _sendAndReceive(command);
1281  _setConfig(false);
1282  return response;
1283 }
1284 
1285 // ------------------------------------------------------------------------------------------------
1286 
1293 bool AllWize::_cacheMemory(uint8_t * buffer) {
1294 
1295  // Read memory
1296  _setConfig(true);
1298  bool ret = (256 == _readBytes((char *) buffer, 256));
1299  _setConfig(false);
1300  return ret;
1301 
1302 }
1303 
1309 
1310  #if not USE_MEMORY_CACHE
1311  uint8_t _memory[0x100] = {0xFF};
1312  bool _ready = _cacheMemory(_memory);
1313  if (!_ready) return;
1314  #endif
1315 
1316  // Look for the part_number
1317  bool found = false;
1318  uint8_t index = 0;
1319  uint8_t len = strlen(MODULE_SIGNATURE);
1320  for (index=0; index<0xFF-32; index++) {
1321  if (memcmp(&_memory[index], (uint8_t *) MODULE_SIGNATURE, len) == 0) {
1322  found = true;
1323  break;
1324  }
1325  }
1326 
1327  // Parse signature
1328  if (found) {
1329 
1330  String part_number = String((char *) &_memory[index]);
1331  part_number.substring(0, 31);
1332  part_number.trim();
1333 
1334  uint8_t end = part_number.indexOf(",");
1335  _model = part_number.substring(0, end);
1336  uint8_t start = end + 1;
1337  end = part_number.indexOf(",", start);
1338  _hw = part_number.substring(start, end);
1339  _fw = part_number.substring(end + 1, 32);
1340  _fw.trim();
1341 
1342  }
1343 
1344 }
1345 
1354 uint8_t AllWize::_getMemory(uint8_t address, uint8_t *buffer, uint8_t len) {
1355  #if USE_MEMORY_CACHE
1356  if (!_ready) return 0;
1357  memcpy(buffer, &_memory[address], len);
1358  return len;
1359  #else
1360  uint8_t count = 0;
1361  if (_setConfig(true)) {
1362  for (uint8_t i=0; i<len; i++) {
1363  if (_sendAndReceive(CMD_READ_MEMORY) == -1) break;
1364  if (_sendAndReceive(address + i) != 1) break;
1365  count++;
1366  buffer[i] = _buffer[0];
1367  }
1368  _setConfig(false);
1369  }
1370  return count;
1371  #endif
1372 }
1373 
1380 uint8_t AllWize::_getMemory(uint8_t address) {
1381  #if USE_MEMORY_CACHE
1382  if (!_ready) return 0;
1383  return _memory[address];
1384  #else
1385  uint8_t response = _sendCommand(CMD_READ_MEMORY, address);
1386  if (response > 0) return _buffer[0];
1387  return 0;
1388  #endif
1389 }
1390 
1398 bool AllWize::_setMemory(uint8_t address, uint8_t data) {
1399 
1400  // Check cached data
1401  #if USE_MEMORY_CACHE
1402  if (_memory[address] == data) return true;
1403  #endif
1404 
1405  // Build query buffer
1406  uint8_t buffer[3] = {address, data, (uint8_t)CMD_EXIT_MEMORY};
1407 
1408  // Execute command
1409  bool ret = (_sendCommand(CMD_WRITE_MEMORY, buffer, 3) != -1);
1410 
1411  // Update cached memory
1412  #if USE_MEMORY_CACHE
1413  if (ret) _memory[address] = data;
1414  #endif
1415 
1416  return ret;
1417 
1418 }
1419 
1428 bool AllWize::_setMemory(uint8_t address, uint8_t *data, uint8_t len) {
1429 
1430  // Check cached data
1431  #if USE_MEMORY_CACHE
1432  if (memcmp(&_memory[address], data, len) == 0) return true;
1433  #endif
1434 
1435  // Build query buffer
1436  uint8_t buffer[len * 2 + 1];
1437  for (uint8_t i = 0; i < len; i++) {
1438  buffer[i * 2] = address + i;
1439  buffer[i * 2 + 1] = data[i];
1440  }
1441  buffer[len * 2] = CMD_EXIT_MEMORY;
1442 
1443  // Execute command
1444  bool ret = (_sendCommand(CMD_WRITE_MEMORY, buffer, len * 2 + 1) != -1);
1445 
1446  // Update cached memory
1447  #if USE_MEMORY_CACHE
1448  if (ret) memcpy(&_memory[address], data, len);
1449  #endif
1450 
1451  return ret;
1452 
1453 }
1454 
1455 // ------------------------------------------------------------------------------------------------
1456 
1463 uint8_t AllWize::_getAddress(uint8_t slot) {
1464  if ((slot >= MEM_MAX_SLOTS) || (MODULE_UNKNOWN == _module)) {
1465  return 0xFF;
1466  }
1467  return MEM_ADDRESS[_module-1][slot];
1468 }
1469 
1478 bool AllWize::_setSlot(uint8_t slot, uint8_t *data, uint8_t len) {
1479  uint8_t address = _getAddress(slot);
1480  if (0xFF == address) return false;
1481  return _setMemory(address, data, len);
1482 }
1483 
1491 bool AllWize::_setSlot(uint8_t slot, uint8_t data) {
1492  uint8_t address = _getAddress(slot);
1493  if (0xFF == address) return false;
1494  return _setMemory(address, data);
1495 }
1496 
1505 uint8_t AllWize::_getSlot(uint8_t slot, uint8_t *buffer, uint8_t len) {
1506  uint8_t address = _getAddress(slot);
1507  if (0xFF == address) return 0;
1508  return _getMemory(address, buffer, len);
1509 }
1510 
1517 uint8_t AllWize::_getSlot(uint8_t slot) {
1518  uint8_t address = _getAddress(slot);
1519  if (0xFF == address) return 0;
1520  return _getMemory(address);
1521 }
1522 
1530 String AllWize::_getSlotAsHexString(uint8_t slot, uint8_t len) {
1531  uint8_t bin[len];
1532  char hex[2 * len + 1];
1533  hex[0] = 0;
1534  if (len == _getSlot(slot, bin, len)) {
1535  _bin2hex(bin, hex, len);
1536  }
1537  return String(hex);
1538 }
1539 
1547 String AllWize::_getSlotAsString(uint8_t slot, uint8_t len) {
1548  uint8_t bin[len];
1549  char hex[len + 1];
1550  hex[0] = 0;
1551  if (len == _getSlot(slot, bin, len)) {
1552  memcpy(hex, bin, len);
1553  hex[len - 1] = 0;
1554  }
1555  return String(hex);
1556 }
1557 
1558 // ------------------------------------------------------------------------------------------------
1559 
1587 
1588  #if defined(ALLWIZE_DEBUG_PORT)
1589  {
1590  char ch[4];
1591  ALLWIZE_DEBUG_PRINT("recv:");
1592  for (uint8_t i = 0; i < _pointer; i++) {
1593  snprintf(ch, sizeof(ch), " %02X", _buffer[i]);
1594  ALLWIZE_DEBUG_PRINT(ch);
1595  }
1597  }
1598  #endif
1599 
1600  // Get current values
1601  uint8_t mbus_mode = getMode();
1602  uint8_t data_interface = getDataInterface();
1603  bool has_start = (data_interface & 0x04) == 0x04;
1604  bool has_header = (mbus_mode != MBUS_MODE_OSP) & ((data_interface & 0x01) == 0x00);
1605  bool has_rssi = getAppendRSSI();
1606  bool has_crc = (data_interface & 0x08) == 0x08;
1607  uint8_t bytes_not_in_len = has_start ? 3 : 1;
1608  uint8_t bytes_not_in_app = (has_header ? 9 : 0) + 1 + (has_rssi ? 1 : 0) + (has_crc ? 2 : 0);
1609  uint8_t bytes_not_in_msg = 0;
1610 
1611  // This variable will contain the pointer to the current reading position
1612  uint8_t in = 0;
1613 
1614  // Start byte
1615  if (has_start) {
1616  if (START_BYTE != _buffer[in++]) return false;
1617  };
1618 
1619  // Get and check buffer length
1620  uint8_t len = _buffer[in++];
1621  if (_pointer != len + bytes_not_in_len) return false;
1622 
1623  if (has_header) {
1624 
1625  // C-field
1626  _message.c = _buffer[in++];
1627 
1628  // Manufacturer
1629  uint16_t man = (_buffer[in + 1] << 8) + _buffer[in];
1630  _message.man[0] = ((man >> 10) & 0x001F) + 64;
1631  _message.man[1] = ((man >> 5) & 0x001F) + 64;
1632  _message.man[2] = ((man >> 0) & 0x001F) + 64;
1633  _message.man[3] = 0;
1634  in += 2;
1635 
1636  // Address
1637  _message.address[0] = _buffer[in + 3];
1638  _message.address[1] = _buffer[in + 2];
1639  _message.address[2] = _buffer[in + 1];
1640  _message.address[3] = _buffer[in + 0];
1641  in += 4;
1642 
1643  // Version
1644  _message.version = _buffer[in++];
1645 
1646  // Type
1647  _message.type = _buffer[in++];
1648 
1649  } else {
1650  _message.c = 0xFF;
1651  _message.type = 0;
1652  _message.version = 0;
1653  _message.man[0] = 0;
1654  memset(_message.address, 0, 6);
1655  }
1656 
1657  // Control information
1658  _message.ci = _buffer[in++];
1659 
1660  // Wize transport layer
1661  if (MODULE_WIZE == _module) {
1662 
1663  if (CI_WIZE == _message.ci) {
1664 
1665  bytes_not_in_app += 5;
1666 
1667  // Wize control
1668  _message.wize_control = _buffer[in++];
1669 
1670  // Wize operator ID
1672 
1673  // Wize counter
1674  _message.wize_counter = (_buffer[in + 1] << 8) + _buffer[in];
1675  in += 2;
1676 
1677  // Wize application
1679 
1680  } else {
1681 
1682  // Undocumented hack
1683  bytes_not_in_msg = 8;
1684 
1685  }
1686 
1687  }
1688 
1689  // Application data
1690  _message.len = len - bytes_not_in_app - bytes_not_in_msg;
1691  memcpy(_message.data, &_buffer[in], _message.len);
1692  _message.data[_message.len] = 0;
1693  in += (_message.len + bytes_not_in_msg);
1694 
1695  // RSSI
1696  if (has_rssi) {
1697  _message.rssi = _buffer[in++];
1698  } else {
1699  _message.rssi = 0xFF;
1700  }
1701 
1702  // CRC
1703  if (has_crc) {
1704  in += 2;
1705  }
1706 
1707  // Stop byte
1708  if (has_start) {
1709  if (STOP_BYTE != _buffer[in]) return false;
1710  }
1711 
1712  return true;
1713 
1714 }
1715 
1716 // -----------------------------------------------------------------------------
1717 
1723 
1724  // Flush TX line
1725  _stream->flush();
1726 
1727  // Flush RX line
1728  while (_stream->available()) _stream->read();
1729 
1730 }
1731 
1738 uint8_t AllWize::_send(uint8_t ch) {
1739  #if defined(ALLWIZE_DEBUG_PORT)
1740  {
1741  char buffer[10];
1742  snprintf(buffer, sizeof(buffer), "w %02X '%c'", ch, (32 <= ch && ch <= 126) ? ch : 32);
1743  ALLWIZE_DEBUG_PRINTLN(buffer);
1744  }
1745  #endif
1746  return _stream->write(ch);
1747 }
1748 
1756 uint8_t AllWize::_send(uint8_t *buffer, uint8_t len) {
1757  uint8_t n = 0;
1758  for (uint8_t i = 0; i < len; i++) {
1759  if (_send(buffer[i])) n++;
1760  }
1761  return n;
1762 }
1763 
1771 }
1772 
1780 int8_t AllWize::_sendAndReceive(uint8_t *buffer, uint8_t len) {
1781  if (_send(buffer, len) != len) return -1;
1782  return _receive();
1783 }
1784 
1791 int8_t AllWize::_sendAndReceive(uint8_t ch) {
1792  if (_send(ch) != 1) return -1;
1793  return _receive();
1794 }
1795 
1802 
1803  uint32_t _start = millis();
1804  int ch = -1;
1805  while (millis() - _start < _timeout) {
1806  #if defined(ARDUINO_ARCH_ESP8266)
1807  yield();
1808  #endif
1809  ch = _stream->read();
1810  if (ch >= 0) break;
1811  };
1812 
1813  #if defined(ALLWIZE_DEBUG_PORT)
1814  /*
1815  {
1816  if (ch < 0) {
1817  ALLWIZE_DEBUG_PRINTLN("r TIMEOUT");
1818  } else {
1819  char buffer[10];
1820  snprintf(buffer, sizeof(buffer), "r %02X '%c'", ch, (32 <= ch && ch <= 126) ? ch : 32);
1821  ALLWIZE_DEBUG_PRINTLN(buffer);
1822  }
1823  }
1824  */
1825  #endif
1826 
1827  return ch;
1828 }
1829 
1837 int AllWize::_readBytes(char * data, uint16_t len) {
1838 
1839  if (len < 1) return 0;
1840 
1841  uint16_t index = 0;
1842  while (index < len) {
1843  int ch = _timedRead();
1844  if (ch < 0) return -1;
1845  *data++ = (char)ch;
1846  index++;
1847  }
1848 
1849  return index;
1850 
1851 }
1852 
1861 int AllWize::_readBytesUntil(char terminator, char *data, uint16_t len) {
1862 
1863  if (len < 1) return 0;
1864 
1865  uint16_t index = 0;
1866  while (index < len) {
1867  int ch = _timedRead();
1868  if (ch < 0) return -1;
1869  if (ch == terminator) break;
1870  *data++ = (char) ch;
1871  index++;
1872  }
1873 
1874  return index;
1875 
1876 }
1877 
1885 void AllWize::_hex2bin(char *hex, uint8_t *bin, uint8_t len) {
1886  for (uint8_t i = 0; i < len; i += 2) {
1887  bin[i / 2] = ((hex[i] - '0') * 16 + (hex[i + 1] - '0')) & 0xFF;
1888  }
1889 }
1890 
1898 void AllWize::_bin2hex(uint8_t *bin, char *hex, uint8_t len) {
1899  for (uint8_t i = 0; i < len; i++) {
1900  sprintf(&hex[i * 2], "%02X", bin[i]);
1901  }
1902 }
1903 
1909 void AllWize::_niceDelay(uint32_t ms) {
1910  uint32_t start = millis();
1911  while (millis() - start < ms) delay(1);
1912 }
AllWize::getMode
uint8_t getMode()
Gets the MBus mode stored in non-volatile memory.
Definition: AllWize.cpp:703
AllWize::_setMemory
bool _setMemory(uint8_t address, uint8_t data)
Sets non-volatile memory contents starting from given address.
Definition: AllWize.cpp:1398
MEM_RSSI_MODE
@ MEM_RSSI_MODE
Definition: RC1701HP.h:79
AllWize::getVersion
uint8_t getVersion()
Returns the device version from non-volatile memory.
Definition: AllWize.cpp:1085
allwize_message_t::wize_network_id
uint8_t wize_network_id
Definition: AllWize.h:65
AllWize::setDevice
void setDevice(uint8_t type)
Sets the device type.
Definition: AllWize.cpp:1109
AllWize::setWizeApplication
void setWizeApplication(uint8_t wize_application)
Sets the wize applicaton field in the transpoprt layer.
Definition: AllWize.cpp:557
AllWize::slave
void slave()
Sets the module in slave mode.
Definition: AllWize.cpp:249
AllWize::waitForReady
bool waitForReady(uint32_t timeout=DEFAULT_TIMEOUT)
Waits for timeout millis for the module to be ready.
Definition: AllWize.cpp:294
AllWize::setAccessNumber
void setAccessNumber(uint8_t value)
Sets new/specific access number.
Definition: AllWize.cpp:960
AllWize::_sw_serial
SoftwareSerial * _sw_serial
Definition: AllWize.h:254
MEM_ADDRESS
static const uint8_t MEM_ADDRESS[MODULE_MAX-1][MEM_MAX_SLOTS]
Definition: RC1701HP.h:115
AllWize::setMAC2CheckOnlyFlag
void setMAC2CheckOnlyFlag(uint8_t flag)
Sets the MAC 2 Check Only flag setting.
Definition: AllWize.cpp:879
AllWize::dump
void dump(Stream &debug)
Dumps the current memory configuration to the given stream.
Definition: AllWize.cpp:307
AllWize::_setSlot
bool _setSlot(uint8_t slot, uint8_t data)
Sets non-volatile memory contents starting from given address.
Definition: AllWize.cpp:1491
MEM_PREAMBLE_LENGTH
@ MEM_PREAMBLE_LENGTH
Definition: RC1701HP.h:81
AllWize::getSerialNumber
String getSerialNumber()
Returns the module serial number.
Definition: AllWize.cpp:1141
allwize_message_t::type
uint8_t type
Definition: AllWize.h:58
AllWize::getDefaultKey
void getDefaultKey(uint8_t *key)
Gets the default encryption key.
Definition: AllWize.cpp:952
AllWize::ready
bool ready()
Test whether the radio module is ready or not.
Definition: AllWize.cpp:285
AllWize::setSleepMode
void setSleepMode(uint8_t mode)
Sets the sleep mode.
Definition: AllWize.cpp:711
AllWize::getNetworkRole
uint8_t getNetworkRole()
Gets the current network role.
Definition: AllWize.cpp:793
AllWize::getEncryptFlag
uint8_t getEncryptFlag()
Gets the encrypt flag setting.
Definition: AllWize.cpp:907
CMD_MBUS_MODE
#define CMD_MBUS_MODE
Definition: RC1701HP.h:44
AllWize::setDataInterface
void setDataInterface(uint8_t value)
Sets the data interface for receiving packets.
Definition: AllWize.cpp:818
AllWize::_wize_network_id
uint16_t _wize_network_id
Definition: AllWize.h:282
MEM_DEFAULT_KEY
@ MEM_DEFAULT_KEY
Definition: RC1701HP.h:104
NETWORK_ROLE_REPEATER
#define NETWORK_ROLE_REPEATER
Definition: RC1701HP.h:236
AllWize::_model
String _model
Definition: AllWize.h:276
AllWize::_getSlotAsString
String _getSlotAsString(uint8_t slot, uint8_t len)
Returns the contents of the memory from a certain address as a String object.
Definition: AllWize.cpp:1547
AllWize::_resetSerial
void _resetSerial()
Resets the serial object.
Definition: AllWize.cpp:120
AllWize::_reset_gpio
uint8_t _reset_gpio
Definition: AllWize.h:257
MEM_UART_BAUD_RATE
@ MEM_UART_BAUD_RATE
Definition: RC1701HP.h:91
allwize_message_t::wize_counter
uint16_t wize_counter
Definition: AllWize.h:66
MEM_LED_CONTROL
@ MEM_LED_CONTROL
Definition: RC1701HP.h:97
AllWize::_append_rssi
bool _append_rssi
Definition: AllWize.h:266
AllWize::sleep
void sleep()
Sets the radio module in sleep mode.
Definition: AllWize.cpp:268
CMD_AWAKE
#define CMD_AWAKE
Definition: RC1701HP.h:34
MEM_MAX_SLOTS
@ MEM_MAX_SLOTS
Definition: RC1701HP.h:110
AllWize::setPreamble
void setPreamble(uint8_t preamble)
Sets the preamble length frame format.
Definition: AllWize.cpp:748
CMD_IDLE_ENABLE_RF
#define CMD_IDLE_ENABLE_RF
Definition: RC1701HP.h:32
AllWize::_readBytes
int _readBytes(char *buffer, uint16_t len)
Reads the stream buffer up to a number of bytes.
Definition: AllWize.cpp:1837
AllWize::_bin2hex
void _bin2hex(uint8_t *bin, char *hex, uint8_t len)
Converts a binary buffer to an hex c-string.
Definition: AllWize.cpp:1898
AllWize::getDecryptFlag
uint8_t getDecryptFlag()
Gets the decrypt flag setting.
Definition: AllWize.cpp:923
AllWize::_hw_serial
HardwareSerial * _hw_serial
Definition: AllWize.h:248
AllWize::getRequiredHardwareVersion
String getRequiredHardwareVersion()
Returns the minimum required hardware version to run the current firmware.
Definition: AllWize.cpp:1125
AllWize::getPreamble
uint8_t getPreamble()
Gets the preamble length frame format.
Definition: AllWize.cpp:758
AllWize::setWizeNetworkId
void setWizeNetworkId(uint8_t wize_network_id)
Sets the wize network ID field in the transpoprt layer.
Definition: AllWize.cpp:549
MEM_CHANNEL_RX
@ MEM_CHANNEL_RX
Definition: RC1701HP.h:71
AllWize::_receive
int8_t _receive()
Listens to incoming data from the module until timeout or END_OF_RESPONSE.
Definition: AllWize.cpp:1769
AllWize::_sendCommand
int8_t _sendCommand(uint8_t command, uint8_t *data, uint8_t len)
Sends a command with the given data.
Definition: AllWize.cpp:1244
AllWize::setMode
void setMode(uint8_t mode, bool persist=false)
Sets the module in one of the available MBus modes.
Definition: AllWize.cpp:681
AllWize::_send
uint8_t _send(uint8_t *buffer, uint8_t len)
Sends a binary buffer to the module UART. Returns the number of bytes actually sent.
Definition: AllWize.cpp:1756
AllWize::setControlField
void setControlField(uint8_t value, bool persist=false)
Sets the control field value.
Definition: AllWize.cpp:838
MEM_DATA_RATE
@ MEM_DATA_RATE
Definition: RC1701HP.h:74
AllWize::_hex2bin
void _hex2bin(char *hex, uint8_t *bin, uint8_t len)
Converts a hex c-string to a binary buffer.
Definition: AllWize.cpp:1885
AllWize::setLEDControl
void setLEDControl(uint8_t value)
Sets the LED control.
Definition: AllWize.cpp:801
AllWize::setCounter
void setCounter(uint16_t counter)
Sets the wize couonter field in the transpoprt layer.
Definition: AllWize.cpp:565
AllWize::_config_gpio
uint8_t _config_gpio
Definition: AllWize.h:258
BAUDRATES
static const uint32_t BAUDRATES[11]
Definition: RC1701HP.h:274
AllWize::_module
uint8_t _module
Definition: AllWize.h:268
MEM_VERSION
@ MEM_VERSION
Definition: RC1701HP.h:89
AllWize::_hw
String _hw
Definition: AllWize.h:277
allwize_message_t::rssi
uint8_t rssi
Definition: AllWize.h:63
AllWize::setPower
void setPower(uint8_t power, bool persist=false)
Sets the RF power.
Definition: AllWize.cpp:626
AllWize::_flush
void _flush()
Flushes the serial line to the module.
Definition: AllWize.cpp:1722
allwize_message_t
Definition: AllWize.h:54
MEM_ENCRYPT_FLAG
@ MEM_ENCRYPT_FLAG
Definition: RC1701HP.h:102
AllWize::setWizeOperatorId
void setWizeOperatorId(uint8_t wize_network_id)
Use AllWize::setWizeNetworkId instead.
Definition: AllWize.cpp:541
DEFAULT_MBUS_MODE
#define DEFAULT_MBUS_MODE
Definition: AllWize.h:48
AllWize::setBaudRate
void setBaudRate(uint8_t baudrate)
Sets the UART baud rate, requires reset to take effect.
Definition: AllWize.cpp:968
AllWize::getDataInterface
uint8_t getDataInterface()
Gets the data interface for receiving packets.
Definition: AllWize.cpp:829
AllWize::_readModel
void _readModel()
Searches for the module model.
Definition: AllWize.cpp:1308
AllWize::_fw
String _fw
Definition: AllWize.h:278
CMD_CONTROL_FIELD
#define CMD_CONTROL_FIELD
Definition: RC1701HP.h:43
AllWize::_tx
int8_t _tx
Definition: AllWize.h:245
MEM_DATA_INTERFACE
@ MEM_DATA_INTERFACE
Definition: RC1701HP.h:93
MODULE_OSP
@ MODULE_OSP
Definition: RC1701HP.h:15
AllWize::_timeout
uint32_t _timeout
Definition: AllWize.h:260
AllWize::getModuleType
uint8_t getModuleType()
Returns the module type.
Definition: AllWize.cpp:1149
AllWize::read
allwize_message_t read()
Returns latest received message.
Definition: AllWize.cpp:505
AllWize::_getSlotAsHexString
String _getSlotAsHexString(uint8_t slot, uint8_t len)
Returns the contents of the memory from a certain address as an HEX String.
Definition: AllWize.cpp:1530
AllWize::getInstallMode
uint8_t getInstallMode()
Gets the install modevalue stored in non-volatile memory.
Definition: AllWize.cpp:871
AllWize::setControlInformation
void setControlInformation(uint8_t ci)
Sets the control information byte.
Definition: AllWize.cpp:585
AllWize::_ready
bool _ready
Definition: AllWize.h:272
DATA_INTERFACE_START_STOP
#define DATA_INTERFACE_START_STOP
Definition: RC1701HP.h:253
DATARATE_2400bps
#define DATARATE_2400bps
Definition: RC1701HP.h:194
MEM_DECRYPT_FLAG
@ MEM_DECRYPT_FLAG
Definition: RC1701HP.h:103
AllWize::getBuffer
uint8_t * getBuffer()
Returns pointer to the last message raw data buffer Should be copied right away since any new incommi...
Definition: AllWize.cpp:514
AllWize::_data_interface
uint8_t _data_interface
Definition: AllWize.h:265
NETWORK_ROLE_SLAVE
#define NETWORK_ROLE_SLAVE
Definition: RC1701HP.h:234
MEM_UNIQUE_ID
@ MEM_UNIQUE_ID
Definition: RC1701HP.h:87
INSTALL_MODE_HOST
#define INSTALL_MODE_HOST
Definition: RC1701HP.h:224
ALLWIZE_DEBUG_PRINT
#define ALLWIZE_DEBUG_PRINT(...)
Definition: AllWize.h:83
AllWize::setDecryptFlag
void setDecryptFlag(uint8_t flag)
Sets the decrypt flag setting.
Definition: AllWize.cpp:915
HARDWARE_SERIAL_PORT
#define HARDWARE_SERIAL_PORT
Definition: AllWize.h:47
AllWize::getPartNumber
String getPartNumber()
Returns the module part number.
Definition: AllWize.cpp:1117
AllWize::_wize_control
uint8_t _wize_control
Definition: AllWize.h:281
AllWize::getFrequency
double getFrequency(uint8_t channel)
Returns the frequency for the given channel.
Definition: AllWize.cpp:1172
AllWize::master
void master()
Sets the module in master mode.
Definition: AllWize.cpp:234
C_SND_NR
#define C_SND_NR
Definition: OMS.h:14
DATARATE_6400bps_OSP
#define DATARATE_6400bps_OSP
Definition: RC1701HP.h:198
AllWize::getControlField
uint8_t getControlField()
Gets the control field value stored in non-volatile memory.
Definition: AllWize.cpp:849
AllWize::getFirmwareVersion
String getFirmwareVersion()
Returns the module firmware revision.
Definition: AllWize.cpp:1133
AllWize::_cacheMemory
bool _cacheMemory(uint8_t *buffer)
Reads and caches the module memory.
Definition: AllWize.cpp:1293
AllWize::setUID
bool setUID(uint32_t uid)
Saved the UID into the module memory.
Definition: AllWize.cpp:1072
CMD_TEMPERATURE
#define CMD_TEMPERATURE
Definition: RC1701HP.h:56
AllWize::getControlInformation
uint8_t getControlInformation()
Gets the control information byte.
Definition: AllWize.cpp:593
AllWize::softReset
void softReset()
Cleans the RX/TX line.
Definition: AllWize.cpp:197
MEM_MANUFACTURER_ID
@ MEM_MANUFACTURER_ID
Definition: RC1701HP.h:86
AllWize::_message
allwize_message_t _message
Definition: AllWize.h:287
AllWize::ack
bool ack()
Sends an ACK.
Definition: AllWize.cpp:432
CMD_WRITE_MEMORY
#define CMD_WRITE_MEMORY
Definition: RC1701HP.h:48
CMD_ENTER_CONFIG
#define CMD_ENTER_CONFIG
Definition: RC1701HP.h:28
CMD_EXIT_CONFIG
#define CMD_EXIT_CONFIG
Definition: RC1701HP.h:29
AllWize::setKey
void setKey(uint8_t reg, const uint8_t *key)
Sets the default encryption key.
Definition: AllWize.cpp:932
AllWize::_baudrate
uint32_t _baudrate
Definition: AllWize.h:261
allwize_message_t::version
uint8_t version
Definition: AllWize.h:59
AllWize::_config
bool _config
Definition: AllWize.h:259
MBUS_MODE_N1
#define MBUS_MODE_N1
Definition: RC1701HP.h:218
C_ACK
#define C_ACK
Definition: OMS.h:13
CMD_KEY_REGISTER
#define CMD_KEY_REGISTER
Definition: RC1701HP.h:46
allwize_message_t::ci
uint8_t ci
Definition: AllWize.h:56
AllWize::setWizeControl
bool setWizeControl(uint8_t wize_control)
Sets the wize control field in the transport layer.
Definition: AllWize.cpp:531
CMD_RF_POWER
#define CMD_RF_POWER
Definition: RC1701HP.h:51
AllWize::getTemperature
uint8_t getTemperature()
Returns the internal temperature of the module.
Definition: AllWize.cpp:1012
MEM_RF_POWER
@ MEM_RF_POWER
Definition: RC1701HP.h:72
MODULE_SIGNATURE
#define MODULE_SIGNATURE
Definition: RC1701HP.h:7
AllWize::getCounter
uint16_t getCounter()
Gets the current wize counter.
Definition: AllWize.cpp:573
CMD_EXIT_MEMORY
#define CMD_EXIT_MEMORY
Definition: RC1701HP.h:35
AllWize::setVersion
void setVersion(uint8_t version)
Sets the device version.
Definition: AllWize.cpp:1093
AllWize::setTimeout
void setTimeout(uint16_t ms)
Sets the buffer timeout (also used for auto sleep modes)
Definition: AllWize.cpp:766
MBUS_MODE_OSP
#define MBUS_MODE_OSP
Definition: RC1701HP.h:219
AllWize::getUID
String getUID()
Returns the Unique ID string.
Definition: AllWize.cpp:1064
AllWize::AllWize
AllWize(HardwareSerial *serial, uint8_t reset_gpio=GPIO_NONE, uint8_t config_gpio=GPIO_NONE)
AllWize object constructor.
Definition: AllWize.cpp:40
AllWize::available
bool available()
Returns true if a new message has been received and decoded This method has to be called in the main ...
Definition: AllWize.cpp:458
NETWORK_ROLE_MASTER
#define NETWORK_ROLE_MASTER
Definition: RC1701HP.h:235
AllWize::setNetworkRole
void setNetworkRole(uint8_t role)
Sets the network role.
Definition: AllWize.cpp:785
AllWize::getDevice
uint8_t getDevice()
Returns the device type from non-volatile memory.
Definition: AllWize.cpp:1101
AllWize::_getAddress
uint8_t _getAddress(uint8_t slot)
Return the physical memory address for the given slot.
Definition: AllWize.cpp:1463
AllWize::_rx
int8_t _rx
Definition: AllWize.h:244
AllWize::getMID
String getMID()
Returns the Manufacturer ID.
Definition: AllWize.cpp:1045
AllWize::getBaudRateSpeed
uint32_t getBaudRateSpeed(uint8_t value)
Gets the UART baud rate speed in bps.
Definition: AllWize.cpp:988
allwize_message_t::wize_control
uint8_t wize_control
Definition: AllWize.h:64
CMD_CHANNEL
#define CMD_CHANNEL
Definition: RC1701HP.h:40
MEM_SERIAL_NUMBER
@ MEM_SERIAL_NUMBER
Definition: RC1701HP.h:107
MODULE_WIZE
@ MODULE_WIZE
Definition: RC1701HP.h:16
CMD_TEST_MODE_0
#define CMD_TEST_MODE_0
Definition: RC1701HP.h:62
GPIO_NONE
#define GPIO_NONE
Definition: AllWize.h:44
AllWize::begin
void begin(uint8_t baudrate=MODEM_DEFAULT_BAUDRATE)
Inits the module communications.
Definition: AllWize.cpp:91
AllWize::send
bool send(uint8_t *buffer, uint8_t len)
Sends a byte array.
Definition: AllWize.cpp:366
AllWize::getLength
uint8_t getLength()
Returns the length of the last message raw data buffer.
Definition: AllWize.cpp:522
AllWize::_memory
uint8_t _memory[0x100]
Definition: AllWize.h:273
AllWize::getDataRate
uint8_t getDataRate()
Gets the data rate stored in non-volatile memory.
Definition: AllWize.cpp:672
allwize_message_t::len
uint8_t len
Definition: AllWize.h:61
AllWize::_pointer
uint8_t _pointer
Definition: AllWize.h:289
AllWize::_sendAndReceive
int8_t _sendAndReceive(uint8_t *buffer, uint8_t len)
Sends a binary buffer and waits for response. Returns the number of bytes received and stored in the ...
Definition: AllWize.cpp:1780
CI_WIZE
#define CI_WIZE
Definition: OMS.h:28
DATARATE_6400bps
#define DATARATE_6400bps
Definition: RC1701HP.h:196
allwize_message_t::wize_application
uint8_t wize_application
Definition: AllWize.h:67
AllWize::factoryReset
bool factoryReset()
Resets the module to factory settings.
Definition: AllWize.cpp:212
CMD_RSSI
#define CMD_RSSI
Definition: RC1701HP.h:54
AllWize::_ci
uint8_t _ci
Definition: AllWize.h:263
AllWize::_wize_application
uint8_t _wize_application
Definition: AllWize.h:283
AllWize::repeater
void repeater()
Sets the module in repeater mode.
Definition: AllWize.cpp:260
AllWize::getAppendRSSI
bool getAppendRSSI()
Gets the current RSSI mode value.
Definition: AllWize.cpp:740
CMD_INSTALL_MODE
#define CMD_INSTALL_MODE
Definition: RC1701HP.h:45
AllWize::_timedRead
int _timedRead()
Reads a byte from the stream with a timeout.
Definition: AllWize.cpp:1801
AllWize::_buffer
uint8_t _buffer[RX_BUFFER_SIZE]
Definition: AllWize.h:288
AllWize::_getSlot
uint8_t _getSlot(uint8_t slot)
Returns the contents of single-byte memory slot.
Definition: AllWize.cpp:1517
AllWize::_length
uint8_t _length
Definition: AllWize.h:290
AllWize::_stream
Stream * _stream
Definition: AllWize.h:247
MEM_CONTROL_FIELD
@ MEM_CONTROL_FIELD
Definition: RC1701HP.h:98
MEM_MAC_2_CHECK_ONLY_FLAG
@ MEM_MAC_2_CHECK_ONLY_FLAG
Definition: RC1701HP.h:108
AllWize::reset
bool reset()
Resets the radio module.
Definition: AllWize.cpp:162
AllWize::_setConfig
bool _setConfig(bool value)
Sets or unsets config mode.
Definition: AllWize.cpp:1215
STOP_BYTE
#define STOP_BYTE
Definition: RC1701HP.h:25
MEM_SLEEP_MODE
@ MEM_SLEEP_MODE
Definition: RC1701HP.h:78
AllWize::setEncryptFlag
void setEncryptFlag(uint8_t flag)
Sets the encrypt flag setting.
Definition: AllWize.cpp:897
AllWize::getMAC2CheckOnlyFlag
uint8_t getMAC2CheckOnlyFlag()
Gets the MAC 2 Check Only flag setting.
Definition: AllWize.cpp:889
AllWize::getDataRateSpeed
uint16_t getDataRateSpeed(uint8_t dr)
Returns the speed for te given datarate.
Definition: AllWize.cpp:1197
AllWize::getLEDControl
uint8_t getLEDControl()
Gets the current LED control.
Definition: AllWize.cpp:810
allwize_message_t::address
uint8_t address[4]
Definition: AllWize.h:60
MBUS_MODE_N2
#define MBUS_MODE_N2
Definition: RC1701HP.h:217
MEM_DATA_RATE_RX
@ MEM_DATA_RATE_RX
Definition: RC1701HP.h:75
RX_BUFFER_SIZE
#define RX_BUFFER_SIZE
Definition: AllWize.h:45
allwize_message_t::data
uint8_t data[RX_BUFFER_SIZE]
Definition: AllWize.h:62
AllWize::_niceDelay
void _niceDelay(uint32_t ms)
Does a non-blocking delay.
Definition: AllWize.cpp:1909
DATARATES
static const uint32_t DATARATES[4]
Definition: RC1701HP.h:200
allwize_message_t::man
char man[4]
Definition: AllWize.h:57
AllWize.h
AllWize::_counter
uint16_t _counter
Definition: AllWize.h:284
CMD_SLEEP
#define CMD_SLEEP
Definition: RC1701HP.h:60
AllWize::_mbus_mode
uint8_t _mbus_mode
Definition: AllWize.h:264
PREAMBLE_FORMAT_B
#define PREAMBLE_FORMAT_B
Definition: RC1701HP.h:259
allwize_message_t::c
uint8_t c
Definition: AllWize.h:55
MEM_NETWORK_ROLE
@ MEM_NETWORK_ROLE
Definition: RC1701HP.h:84
MEM_INSTALL_MODE
@ MEM_INSTALL_MODE
Definition: RC1701HP.h:101
CMD_ACCESS_NUMBER
#define CMD_ACCESS_NUMBER
Definition: RC1701HP.h:49
START_BYTE
#define START_BYTE
Definition: RC1701HP.h:24
AllWize::_init
void _init()
Definition: AllWize.cpp:75
CMD_VOLTAGE
#define CMD_VOLTAGE
Definition: RC1701HP.h:57
AllWize::getVoltage
uint16_t getVoltage()
Returns the internal voltage of the module.
Definition: AllWize.cpp:1029
AllWize::getModuleTypeName
String getModuleTypeName()
Returns the module type.
Definition: AllWize.cpp:1157
AllWize::setMID
bool setMID(uint16_t mid)
Sets the Manufacturer ID.
Definition: AllWize.cpp:1053
MEM_CONFIG_INTERFACE
@ MEM_CONFIG_INTERFACE
Definition: RC1701HP.h:95
MEM_CHANNEL
@ MEM_CHANNEL
Definition: RC1701HP.h:70
CMD_IDLE_DISABLE_RF
#define CMD_IDLE_DISABLE_RF
Definition: RC1701HP.h:33
AllWize::getRSSI
float getRSSI()
Returns the RSSI of the last valid packet received TODO: values do not seem right and are not the sam...
Definition: AllWize.cpp:1002
MEM_TIMEOUT
@ MEM_TIMEOUT
Definition: RC1701HP.h:83
AllWize::_decode
bool _decode()
Decodes the current RX buffer contents.
Definition: AllWize.cpp:1586
SLEEP_MODE_DISABLE
#define SLEEP_MODE_DISABLE
Definition: RC1701HP.h:227
AllWize::_readBytesUntil
int _readBytesUntil(char terminator, char *buffer, uint16_t len)
Reads the stream buffer up to a certain char or times out.
Definition: AllWize.cpp:1861
AllWize::_getMemory
uint8_t _getMemory(uint8_t address)
Returns the contents of memory address.
Definition: AllWize.cpp:1380
PREAMBLE_FORMAT_A
#define PREAMBLE_FORMAT_A
Definition: RC1701HP.h:258
POWER_20dBm
#define POWER_20dBm
Definition: RC1701HP.h:205
AllWize::wakeup
void wakeup()
Wakes up the radio from sleep mode.
Definition: AllWize.cpp:276
AllWize::getBaudRate
uint8_t getBaudRate()
Gets the UART baud rate.
Definition: AllWize.cpp:979
CMD_READ_MEMORY
#define CMD_READ_MEMORY
Definition: RC1701HP.h:59
AllWize::setDefaultKey
void setDefaultKey(const uint8_t *key)
Sets the default encryption key.
Definition: AllWize.cpp:944
MODULE_UNKNOWN
@ MODULE_UNKNOWN
Definition: RC1701HP.h:12
ALLWIZE_DEBUG_PRINTLN
#define ALLWIZE_DEBUG_PRINTLN(...)
Definition: AllWize.h:84
MODULE_MBUS4
@ MODULE_MBUS4
Definition: RC1701HP.h:14
AllWize::_access_number
uint8_t _access_number
Definition: AllWize.h:267
AllWize::getTimeout
uint16_t getTimeout()
Gets the current buffer timeout (also used for auto sleep modes)
Definition: AllWize.cpp:776
AllWize::setChannel
void setChannel(uint8_t channel, bool persist=false)
Sets the communications channel (for MBUS_MODE_R2 only)
Definition: AllWize.cpp:602
MEM_DEVICE
@ MEM_DEVICE
Definition: RC1701HP.h:90
AllWize::setAppendRSSI
void setAppendRSSI(bool value)
Sets the RSSI mode value.
Definition: AllWize.cpp:727
AllWize::getChannel
uint8_t getChannel()
Gets the channel stored in non-volatile memory.
Definition: AllWize.cpp:617
AllWize::getSleepMode
uint8_t getSleepMode()
Gets the sleep mode stored in non-volatile memory.
Definition: AllWize.cpp:719
AllWize::getPower
uint8_t getPower()
Gets the RF power stored in non-volatile memory.
Definition: AllWize.cpp:639
AllWize::setInstallMode
void setInstallMode(uint8_t mode, bool persist=false)
Sets the module in one of the available operations modes.
Definition: AllWize.cpp:858
MEM_MBUS_MODE
@ MEM_MBUS_MODE
Definition: RC1701HP.h:77
AllWize::enableRX
bool enableRX(bool enable)
Enables or disables RF recever.
Definition: AllWize.cpp:443
AllWize::setDataRate
void setDataRate(uint8_t dr)
Sets the data rate.
Definition: AllWize.cpp:647
END_OF_RESPONSE
#define END_OF_RESPONSE
Definition: RC1701HP.h:23