Demand response (DR) involves economic incentives aimed at balancing energy demand during critical demand periods. In doing so DR offers the potential to assist with grid balancing, integrate renewable energy generation and improve energy network security. Buildings account for roughly 40% of global energy consumption. Therefore, the potential for DR using building stock offers a largely untapped resource. Heating, ventilation and air conditioning (HVAC) systems provide one of the largest possible sources for DR in buildings. However, coordinating the real-time aggregated response of multiple HVAC units across large numbers of buildings and stakeholders poses a challenging problem. Leveraging upon the concepts of Industry 4.0, this paper presents a large-scale decentralized discrete optimization framework to address this problem. Specifically, the paper first focuses upon the real-time dispatch problem for individual HVAC units in the presence of a tertiary DR program. The dispatch problem is formulated as a non-linear constrained predictive control problem, and an efficient dynamic programming (DP) algorithm with fixed memory and computation time overheads is developed for its efficient solution in real-time on individual HVAC units. Subsequently, in order to coordinate dispatch among multiple HVAC units in parallel by a DR aggregator, a flexible and efficient allocation/reallocation DP algorithm is developed to extract the cost-optimal solution and generate dispatch instructions for individual units. Accurate baselining at individual unit and aggregated levels for post-settlement is considered as an integrated component of the presented algorithms. A number of calibrated simulation studies and practical experimental tests are described to verify and illustrate the performance of the proposed schemes. The results illustrate that the distributed optimization algorithm enables a scalable, flexible solution helping to deliver the provision of aggregated tertiary DR for HVAC systems for both aggregators and individual customers. The paper concludes with a discussion of future work.