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) {
44 #if not defined(ARDUINO_ARCH_SAMD) && not defined(ARDUINO_ARCH_ESP32) 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) {
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) 67 #elif defined(ARDUINO_ARCH_ESP32) 84 randomSeed(analogRead(0));
100 if (part_number.equals(
"RC1701HP-MBUS4")) {
102 }
else if (part_number.equals(
"RC1701HP-OSP")) {
104 }
else if (part_number.equals(
"RC1701HP-WIZE")) {
125 #if defined(ARDUINO_ARCH_ESP32) 126 if ((
_rx != -1) && (
_tx != -1)) {
127 pinMode(
_rx, FUNCTION_4);
128 pinMode(
_tx, FUNCTION_4);
139 #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_SAMD) 295 uint32_t start = millis();
296 while (millis() - start < timeout) {
297 if (
ready())
return true;
309 #if not USE_MEMORY_CACHE 310 uint8_t
_memory[0x100] = {0xFF};
315 debug.println(
"Error doing memory dump...");
320 char ascii[17] = {0};
326 for (address = 0; address <= 0x0F; address++) {
327 snprintf(ch,
sizeof(ch),
"%02X ", address);
331 debug.print(
"------------------------------------------------------");
336 if ((address % 16) == 0) {
339 snprintf(ch,
sizeof(ch),
"\n0x%02X: ", address);
343 ascii[address % 16] = (char)
_memory[address];
345 ascii[address % 16] =
' ';
347 snprintf(ch,
sizeof(ch),
"%02X ", (uint8_t)
_memory[address]);
350 if (0xFF == address)
break;
375 if (0 == len)
return (1 ==
_send(0xFE));
381 uint8_t message_len = len + 1;
383 if (send_wize_transport_layer) message_len += 6;
386 if (message_len > 0xF6)
return false;
389 if (1 !=
_send(message_len))
return false;
395 if (send_wize_transport_layer) {
405 if (len !=
_send(buffer, len))
return false;
425 return send((uint8_t *)buffer, strlen(buffer));
460 bool response =
false;
462 static uint32_t when = millis();
468 #if defined(ALLWIZE_DEBUG_PORT) 471 snprintf(buffer,
sizeof(buffer),
"r %02X '%c'", ch, (32 <= ch && ch <= 126) ? ch : 32);
479 #if defined(ARDUINO_ARCH_ESP8266) 486 if ((
_pointer > 0) && (millis() - when > 100)) {
514 if (wize_control > 14)
return false;
577 if (channel > 41)
return;
601 if (0 < power && power < 6) {
741 if (ms > 4080)
return;
742 uint8_t timeout = (ms / 16) - 1;
752 return 16 * (uint16_t) (timeout + 1);
776 if (value > 3)
return;
854 if (0 == flag || 1 == flag || 3 == flag) {
889 if (reg > 128)
return;
892 memcpy(&data[1], key, 16);
925 if ((0 < value) & (value < 12)) {
945 if ((0 < value) & (value < 12)) {
960 if (response > 0)
return -0.5 * (float)
_buffer[0];
1011 buffer[0] = (mid >> 8) & 0xFF;
1012 buffer[1] = (mid >> 0) & 0xFF;
1030 buffer[0] = (uid >> 24) & 0xFF;
1031 buffer[1] = (uid >> 16) & 0xFF;
1032 buffer[2] = (uid >> 8) & 0xFF;
1033 buffer[3] = (uid >> 0) & 0xFF;
1119 return String(
"Unknown");
1130 return 169.40625 + 0.0125 * (channel - 1);
1131 }
else if (channel == 7) {
1133 }
else if (channel == 8) {
1135 }
else if (channel == 9) {
1137 }
else if (channel == 10) {
1139 }
else if (channel < 38) {
1140 return 169.48125 + 0.0125 * (channel - 11);
1141 }
else if (channel < 42) {
1142 return 169.62500 + 0.0500 * (channel - 38);
1155 if ((0 < dr) && (dr < 5)) {
1201 int8_t response = -1;
1218 int8_t response = -1;
1234 int8_t response = -1;
1254 bool ret = (256 ==
_readBytes((
char *) buffer, 256));
1266 #if not USE_MEMORY_CACHE 1267 uint8_t
_memory[0x100] = {0xFF};
1276 for (index=0; index<0xFF-32; index++) {
1286 String part_number = String((
char *) &
_memory[index]);
1287 part_number.substring(0, 31);
1290 uint8_t end = part_number.indexOf(
",");
1291 _model = part_number.substring(0, end);
1292 uint8_t start = end + 1;
1293 end = part_number.indexOf(
",", start);
1294 _hw = part_number.substring(start, end);
1295 _fw = part_number.substring(end + 1, 32);
1311 #if USE_MEMORY_CACHE 1313 memcpy(buffer, &
_memory[address], len);
1318 for (uint8_t i=0; i<len; i++) {
1337 #if USE_MEMORY_CACHE 1342 if (response > 0)
return _buffer[0];
1357 #if USE_MEMORY_CACHE 1358 if (
_memory[address] == data)
return true;
1368 #if USE_MEMORY_CACHE 1369 if (ret)
_memory[address] = data;
1387 #if USE_MEMORY_CACHE 1388 if (memcmp(&
_memory[address], data, len) == 0)
return true;
1392 uint8_t buffer[len * 2 + 1];
1393 for (uint8_t i = 0; i < len; i++) {
1394 buffer[i * 2] = address + i;
1395 buffer[i * 2 + 1] = data[i];
1403 #if USE_MEMORY_CACHE 1404 if (ret) memcpy(&
_memory[address], data, len);
1436 if (0xFF == address)
return false;
1449 if (0xFF == address)
return false;
1463 if (0xFF == address)
return 0;
1475 if (0xFF == address)
return 0;
1488 char hex[2 * len + 1];
1490 if (len ==
_getSlot(slot, bin, len)) {
1507 if (len ==
_getSlot(slot, bin, len)) {
1508 memcpy(hex, bin, len);
1544 #if defined(ALLWIZE_DEBUG_PORT) 1548 for (uint8_t i = 0; i <
_pointer; i++) {
1549 snprintf(ch,
sizeof(ch),
" %02X",
_buffer[i]);
1561 uint8_t mbus_mode =
getMode();
1563 bool has_start = (data_interface & 0x04) == 0x04;
1564 bool has_header = (mbus_mode !=
MBUS_MODE_OSP) & ((data_interface & 0x01) == 0x00);
1566 bool has_crc = (data_interface & 0x08) == 0x08;
1567 uint8_t bytes_not_in_len = has_start ? 3 : 1;
1568 uint8_t bytes_not_in_app = (has_header ? 9 : 0) + 1 + (has_rssi ? 1 : 0) + (has_crc ? 2 : 0);
1580 uint8_t len = _local[in];
1581 if (
_pointer != len + bytes_not_in_len)
return false;
1591 uint16_t man = (_local[in + 1] << 8) + _local[in];
1627 bytes_not_in_app += 6;
1668 if (
STOP_BYTE != _local[in])
return false;
1698 #if defined(ALLWIZE_DEBUG_PORT) 1701 snprintf(buffer,
sizeof(buffer),
"w %02X '%c'", ch, (32 <= ch && ch <= 126) ? ch : 32);
1717 for (uint8_t i = 0; i < len; i++) {
1718 if (
_send(buffer[i])) n++;
1740 if (
_send(buffer, len) != len)
return -1;
1751 if (
_send(ch) != 1)
return -1;
1762 uint32_t _start = millis();
1764 while (millis() - _start <
_timeout) {
1765 #if defined(ARDUINO_ARCH_ESP8266) 1772 #if defined(ALLWIZE_DEBUG_PORT) 1798 if (len < 1)
return 0;
1801 while (index < len) {
1803 if (ch < 0)
return -1;
1822 if (len < 1)
return 0;
1825 while (index < len) {
1827 if (ch < 0)
return -1;
1828 if (ch == terminator)
break;
1829 *data++ = (char) ch;
1845 for (uint8_t i = 0; i < len; i += 2) {
1846 bin[i / 2] = ((hex[i] -
'0') * 16 + (hex[i + 1] -
'0')) & 0xFF;
1858 for (uint8_t i = 0; i < len; i++) {
1859 sprintf(&hex[i * 2],
"%02X", bin[i]);
String getModuleTypeName()
Returns the module type.
uint8_t getChannel()
Gets the channel stored in non-volatile memory.
void setInstallMode(uint8_t mode, bool persist=false)
Sets the module in one of the available operations modes.
uint8_t data[RX_BUFFER_SIZE]
bool enableRX(bool enable)
Enables or disables RF recever.
#define PREAMBLE_FORMAT_A
uint16_t wize_operator_id
bool setUID(uint32_t uid)
Saved the UID into the module memory.
void setEncryptFlag(uint8_t flag)
Sets the encrypt flag setting.
uint8_t getSleepMode()
Gets the sleep mode stored in non-volatile memory.
uint8_t getLEDControl()
Gets the current LED control.
uint8_t getPower()
Gets the RF power stored in non-volatile memory.
#define DATA_INTERFACE_START_STOP
void setWizeOperatorId(uint16_t wize_operator_id)
Sets the wize operator ID field in the transpoprt layer.
int _readBytes(char *buffer, uint16_t len)
Reads the stream buffer up to a number of bytes.
void setWizeApplication(uint8_t wize_application)
Sets the wize applicaton field in the transpoprt layer.
uint8_t getEncryptFlag()
Gets the encrypt flag setting.
void setVersion(uint8_t version)
Sets the device version.
void slave()
Sets the module in slave mode.
allwize_message_t _message
void setPower(uint8_t power, bool persist=false)
Sets the RF power.
#define INSTALL_MODE_HOST
void setNetworkRole(uint8_t role)
Sets the network role.
uint8_t _wize_application
#define HARDWARE_SERIAL_PORT
uint32_t getBaudRateSpeed(uint8_t value)
Gets the UART baud rate speed in bps.
bool getAppendRSSI()
Gets the current RSSI mode value.
bool _decode()
Decodes the current RX buffer contents.
uint8_t getVersion()
Returns the device version from non-volatile memory.
void setControlField(uint8_t value, bool persist=false)
Sets the control field value.
void begin(uint8_t baudrate=MODEM_DEFAULT_BAUDRATE)
Inits the module communications.
int8_t _sendCommand(uint8_t command, uint8_t *data, uint8_t len)
Sends a command with the given data.
void setMode(uint8_t mode, bool persist=false)
Sets the module in one of the available MBus modes.
void dump(Stream &debug)
Dumps the current memory configuration to the given stream.
#define ALLWIZE_DEBUG_PRINT(...)
String getRequiredHardwareVersion()
Returns the minimum required hardware version to run the current firmware.
void setPreamble(uint8_t preamble)
Sets the preamble length frame format.
#define NETWORK_ROLE_REPEATER
uint8_t getControlField()
Gets the control field value stored in non-volatile memory.
uint8_t getPreamble()
Gets the preamble length frame format.
int8_t _receive()
Listens to incoming data from the module until timeout or END_OF_RESPONSE.
uint16_t _wize_operator_id
bool send(uint8_t *buffer, uint8_t len)
Sends a byte array.
void setCounter(uint16_t counter)
Sets the wize couonter field in the transpoprt layer.
void softReset()
Cleans the RX/TX line.
uint8_t _getSlot(uint8_t slot)
Returns the contents of single-byte memory slot.
#define CMD_ACCESS_NUMBER
uint8_t _buffer[RX_BUFFER_SIZE]
String getUID()
Returns the Unique ID string.
uint8_t getTemperature()
Returns the internal temperature of the module.
#define DEFAULT_MBUS_MODE
bool ready()
Test whether the radio module is ready or not.
void setSleepMode(uint8_t mode)
Sets the sleep mode.
uint8_t getDecryptFlag()
Gets the decrypt flag setting.
void setControlInformation(uint8_t ci)
Sets the control information byte.
void wakeup()
Wakes up the radio from sleep mode.
void master()
Sets the module in master mode.
bool available()
Returns true if a new message has been received and decoded This method has to be called in the main ...
String _getSlotAsHexString(uint8_t slot, uint8_t len)
Returns the contents of the memory from a certain address as an HEX String.
uint8_t getDevice()
Returns the device type from non-volatile memory.
void setLEDControl(uint8_t value)
Sets the LED control.
#define CMD_CONTROL_FIELD
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 ...
#define CMD_IDLE_DISABLE_RF
void setDecryptFlag(uint8_t flag)
Sets the decrypt flag setting.
String getPartNumber()
Returns the module part number.
uint8_t getModuleType()
Returns the module type.
void _reset_serial()
Resets the serial object.
static const uint32_t DATARATES[4]
void repeater()
Sets the module in repeater mode.
bool waitForReady(uint32_t timeout=DEFAULT_TIMEOUT)
Waits for timeout millis for the module to be ready.
void setTimeout(uint16_t ms)
Sets the buffer timeout (also used for auto sleep modes)
uint8_t getNetworkRole()
Gets the current network role.
bool setWizeControl(uint8_t wize_control)
Sets the wize control field in the transpoprt layer.
String _getSlotAsString(uint8_t slot, uint8_t len)
Returns the contents of the memory from a certain address as a String object.
static const uint32_t BAUDRATES[11]
uint16_t getCounter()
Gets the current wize counter.
AllWize(HardwareSerial *serial, uint8_t reset_gpio=GPIO_NONE, uint8_t config_gpio=GPIO_NONE)
AllWize object constructor.
int _readBytesUntil(char terminator, char *buffer, uint16_t len)
Reads the stream buffer up to a certain char or times out.
uint8_t _getMemory(uint8_t address)
Returns the contents of memory address.
void sleep()
Sets the radio module in sleep mode.
String getFirmwareVersion()
Returns the module firmware revision.
bool _cacheMemory(uint8_t *buffer)
Reads and caches the module memory.
#define CMD_IDLE_ENABLE_RF
#define NETWORK_ROLE_MASTER
bool _setConfig(bool value)
Sets or unsets config mode.
double getFrequency(uint8_t channel)
Returns the frequency for the given channel.
String getMID()
Returns the Manufacturer ID.
void setChannel(uint8_t channel, bool persist=false)
Sets the communications channel (for MBUS_MODE_R2 only)
#define NETWORK_ROLE_SLAVE
void setAppendRSSI(bool value)
Sets the RSSI mode value.
String getSerialNumber()
Returns the module serial number.
#define PREAMBLE_FORMAT_B
#define SLEEP_MODE_DISABLE
void getDefaultKey(uint8_t *key)
Gets the default encryption key.
void _readModel()
Searches for the module model.
uint8_t getMode()
Gets the MBus mode stored in non-volatile memory.
uint8_t getControlInformation()
Gets the control information byte.
void setDataInterface(uint8_t value)
Sets the data interface for receiving packets.
int _timedRead()
Reads a byte from the stream with a timeout.
void setKey(uint8_t reg, const uint8_t *key)
Sets the default encryption key.
void setDefaultKey(const uint8_t *key)
Sets the default encryption key.
uint8_t getInstallMode()
Gets the install modevalue stored in non-volatile memory.
void setAccessNumber(uint8_t value)
Sets new/specific access number.
SoftwareSerial * _sw_serial
bool reset()
Resets the radio module.
bool _setSlot(uint8_t slot, uint8_t data)
Sets non-volatile memory contents starting from given address.
bool setMID(uint16_t mid)
Sets the Manufacturer ID.
float getRSSI()
Returns the RSSI of the last valid packet received TODO: values do not seem right and are not the sam...
void _bin2hex(uint8_t *bin, char *hex, uint8_t len)
Converts a binary buffer to an hex c-string.
#define DATARATE_6400bps_OSP
uint8_t _getAddress(uint8_t slot)
Return the physical memory address for the given slot.
bool _setMemory(uint8_t address, uint8_t data)
Sets non-volatile memory contents starting from given address.
uint8_t _send(uint8_t *buffer, uint8_t len)
Sends a binary buffer to the module UART. Returns the number of bytes actually sent.
static const uint8_t MEM_ADDRESS[MODULE_MAX-1][MEM_MAX_SLOTS]
uint16_t getTimeout()
Gets the current buffer timeout (also used for auto sleep modes)
void _hex2bin(char *hex, uint8_t *bin, uint8_t len)
Converts a hex c-string to a binary buffer.
uint16_t getDataRateSpeed(uint8_t dr)
Returns the speed for te given datarate.
void _flush()
Flushes the serial line to the module.
uint8_t getDataInterface()
Gets the data interface for receiving packets.
void setDataRate(uint8_t dr)
Sets the data rate.
void setBaudRate(uint8_t baudrate)
Sets the UART baud rate, requires reset to take effect.
HardwareSerial * _hw_serial
uint8_t getDataRate()
Gets the data rate stored in non-volatile memory.
allwize_message_t read()
Returns latest received message.
void setDevice(uint8_t type)
Sets the device type.
uint8_t getBaudRate()
Gets the UART baud rate.
uint16_t getVoltage()
Returns the internal voltage of the module.
#define ALLWIZE_DEBUG_PRINTLN(...)
bool factoryReset()
Resets the module to factory settings.