A secure routing scheme plays an integral role in ensuring the secure routing and efficiency of wireless sensor networks (WSNs). Recently, many studies have been undertaken to improve the data aggregation process, data security, and routing security. However, these approaches are highly suffered due to major limitations like time complexity, malicious attacks, and data insecurity. This research aims to develop an improved blockchain based encryption scheme for secure routing in wireless sensor networks using machine learning techniques. An improved artificial quantum beetle swarm neural network approach is proposed to perform the data aggregation among the sensor nodes. The cosine equivalent function and packet density correlation degree approach is emphasized to perform clustering in sensor nodes. The cluster head (CH) is selected based on the similarity of each sensor node. The Kalman filtering technique is introduced to filter out the data from unwanted malicious. This technique helps to filter the data before it is sent to the CH. Blockchain‐based encryption scheme is emphasized for data security by preventing malicious attacks during data transmission. The enhanced differential evaluation based firefly routing protocol is presented to maintain secure routing by choosing the optimal routing for the protected data transmission. Performance metrics such as latency, throughput, routing performance, energy consumption, FPR, blockchain (BC) computational performance, dead sensor count and average hop count are analyzed and compared with existing techniques. In an experimental scenario, the proposed approach achieves a maximum packet delay of 500 slots, throughput of 581 times/s, a latency of 0.31 ms, a computation complexity of 0.6, and a routing complexity of 2081 ms with a BC computation time of 95 ms. The performance of the proposed approach shows a better result than other existing approaches.