Tasmota/tasmota/tasmota_xsns_sensor/xsns_51_rdm6300.ino

171 lines
5.0 KiB
Arduino
Raw Normal View History

2019-08-19 09:54:58 +01:00
/*
2020-10-18 12:04:02 +01:00
xsns_51_rdm6300.ino - Support for RDM630(0) 125kHz NFC Tag Reader on Tasmota
2019-08-19 09:54:58 +01:00
2021-01-01 12:44:04 +00:00
Copyright (C) 2021 Gerhard Mutz and Theo Arends
2019-08-19 09:54:58 +01:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef USE_RDM6300
2020-10-18 12:04:02 +01:00
/*********************************************************************************************\
* Seeed studio Grove / RDM630 / RDM6300 125kHz rfid reader
*
* Expected 14 byte data:
* 0 1 2 3 4 5 6 7 8 9 10 11 12 13
* Hd ------ ASCII characters ----- Chksm Tl
* 02 31 34 30 30 38 45 43 37 39 33 43 45 03 = 02-14-008EC793-CE-03
* Versn --------- Tag ---------
\*********************************************************************************************/
2019-08-19 09:54:58 +01:00
2020-10-18 12:04:02 +01:00
#define XSNS_51 51
2019-08-19 09:54:58 +01:00
2020-10-18 12:04:02 +01:00
#define RDM6300_BAUDRATE 9600
#define RDM_TIMEOUT 100
#define RDM6300_BLOCK 2 * 10 // 2 seconds block time
2019-08-19 09:54:58 +01:00
#include <TasmotaSerial.h>
2020-10-18 12:04:02 +01:00
TasmotaSerial *RDM6300Serial = nullptr;
2019-08-19 09:54:58 +01:00
2020-10-18 12:04:02 +01:00
struct {
uint32_t uid = 0;
uint8_t block_time = 0;
} Rdm;
2019-08-19 09:54:58 +01:00
2020-10-18 12:04:02 +01:00
/********************************************************************************************/
2019-08-19 09:54:58 +01:00
2020-10-18 12:04:02 +01:00
uint8_t RDM6300HexNibble(char chr) {
uint8_t rVal = 0;
if (isdigit(chr)) { rVal = chr - '0'; }
else if (chr >= 'A' && chr <= 'F') { rVal = chr + 10 - 'A'; }
else if (chr >= 'a' && chr <= 'f') { rVal = chr + 10 - 'a'; }
return rVal;
}
// Convert hex string to int array
void RDM6300HexStringToArray(uint8_t array[], uint8_t len, char buffer[]) {
char *cp = buffer;
for (uint32_t i = 0; i < len; i++) {
uint8_t val = RDM6300HexNibble(*cp++) << 4;
array[i] = val | RDM6300HexNibble(*cp++);
}
}
2019-08-19 09:54:58 +01:00
2020-10-18 12:04:02 +01:00
/********************************************************************************************/
void RDM6300Init() {
2020-04-27 15:47:29 +01:00
if (PinUsed(GPIO_RDM6300_RX)) {
2020-10-18 12:04:02 +01:00
RDM6300Serial = new TasmotaSerial(Pin(GPIO_RDM6300_RX), -1, 1);
if (RDM6300Serial->begin(RDM6300_BAUDRATE)) {
if (RDM6300Serial->hardwareSerial()) {
2019-08-19 09:54:58 +01:00
ClaimSerial();
}
#ifdef ESP32
AddLog(LOG_LEVEL_DEBUG, PSTR("RDM: Serial UART%d"), RDM6300Serial->getUart());
#endif
2019-08-19 09:54:58 +01:00
}
}
}
2020-10-18 12:04:02 +01:00
void RDM6300ScanForTag() {
if (!RDM6300Serial) { return; }
2019-08-19 09:54:58 +01:00
2020-10-18 12:04:02 +01:00
if (Rdm.block_time > 0) {
Rdm.block_time--;
while (RDM6300Serial->available()) {
RDM6300Serial->read(); // Flush serial buffer
}
2019-08-19 09:54:58 +01:00
return;
}
2020-10-18 12:04:02 +01:00
if (RDM6300Serial->available()) {
char c = RDM6300Serial->read();
if (c != 2) { return; } // Head marker
2019-08-19 09:54:58 +01:00
// read rest of message 11 more bytes
2020-10-18 12:04:02 +01:00
char rdm_buffer[14];
uint8_t rdm_index = 0;
rdm_buffer[rdm_index++] = c;
uint32_t cmillis = millis();
2019-08-19 09:54:58 +01:00
while (1) {
2020-10-18 12:04:02 +01:00
if (RDM6300Serial->available()) {
c = RDM6300Serial->read();
rdm_buffer[rdm_index++] = c;
if (3 == c) { break; } // Tail marker
if (rdm_index > 14) { break; } // Illegal message
2019-08-19 09:54:58 +01:00
}
2020-10-18 12:04:02 +01:00
if ((millis() - cmillis) > RDM_TIMEOUT) {
return; // Timeout
2019-08-19 09:54:58 +01:00
}
}
2020-10-18 12:04:02 +01:00
AddLogBuffer(LOG_LEVEL_DEBUG, (uint8_t*)rdm_buffer, sizeof(rdm_buffer));
2019-08-19 09:54:58 +01:00
2020-10-18 12:04:02 +01:00
if (rdm_buffer[13] != 3) { return; } // Tail marker
2019-08-19 09:54:58 +01:00
2020-10-18 12:04:02 +01:00
Rdm.block_time = RDM6300_BLOCK; // Block for 2 seconds
2019-08-19 09:54:58 +01:00
2020-10-18 12:04:02 +01:00
uint8_t rdm_array[6];
RDM6300HexStringToArray(rdm_array, sizeof(rdm_array), (char*)rdm_buffer +1);
uint8_t accu = 0;
for (uint32_t count = 0; count < 5; count++) {
accu ^= rdm_array[count]; // Calc checksum,
}
if (accu != rdm_array[5]) { return; } // Checksum error
rdm_buffer[11] = '\0';
uint32_t uid = strtoul(rdm_buffer +3, nullptr, 16);
if (uid > 0) { // Ignore false positive all zeros
Rdm.uid = uid;
ResponseTime_P(PSTR(",\"RDM6300\":{\"UID\":\"%08X\"}}"), Rdm.uid);
MqttPublishTeleSensor();
}
2019-08-19 09:54:58 +01:00
}
}
#ifdef USE_WEBSERVER
2020-10-18 12:04:02 +01:00
void RDM6300Show(void) {
if (!RDM6300Serial) { return; }
WSContentSend_PD(PSTR("{s}RDM6300 UID{m}%08X {e}"), Rdm.uid);
2019-08-19 09:54:58 +01:00
}
#endif // USE_WEBSERVER
/*********************************************************************************************\
* Interface
\*********************************************************************************************/
2022-11-11 09:44:56 +00:00
bool Xsns51(uint32_t function) {
2019-08-19 09:54:58 +01:00
bool result = false;
2020-10-18 12:04:02 +01:00
switch (function) {
case FUNC_INIT:
RDM6300Init();
break;
case FUNC_EVERY_100_MSECOND:
RDM6300ScanForTag();
break;
2019-08-19 09:54:58 +01:00
#ifdef USE_WEBSERVER
2020-10-18 12:04:02 +01:00
case FUNC_WEB_SENSOR:
RDM6300Show();
break;
2019-08-19 09:54:58 +01:00
#endif // USE_WEBSERVER
2020-10-18 12:04:02 +01:00
}
return result;
2019-08-19 09:54:58 +01:00
}
#endif // USE_RDM6300