Skip to content

Commit 120e4d1

Browse files
committed
Add esp8266 hardware uart support with an example!
1 parent 3b8fc3e commit 120e4d1

File tree

6 files changed

+126
-19
lines changed

6 files changed

+126
-19
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* A simple example to interface with rdm6300 rfid reader using esp8266.
3+
* We use hardware uart "Serial" instead of the default software uart driver.
4+
*
5+
* Note:
6+
* The esp8266 let us use 1.5 uarts:
7+
* Serial=uart0=rx+tx, Serial1=uart1=tx only (rx pin is used as flash io).
8+
* Here we sawp the uart pins so uart0_rx goes to the rdm6300_tx,
9+
* and uart1_tx goes to what used to be uart0_tx-
10+
* so debug message goes from Serial1.print(...) to the pc debug terminal.
11+
* https://github.com/esp8266/Arduino/blob/master/doc/reference.rst#serial
12+
*
13+
* Connections:
14+
* | esp8266 | nodemcu | rdm6300 | notes |
15+
* |---------+---------+---------|-------------------------------------|
16+
* | GPIO-01 | TX | | This is TXD0, connect it to GPIO-02 |
17+
* | GPIO-02 | D4 | | This is TXD1, connect it to GPIO-01 |
18+
* | GPIO-03 | RX | | Leave it unconnected for flashing |
19+
* | GPIO-13 | D7 | TX | Via voltage divide / level shifter |
20+
* | | VU (5V) | VCC | |
21+
* | GND | GND | GND | |
22+
*
23+
* * GPIO-01 to GPIO-02 is for debug terminal output.
24+
* * You must divide the TX out of the rdm6300 to the 3.3V levels,
25+
* I used the following resistor divider:
26+
* GND--<2K resistor>--GPIO-13=D7--<1K resistor>--TX(rdm6300).
27+
*
28+
* Arad Eizen (https://github.com/arduino12) 08/05/19.
29+
*/
30+
31+
#include <rdm6300.h>
32+
33+
#define RDM6300_RX_PIN 13 // can be only 13 - on esp8266 force hardware uart!
34+
#define READ_LED_PIN 16
35+
36+
Rdm6300 rdm6300;
37+
38+
void setup()
39+
{
40+
/* Serial1 is the debug! remember to bridge GPIO-01 to GPIO-02 */
41+
Serial1.begin(115200);
42+
43+
pinMode(READ_LED_PIN, OUTPUT);
44+
digitalWrite(READ_LED_PIN, LOW);
45+
46+
rdm6300.begin(RDM6300_RX_PIN);
47+
48+
Serial1.println("\nPlace RFID tag near the rdm6300...");
49+
}
50+
51+
void loop()
52+
{
53+
/* if non-zero tag_id, update() returns true- a new tag is near! */
54+
if (rdm6300.update())
55+
Serial1.println(rdm6300.get_tag_id(), HEX);
56+
57+
digitalWrite(READ_LED_PIN, rdm6300.is_tag_near());
58+
59+
delay(10);
60+
}

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Rdm6300 KEYWORD1
1212
# Methods and Functions (KEYWORD2)
1313
#######################################
1414
begin KEYWORD2
15+
end KEYWORD2
1516
update KEYWORD2
1617
get_tag_id KEYWORD2
1718
is_tag_near KEYWORD2

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rdm6300",
3-
"version": "1.0.2",
3+
"version": "1.1.0",
44
"keywords": "rdm6300, rfid",
55
"description": "A simple library to interface with rdm6300 rfid reader.",
66
"repository":

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Rdm6300
2-
version=1.0.2
2+
version=1.1.0
33
author=Arad Eizen
44
maintainer=Arad Eizen <https://github.com/arduino12>
55
sentence=A simple library to interface with rdm6300 rfid reader.

src/rdm6300.cpp

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,40 @@
1010
void Rdm6300::begin(int rx_pin, uint8_t uart_nr)
1111
{
1212
/* init serial port to rdm6300 baud, without TX, and 20ms read timeout */
13-
#ifdef ARDUINO_ARCH_ESP32
14-
_serial = new HardwareSerial(uart_nr);
15-
_serial->begin(RDM6300_BAUDRATE, SERIAL_8N1, rx_pin, -1);
16-
#else
17-
_serial = new SoftwareSerial(rx_pin, -1);
18-
_serial->begin(RDM6300_BAUDRATE);
13+
end();
14+
#if defined(ARDUINO_ARCH_ESP32)
15+
_stream = _hardware_serial = new HardwareSerial(uart_nr);
16+
_hardware_serial->begin(RDM6300_BAUDRATE, SERIAL_8N1, rx_pin, -1);
17+
#elif defined(ARDUINO_ARCH_ESP8266)
18+
if (rx_pin == 13) {
19+
_stream = _hardware_serial = &Serial;
20+
_hardware_serial->begin(RDM6300_BAUDRATE, SERIAL_8N1, SERIAL_RX_ONLY);
21+
if (uart_nr)
22+
_hardware_serial->swap();
23+
}
24+
#endif
25+
#ifdef RDM6300_SOFTWARE_SERIAL
26+
if (!_hardware_serial) {
27+
_stream = _software_serial = new SoftwareSerial(rx_pin, -1);
28+
_software_serial->begin(RDM6300_BAUDRATE);
29+
}
30+
#endif
31+
if (!_stream)
32+
return;
33+
_stream->setTimeout(RDM6300_READ_TIMEOUT);
34+
}
35+
36+
void Rdm6300::end()
37+
{
38+
_stream = NULL;
39+
#ifdef RDM6300_HARDWARE_SERIAL
40+
if (_hardware_serial)
41+
_hardware_serial->end();
42+
#endif
43+
#ifdef RDM6300_SOFTWARE_SERIAL
44+
if (_software_serial)
45+
_software_serial->end();
1946
#endif
20-
_serial->setTimeout(20);
2147
}
2248

2349
bool Rdm6300::update(void)
@@ -26,15 +52,18 @@ bool Rdm6300::update(void)
2652
uint32_t tag_id;
2753
uint8_t checksum;
2854

29-
if (!_serial->available())
55+
if (!_stream)
56+
return false;
57+
58+
if (!_stream->available())
3059
return false;
3160

3261
/* if a packet doesn't begin with the right byte, remove that byte */
33-
if (_serial->peek() != RDM6300_PACKET_BEGIN && _serial->read())
62+
if (_stream->peek() != RDM6300_PACKET_BEGIN && _stream->read())
3463
return false;
3564

3665
/* if read a packet with the wrong size, drop it */
37-
if (RDM6300_PACKET_SIZE != _serial->readBytes(buff, RDM6300_PACKET_SIZE))
66+
if (RDM6300_PACKET_SIZE != _stream->readBytes(buff, RDM6300_PACKET_SIZE))
3867
return false;
3968

4069
/* if a packet doesn't end with the right byte, drop it */

src/rdm6300.h

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,53 @@
11
/*
22
* A simple library to interface with rdm6300 rfid reader.
3-
* Arad Eizen (https://github.com/arduino12) 23/09/18.
3+
*
4+
* Arad Eizen (https://github.com/arduino12) 23/09/18, 08/05/19.
45
*/
56

67
#ifndef _RDM6300_h_
78
#define _RDM6300_h_
89

9-
#ifdef ARDUINO_ARCH_ESP32
10+
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
11+
#define RDM6300_HARDWARE_SERIAL
12+
#endif
13+
14+
#if !defined(ARDUINO_ARCH_ESP32)
15+
#define RDM6300_SOFTWARE_SERIAL
16+
#endif
17+
18+
#ifdef RDM6300_HARDWARE_SERIAL
1019
#include <HardwareSerial.h>
11-
#else
20+
#endif
21+
#ifdef RDM6300_SOFTWARE_SERIAL
1222
#include <SoftwareSerial.h>
1323
#endif
1424

25+
#include <Stream.h>
26+
1527
#define RDM6300_BAUDRATE 9600
1628
#define RDM6300_PACKET_SIZE 14
1729
#define RDM6300_PACKET_BEGIN 0x02
1830
#define RDM6300_PACKET_END 0x03
1931
#define RDM6300_NEXT_READ_MS 220
32+
#define RDM6300_READ_TIMEOUT 20
2033

2134
class Rdm6300
2235
{
2336
public:
2437
void begin(int rxPin, uint8_t uart_nr=1);
38+
void end(void);
2539
bool update(void);
2640
uint32_t get_tag_id(void);
2741
bool is_tag_near(void);
2842
private:
29-
#ifdef ARDUINO_ARCH_ESP32
30-
HardwareSerial * _serial;
31-
#else
32-
SoftwareSerial * _serial;
43+
void _begin(void);
44+
#ifdef RDM6300_HARDWARE_SERIAL
45+
HardwareSerial *_hardware_serial = NULL;
46+
#endif
47+
#ifdef RDM6300_SOFTWARE_SERIAL
48+
SoftwareSerial *_software_serial = NULL;
3349
#endif
50+
Stream *_stream = NULL;
3451
uint32_t _tag_id = 0;
3552
uint32_t _last_tag_id = 0;
3653
uint32_t _next_read_ms = 0;

0 commit comments

Comments
 (0)