33 Allwize::Allwize(Stream& stream, uint8_t reset_gpio) : _stream(stream), _reset_gpio(reset_gpio) {
34 if (0xFF != _reset_gpio) {
35 pinMode(_reset_gpio, OUTPUT);
36 digitalWrite(_reset_gpio, HIGH);
57 if (0xFF == _reset_gpio) {
69 digitalWrite(_reset_gpio, LOW);
71 digitalWrite(_reset_gpio, HIGH);
110 _setMemory(MEM_DATA_INTERFACE, DATA_INTERFACE_START_STOP);
167 char ascii[17] = {0};
172 for (uint16_t address = 0; address <= 0x0F; address++) {
173 snprintf(ch,
sizeof(ch),
"%02X ", address);
177 debug.print(
"------------------------------------------------------");
179 for (uint16_t address = 0; address <= 255; address++) {
180 if ((address % 16) == 0) {
181 if (address > 0) debug.print(ascii);
182 snprintf(ch,
sizeof(ch),
"\n0x%02X: ", address);
185 if (31 < buffer[address] && buffer[address] < 127) {
186 ascii[address % 16] = (char) buffer[address];
188 ascii[address % 16] =
' ';
190 snprintf(ch,
sizeof(ch),
"%02X ", (uint8_t) buffer[address]);
198 debug.println(
"Error doing memory dump...");
212 if (_config)
return false;
213 if (0 == len)
return (1 ==
_send(0xFE));
214 if (1 !=
_send(len+1))
return false;
215 if (1 !=
_send(_ci))
return false;
216 if (len !=
_send(buffer, len))
return false;
226 return send((uint8_t *) buffer, strlen(buffer));
236 bool response =
false;
240 static uint32_t when = millis();
242 while (_stream.available()) {
243 uint8_t ch = _stream.read();
244 _buffer[_pointer++] = ch;
246 #if defined(ARDUINO_ARCH_ESP8266) 252 if ((_pointer > 0) && (millis() - when > 100)) {
297 if (persist)
_setMemory(MEM_CHANNEL, channel);
315 if (0 < power && power < 6) {
334 if (0 < dr && dr < 6 && dr != 3) {
402 if (PREAMBLE_FORMAT_A == preamble || PREAMBLE_FORMAT_B == preamble) {
487 if (persist)
_setMemory(MEM_CONTROL_FIELD, value);
505 if (persist)
_setMemory(MEM_INSTALL_MODE, mode);
586 if (response > 0)
return (_buffer[0] - 128);
596 if (response > 0)
return 30 * _buffer[0];
678 if (value != _config) {
686 _send(CMD_EXIT_CONFIG);
702 int8_t response = -1;
719 int8_t response = -1;
735 int8_t response = -1;
751 uint8_t buffer[len*2+1];
752 for (uint8_t i=0; i<len; i++) {
753 buffer[i*2] = address + i;
754 buffer[i*2+1] = data[i];
756 buffer[len*2] = CMD_EXIT_MEMORY;
757 return (
_sendCommand(CMD_WRITE_MEMORY, buffer, len*2+1) != -1);
768 uint8_t buffer[3] = {address, value, CMD_EXIT_MEMORY};
769 return (
_sendCommand(CMD_WRITE_MEMORY, buffer, 3) != -1);
783 for (uint8_t i=0; i<len; i++) {
787 buffer[i] = _buffer[0];
801 uint8_t response =
_sendCommand(CMD_READ_MEMORY, address);
802 if (response > 0)
return _buffer[0];
835 memcpy(hex, bin, len);
846 if (_model.length() > 0)
return;
848 if (line.indexOf(
"RC") != 0) String line =
_getMemoryAsString(MEM_PART_NUMBER_OLD, 32);
850 if (line.indexOf(
"RC") != 0) {
851 _model = String(
"Unknown");
856 uint8_t end = line.indexOf(
",", start);
857 _model = line.substring(start, end);
860 end = line.indexOf(
",", start);
861 _hw = line.substring(start, end);
864 end = line.indexOf(
",", start);
865 _fw = line.substring(start, end);
895 uint8_t len = _buffer[1];
896 if (_pointer != len+3)
return false;
902 _message.ci = _buffer[2];
905 _message.len = len - 2;
906 if (_message.data)
delete[] _message.data;
907 _message.data =
new uint8_t[_message.len + 1];
908 memcpy(_message.data, &_buffer[3], _message.len);
909 _message.data[_message.len] = 0;
912 _message.rssi = _buffer[_pointer-2];
935 return _stream.write(ch);
947 for (uint8_t i=0; i<len; i++) {
948 if (
_send(buffer[i])) n++;
959 return _readBytesUntil(END_OF_RESPONSE, (
char*) _buffer, RX_BUFFER_SIZE);
970 if (
_send(buffer, len) != len)
return -1;
981 if (
_send(ch) != 1)
return -1;
995 uint32_t _start = millis();
996 while (millis() - _start < _timeout) {
997 int ch = _stream.read();
998 if (ch >= 0)
return ch;
999 #if defined(ARDUINO_ARCH_ESP8266) 1014 if (len < 1)
return 0;
1016 while (index < len) {
1019 *buffer++ = (char) ch;
1034 if (len < 1)
return 0;
1036 while (index < len) {
1039 if (ch == terminator)
break;
1040 *buffer++ = (char) ch;
1054 for (uint8_t i=0; i<len; i+=2) {
1055 bin[i/2] = ((hex[i] -
'0') * 16 + (hex[i+1] -
'0')) & 0xFF;
1067 for (uint8_t i=0; i<len; i++) {
1068 sprintf(&hex[i*2],
"%02X", bin[i]);
String getFirmwareVersion()
Returns the module firmware revision.
void _flush()
Flushes the serial line to the module.
uint8_t getControlInformation()
Gets the control information byte.
int8_t _sendCommand(uint8_t command, uint8_t *data, uint8_t len)
Sends a command with the given data.
uint8_t _getMemory(uint8_t address, uint8_t *buffer, uint8_t len)
Returns the contents of consecutive memory addresses.
void setChannel(uint8_t channel, bool persist=false)
Sets the communications channel (for MBUS_MODE_R2 only)
void setDataRate(uint8_t dr)
Sets the data rate.
void setSleepMode(uint8_t mode)
Sets the sleep mode.
void slave()
Sets the module in slave mode.
void setEncryptFlag(uint8_t flag)
Sets the encrypt flag setting.
bool factoryReset()
Resets the module to factory settings You must reset the serial connection after the factoryReset: Se...
uint8_t getPreamble()
Gets the preamble length frame format.
void _readModel()
Reads and caches the module model & version.
void setInstallMode(uint8_t mode, bool persist=false)
Sets the module in one of the available operations modes.
int _readBytes(char *buffer, uint16_t len)
Reads the stream buffer up to a number of bytes.
String getMID()
Returns the Manufacturer ID string.
String getUID()
Returns the Unique ID string.
void repeater()
Sets the module in repeater mode.
void master()
Sets the module in master mode.
allwize_message_t read()
Returns latest received message.
void setDecryptFlag(uint8_t flag)
Sets the decrypt flag setting.
int _readBytesUntil(char terminator, char *buffer, uint16_t len)
Reads the stream buffer up to a certain char or times out.
void dump(Stream &debug)
Dumps the current memory configuration to the given stream.
String _getMemoryAsString(uint8_t address, uint8_t len)
Returns the contents of the memory from a certain address as a String object.
bool available()
Returns true if a new message has been received and decoded This method has to be called in the main ...
void _bin2hex(uint8_t *bin, char *hex, uint8_t len)
Converts a binary buffer to an hex c-string.
void getDefaultKey(uint8_t *key)
Gets the default encryption key.
void setControlInformation(uint8_t ci)
Sets the control information byte.
void setTimeout(uint8_t timeout)
Sets the timeout for auto sleep modes.
bool ready()
Test whether the radio module is ready or not.
bool _decode()
Decodes the current RX buffer contents.
int _timedRead()
Reads a byte from the stream with a timeout.
uint8_t getInstallMode()
Gets the install modevalue stored in non-volatile memory.
uint8_t getNetworkRole()
Gets the current network role.
uint8_t getSleepMode()
Gets the sleep mode stored in non-volatile memory.
void setControlField(uint8_t value, bool persist=false)
Sets the data interface for receiving packets.
uint8_t getDataRate()
Gets the data rate stored in non-volatile memory.
uint8_t getTemperature()
Returns the RSSI of the last valid packet received TODO: values do not seem right and are not the sam...
uint8_t getChannel()
Gets the channel 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 ...
void setPower(uint8_t power, bool persist=false)
Sets the RF power.
uint8_t getLEDControl()
Gets the current LED control.
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.
bool reset()
Resets the radio module. You must reset the serial connection after the reset: Serial1.end(); Serial1.begin(19200); delay(200);.
uint8_t getDevice()
Returns the device version from non-volatile memory.
uint8_t getEncryptFlag()
Gets the encrypt flag setting.
bool send(uint8_t *buffer, uint8_t len)
Sends a byte array.
void begin()
Inits the module communications.
uint8_t getMode()
Gets the MBus mode stored in non-volatile memory.
uint8_t getVersion()
Returns the module version from non-volatile memory.
String getPartNumber()
Returns the module part number.
void setDefaultKey(uint8_t *key)
Sets the default encryption key.
void wakeup()
Wakes up the radio from sleep mode.
void _hex2bin(char *hex, uint8_t *bin, uint8_t len)
Converts a hex c-string to a binary buffer.
void setPreamble(uint8_t preamble)
Sets the RSSI mode value.
void sleep()
Sets the radio module in sleep mode.
bool _setConfig(bool value)
Sets or unsets config mode.
uint16_t getVoltage()
Returns the internal voltage of the module.
uint8_t getTimeout()
Gets the current timeout for auto sleep modes.
void setNetworkRole(uint8_t role)
Sets the network role.
int8_t _receive()
Listens to incomming data from the module until timeout or END_OF_RESPONSE. Returns the number of byt...
String _getMemoryAsHexString(uint8_t address, uint8_t len)
Returns the contents of the memory from a certain address as an HEX String.
uint8_t getDecryptFlag()
Gets the decrypt flag setting.
Allwize(Stream &stream, uint8_t reset_gpio=0xFF)
Allwize object constructor.
String getSerialNumber()
Returns the module serial number.
void setLEDControl(uint8_t value)
Sets the LED control.
bool _setMemory(uint8_t address, uint8_t *data, uint8_t len)
Sets non-volatile memory contents starting from given address.
uint8_t getPower()
Gets the RF power stored in non-volatile memory.
uint8_t getControlField()
Gets the control field value stored in non-volatile memory.
String getRequiredHardwareVersion()
Returns the module hardware revision.