Low-Power Wide-Area Network (LPWAN) technologies, particularly LoRa and LoRaWAN, are pivotal in the rapidly expanding Internet of Things (IoT) landscape, enabling wide-area communication with minimal power consumption. However, these technologies face significant challenges related to energy efficiency, scalability, and security, especially in dense deployments specially in smart cities and other IoT applications. The study presents a comprehensive approach to optimize energy efficiency, scalability, and security in dense network deployments. Two novel algorithms are introduced for dynamic optimization of transmission power and spreading factor based on node distance, validated through hardware implementation. The research extends to network-level optimization through an integer linear programming model designed to balance energy consumption and delivery ratios in large-scale deployments. Furthermore, the study explores machine learning applications, developing predictive models for energy consumption and implementing detection systems to identify power-greedy nodes. Through extensive evaluation of twelve machine learning regression models, the research establishes effective methods for predicting and optimizing energy consumption patterns.  A notable contribution includes the development of detection and classification models that differentiate between standard and power-greedy nodes based on transmission parameters and energy consumption patterns, enabling optimal node management. The findings contribute significant advancements in energy-efficient communication for wireless sensor networks, particularly beneficial for mobile devices and dense IoT deployments, while opening avenues for future research in artificial intelligence and edge computing integration.