I recently came across a challenge while working with an Arduino serial interface. I suspect many others have also encountered the same issue. And the solution could save you precious time. So here it is…
After playing with an Arduino for a while, collecting sensor data, the logical next step is to add a method of saving this data for future consumption.
One option is to add an SD card or some similar storage device to your Arduino circuit. That would protect the data from system crashes. But there is a shortcoming with this approach…
You cannot get access to the data unless the Arduino is running, and you are located in the same area as the system. This approach, if relied solely for data storage, keeps your system isolated, Not exactly an IoT solution.
Saving the data somewhere independent of the hardware that first acquired the information makes more sense. In the cloud if possible.
Many turn to ThingSpeak. This free service supports up to 8 updates at a time. The API uses a single http “GET” request for this purpose. The strings are typically 100-150 characters long, data dependent of course.
No problem, right? Since the Arduino often uses a serial to WIFI device (like an ESP8266), all you need to do is write the string to a serial port. A very simple one-line sketch command.
Yet there lies the essence of the problem….
Really? What is so hard about writing a string to a serial port? Well, here is the rub…
You see, the Arduino Serial port buffers only hold up to 64 bytes by default. Try to send a string longer than 64 bytes and it will be truncated. Cut off at the buffer size. Not exactly what you want.
I’ve run into this and found that there are two different solutions. The one to use depends on whether you are using a hardware or software serial port.
It is important to note that these changes are at the Arduino core level. This means that every serial port used in all your projects are effected by this change. These minor file changes need to be undone for projects that work just fine with the default 64 byte buffer size.
The reason you may not want to expand the buffer all the time is that the buffer consumes valuable Arduino RAM. An ATmega328 based Arduino, typical in many models, only has 2048 bytes of run-time RAM available. With two separate serial port buffers (on for Tx, one for Rx), changing the buffers from 64 to 256 bytes increases the RAM requirements from 128 bytes to 512 bytes. That is a full 25% of the available RAM for your entire sketch.
The Arduino Mega is the model of choice if extra RAM is important. This beast sports the ATmega2560 chip which provides 8192 bytes of RAM and 4 hardware serial ports.
Software Serial Buffer Expansion
The change for software serial ports require a simple modification of the file:
<base Arduino folder>\hardware\arduino\avr\libraries\SoftwareSerial\SoftwareSerial.h
__#define _SS_MAX_RX_BUFF 64 // RX buffer size
__#define _SS_MAX_RX_BUFF 256 // RX buffer size
Hardware Serial Buffer Expansion
The change for hardware serial ports require a simple modification of the file:
<base Arduino folder>\hardware\arduino\avr\cores\arduino\HardwareSerial.h
__#define SERIAL_TX_BUFFER_SIZE 64
__#define SERIAL_RX_BUFFER_SIZE 64
__#define SERIAL_TX_BUFFER_SIZE 256
__#define SERIAL_RX_BUFFER_SIZE 256
I hope this information proves to be useful….