EasyTransfer Arduino Library

Posted in Arduino Libraries by Bill
30 May 2011
EasyTransfer Arduino Library

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.

 

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

Share

Trackbacks / Pingbacks

  1. Arduino a sériová komunikace | μArt.cz

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
  1. 443 Comments.

    • […] Nedávno jsem narazil na velice zajímavou knihovnu, která ještě více ulehčuje odesílaní dat pomocí rozhraní UART. Jejím autorem je Bill Porter a její zdrojový kód je ke stažení na jeho stránkách. […]

    • ConstantinNo Gravatar says:

      First of all: Congratulations on your method of proposing. What a great way to do it and even better, she said yes. I hope you have many. many happy years together.

      Now to my silly question:

      Is it possible for an Arduino running Easy Transfer (i.e. using the hardware UART pins) to communicate with an Arduino running Soft Easy Transfer (on non-UART pins)?

    • Carlos AsmatNo Gravatar says:

      First of all your library is amazing. Thanks a lot for sharing. I used it for some of my projects and it works great. I love the fact that I can stop reinventing serial data transfer every single time I need it.

      Second, I have been experimenting with your library in order to send strings and I am running onto some issues, basically, it the data-streams get out of sync because of the strings. I guess this is caused by the fact that the string length is not specified on the structure. Any idea on how to solve this?

    • ConstantinNo Gravatar says:

      Hi Bill, here again.

      My previous board was a bit too teensy when it came to ram, so I made a new one using a 1284P MCU (the brains) and a 328P MCU (to measure power). Since the standard Arduino IDE does not support the 1284P out of the box, I downloaded the Brewtroller version of the IDE (based on the Arduino IDE version 22) that includes all required Sanguino cores.

      Here is my issue: While I can program the 328P without any issues, I get compiler errors when I try to compile the SoftEasyTransfer RX program. Here is the quote the compiler spits at me:

      In file included from C:\Documents and Settings\cvonwentzel\My Documents\Downloads\BTArduino-0022\hardware\arduino\cores\sanguino/WString.h:24,
      from C:\Documents and Settings\cvonwentzel\My Documents\Downloads\BTArduino-0022\hardware\arduino\cores\sanguino/Print.h:26,
      from C:\Documents and Settings\cvonwentzel\My Documents\Downloads\BTArduino-0022\libraries\NewSoftSerial\/NewSoftSerial.h:32,
      from C:\Documents and Settings\cvonwentzel\My Documents\Downloads\BTArduino-0022\libraries\NewSoftSerial\NewSoftSerial.cpp:41:
      c:/documents and settings/cvonwentzel/my documents/downloads/btarduino-0022/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:111: error: expected unqualified-id before ‘int’
      c:/documents and settings/cvonwentzel/my documents/downloads/btarduino-0022/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:111: error: expected `)’ before ‘int’
      c:/documents and settings/cvonwentzel/my documents/downloads/btarduino-0022/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:111: error: expected `)’ before ‘int’

      What to do?

      • ConstantinNo Gravatar says:

        … and it only happens with SoftEasyTransfer. The hardware-based version (i.e. Easy Transfer) works just fine – compiling and uploading. I suspect my issue has to do with the NewSoftSerial library?

        Many, many thanks again, Constantin

    • ConstantinNo Gravatar says:

      Great idea. I blanked everything related to SET and still get the same compilation errors. So my guess is that NSS is the culprit. No worries, I’ll simply redesign the board to take advantage of the second hardware serial port that the 1284P offers, hook up the two via 1k resistors, and call it a day.

      Bill, as usual, you reign supreme. Many thanks for the help!

    • chiragNo Gravatar says:

      Hello,

      My project is about data logging. Various sensors connected to my arduino board sense data and store it in EEPROM. I have to use Python code to access this data over serial communication. My communication is working fine , but I want to send data packets instead of only data. Packets will consists of header file and few more type of data as I have 4-5 sensors connected to my board. I used this file to send data packets but it is sending some arbitrary values over serial port.
      Please guide me properly.
      Thank you.

      • RickNo Gravatar says:

        Hi,
        This library is designed to allow two way communication between two ARDUINOS. Not arduino to computer. I think the system uses some BCD to code the number and send it over the serial port. You would have to look at the .h file to see how it is done. Then, you can copy/modify that to the python code.

        • chiragNo Gravatar says:

          Hey thanx Rick……..
          Right now I am not concern about two way communication……….I am just checking data packets to be send over serial……I have used this library bt I am getting packets in the form of some characters ……..
          May be I will try to modify .h file……
          thankss

          • RickNo Gravatar says:

            is it arduino to computer right?
            Your terminal might not be setup for reading this charters.

            • chiragNo Gravatar says:

              So what is the best possible way to attempt this data packet communication………?

            • BillNo Gravatar says:

              chirag,

              Communications between devices is hard and complicated for beginners. That’s why I wrote this library to simplify communications between two Arduinos. But you are trying to do communications between an Arduino and processing. In order for use of my library to work, someone would have to re-write my library in processing to make sense of the weird characters you see in your terminal window. There’s a lot involved, things like bit order, data types, sync bytes, checksums etc that all need to agree between two systems to properly relay information.

              I don’t have experience with processing, so I haven’t looked into writing a processing library for EasyTransfer, but it is on my to-do list. If you have experience with Processing, you might be able to port the library code over. The things you would need to check if both systems use the same byte order (Big or Little Endian) and store data the same way (How many bytes in a Long, Int, etc). If they are identical, then it’s just a matter of porting the code to processing.

            • BillNo Gravatar says:

              Oops, I just read you are using python, not processing. Same still applies though.

          • BillNo Gravatar says:

            Easytransfer uses binary communications so it will look like wierd characters in a terminal window. You need to write a processing sketch that will read the binary communications.

            • chiragNo Gravatar says:

              yeah exactly…….I am getting all weired characters on my terminal window……..

            • chiragNo Gravatar says:

              Yes you are right bill……..I am using python……I am able to send data from arduino over serial and python code can easily access it………but right nw my intention is to send data packets from my arduino EEPROM to serial COM port……..At this point i am not worry abt how python will read it………May be I am not good in data pakets concept……….hence I am still struggling for that………
              I jst want to send data packet in terms of header,length of data,data,checksum byte……..(my data is time and corresponding sensor reading)
              Please advice something towards it………..
              Thanks.

            • BillNo Gravatar says:

              chirag,

              Computers store everything in binary. So the number 3 is stored as 00000011. My library sends packets in their original binary form. so it sends 00000011 just as it is. Your terminal window is expecting to see the number 3 in ASCII form, or 00110011. But it’s not getting that, it’s getting 3 in binary when it’s expecting ASCII so it displays weird characters. This is the difference between ASCII and binary communications. This is the first challenge to communications.

    • chiragNo Gravatar says:

      Please guys……..somebody help me to sort out the problem…..

    • Jupiter MollNo Gravatar says:

      Hello ….
      first thx for sharing your ideas…
      I have a question ?

      Which Pins you connect between the two arduinos for one way communication?

      (arduino 1) tx >> rx (arduino2) ???

    • ConstantinNo Gravatar says:

      Bill,
      Back again… like a bad piece of fashion. I am tearing my hair out on EasyTransfer, this time between a 328P and a 1284p. On the 328, I’m using the only serial port available, the 1284P uses serial 1, i.e. pins 11 and 12.

      I tried using the examples (i.e. blink) and I get no response from the 1284p. I know that the 328 has its rx/tx wires correct since I can correspond via the serial monitor with the unit. I also made sure that the RX of the 328 is wired to the TX1 pin of the 1284P and the TX pin to the RX1 pin. What else can I be checking to see why the blink example won’t work?

      • BillNo Gravatar says:

        Hey Constantin.

        Have you confirmed the wiring and serial port setup by trying example code (NOT my library, just regular Serial.print and serial.read commands) that sends one byte (like the letter ‘G’) across and if it is received properly? Just a sanity check before we start messing with library code.

        • ConstantinNo Gravatar says:

          Hi, this is Mr. Mud, aka egg-on-face, aka.. oh you get the picture.

          Anyhow, the issue related to the Serial port not being initiated. Since I used Serial Port 1 on the 1284P (as opposed to the master serial port), I needed to declare a separate Serial.begin command for Serial1 (i.e. Serial1.begin(speed). For some silly reason, I thought EasyTransfer took care of that for me!

          Oh dear…

          • BillNo Gravatar says:

            Lol, no, you have to start the serial on your own. I’d have to add another variable (Serial speed) to EasyTransfer’s begin function if i had it start the serial port. Figured either way it had to get done, and I wanted to abstract from the actual hardware as much as I could.

            • ConstantinNo Gravatar says:

              I completely agree with your approach. It makes sense for all the right reasons and my expectation didn’t make any sense at all. The good fortune of using the mega Serial example saved me in this particular instance from more hair pulling and yet another PCB revision.

              Now for a stickier wicket, i.e. going after SD card init errors…

    • DanNo Gravatar says:

      Cant compile examples into SoftEasyTransfer.
      I get this error:
      SoftEasyTransfer.cpp:34: error: within this context

      IDE 22 (sketch modified for the 22).

      EasyTransfer compiles well.

      How to fix?

    • KumyNo Gravatar says:

      Hi,

      Do you think your library could be hacked to communicate via I2C (http://www.arduino.cc/en/Reference/Wire) instead of Uarts ?

      Else, do you know a library that can send structs over I2C like you do here ?

      Bests

    • AndrewNo Gravatar says:

      First off, great work man, I really appreciate all the help it has gotten me. But on the flipside, I am asking for a little guidance, because I am working on a project with a very limited budget and not a lot of super programming skills involving one arduino board, an rc car, and a ps2 controller. Any way you can help would be greatly appreciated, Andrew Clark

    • coopermaaNo Gravatar says:

      Hi,

      I’m using Arduino 1.0, while running EasyTransferI2C examples, the compiler complains:

      error: ‘class TwoWire’ has no member named ‘receive’
      error: ‘class TwoWire’ has no member named ‘send’

      I think it is beacuase send() and receive() have been replaced with read() and write() in Arduino 1.0. Just FYI.

    • coopermaaNo Gravatar says:

      Hi bill,

      I am trying the EasyTransferI2C TX/RX example.
      The example does’t behave as expected. The RX side (slave) didn’t blink normally.

      Because the receive() function is called from an interrupt function, so I think the processing time inside receive() function can’t be too long.

      • BillNo Gravatar says:

        Hi coopermaa,

        The I2C version of the library was written by someone else and I haven’t tested it other then it did compile for me using Arduino 0022. I also have not tested it on the hardware.

        But it sounds like you got it to compile, but are getting odd results? Does the Receiving arduino blink at all?

    • coopermaaNo Gravatar says:

      Yes, I got it compiled using Arduino 1.0. What I did is to change all _serial->send() calls to _serial->write() and _serial->receive() calls to _serial->read() in EasyTransferI2C.cpp

      But, this will make it not compatible with Arduino-0022 or earlier version. May I should use conditional compile to make it compatible with old Arduino IDE. Like this:

      #if ARDUINO >= 100
      _serial->write(…);
      #else
      _serial->send(…);
      #endif

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.