VIJAY GM 1740345
Published © GPL3+

Moisture monitoring and irrigattion system

The moisture sensor monitors the moisture of the soil, and sends a SMS to the user when the system breaks the threshold moisture.

IntermediateWork in progress12 hours864
Moisture monitoring and irrigattion system

Things used in this project

Story

Read more

Schematics

engineering(1)_Ge3798HuRR.png

Code

Sending an SMS when moisture Crosses Threshold

Python
The code continuously fetches the moisture value using `analogRead` function. Since the sensor is connected to A0 pin of the Bolt, we will execute the analogRead() function on the pin A0.

The response from the Bolt Cloud using the analogRead() function is in a JSON format, so we will need to load the JSON data sent by the cloud using Python's json library.

The moisture value is inside a field labelled as "value" in the response. We can access the JSON values using the statement `sensor_value = int(data['value'])`. This line also converts the sensor reading to integer data type for comparing the moisture range.

This is enclosed inside a try-except block to handle any error that may occur in the code. More explanation of try-except code block is given here.

The next line of code checks if the moisture reading is below the minimum limit. If it exceeds, then the SMS will be sent.

The SMS to be sent will contain the text "The Current moisture sensor value is" followed by the moisture value.

The response from Twilio will be stored inside the `response` variable.

Once the temperature reading has been sent, we will need to wait for 10 seconds to get the next reading. For this, we will put the program to sleep once every loop iteration.

The statement `time.sleep(10000)` puts the program execution on hold for 10000 seconds. This means that the program would not execute for a period of 10000 seconds.
import conf
from boltiot import Sms, Bolt
import json, time

minimum_limit = 300

mybolt = Bolt(conf.API_KEY, conf.DEVICE_ID)
sms = Sms(conf.SID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)


while True: 
    print ("Reading sensor value")
    response = mybolt.analogRead('A0') 
    data = json.loads(response) 
    print("Sensor value is: " + str(data['value']))
    try: 
        sensor_value = int(data['value']) 
        if sensor_value < minimum_limit:
            print("Making request to Twilio to send a SMS")
            response = sms.send_sms("The Current moisture sensor value is " +str(sensor_value))
            print("Response received from Twilio is: " + str(response))
            print("Status of SMS at Twilio is :" + str(response.status))
    except Exception as e: 
        print ("Error occured: Below are the details")
        print (e)
    time.sleep(10000)

Credits

VIJAY GM 1740345

VIJAY GM 1740345

1 project • 0 followers

Comments