I can definitely imagine that if I had been a farmer in middle-age I could have been happier, because I wouldn't have time to get bored, and I would be doing something useful.
In our modern world however, really useful stuff is done by robots or a tiny part of the population. Thus you have to find meaning on your own and that can be quite difficult.
In my opinion, there are plenty of useful things to do as a programmer. There are many non-profit organizations doing important work that would love to have a volunteer programmer. You could also teach math or programming to high school kids, etc. Paid programming work can sometimes seem far removed from "useful" work, but I think that's partly because of the complexity of society and all the abstractions that we have built. If you'd like to do something that feels more direct, I would suggest volunteering somewhere. But I do agree with your sentiment that finding meaning on your own can be quite difficult.