32 #if not defined(ARDUINO_ARCH_SAMD) && not defined(ARDUINO_ARCH_ESP32) 33 #include <SoftwareSerial.h> 41 #define MODEM_BAUDRATE 19200 42 #define GPIO_NONE 0x99 43 #define CONTROL_INFORMATION 0x67 44 #define END_OF_RESPONSE '>' 45 #define CMD_ENTER_CONFIG (char) 0x00 46 #define CMD_EXIT_CONFIG (char) 0x58 47 #define CMD_EXIT_MEMORY_ENABLE_RF (char) 0xFD 48 #define CMD_EXIT_MEMORY_DISABLE_RF (char) 0xFF 49 #define CMD_AWAKE (char) 0xFF // Deprecated 50 #define CMD_EXIT_MEMORY (char) 0xFF 51 #define RX_BUFFER_SIZE 255 52 #define DEFAULT_TIMEOUT 1000 53 #define HARDWARE_SERIAL_PORT 1 54 #define SIGNATURE_FIELD 0x04 // TODO: should not be here 55 #define START_BYTE 0x68 56 #define STOP_BYTE 0x16 57 #define DEFAULT_MBUS_MODE MBUS_MODE_N1 67 uint8_t data[RX_BUFFER_SIZE];
80 #if defined(ALLWIZE_DEBUG_PORT) 81 #define ALLWIZE_DEBUG_PRINT(...) ALLWIZE_DEBUG_PORT.print(__VA_ARGS__) 82 #define ALLWIZE_DEBUG_PRINTLN(...) ALLWIZE_DEBUG_PORT.println(__VA_ARGS__) 84 #define ALLWIZE_DEBUG_PRINT(...) 85 #define ALLWIZE_DEBUG_PRINTLN(...) 96 AllWize(HardwareSerial * serial, uint8_t reset_gpio = GPIO_NONE);
97 #if not defined(ARDUINO_ARCH_SAMD) && not defined(ARDUINO_ARCH_ESP32) 98 AllWize(SoftwareSerial * serial, uint8_t reset_gpio = GPIO_NONE);
100 AllWize(uint8_t rx, uint8_t tx, uint8_t reset_gpio = GPIO_NONE);
109 void dump(Stream & debug);
111 bool send(uint8_t * buffer, uint8_t len);
112 bool send(
const char * buffer);
123 void setChannel(uint8_t channel,
bool persist =
false);
124 void setPower(uint8_t power,
bool persist =
false);
126 void setMode(uint8_t mode,
bool persist =
false);
138 void setKey(uint8_t reg,
const uint8_t * key);
162 bool setMID(uint16_t mid);
164 bool setUID(uint32_t uid);
177 int8_t
_sendCommand(uint8_t command, uint8_t * data, uint8_t len);
180 bool _setMemory(uint8_t address, uint8_t * data, uint8_t len);
181 bool _setMemory(uint8_t address, uint8_t data);
182 uint8_t
_getMemory(uint8_t address, uint8_t * buffer, uint8_t len);
192 uint8_t
_send(uint8_t * buffer, uint8_t len);
193 uint8_t
_send(uint8_t ch);
201 void _hex2bin(
char * hex, uint8_t * bin, uint8_t len);
202 void _bin2hex(uint8_t * bin,
char * hex, uint8_t len);
213 Stream * _stream = NULL;
214 HardwareSerial * _hw_serial = NULL;
215 #if defined(ARDUINO_ARCH_SAMD) 217 #elif defined(ARDUINO_ARCH_ESP32) 220 SoftwareSerial * _sw_serial = NULL;
223 uint8_t _reset_gpio = GPIO_NONE;
224 bool _config =
false;
225 uint32_t _timeout = DEFAULT_TIMEOUT;
226 uint8_t _ci = CONTROL_INFORMATION;
228 uint8_t _mbus_mode = 0xFF;
229 uint8_t _data_interface = 0xFF;
230 bool _append_rssi =
false;
233 bool _encrypt =
false;
234 unsigned char _access_number = 0;
242 uint8_t _buffer[RX_BUFFER_SIZE];
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.
bool setUID(uint32_t uid)
Saved the UID into the module memory uid UID to save.
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.
int _readBytes(char *buffer, uint16_t len)
Reads the stream buffer up to a number of bytes.
bool _setMemory(uint8_t address, uint8_t *data, uint8_t len)
Sets non-volatile memory contents starting from given address.
uint8_t getEncryptFlag()
Gets the encrypt flag setting.
AllWize(HardwareSerial *serial, uint8_t reset_gpio=GPIO_NONE)
AllWize object constructor.
void slave()
Sets the module in slave mode.
void setPower(uint8_t power, bool persist=false)
Sets the RF power.
void setNetworkRole(uint8_t role)
Sets the network role.
String _getMemoryAsHexString(uint8_t address, uint8_t len)
Returns the contents of the memory from a certain address as an HEX String.
bool getAppendRSSI()
Gets the current RSSI mode value.
bool _decode()
Decodes the current RX buffer contents.
uint8_t getVersion()
Returns the module version from non-volatile memory.
void setControlField(uint8_t value, bool persist=false)
Sets the control field value.
int8_t _sendCommand(uint8_t command, uint8_t *data, uint8_t len)
Sends a command with the given data.
void dump(Stream &debug)
Dumps the current memory configuration to the given stream.
void setMode(uint8_t mode, bool persist=false)
Sets the module in one of the available MBus modes.
void setPreamble(uint8_t preamble)
Sets the preamble length frame format.
uint8_t getControlField()
Gets the control field value stored in non-volatile memory.
uint8_t getTimeout()
Gets the current timeout for auto sleep modes.
uint8_t getPreamble()
Gets the preamble length frame format.
int8_t _receive()
Listens to incomming data from the module until timeout or END_OF_RESPONSE.
bool send(uint8_t *buffer, uint8_t len)
Sends a byte array.
String getUID()
Returns the Unique ID string.
uint8_t getTemperature()
Returns the RSSI of the last valid packet received TODO: values do not seem right and are not the sam...
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 ...
uint8_t getDevice()
Returns the device version from non-volatile memory.
void setLEDControl(uint8_t value)
Sets the LED control.
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 setTimeout(uint8_t timeout)
Sets the timeout for auto sleep modes.
uint8_t _getMemory(uint8_t address, uint8_t *buffer, uint8_t len)
Returns the contents of consecutive memory addresses.
void setDecryptFlag(uint8_t flag)
Sets the decrypt flag setting.
String getPartNumber()
Returns the module part number.
void _reset_serial()
Resets the serial object.
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.
uint8_t getNetworkRole()
Gets the current network role.
uint8_t getMode()
Gets the MBus mode stored in non-volatile memory.
int _readBytesUntil(char terminator, char *buffer, uint16_t len)
Reads the stream buffer up to a certain char or times out.
void sleep()
Sets the radio module in sleep mode.
String getFirmwareVersion()
Returns the module firmware revision.
bool _setConfig(bool value)
Sets or unsets config mode.
String getMID()
Returns the Manufacturer ID string.
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.
String getSerialNumber()
Returns the module serial number.
void getDefaultKey(uint8_t *key)
Gets the default encryption key.
String _getMemoryAsString(uint8_t address, uint8_t len)
Returns the contents of the memory from a certain address as a String object.
void _readModel()
Reads and caches the module model & version.
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.
bool reset()
Resets the radio module.
bool setMID(uint16_t mid)
Sets the Manufacturer ID mid MID to save.
String getRequiredHardwareVersion()
Returns the module hardware revision.
void _bin2hex(uint8_t *bin, char *hex, uint8_t len)
Converts a binary buffer to an hex c-string.
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 begin()
Inits the module communications.
void _hex2bin(char *hex, uint8_t *bin, uint8_t len)
Converts a hex c-string to a binary buffer.
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.
uint8_t getDataRate()
Gets the data rate stored in non-volatile memory.
allwize_message_t read()
Returns latest received message.
uint16_t getVoltage()
Returns the internal voltage of the module.
bool factoryReset()
Resets the module to factory settings.