Uploading sketches via XBee doesn't work

Since I'm using XBee for wireless data-transfer, I thought I might as well use it to upload sketches. However, that doesn't really work out:

avrdude: stk500_getsync(): not in sync: resp=0x00
avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x51

I'm using a Baudrate of 57600 and this code to reset my Arduino remotely: XBee Programming Arduino Wirelessly

I also tried manually resetting my Arduino before uploading, but that didn't work either.

Any ideas?

I think it updates the sketch on a set baud rate what ever you set the serial monitor to.

I cant remember if its 9600 , or the next faster.
So the xbees will have to be set to that baud rate.

Serial.begin(19200); // start serial at 19200 baud, same as programmer speed

this may be a clue to the baud rate you need to set it too.

Well, I set the XBees as well as the serial monitor to 57600 baud, and that works without any problem for transmitting data. (Say, for logging the resistance of my bend sensor at a given angle)

Only when I'm trying to upload a sketch, it won't synch. Before using 57600 baud, I also tried 9600 and 19200 baud, neither of which worked.

I`ve got a BBB and thats the only way I cand do update it.
But I have to use 19200 baud, and a finger on the reset button.
AND a bit of luck with timeing , because you have to press the button at the right time.

Could you tell me at which point you hit the reset button? I never seemed to get it quite right :-/

Hi Mononofu,

the timing for resetting the Arduino is very critical because the wait time in the bootloader is really short (2s ?). When you don't start avrdude during this time, your sketch will start again.

The right way would be to tell the bootloader via an EEPROM variable, that it shall endlessly wait for a new sketch and not time out and start the existing sketch. This would involve tweaking the bootloader.

Another idea: At the beginning of your sketch, you could check if your sketch has triggered a reset and do reset the Arduino again when no sketch has been uploaded.

This could be implemented using the following variables:

#define RESET_SIGNATURE (0xE79BF1A2UL)
unsigned long loopResetSignature;
int loopResetCounter;

As long as loopResetSignature contains a predefined value and the loop counter is greater than 0, your sketch will immediately reset the ATmega.

The variables shall not be initialized (then they will be placed in the BSS section) and be defined at the very end of your other global variables, so the chance that they are overwrittten by the bootloader is as small as possible. Maybe this doesn't work at all and you have to use the EEPROM instead of RAM.

At the beginning of your sketch in setup():

SoftReset(0); // check if the reset loop is active

Some helper functions:

void StartResetLoop(void)
{
  loopResetCounter = 20; // tune this to get an acceptable timing
  loopResetSignature = RESET_SIGNATURE;
  SoftReset();
}

// keep resetting the ATmega unless we reached the number of reset loops
// should actually exit when a new sketch has been loaded, but
// how can this be detected?
void CheckResetLoop(void)
{
  if(loopResetSignature == RESET_SIGNATURE)
  {
    if(loopResetCounter > 0)
    {
      loopResetCounter--;
      SoftReset();
    }
    else
    {
      loopResetCounter = 0;
      loopResetSignature = 0;
    }
  }
}

void SoftReset()
{
  // here your normal reset code
  ...
}

And where you want to enter the reset loop:
[code]StartResetLoop();

According to Atmel documents, resetting the ATmega shall not be done using one of its PIO pins, because the PIO signal will go to tristate immidiately when the reset starts. To reliably resetting the ATmega, the reset signal must be kept below 1V (0.2*Vcc) for at least 2.5us. To be honest, I don't think this is your problem. Normally the WatchDogTimer shall be used for software reset, but this is not supported with the standard Arduino bootloader.

MikeT

Thank you very much for this detail information, but I never manage to get the timing quite right. Maybe this has something to do with me using the Arduino GUI - you mentioned using avrdude, am I supposed to use it directly from a terminal?

Imho, if I upload a sketch via USB, my Arduino is reset automatically, the same should be possible via XBee according to this webiste:
http://www.ladyada.net/make/xbee/arduino.html

However, I don't manage to get any voltage change on the configured pin on my remote XBee - do I have to use that FTDI cable or should it also work with the USB adapter from Sparkfun? (SparkFun XBee Explorer USB - WRL-11812 - SparkFun Electronics)

You can also try this method for remote sketch upload:
http://www.ladyada.net/make/xbee/arduino.html

Hi Mononofu,

Thank you very much for this detail information, but I never manage to get the timing quite right.

That was the reason for my suggestion in my previous posting. Instead of changing the bootloader timeout, I reset the ATmega some times so you have n times the timeout which should be enough to do the download via the Arduino IDE.

Maybe this has something to do with me using the Arduino GUI - you mentioned using avrdude, am I supposed to use it directly from a terminal?

When you call it directly, you have some more control about the timing, because when you use the IDE, it always compiles the sketch before the download.

Imho, if I upload a sketch via USB, my Arduino is reset automatically, the same should be possible via XBee according to this webiste:
Xbee Adapter - wireless Arduino programming

Sure you can do this, just make sure that you don't set the RTS line when you don't want to load a new sketch, otherwise it will also do a reset.

However, I don't manage to get any voltage change on the configured pin on my remote XBee - do I have to use that FTDI cable or should it also work with the USB adapter from Sparkfun? (SparkFun XBee Explorer USB - WRL-11812 - SparkFun Electronics)

The USB adapter is absolutely ok. You probably don't have a hardware problem but a software problem. Have you checked, that you have a voltage change on the locally connected XBee / USB Adapter? Have you followed the instructions to configure the local and remote XBee?

You might want to use the DTR pin instead of DTR. The Arduino version of avrdude sets DTR to high before uploading the sketch, but ony for the Windows version. On linux you need a small script which you can find in the forum.

MikeT

You might find something helpful here as well:

http://www.sparkfun.com/commerce/tutorial_info.php?tutorials_id=122