
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 there any way to use EasyTransfer to communicate to a PC or similar using processing or something like that?
Where there’s a will there’s a way. My library could be ported to processing to enable comms with a PC, but no one has done it yet. I have a wedding in the works and don’t know processing so it wouldn’t be me for a long time. Hopefully someone else can step up.
Reading this, I initially thought – gee a PC port would be cool, then I thought about it a bit more and wondered:
a) what i/o port on a PC would you use?
most pc’s have USB or bluetooth and little else these days
b) as long as its an intel pc you dont have to worry about Endian issues – i think.
c) you will have to worry about structure packing and data alignment issues.
So the question would turn more on – could you port this to run over USB? probably. Under the hood it is basically passing a binary structure, without packing or data alignment padding. You could do that over USB, or bluetooth.
Jerry,
I looked into it a while back. I assumed a serial port (over USB or otherwise) would be the low layer connection. Endian/data type issues do exist with a native windows program, but Processing uses their own scheme that happens to match with a Arduino perfectly. (You’d think Arduino was based off Processing or something. (Bad joke, it is)).
It shouldn’t need any more effort then just changing API calls to match Processing. I just have no time to try it.
Thanks guys, anyone know processing to give it a go? I haven’t used it yet just knew that android was based on processing platform so figured they’d be close in that regard.
Another question, could you send more than one struct in your message? Or could you use two ET instances to send two structs?
Cheers Bill! And cudo’s once again on an excellent library. PS did you get any traction on the ET over Ethernet/web work? There is a chunk of beer money in it for you if you do 😛
I can use the easytransfer over rs485 without data loss? Sorry my english google translate.
Hey Mate, yea that’s how I use it 🙂
Great library, but I’m running into a problem. I have a structure with 7 integers which are reflecting the state of 7 buttons on a remote Leonardo. I have a serial print running listing the values of the data structure on the local Leonardo and I am getting random values every once in a while.
I have tried varying the baud rates, the number of times I query the receive data structure, the time delay between reads, etc.
Basically, the goal of this is to add I/Os to the local Leonardo so I can’t slow it down too much or I lose responsiveness.
Any ideas?
Thanks
None. Did the sample code work without corruption? Do you have a way of eavesdropping in on the communications between the two? Checksums should catch bad packets. There may be a memory leak in your code that’s writing something to the area in memory occupied by your data struct.
Bill, thanks for a great library, it solved my Arduino/ Nanode serial problem perfectly 😉
Hi,
I need to transfer some variables(3bytes of data) over the XBee connected to Fio on one side and XBee connected to Uno on the other side.
I have tried the EasyTransfer .But this has quite long response for my 4WD robot.
So I have written my code to send the variables.It is sending the Start byte(255) the ID byte(to identify which data I am sending) 3 bytes of data(3 variables 0-255) and two bytes of some check if the data was recieved correctly. It is working but only when I send this packet one per +-100ms which is even worse than the EasyTransfer.
What could be the problem?
Hardware:
Arduino Uno
Arduino Fio
XBee S2 with XB24-ZB firmware(Coordinator and end device)
Software:
[removed]
I hope someone will find where is the problem.Thank you very much for every input
EasyTransfer does not have much overhead and should not delay messages to your robot. If you perceived a delay, then you did not follow the instructions in the example. You probably didn’t have the receiver process messages 2x faster then the transmitter sent them. This cause the messages to stack up and make it look like the system has a delay.
I’ll help you get EasyTransfer working but I don’t have the time to help you write your own version from the ground up.
I have had serial transfer problems that seem to be timing issues that get better when I cut down on the terminal print statements. See what happens if you do internal data check and print only 1 – ok, 0 – bad, or switch an LED to get rid of all print statements.
I have tried that and same problem.I have changed the datatypes from integers to bytes and Serial.begin at 115200 but still I can get just to 45ms at delays.It is still to much i think that just 5 max 10ms must be enough at 9600 when I am sending just 7 bytes..
Commercial RC control has and update rate of 50Hz or every 20ms. Realistically though there is some delay added on top of that and it could be around 30ms for most brands. Why on earth do you need 10ms or less delay? Remember both processors on either end must stop, assemble the data, transmit it; receive it, finish other code before processing what was received, then process what was received. Baud rate plays a minor role in what makes up the start to finish delay.
Also, remove the Xbee’s from the system while testing. Xbees themselves add a delay to the messages.
Yes I know that 30ms is good enough but when I try in that code to set: delay(z); and on the RX side I print just the recieved z variable it looks like this:…254-255-0-1-2-3…. so It must be possible to send data much faster.It is just driving me crazy that when I set the delay manually to 15ms it is not working but when it counts from zero to 255 it is working..
[…] See on http://www.billporter.info […]
Hey how would we go about using Easy Transfer with the Wireless RF24L01SE+ Chips?
Any pointers as to how would you create two way communication by using the EasyTranferI2C library?
I do not seem to be able to come up with anything…
Hi Bill et al, This is just what I have been looking for – thank you 🙂 I have some tests up and running nicely but one thing I would like to do is decode the packet that I can see coming over serial into a human readable format – any ideas how I do this? Thanks.
That depends on what is listening to the data stream. Another Arduino is easy, use easytransfer to receive the data and print it out on another serial port. A pc is a different story because easytransfer sends the data I am the same way the Arduino saves the data in memory. So to be able to decode it, you have to know how the Arduino and avr saves data.
Thanks Bill, Indeed hoping to read the packet over a PC serial port (Arduino to Arduino works a treat) as the third part of the system…. any clues on how to decode this appreciated 🙂
Hello again Bill, I think some of the packet structure is worked out, hoping you might be able to demystify the header and terminator? So for example my packet 06 85 0c 00 00 A4 41 BA 5B 68 42 56 91 3f 41 9b has the following structure?
Header: 06 85 0C
Float 1: 00 00 A4 41
Float 2: BA 5B 68 42
Float 3: 56 91 3F 41
Terminate: 9B
The header value doesn’t change but the terminator does so suspect some sort of checksum?
Any pointers and explanation welcome 🙂
Hello Bill, thanks for your library, it’s really useful. I’m wondering that whether this library can do three way communication or not? Is it possible for me to create a packet in order to prevent distortion ?Thanks again.
I want to create a network with more than 2 arduinos, so I will have one master and two or more slaves, can I use the easyTransfer library? Is there any way to create a filter in the slaves so that they can read only when they are addressed from the master?
Looking to use easytransfer to connect Ardunio to Android. Outline of ideas to try please. I have data on the ARduino that I want to display on the Android via an app.