It depends where you are in the world and everywhere has different harassment laws.
Using detection devices to gather proof of harassment is perfectly fine. If it went to court, you wouldn't be the person on the stand; the defendant would be. And, as long as you didn't cause harm to them, you will be treated as a witness.
As to the judge questioning why the incidents weren't reported sooner, well, you can't report it if you have no proof. As I say, check your local laws and regulations for clear guidance. Every police department has some kind of online presence, so take a look at their website. There are also many national resources for victims of stalking and harassment. They will also have legal information available and possibly a helpline.