Hello All,
I'm working on a solution where I can update the code on an Arduino Nano via a cheap JDY-31 Bluetooth dongle.
- The Arduino RX0 TX1 is connected to the JDY-31's TX and RX, using voltage divider for RX so the 5V will not harm the HC06 RX that is 3.3V capable
- The communication works smooth using my phone or my PC
- Since the boot loader on the Arduino use 115200 baudrate, I set up the JDY-31 to use that baud rate (by AT+BAUD8\r\n command)
- I can upload sketch via Bluetooth from my PC. I paired the PC's Bluetooth device with JDY-31 and COM11 appeared as an option for serial communication. I selected it in Arduino IDE and if I upload, I have to manually reset the Arduino board and uploading the sketch from Arduino IDE works perfectly - if I reset the microcontroller using good timing, then it works 100% correctly.
I thought I might be able to replicate the protocol that the avrdude.exe uses in my PC with an AI2 app. I know it uses the STK500v1 protocol. I want to create a tutorial for everyone if I succeed because I haven't found a complete tutorial. Maybe it's not a coincidence, considering my findings.
I created an app that is very basic:
- It downloads the binary sketch in a text format, connects to the dongle
- It initiates the update, waiting for byte 0 from Bluetooth client
- When I reset the Arduino board it sends byte "0" and the app sends back "0x30 0x20" (STK_GET_SYNC CRC_EOP) to get sync from the remote end, waiting for "0x14 0x10" etc.
The exact details of the protocol commands I derived by using the avrdude.exe over the air and at the same time I captured the communication with another Nano board. So at the end, it should've been a matter of time to implement the full protocol by simply having a chat between the phone and Arduino Nano (and of course JDY-31 in between).
The thing is, it works until one point and it's not stable. Some times it gets SYNC but fails the next command. Some times it goes through SYNC, query parameters, set device, set device extended parameters and then fails. The whole thing is unstable and unpredictable. I don't know what could I do better? I can upload from my PC, using the same dongle and Nano board 100% correctly and it fails from the phone.
I can't share the full app due to lack of visibility so I'll try to focus on explaining how it works. Maybe the problem is in the way I use the BluetoothClient component, or using Clock to schedule the communication?
- When "but_update" is clicked, I start the timer so "when clock.Timer()" will be triggered
- There is a helper function "wait_optiboot_1s" that receives all bytes from Bluetooth and looks for a certain value (usually 0x10 = STK_OK). If I get the correct answer, I'll go to the next phase (increase the global variable "phase") and send the next command, wait again for the correct answer etc.
Is this a correct approach? Why it works from my PC, using avrdude.exe and lost sync when I recreate the same conversation with AI2? Any suggestions, please?
Thank you!