Samsung has agreed to buy SmartThings, a two-year-old startup that makes software to connect household objects and let them be controlled from afar via smartphone.