Can I determine which sketch is currently loaded on my Uno?

I suffer from frequent episodes of quite serious stupidity. This means that after a few weeks, I forget which ino file is the latest that I'm running on my Uno. Is there some technical method I can use to query an Uno and some how compare /identify which sketch is currently loaded?

In the future, place a Serial.print in setup() that prints the name of the loaded program.

Okay, but anything more conclusive? You're assuming Serial is available, not //ed out and the version string is current. I was thinking along the lines of:-

  • Connect wires
  • Download the upload from target
  • Compile candidate sketch on development machine
  • Perform MD5 hash on both binaries and compare

That will spot the "." I added somewhere without modifying your "Version x.xxx..xx.x" serial string. Or something like that? Something that is beyond reproach?

Or can an Uno Buzzhash itself and spit that out through Serial?

I have never tried to read the contents from an Arduino controller.

However I do write on a removable white sticky and put it on the controller with a project name.

Larry, where can I find the datasheet for the Post-it? Does it work with other Arduinos other than the Uno?

1 inch by 700 inches, cut to size.

Not something I am familiar with but did read at one time the possibility of exporting binaries, uploading and downloading flash using avrdude, might be a good place to start googling

In addition to a program name, you can print the time and date the program was built and flashed to the processor by including following two lines to your setup() code:

Serial.println(TIME);
Serial.println(DATE);

WattsThat:
In addition to a program name, you can print the time and date the program was built and flashed to the processor by including following two lines to your setup() code:

Serial.println(TIME);
Serial.println(DATE);

That's sounding better! Is there any corresponding time/date that can be checked on the PC end, or is it all temporary file based (thus transient)?

WattsThat:
In addition to a program name, you can print the time and date the program was built and flashed to the processor by including following two lines to your setup() code:

Serial.println(TIME);
Serial.println(DATE);

Pretty sure that's the time/date that the containing file was compiled. How could a compiler macro possibly know when a binary image was flashed to the target processor?

How could a compiler macro possibly know when a binary image was flashed to the target processor?

It doesn’t. But it does know when the hex image was created and in the Arduino environment, there is an extremely high likelyhood the program was downloaded to flash in same build/make cycle when the image was created.

In any other environment, the statement is not appropriate.

I created this Python compile and upload program that creates an archive copy of the program and all the 3rd-party included files and then adds a Serial.print() statement into setup() with the name of the archive.

That way I should always be able to re-create the program that is on the Arduino.

I do all my program editing with Geany and I can call the Python program from Geany, but it can also be used stand-alone.

…R

What if you put the sketch name in program memory? Then you could find it later by looking at the extracted assembly.

  const char progName[] PROGMEM  = {"<<myProgram>>"};

gfvalvo:
...piled. How could a compiler macro possibly know when a binary image was flashed to the target processor?

Well, I don't know other than professional IDEs will know when a file was compiled. VS, NetBeans and SQLDeveloper all date stamp various output files, ant tasks and keep track within the IDE project metadata. They keep copies of distribution directories. And they know when a file was executed within them. AVR studio probably knows too but I can't run that. They might keep logs of transfer meta data as well.

So it's not entirely impossible...

Robin2:
I created this Python compile and upload program that creates an archive copy of the program and all the 3rd-party included files and then adds a Serial.print() statement into setup() with the name of the archive.

That way I should always be able to re-create the program that is on the Arduino.

I do all my program editing with Geany and I can call the Python program from Geany, but it can also be used stand-alone.

...R

Wow! If you have programmatic access to the compile and upload process, is it possible to CRC /MD5 the upload prior to flashing? Then inject that hash value into the Serial statement before flashing? So the sketch would output the hash on boot which could be compared with a manual hash of the archive? Please say yes ::slight_smile:

Hmm. That would alter the hash wouldn't it? CRC /MD5 collisions are almost trivial to make now, perhaps all is not lost...

cossoft:
Wow! If you have programmatic access to the compile and upload process, is it possible to CRC /MD5 the upload prior to flashing? Then inject that hash value into the Serial statement before flashing? So the sketch would output the hash on boot which could be compared with a manual hash of the archive? Please say yes ::slight_smile:

You might find the Pre and post build hooks feature of the Arduino IDE useful for this purpose:

Perhaps you could write it to an .eep file, which would store the hash to the EEPROM and thus not interfere with the .hex file hash.

There has been some talk previously about automatically writing the Git commit hash to the sketch, which I think would be much easier to work with:

cossoft:
Wow! If you have programmatic access to the compile and upload process, is it possible to CRC /MD5 the upload prior to flashing?

That is certainly not what my program is intended to do because it is only dealing with the source code. However if the purpose of your "hash" is just to put a unique number as a Serial.print() then I'm sure there are many ways to achieve that with a small extension to my program. For example you could make a hash of the source code with the date and time included in a Serial.print() statement before the hash was calculated.

I have to say, however, that I don't see how adding a hash (or any form of unique number) has any advantage over the system that is already in my program. My program produces a unique archive and archive name for every upload.

Using a hash as a means to ensure that the code on the Arduino has not been tampered with after it was uploaded is well outside the scope of your original question.

...R

Oh, it's not meant to be a security thing (although I guess it would serve that purpose). It's an audit thing to use the vernacular. If one has several devices scattered about the place, it's hard to keep track of the current firmware being executed on each one. Especially considering my medical condition ( :disappointed_relieved: ). Otherwise it's easy to reintroduce subtle bugs back into the code by re-flashing with the wrong code base. If it's a DIY heart monitor or perhaps burglar alarm, you want a degree of certainty that you're working on the correct source. Without closing the feedback loop, you have no confidence that a serially printed date precludes having had a fiddle with the code later.

So I was proposing comparing a hash of the flashed code with a hash of the recompiled source code on the development machine. If the hashes are identical, you have positive feedback that the source you're looking at is actually being executed. This seems even more important in a multi developer scenario where Fred can do the flashing, whilst Freddy does the code fiddling and it all gets very confusing.

pert:
There has been some talk previously about automatically writing the Git commit hash to the sketch, which I think would be much easier to work with:
Allow sketch version loaded on board to be identified by its Git commit hash · Issue #5618 · arduino/Arduino · GitHub

That sounds exactly like my thinking. The obvious question is therefore what do the professionals do? Does this type of firmware identification take place in industry? What do Panasonic, BEA and NASA do? Nobody want's the human waste extraction system on the ISS to go wrong, do they?