Go to the documentation of this file.
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 += 5;
386 if (message_len > 0xF6)
return false;
389 if (1 !=
_send(message_len))
return false;
395 if (send_wize_transport_layer) {
404 if (len !=
_send(buffer, len))
return false;
408 unsigned long ts = millis() / 1000;
409 _send((ts >> 8) & 0xFF);
410 _send((ts >> 0) & 0xFF);
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)) {
532 if (wize_control > 14)
return false;
603 if (channel > 41)
return;
627 if (0 < power && power < 6) {
767 if (ms > 4080)
return;
768 uint8_t timeout = (ms / 16) - 1;
778 return 16 * (uint16_t) (timeout + 1);
802 if (value > 3)
return;
880 if (0 == flag || 1 == flag) {
898 if (0 == flag || 1 == flag || 3 == flag) {
933 if (reg > 128)
return;
936 memcpy(&data[1], key, 16);
969 if ((0 < value) & (value < 12)) {
989 if ((0 < value) & (value < 12)) {
1004 if (response > 0)
return -0.5 * (float)
_buffer[0];
1014 uint8_t ret_val = 0;
1055 buffer[0] = (mid >> 8) & 0xFF;
1056 buffer[1] = (mid >> 0) & 0xFF;
1074 buffer[0] = (uid >> 24) & 0xFF;
1075 buffer[1] = (uid >> 16) & 0xFF;
1076 buffer[2] = (uid >> 8) & 0xFF;
1077 buffer[3] = (uid >> 0) & 0xFF;
1163 return String(
"Unknown");
1174 return 169.40625 + 0.0125 * (channel - 1);
1175 }
else if (channel == 7) {
1177 }
else if (channel == 8) {
1179 }
else if (channel == 9) {
1181 }
else if (channel == 10) {
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);
1199 if ((0 < dr) && (dr < 5)) {
1245 int8_t response = -1;
1262 int8_t response = -1;
1278 int8_t response = -1;
1298 bool ret = (256 ==
_readBytes((
char *) buffer, 256));
1310 #if not USE_MEMORY_CACHE
1311 uint8_t
_memory[0x100] = {0xFF};
1320 for (index=0; index<0xFF-32; index++) {
1330 String part_number = String((
char *) &
_memory[index]);
1331 part_number.substring(0, 31);
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);
1355 #if USE_MEMORY_CACHE
1357 memcpy(buffer, &
_memory[address], len);
1362 for (uint8_t i=0; i<len; i++) {
1381 #if USE_MEMORY_CACHE
1386 if (response > 0)
return _buffer[0];
1401 #if USE_MEMORY_CACHE
1402 if (
_memory[address] == data)
return true;
1412 #if USE_MEMORY_CACHE
1413 if (ret)
_memory[address] = data;
1431 #if USE_MEMORY_CACHE
1432 if (memcmp(&
_memory[address], data, len) == 0)
return true;
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];
1447 #if USE_MEMORY_CACHE
1448 if (ret) memcpy(&
_memory[address], data, len);
1480 if (0xFF == address)
return false;
1493 if (0xFF == address)
return false;
1507 if (0xFF == address)
return 0;
1519 if (0xFF == address)
return 0;
1532 char hex[2 * len + 1];
1534 if (len ==
_getSlot(slot, bin, len)) {
1551 if (len ==
_getSlot(slot, bin, len)) {
1552 memcpy(hex, bin, len);
1588 #if defined(ALLWIZE_DEBUG_PORT)
1592 for (uint8_t i = 0; i <
_pointer; i++) {
1593 snprintf(ch,
sizeof(ch),
" %02X",
_buffer[i]);
1601 uint8_t mbus_mode =
getMode();
1603 bool has_start = (data_interface & 0x04) == 0x04;
1604 bool has_header = (mbus_mode !=
MBUS_MODE_OSP) & ((data_interface & 0x01) == 0x00);
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;
1621 if (
_pointer != len + bytes_not_in_len)
return false;
1665 bytes_not_in_app += 5;
1683 bytes_not_in_msg = 8;
1690 _message.
len = len - bytes_not_in_app - bytes_not_in_msg;
1739 #if defined(ALLWIZE_DEBUG_PORT)
1742 snprintf(buffer,
sizeof(buffer),
"w %02X '%c'", ch, (32 <= ch && ch <= 126) ? ch : 32);
1758 for (uint8_t i = 0; i < len; i++) {
1759 if (
_send(buffer[i])) n++;
1781 if (
_send(buffer, len) != len)
return -1;
1792 if (
_send(ch) != 1)
return -1;
1803 uint32_t _start = millis();
1805 while (millis() - _start <
_timeout) {
1806 #if defined(ARDUINO_ARCH_ESP8266)
1813 #if defined(ALLWIZE_DEBUG_PORT)
1839 if (len < 1)
return 0;
1842 while (index < len) {
1844 if (ch < 0)
return -1;
1863 if (len < 1)
return 0;
1866 while (index < len) {
1868 if (ch < 0)
return -1;
1869 if (ch == terminator)
break;
1870 *data++ = (char) ch;
1886 for (uint8_t i = 0; i < len; i += 2) {
1887 bin[i / 2] = ((hex[i] -
'0') * 16 + (hex[i + 1] -
'0')) & 0xFF;
1899 for (uint8_t i = 0; i < len; i++) {
1900 sprintf(&hex[i * 2],
"%02X", bin[i]);
1910 uint32_t start = millis();
1911 while (millis() - start < ms) delay(1);
uint8_t getMode()
Gets the MBus mode stored in non-volatile memory.
bool _setMemory(uint8_t address, uint8_t data)
Sets non-volatile memory contents starting from given address.
uint8_t getVersion()
Returns the device version from non-volatile memory.
void setDevice(uint8_t type)
Sets the device type.
void setWizeApplication(uint8_t wize_application)
Sets the wize applicaton field in the transpoprt layer.
void slave()
Sets the module in slave mode.
bool waitForReady(uint32_t timeout=DEFAULT_TIMEOUT)
Waits for timeout millis for the module to be ready.
void setAccessNumber(uint8_t value)
Sets new/specific access number.
SoftwareSerial * _sw_serial
static const uint8_t MEM_ADDRESS[MODULE_MAX-1][MEM_MAX_SLOTS]
void setMAC2CheckOnlyFlag(uint8_t flag)
Sets the MAC 2 Check Only flag setting.
void dump(Stream &debug)
Dumps the current memory configuration to the given stream.
bool _setSlot(uint8_t slot, uint8_t data)
Sets non-volatile memory contents starting from given address.
String getSerialNumber()
Returns the module serial number.
void getDefaultKey(uint8_t *key)
Gets the default encryption key.
bool ready()
Test whether the radio module is ready or not.
void setSleepMode(uint8_t mode)
Sets the sleep mode.
uint8_t getNetworkRole()
Gets the current network role.
uint8_t getEncryptFlag()
Gets the encrypt flag setting.
void setDataInterface(uint8_t value)
Sets the data interface for receiving packets.
uint16_t _wize_network_id
#define NETWORK_ROLE_REPEATER
String _getSlotAsString(uint8_t slot, uint8_t len)
Returns the contents of the memory from a certain address as a String object.
void _resetSerial()
Resets the serial object.
void sleep()
Sets the radio module in sleep mode.
void setPreamble(uint8_t preamble)
Sets the preamble length frame format.
#define CMD_IDLE_ENABLE_RF
int _readBytes(char *buffer, uint16_t len)
Reads the stream buffer up to a number of bytes.
void _bin2hex(uint8_t *bin, char *hex, uint8_t len)
Converts a binary buffer to an hex c-string.
uint8_t getDecryptFlag()
Gets the decrypt flag setting.
HardwareSerial * _hw_serial
String getRequiredHardwareVersion()
Returns the minimum required hardware version to run the current firmware.
uint8_t getPreamble()
Gets the preamble length frame format.
void setWizeNetworkId(uint8_t wize_network_id)
Sets the wize network ID field in the transpoprt layer.
int8_t _receive()
Listens to incoming data from the module until timeout or END_OF_RESPONSE.
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.
uint8_t _send(uint8_t *buffer, uint8_t len)
Sends a binary buffer to the module UART. Returns the number of bytes actually sent.
void setControlField(uint8_t value, bool persist=false)
Sets the control field value.
void _hex2bin(char *hex, uint8_t *bin, uint8_t len)
Converts a hex c-string to a binary buffer.
void setLEDControl(uint8_t value)
Sets the LED control.
void setCounter(uint16_t counter)
Sets the wize couonter field in the transpoprt layer.
static const uint32_t BAUDRATES[11]
void setPower(uint8_t power, bool persist=false)
Sets the RF power.
void _flush()
Flushes the serial line to the module.
void setWizeOperatorId(uint8_t wize_network_id)
Use AllWize::setWizeNetworkId instead.
#define DEFAULT_MBUS_MODE
void setBaudRate(uint8_t baudrate)
Sets the UART baud rate, requires reset to take effect.
uint8_t getDataInterface()
Gets the data interface for receiving packets.
void _readModel()
Searches for the module model.
#define CMD_CONTROL_FIELD
uint8_t getModuleType()
Returns the module type.
allwize_message_t read()
Returns latest received message.
String _getSlotAsHexString(uint8_t slot, uint8_t len)
Returns the contents of the memory from a certain address as an HEX String.
uint8_t getInstallMode()
Gets the install modevalue stored in non-volatile memory.
void setControlInformation(uint8_t ci)
Sets the control information byte.
#define DATA_INTERFACE_START_STOP
uint8_t * getBuffer()
Returns pointer to the last message raw data buffer Should be copied right away since any new incommi...
#define NETWORK_ROLE_SLAVE
#define INSTALL_MODE_HOST
#define ALLWIZE_DEBUG_PRINT(...)
void setDecryptFlag(uint8_t flag)
Sets the decrypt flag setting.
#define HARDWARE_SERIAL_PORT
String getPartNumber()
Returns the module part number.
double getFrequency(uint8_t channel)
Returns the frequency for the given channel.
void master()
Sets the module in master mode.
#define DATARATE_6400bps_OSP
uint8_t getControlField()
Gets the control field value stored in non-volatile memory.
String getFirmwareVersion()
Returns the module firmware revision.
bool _cacheMemory(uint8_t *buffer)
Reads and caches the module memory.
bool setUID(uint32_t uid)
Saved the UID into the module memory.
uint8_t getControlInformation()
Gets the control information byte.
void softReset()
Cleans the RX/TX line.
allwize_message_t _message
void setKey(uint8_t reg, const uint8_t *key)
Sets the default encryption key.
bool setWizeControl(uint8_t wize_control)
Sets the wize control field in the transport layer.
uint8_t getTemperature()
Returns the internal temperature of the module.
uint16_t getCounter()
Gets the current wize counter.
void setVersion(uint8_t version)
Sets the device version.
void setTimeout(uint16_t ms)
Sets the buffer timeout (also used for auto sleep modes)
String getUID()
Returns the Unique ID string.
AllWize(HardwareSerial *serial, uint8_t reset_gpio=GPIO_NONE, uint8_t config_gpio=GPIO_NONE)
AllWize object constructor.
bool available()
Returns true if a new message has been received and decoded This method has to be called in the main ...
#define NETWORK_ROLE_MASTER
void setNetworkRole(uint8_t role)
Sets the network role.
uint8_t getDevice()
Returns the device type from non-volatile memory.
uint8_t _getAddress(uint8_t slot)
Return the physical memory address for the given slot.
String getMID()
Returns the Manufacturer ID.
uint32_t getBaudRateSpeed(uint8_t value)
Gets the UART baud rate speed in bps.
void begin(uint8_t baudrate=MODEM_DEFAULT_BAUDRATE)
Inits the module communications.
bool send(uint8_t *buffer, uint8_t len)
Sends a byte array.
uint8_t getLength()
Returns the length of the last message raw data buffer.
uint8_t getDataRate()
Gets the data rate stored in non-volatile memory.
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 ...
bool factoryReset()
Resets the module to factory settings.
uint8_t _wize_application
void repeater()
Sets the module in repeater mode.
bool getAppendRSSI()
Gets the current RSSI mode value.
int _timedRead()
Reads a byte from the stream with a timeout.
uint8_t _buffer[RX_BUFFER_SIZE]
uint8_t _getSlot(uint8_t slot)
Returns the contents of single-byte memory slot.
@ MEM_MAC_2_CHECK_ONLY_FLAG
bool reset()
Resets the radio module.
bool _setConfig(bool value)
Sets or unsets config mode.
void setEncryptFlag(uint8_t flag)
Sets the encrypt flag setting.
uint8_t getMAC2CheckOnlyFlag()
Gets the MAC 2 Check Only flag setting.
uint16_t getDataRateSpeed(uint8_t dr)
Returns the speed for te given datarate.
uint8_t getLEDControl()
Gets the current LED control.
uint8_t data[RX_BUFFER_SIZE]
void _niceDelay(uint32_t ms)
Does a non-blocking delay.
static const uint32_t DATARATES[4]
#define PREAMBLE_FORMAT_B
#define CMD_ACCESS_NUMBER
uint16_t getVoltage()
Returns the internal voltage of the module.
String getModuleTypeName()
Returns the module type.
bool setMID(uint16_t mid)
Sets the Manufacturer ID.
#define CMD_IDLE_DISABLE_RF
float getRSSI()
Returns the RSSI of the last valid packet received TODO: values do not seem right and are not the sam...
bool _decode()
Decodes the current RX buffer contents.
#define SLEEP_MODE_DISABLE
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.
#define PREAMBLE_FORMAT_A
void wakeup()
Wakes up the radio from sleep mode.
uint8_t getBaudRate()
Gets the UART baud rate.
void setDefaultKey(const uint8_t *key)
Sets the default encryption key.
#define ALLWIZE_DEBUG_PRINTLN(...)
uint16_t getTimeout()
Gets the current buffer timeout (also used for auto sleep modes)
void setChannel(uint8_t channel, bool persist=false)
Sets the communications channel (for MBUS_MODE_R2 only)
void setAppendRSSI(bool value)
Sets the RSSI mode value.
uint8_t getChannel()
Gets the channel stored in non-volatile memory.
uint8_t getSleepMode()
Gets the sleep mode stored in non-volatile memory.
uint8_t getPower()
Gets the RF power stored in non-volatile memory.
void setInstallMode(uint8_t mode, bool persist=false)
Sets the module in one of the available operations modes.
bool enableRX(bool enable)
Enables or disables RF recever.
void setDataRate(uint8_t dr)
Sets the data rate.