
The purpose of this library is to make it easy for the everyday Arduino user working on projects with multiple Arduinos communicating with each other and sharing data. I had many people ask me for the code I used in my PS2X library example that sent PS2 Controller values wirelessly to my SAGAR robot. It got to be tiresome answering the questions and I had an idea to write a library to help the inexperienced with micro controller communications. This is a easy to use and no frills way to send data between two Arduinos.
In most of my own projects I define and write my own NMEA standard communication protocols. This makes communications human readable and easy to debug, but proves wasteful with bandwidth and processing power so it’s not right for every application. Binary communications is much more efficient and versatile, but requires careful handling. This library abstracts the finer points of packetized serial communication away from the user so it easy to use and understand.
To use the library, simple define all the data types you want to share inside a data structure. This keeps all the data stored together in memory.
struct SEND_DATA_STRUCTURE{ //put your variable definitions here for the data you want to send //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO int blinks; int pause; };
When requested, the library will send all the binary data in the structure to another Arduino via serial with a checksum to avoid transfer errors.
void loop(){ //this is how you access the variables. [name of the group].[variable name] mydata.blinks = random(5); mydata.pause = random(5); //send the data ET.sendData(); delay(10000); }
The receiving Arduino will verify the checksum, and copy the new data onto a identical structure in it’s memory.
void loop(){ //check and see if a data packet has come in. if(ET.receiveData()){ //this is how you access the variables. [name of the group].[variable name] //since we have data, we will blink it out. for(int i = mydata.blinks; i>0; i--){ digitalWrite(13, HIGH); delay(mydata.pause * 100); digitalWrite(13, LOW); delay(mydata.pause * 100); } } delay(2500); }
It’s important to make sure the structure is the same on both Arduinos for this to work. Now sharing data between Arduinos is easy without having to define and program your own communications protocol and have to worry about syncing or transmit errors. EasyTransfer will do that for you.
Using Structures to hold the data allows for versatile communications by allowing any type and number of data points to be shared, as long as the whole structure is under 255 bytes. You could define int’s, arrays, longs, etc inside the structure and share the data. It’s also possible to create two way communications by defining two sets of structs on each end and creating two objects using the library. I will have an example of that up shortly.
You can download the library and example below. The example requires two Arduino boards to be connected to each other via their Uarts. One will create two random numbers and send the data to the other that will flash out a sequence of flashes based on those numbers. The first one will also flash out the same sequence.
Now it’s easier to pick which Serial port to use; Serial, Serial1, etc. AND support for the NewSoftSerial library for creating software serial ports on any pin. EasyTransfer is for hardware serial, SoftEasyTransfer is for software; each has there own set of examples. There’s also an example for two way communications using the library.
Source Code available on GitHub project pages.
Or Direct Download
UPDATE
Thanks to Kumy, there’s now an I2C version of EasyTransfer. There’s also an experimental VirtualWire version for use with those cheap low frequency radios. All are in the single download zip file above.
To install the Library, open the zip file and transfer the EasyTransfer folder into your Arduino ‘libraries’ folder. Then follow the examples to add the code to your project. If you have any problems or questions, let me know in the Support Forum.
Need Help?
Please use the Support Forum to ask for help. Don’t use the comments below.
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Warning: Use of undefined constant single - assumed 'single' (this will throw an Error in a future version of PHP) in /homepages/46/d285670699/htdocs/bill/wp-content/themes/wordpress_survive/single.php on line 63
Trackbacks / Pingbacks
Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/46/d285670699/htdocs/bill/wp-includes/comment.php on line 879
443 Comments.
[…] is used to make this process painless. Here is the link to the instruction website we used: http://www.billporter.info/2011/05/30/easytransfer-arduino-library/. Just make sure you download the libabrary files and SAVE them to the “C:Program Files […]
Bill — this library is fanTASTIC. So easy and intuitive to use. For me, this library is a classic example of “standing on the shoulders of giants”.
Thanks again for your work.
Hi,
Thank-you for doing and releasing this library. I’m using it as part of a wireless control system for my model railroad, and I just have one question. Is it possible to have two ‘sets’ of easytransfer objects (i.e. two pairs of easytransfers, 1 send/receive each) sending and receiving over the same serial link with different structs?
For example, I’m automating part of my model railroad, and the Arduino running the sensors, etc. for this will need to communicate with the control system base station, but it will need to transfer a different set of data to the handheld controller.
Cheers and thanks,
Danny
An example of two ET is on Github:
https://github.com/madsci1016/Arduino-EasyTransfer/blob/master/EasyTransfer/Examples/EasyTransfer_2Way_wPot_Example/EasyTransfer_2Way_wPot_Example.pde
An example of two-way ET is on Github:
https://github.com/madsci1016/Arduino-EasyTransfer/blob/master/EasyTransfer/Examples/EasyTransfer_2Way_wPot_Example/EasyTransfer_2Way_wPot_Example.pde
Dave,
Thank-you for the link. I am aware of that example, and used it to get two-way communication going between the handheld controller and the base station. But what I meant was that I need to add a third box, which will transfer a different set of data, and use a different struct to do so.
E.g.
struct data1{
int int1
int int2
int int3
}
struct data2{
int int1
boolean bool1
}
EasyTransfer easy1send;
EasyTransfer easy1receive;
EasyTransfer easy2send;
EasyTransfer easy2receive;
void setup(){
easy1send.begin(details(data1), &Serial);
easy1receove.begin(details(data1), &Serial);
easy2send.begin(details(data2), &Serial);
easy2receive.begin(details(data2), &Serial);
}
So, two completely different structs, four easytransfer objects, but the same serial link (in this case, an Xbee receiving from two different sources). I was wondering if this would work?
I need that too !
I think the best and easiest way to solve this problem is merge two different structure into one structure like this.
struct data1{
int int1
int int2
int int3
boolean bool1
}
So with this arrangement two different source could work into one data structure but different behavior handles data member.
[…] more responsive, the first chip decodes the NMEA data from the GPS, then sends the location via Bill Porter’s EasyTransfer Library to the other […]
Sir I am finding it difficult to pass an char array. Can you give me one example where i can pass an character array
Thanks in advance
Hello, I am wondering if this can be used on the Uno r3 and Mega 2560 r3, and if it would create an issue when i go to reprogram the Uno (if i am using the hardware serial port
Hi, can you read a forum?
i need undestand how calculate Checksum to transmit from pc to arduino
This is a great article, and thanks for the code.
But I have one question…
In the I2C version of EasyTransfer, is there a way for the slave to send data back to the master?
I would like to see the response to this question also!! It may be simple, but I haven’t got it either. I think the issue is that the master is assumed, not defined.
Hello ! Your lib. seams to be exactly what I am looking for and 2 way example is perfect !
Just one question :
In 2 way mode, what happen if some bytes are lost during transfer between the 2 Arduino ?
In other words : what happen if checksum is wrong ?
– message is lost ?
– receiver ask message to b resend by sender until fully ok ?
for more details, working flow between my arduinos is as follow :
Arduino1 -(cmd)-> Arduino2 -(answer)-> Arduino1
(arduino 1 send command to Arduino 2, and Arduino 2 answer to Arduino 1 once job is done.)
My question is just to know if I need to manage some timer on Arduino 1 between “cmd” and “answer”, in case no answer after certain time, mean problem in communication => resend cmd.
Thanks
You have to come up with a protocol if you want to guard against that. Ie once a message has been received, acknowledge the receipt. Or retransmit at a set interval for x times before timing out and giving up.
You don’t want infinite retries or you may end up with a packet storm. Some systems use a somewhat randomized delay before responding. That limits the number of units stomping on each other by accident.
In my implementations I assign a single master that coordinates all communications. That makes stomping much less likely as clients on the bus will only transmit when called upon.
For a more comprehensive system check out radio head at airspayce. They have a library for radio and serial communications that takes care of the details you’re concerned with.
hi,please help me,i cant even load the library into my arduino software,dont know where i made mistake,before this,i had uploaded many libraries,but this time,im having difficulties.Thanks
I have used the fantastic ET library for 3 years and it has been great. The documentation says the structure can accommodate data up to 255 bytes. I have found however that every time I go above 64 bytes that I get erratic results.
This morning I am looking at SoftwareSerial and found this:
” The native serial support happens via a piece of hardware (built into the chip) called a UART. This hardware allows the Atmega chip to receive serial communication even while working on other tasks, as long as there room in the 64 byte serial buffer.”
I am wondering has anyone else had success or difficulty at greater than 64 bytes. Or is there a solution that I don’t know.
Jack,
Can you call the read() function more often? If you call it fast enough, you can avoid the 64 byte limit.
Hi Guys
I am working on a project using easy transfer library, i connected number of internal pullup buttons to an arduino mega, and number of relays to another arduino mega, i should control the relays through the buttons, the two megas are connected by serial connection.All went right except these issues:
– the two boards should be reset at the same time in order for the control to be done.If relay board is reset slightly before the buttons board, all the relays will turn on for a short period and then off and then the system works. If the buttons board is reset slightly before the relays board, the system works without problems. If any of the boards is reset before the other by a long time, no connection is established and nothing works.Please help.
Hey,
At first, you question is not related to the library, its more related to your hardware setup.
resetting Relay Board is definitely not an issue with the lib. In-fact it is not an issue at all. Which relay board do you use? Is your R. Board have PCF#### like I/O expander, then read the datasheet. If you can break com/gnd of your relays at the time of reset, you can avoid that nasty effect of reset. Also try to avoid drawing power out of mega for relay board, just add one additional power supply. I think you are using I2c/SPI relay board which should get only the desire data from arduino and not the power.
All I/O expander I/C will trigger their out ports at every reset cycle. Please refer to datasheet for more details.
great work! esp8266 voltage divider <- arduino 115200 baud, floating point number. very easy to use
Hello!
We used the easy transfer for the communications protocol of our system but I will still need to synchronize it for my MSK IC (CMX469A)
Can I synchronize the data coming out from the arduino’s UART using the falling edge of the TX sync coming from CMX469a?
Thank you very much!
Hello Bill
First off, thank you for the fantastic library. This is exactly what I was looking for.
I wanted to confirm though, it seems that you can only use on variable type in the structure that you are sending?
I tried mixing floats, ints and bytes but then it ended up that I didn’t receive any of my transmitted information.
Only when I changed all the variables to floats did I manage to get the information that I needed.
This is not a problem but I wanted to confirm whether I’m missing something or not as not using floats only will result in a massive space saving when looking at the 250 byte limit.
Thanks again
No you should be able to fix types, something else must be going on.