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) 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);
342 if ((31 < _memory[address]) && (_memory[address] < 127)) {
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;
424 return send((uint8_t *)buffer, strlen(buffer));
459 bool response =
false;
461 static uint32_t when = millis();
467 #if defined(ALLWIZE_DEBUG_PORT) 470 snprintf(buffer,
sizeof(buffer),
"r %02X '%c'", ch, (32 <= ch && ch <= 126) ? ch : 32);
478 #if defined(ARDUINO_ARCH_ESP8266) 485 if ((
_pointer > 0) && (millis() - when > 100)) {
513 if (wize_control > 14)
return false;
584 if (channel > 41)
return;
608 if (0 < power && power < 6) {
748 if (ms > 4080)
return;
749 uint8_t timeout = (ms / 16) - 1;
759 return 16 * (uint16_t) (timeout + 1);
783 if (value > 3)
return;
861 if (0 == flag || 1 == flag || 3 == flag) {
896 if (reg > 128)
return;
899 memcpy(&data[1], key, 16);
932 if ((0 < value) & (value < 12)) {
952 if ((0 < value) & (value < 12)) {
967 if (response > 0)
return -0.5 * (float)
_buffer[0];
1018 buffer[0] = (mid >> 8) & 0xFF;
1019 buffer[1] = (mid >> 0) & 0xFF;
1037 buffer[0] = (uid >> 24) & 0xFF;
1038 buffer[1] = (uid >> 16) & 0xFF;
1039 buffer[2] = (uid >> 8) & 0xFF;
1040 buffer[3] = (uid >> 0) & 0xFF;
1126 return String(
"Unknown");
1137 return 169.40625 + 0.0125 * (channel - 1);
1138 }
else if (channel == 7) {
1140 }
else if (channel == 8) {
1142 }
else if (channel == 9) {
1144 }
else if (channel == 10) {
1146 }
else if (channel < 38) {
1147 return 169.48125 + 0.0125 * (channel - 11);
1148 }
else if (channel < 42) {
1149 return 169.62500 + 0.0500 * (channel - 38);
1162 if ((0 < dr) && (dr < 5)) {
1208 int8_t response = -1;
1225 int8_t response = -1;
1241 int8_t response = -1;
1261 bool ret = (256 ==
_readBytes((
char *) buffer, 256));
1273 #if not USE_MEMORY_CACHE 1274 uint8_t
_memory[0x100] = {0xFF};
1276 if (!_ready)
return;
1283 for (index=0; index<0xFF-32; index++) {
1293 String part_number = String((
char *) &_memory[index]);
1294 part_number.substring(0, 31);
1297 uint8_t end = part_number.indexOf(
",");
1298 _model = part_number.substring(0, end);
1299 uint8_t start = end + 1;
1300 end = part_number.indexOf(
",", start);
1301 _hw = part_number.substring(start, end);
1302 _fw = part_number.substring(end + 1, 32);
1318 #if USE_MEMORY_CACHE 1320 memcpy(buffer, &
_memory[address], len);
1325 for (uint8_t i=0; i<len; i++) {
1344 #if USE_MEMORY_CACHE 1349 if (response > 0)
return _buffer[0];
1364 #if USE_MEMORY_CACHE 1365 if (
_memory[address] == data)
return true;
1375 #if USE_MEMORY_CACHE 1376 if (ret)
_memory[address] = data;
1394 #if USE_MEMORY_CACHE 1395 if (memcmp(&
_memory[address], data, len) == 0)
return true;
1399 uint8_t buffer[len * 2 + 1];
1400 for (uint8_t i = 0; i < len; i++) {
1401 buffer[i * 2] = address + i;
1402 buffer[i * 2 + 1] = data[i];
1410 #if USE_MEMORY_CACHE 1411 if (ret) memcpy(&
_memory[address], data, len);
1443 if (0xFF == address)
return false;
1456 if (0xFF == address)
return false;
1470 if (0xFF == address)
return 0;
1482 if (0xFF == address)
return 0;
1495 char hex[2 * len + 1];
1497 if (len ==
_getSlot(slot, bin, len)) {
1514 if (len ==
_getSlot(slot, bin, len)) {
1515 memcpy(hex, bin, len);
1551 #if defined(ALLWIZE_DEBUG_PORT) 1555 for (uint8_t i = 0; i <
_pointer; i++) {
1556 snprintf(ch,
sizeof(ch),
" %02X",
_buffer[i]);
1564 uint8_t mbus_mode =
getMode();
1566 bool has_start = (data_interface & 0x04) == 0x04;
1567 bool has_header = (mbus_mode !=
MBUS_MODE_OSP) & ((data_interface & 0x01) == 0x00);
1569 bool has_crc = (data_interface & 0x08) == 0x08;
1570 uint8_t bytes_not_in_len = has_start ? 3 : 1;
1571 uint8_t bytes_not_in_app = (has_header ? 9 : 0) + 1 + (has_rssi ? 1 : 0) + (has_crc ? 2 : 0);
1572 uint8_t bytes_not_in_msg = 0;
1584 if (
_pointer != len + bytes_not_in_len)
return false;
1628 bytes_not_in_app += 5;
1646 bytes_not_in_msg = 8;
1653 _message.
len = len - bytes_not_in_app - bytes_not_in_msg;
1702 #if defined(ALLWIZE_DEBUG_PORT) 1705 snprintf(buffer,
sizeof(buffer),
"w %02X '%c'", ch, (32 <= ch && ch <= 126) ? ch : 32);
1721 for (uint8_t i = 0; i < len; i++) {
1722 if (
_send(buffer[i])) n++;
1744 if (
_send(buffer, len) != len)
return -1;
1755 if (
_send(ch) != 1)
return -1;
1766 uint32_t _start = millis();
1768 while (millis() - _start <
_timeout) {
1769 #if defined(ARDUINO_ARCH_ESP8266) 1776 #if defined(ALLWIZE_DEBUG_PORT) 1802 if (len < 1)
return 0;
1805 while (index < len) {
1807 if (ch < 0)
return -1;
1826 if (len < 1)
return 0;
1829 while (index < len) {
1831 if (ch < 0)
return -1;
1832 if (ch == terminator)
break;
1833 *data++ = (char) ch;
1849 for (uint8_t i = 0; i < len; i += 2) {
1850 bin[i / 2] = ((hex[i] -
'0') * 16 + (hex[i + 1] -
'0')) & 0xFF;
1862 for (uint8_t i = 0; i < len; i++) {
1863 sprintf(&hex[i * 2],
"%02X", bin[i]);
1873 uint32_t start = millis();
1874 while (millis() - start < ms) delay(1);
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
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
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.
uint16_t _wize_network_id
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 _resetSerial()
Resets the serial object.
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.
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
void setWizeOperatorId(uint16_t wize_network_id)
Use AllWize::setWizeNetworkId instead.
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.
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
void _niceDelay(uint32_t ms)
Does a non-blocking delay.
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.
void setWizeNetworkId(uint16_t wize_network_id)
Sets the wize network ID field in the transpoprt layer.
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.