A Cody man accused of stalking his ex-wife and hoarding homemade explosives has pleaded guilty to two federal firearm charges in a plea deal.