The smart city vision has driven the rapid development and advancement of interconnected technologies using the Internet of Things (IoT) and cyber-physical systems (CPS). In this paper, various aspects of IoT and CPS in recent years (from 2013 to May 2023) are surveyed. It first begins with industry standards which ensure cost-effective solutions and interoperability. With ever-growing big data, tremendous undiscovered knowledge can be mined to be transformed into useful applications. Machine learning algorithms are taking the lead to achieve various target applications with formulations such as classification, clustering, regression, prediction, and anomaly detection. Notably, attention has shifted from traditional machine learning algorithms to advanced algorithms, including deep learning, transfer learning, and data generation algorithms, to provide more accurate models. In recent years, there has been an increasing need for advanced security techniques and defense strategies to detect and prevent the IoT and CPS from being attacked. Research challenges and future directions are summarized. We hope that more researchers can conduct more studies on the IoT and on CPS.