We introduce a framework for optimal resource allocation in smart grids. We consider two components of the smart grid; the power distribution network and the data communication network. By defining suitable utility functions, the power and bandwidth resources are optimally allocated. This requires the solution of the, so called, local public goods problem, in mathematical economics terminology. We propose an iterative, distributed algorithm for its solution. The algorithm is scalable for deployment in large networks since it requires only O(N ) messages per network user per iteration, where N is the number of users. Moreover, it is guaranteed to converge, does not require revelation of private information from each user and all algorithm actions can be realized by programmable smart devices of the smart grid.