不管是 UNO 還是 Nano, 它們都只有一組 UART 串列埠 TX0 與 RX0, 而且與 USB 是共接的, 亦即如果將 Arduino 的 USB 接到電腦用來上傳程式與透過 Serial.print() 來除錯與追蹤執行結果, 那麼串列埠 TX0/RX0 就不能同時接到 ESP8266 來傳送 AT 指令與接收回應.
解決之道是使用 SoftwareSerial 函式庫來將一般 DIO 針腳模擬成軟體串列埠, 參考小狐狸事務所寫的 UART 串列埠測試 :
# Arduino 串列埠測試 (UART)
但必須注意, SoftSerial not working above 19200 baud.
因此, Arduino UNO+ESP8266+SoftwareSerial 搭配使用時, Baud rate 建議使用 9600 bps.
Serial Port Options
- HardwareSerial - Best performance. Always use this first, if available! Teensy and Teensy++ have one HardwareSerial port which is available (not used for uploading or the Arduino Serial Monitor). Arduino Mega 2560 has 3 extra HardwareSerial ports. Arduino Uno has none.
- AltSoftSerial - Can simultaneously transmit and receive. Minimal interference with simultaneous use of HardwareSerial and other libraries. Consumes a 16 bit timer (and will not work with any libraries which need that timer) and disables some PWM pins. Can be sensitive to interrupt usage by other libraries.
- SoftwareSerial(formerly "NewSoftSerial") - Can have multiple instances on almost any pins, but only 1 can be active at a time. Can not simultaneously transmit and receive. Can interfere with other libraries or HardwareSerial if used at slower baud rates. Can be sensitive to interrupt usage by other libraries.
- Old SoftwareSerial (SoftwareSerial in Arduino 0023 & earlier) - Very poor performance.
- If using multiple software serial ports, only one can receive data at a time.
- Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).
- Not all pins on the Leonardo and Micro support change interrupts, so only the following can be used for RX: 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
- On Arduino or Genuino 101 the current maximum RX speed is 57600bps
- On Arduino or Genuino 101 RX doesn't work on Pin 13
The library has the following known limitations:
If your project requires simultaneous data flows, see Paul Stoffregen's AltSoftSerial library. AltSoftSerial overcomes a number of other issues with the core SoftwareSerial, but has it's own limitations. Refer to the AltSoftSerial site for more information.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.