Nowadays, we forget to nourish and water plants that makes our home clean and soothing. It would be nice if plants were able to speak and tell how much water they need as per the temperature. It would be really helpful if we get a notification on our phones about our plant's health and needs. Taking account of this we came up with the idea of building a smart garden with Temperature Monitoring System on Plants. It checks the intensity of sunlight falling on plant at regular time intervals. After the data is processed an SMS is sent about the plant's health. It will make our life simple and easy and will also help us to take better care of our plants. So let's get started!
NOTE: We use TWILIO to provide us with messaging services if threshold level is exceeded. TWILIO provides us with 'SID NO', 'AUTH TOKEN', a number for communication, 'API KEY.'
Code Explanation:
In the code, we first have to import our conf file which has all the credentials. The Python JSON and time libraries are also imported in the same line. Since we have saved our conf file with the .py extension, we can directly import it.
import conf, json, time
JSON is a Python library used for handling all operations on JSON objects. JSON is nothing but a data communication format widely used on the Internet for sending/receiving data between a client and server. More information on JSON can be found here. Remember, 'json' is the Python library used for handling JSON objects and JSON is a data communication format.
Now we will import Bolt Python library which will let us fetch the data stored in Bolt Cloud. To send the SMS, the Sms library is also imported. The below line of code imports the required libraries.
from boltiot import Sms, Bolt
SMS which will be used to send SMS alerts and the other one is Bolt which is used for accessing data from your Bolt device like the temperature reading is done here.
maximum and minimum variables are used to set the threshold value.
This would send an alert if the temperature reading goes below the minimum limit or goes above the maximum limit.
minimum_limit = 400, maximum_limit = 500
To fetch the data from Bolt Cloud, we will create an object called 'mybolt' using which you can access the data on your Bolt.
For the Bolt Cloud to identify your device, you will need to provide the API key and the Device ID when creating the mybolt object. Since the conf file holds the API key and Device ID variables, you can use them as follows,
mybolt = Bolt(conf.API_KEY, conf.DEVICE_ID)
The above code will automatically fetch your API key and Device ID that you have initialized in conf.py file.
Now to send an SMS, we will create an object of the same.
sms = Sms(conf.SID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)
The above code will automatically fetch your SID, AUTH_TOKEN, TO_NUMBER and FROM_NUMBER that you have initialized in conf.py file. Make sure you have given correct value in conf.py file.
Infinite loop while is used to fetch data regularly and continuosly.
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 > maximum_limit or sensor_value < minimum_limit: print("Making request to Twilio to send a SMS") response = sms.send_sms("The Current temperature 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(10)
The code continuously fetches the temperature 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 temperature 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 temperature 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 temperature reading is above the maximum limit or below the minimum limit. If it exceeds, then the SMS will be sent.
The SMS to be sent will contain the text "The Current temperature sensor value is" followed by the temperature 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(10)` puts the program execution on hold for 10 seconds. This means that the program would not execute for a period of 10 seconds.
In the above code, we are fetching the data every 10sec. You can change the value but ideally, it should be good if the time interval between 2 data points is more than 10 sec.
Comments