# Blockchain-Based Cloud-Enabled Security Monitoring Using Internet of Things in Smart Agriculture
## 6 Implementation
The code comprises the WIFI connection credentials; AWS IoT Core connection requirements such as Client ID, AWS Host URL; and the MQTT topic name and the programming logic to read the sensor data as an MQTT topic and publish the MQTT topic in the AWS IoT cloud using the network connection. The code is dumped on the Arduino micro-controller to run the application and post the data in AWS IoT Cloud. Figure 4 displays the print statements indicating the Arduino kit connected to the author's home WIFI network \"maverickcreek-7-709\" and initiating a connection to the AWS Cloud. Once it is connected to the AWS, the sensor data is published as an MQTT topic with values temperature: 26, light: 26, and humidity: 51.The data publish-success message can also be seen in the Figure 4.  
Figure 3: Arduino sensor kit to sense the environment. The AWS lambda function written in JavaScript reads the AWS IoT Core published data and compares the sensor threshold values for anomaly detection. The code may trigger a sensor device health alert if the data is not received for a specific time interval. To interact with the Ethereum blockchain, the Infura API credentials are stored as variables, and the AWS lambda function reads the credentials to connect with Infura maintained Ethereum main node. The metamask application is used for the software wallet and to interact with the Ethereum blockchain. The wallet details are also provided in the AWS lambda function to perform the transactions in Ethereum. The smart-contract code is written using solidity programming language and sends the alert triggered data as a transaction in the Ethereum blockchain. Figure 6 shows the Ethereum transaction details when the  
Figure 4: Sensor devices connected to Wi-Fi and initializing connection to AWS Cloud. Figure 5: Sensor data real time recording in AWS Cloud-IoT core service. temperature-threshold-exceeded alert is seen in the AWS IoT core. The transactions include the block number, from and to address, transaction fee, gas price, and timestamp. Based on the timestamps observed in the end-to-end blockchain- and cloud-based implementation, we determined that the time to update the farmer when the agriculture environment anomaly alerts trigger is 9 s. The Ethereum transaction completion time is 7 s. However, we used the Rinkeby testing network to test the Ethereum network, and the overall alert notification network latency will not be the same in the Ethereum production network. Overall, we prove that network latency is minimal when performing agriculture security monitoring using blockchain and cloud services and alerting the farmers. Figure 7 indicates the data field format in the Ethereum transaction. The sensor threshold value, current value, and alert message are stored in the data transaction. This data will not be tampered with and will be stored securely in the blockchain. The boxes highlighted in red clearly show that the temperature value of 25 does not exceed the threshold value of 26. Figure 6: Ethereum smart-contract transaction details. Figure 7: Ethereum smart-contract transaction storing the sensor data. The experimental transaction performed on the rinkeby network can be seen publicly for reader understanding. Figure 8 displays the list of transactions stored in the Ethereum test network. The from and to address, transaction hash value, and the block ID can be seen for each transaction. The details of the transaction can be explored by browsing the URL \"[[https://rinkeby.etherscan.io/address/0x001](https://rinkeby.etherscan.io/address/0x001) aed30b8 dabb3e7 ccc7a4 cb06 ad34115 aa390]([https://rinkeby.etherscan.io/address/0x001](https://rinkeby.etherscan.io/address/0x001) aed30b8 dabb3e7 ccc7a4 cb06 ad34115 aa390) (accessed on 19 August 2022)\". We developed the web frontend application to receive the agriculture security alerts such as anomaly and device-status alerts. The frontend application displays the alert notifications in the form of Ethereum transactions.