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. Sector67 ::: Madison - Wisconsin ::: Blog

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.

    • stingNo Gravatar says:

      I have the EasyTransfer library working on a project I am building with Software Serial. I am using it to connect my main project board to an SD Logger and the SD logger and Software serial use all available memory. I have a couple of small structures I am trying to receive with this arrangement.

      So, now I have to consider using the hardware UART and wonder if there is a conflict with uploading to the arduino? My arduino’s are all built from parts, for my project boards. So it isn’t easy to prototype.

    • Michael PeñalosaNo Gravatar says:

      I used the examples included in the library. the one that has a potentiometer and servo. I compiled and uploaded it in my arduino successfully. But, whenever i turn the pot, nothing happens.I revised the code of the receiver arduino(the one with servo attached) to print the incoming values from the sender arduino. The serial monitor prints the value from the pot in the sender arduino but i found out that the incoming values didn’t show correctly. the code is okay but the serial monitor prints the value twice but it is correct. its like this:

      123 123 80 80 90 90 45 45

      (the values didn’t have a space between them actually)

      what can i do?

      i didn’t delete anything from the code except for replacing the myservo.write() for serial.print().

      Thanks,
      Michael

      • BillNo Gravatar says:

        Are you still having problems? There’s something very wrong with your setup, that’s no where near what you should be seeing. Perhaps the baud rate isn’t set right?

        • Michael PeñalosaNo Gravatar says:

          the baud rate is set to 115200. the baud rate is just fine for the 2 bluetooth shields. I’ll try to hook them up with just to wires and see if the baud rate is the problem…

    • MattNo Gravatar says:

      I am just getting familiar with arduino and am having trouble uploading the example of easy transfer. It gives me the following error when I try to upload it. Any suggestion.

      EasyTransfer_2Way_wPot_Example.cpp: In function ‘void setup()’:
      EasyTransfer_2Way_wPot_Example.pde:-1: error: no matching function for call to ‘EasyTransfer::begin(byte*, unsigned int, usb_serial_class*)’
      C:\Users\Matt\Documents\arduino-1.0\libraries\EasyTransfer/EasyTransfer.h:46: note: candidates are: void EasyTransfer::begin(uint8_t*, uint8_t, HardwareSerial*)
      EasyTransfer_2Way_wPot_Example.pde:-1: error: no matching function for call to ‘EasyTransfer::begin(byte*, unsigned int, usb_serial_class*)’
      C:\Users\Matt\Documents\arduino-1.0\libraries\EasyTransfer/EasyTransfer.h:46: note: candidates are: void EasyTransfer::begin(uint8_t*, uint8_t, HardwareSerial*)

    • MattNo Gravatar says:

      I think it is because I am using a Teensy Board instead of an Arduino. I had a similar problem with a library before but forget how I fixed it.

      • Jerry IsdaleNo Gravatar says:

        Teensy needs you to use particular pins that are not where you expect from UNO experience. My Teensy code used …

        EasyTransfer controlEZXfer;

        HardwareSerial Uart = HardwareSerial();

        Uart.begin(19200);

        controlEZXfer.begin(details(newGlowegoControlData), (&Uart));

        The uart RX/TX is pins 7/8 on Teensy 2.0 (see pinout diagrams on pjrc site).

        However, your errors look like the IDE is not finding the library. Or some other weird IDE issue – I have had a bunch. Be sure it is in the right place, and you restart after installing.

    • Bill,

      You had asked to see my project when it was finished.
      You can check it out here: http://poisson.me.dal.ca/~dp_11_10/

      Thanks again for the use of the library!

      Cheers,
      Jeremy

    • ConstantinNo Gravatar says:

      Hi Bill,
      Did any of your users ever share a successful RS485 implementation with you? I am getting anything but proper behavior, hence my question…

      • BillNo Gravatar says:

        Since 485 is half duplex over a shared channel, most driver chips echo back everything the Arduino sends out. Are you trying to do two way comms?

        • ConstantinNo Gravatar says:

          For now, I am just trying one-way communication. That is, pull DE/RE high on one chip, TX, while the other chip has the DE/RE on the RS485 chip pulled low and RX’ing in a endless serial.read loop.

          I am getting something, but it’s not what I sent. For example, if I send an ASCII ‘A’, I get À instead. Send ‘B’ and I get Á instead. The numerical values are 192 and 193, respectively. I double-checked the electrical connections, they are OK. The terminating resistor is 180 Ohm, not 120, but I doubt that makes a huge difference?

          Just pulling my hair out, as usual.

    • Jupiter MollNo Gravatar says:

      I try yesterday to communicate an Arduino UNO (transmitter) and a Arduino Mega2560(Reciver)via the SoftEasyTransfer examples , but it dont work? Any idea? THX.

    • buycrisNo Gravatar says:

      Hello there,

      Just been pointed this direction from the Ard. forum. Looks like a great lib and hopefully it will work for me. I will be using 3x standalone ATMega 328’s, one master two slaves to operate stepper motors via the i2c bus. The stepper library is found here: http://www.open.com.au/mikem/arduino/AccelStepper/classAccelStepper.html#a344f58fef8cc34ac5aa75ba4b665d21c
      The function runToPosition(), which I will be using ‘blocks’ and is suggested not to be used in event loops. Is there some way for the master to know when the slave is ready to receive the next event or will this cause me issues.

      Thanks

    • […] and an Arduino + Motor Shield. The two Arduinos communicate via Bill Porter’s excellent EasyTransfer library. The pointer starts moving when the audio playback begins, and when the playback ends, the pointer […]

    • Ryan CannonNo Gravatar says:

      I keep getting: “avrdude: stk500_getsync(): not in sync: resp=0x00”

      whenever I try to upload one of the sketches, I have the arduinos both hooked up as in the UARTS_wiring image.

      I also am wondering, should both arduinos turn on when only one of them is connected?

      I can get the sketches to upload properly by disconnecting each arduino, but when I hook them up together nothing happens on the receiving end. (with or without its own power source)

      • Ryan CannonNo Gravatar says:

        Okay, I got them to start communicating.

        Just needed to be reset after hooking them together.

        Thanks for the library.

      • BillNo Gravatar says:

        You cannot upload code or connect to the Arduino via USB at all if you are using the hardware serial port (pins 0 and 1) to connect to another device like another Arduino. You can plug in the USB for power but not for serial communications with the Arduino. That’s why the example code blinks the light on pin 13 to show you it’s working. Don’t forget, if one Arduino has it’s own power, they still need to share a ground connection.

    • MichaelNo Gravatar says:

      Hi Bill,

      will this library work in communicating arduino mega and arduino uno?

      Thanks,
      Michael

    • Jerry BranchNo Gravatar says:

      missing from the top of my code
      try again.
      jerryb

      #include

      #include

      or

      #include less than sign SoftEasyTransfer.h more than sign

      #include less than sign SoftwareSerial.h more than sign

    • amr hamedNo Gravatar says:

      Hi Porter i need to ask about ( EasyTransfer Library ) in case of ( EasyTransfer_2way_wservo_Example ) which type of buttons we should use & i need to confirm that i will connect the button with arduino GND & pin 12

    • paksoftNo Gravatar says:

      Hi dude, what does “details” mean in the following code? Could you please point me where I can get reference about it?

      void setup(){
      Serial.begin(9600);
      ETin.begin(details(rxdata), &Serial);
      ETout.begin(details(txdata), &Serial);

      pinMode(13, OUTPUT);
      pinMode(12, INPUT);
      //enable pull-up
      digitalWrite(12, HIGH);

      }

      • amr hamedNo Gravatar says:

        hello Paksoft i need to control 2 servos wireless with 2 potentiometers without any buttons so i have modified the code but i need u to confirm it

        #include
        #include

        EasyTransfer ETin, ETout;

        Servo myservo ;
        Servo myservo2 ;

        struct RECEIVE_DATA_STRUCTURE{

        int servoval;
        int servoval2;

        };

        struct SEND_DATA_STRUCTURE{

        int servoval;
        int servoval2;
        };

        RECEIVE_DATA_STRUCTURE rxdata;
        SEND_DATA_STRUCTURE txdata;

        void setup(){
        Serial.begin(9600);
        ETin.begin(details(rxdata), &Serial);
        ETout.begin(details(txdata), &Serial);

        myservo.attach(9);
        myservo2.attach(10);
        }

        void loop(){

        txdata.servoval = analogRead(0);
        txdata.servoval2 = analogRead(1);

        ETout.sendData();

        for(int i=0; i<5; i++){

        ETin.receiveData();

        myservo.write(map(rxdata.servoval, 0, 1023, 0, 179));
        myservo2.write(map(rxdata.servoval2, 0, 1023, 0, 179));

        delay(10);
        }

        delay(10);
        }

      • BillNo Gravatar says:

        My library needs to know where the structure is stored in memory and how much memory it takes up. Details is a easy to read pre-processor macro that extracts those two pieces of information. You will find it’s definition in the .h file.

        • amr hamedNo Gravatar says:

          Hello Bill thanks 4 reply & interest yes it’s working but , i need to ask is this code can work as a serial link between 2 arduinos & 2 Xbees

          sincerely – amr

    • amr hamedNo Gravatar says:

      forgot that

      #include
      #include

Leave a Reply

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