SparkFun_ESP8266_AT_Arduino_Library icon indicating copy to clipboard operation
SparkFun_ESP8266_AT_Arduino_Library copied to clipboard

Don't call Serial.begin()

Open matthijskooijman opened this issue 8 years ago • 3 comments

I noticed that right now, the begin() method accepts an enumeration to switch between the primary serial port and softwareserial, and begin() also calls the begin() method on the chosen serial object. This approach is very inflexible, since:

  • It doesn't allow use of other serial ports (like Serial1, AltSoftSerial, etc.)
  • It requires SoftwareSerial to always be linked in, even if it is unused (which hijacks the PCINT ISRs on AVR).

Instead, it would be better to just let begin() accept a Stream& reference, and expect the sketch to call begin() on that and specify the baudrate. For example, the xbee-arduino library also uses this approach: https://github.com/andrewrapp/xbee-arduino/blob/master/XBee.cpp#L786-L788 https://github.com/andrewrapp/xbee-arduino/blob/master/examples/AtCommand/AtCommand.pde#L54-L55

With this library still being new, hopefully you'll consider this change now, before it would break too many existing sketches.

matthijskooijman avatar Jul 25 '15 10:07 matthijskooijman

I would support this idea because I want to use an Mega2560 and so my idea is to connect the PINs 0 and 1 not to the board, but to other available HW-Serial-PIns (like Serial1).ö I would also do this change and send oull-request if you are interested in!

Apollon77 avatar Oct 06 '15 18:10 Apollon77

I am not sure if I understood your question correctly. However, it seems like you might be referring to the examples that require both the hardware serial ports and the software serial pins to interact with the shield. The hardware pins allow your computer to talk to the microcontroller and software serial pins allow your microcontroller to interact with the ESP8266 shield on a separate serial port. Without which, it would create collisions between the two on the UART hardware pins.

Otherwise, you should be able to exclude the software serial library and communicate from the microcontroller to the ESP8266 using the hardware pins. You just need to specify which port to use in the begin statement as written in the source code.

esp8266.begin(9600, ESP8266_HARDWARE_SERIAL);

WARNING: If you try to program your microcontroller and the shield switch is in the hardware position, you can brick the ESP8266. The stream of data that is meant for programming the microcontroller on the hardware pins can create random AT commands and lock up or break the ESP8266. This is why it is usually a best practice to use separate serial ports.

santaimpersonator avatar Feb 08 '19 21:02 santaimpersonator

You may want to look at this pull reques too: https://github.com/sparkfun/SparkFun_ESP8266_AT_Arduino_Library/pull/10

santaimpersonator avatar Feb 08 '19 22:02 santaimpersonator