The connections for hardware are:
1. LDR
Connect one pin to 3V and another to A0 pin. Use 10K Ohm, connect to GND and A0.
2. LED
Connect the longer (positive) leg to 0 pin and shorter (negative) leg to GND.
After successfully connecting the hardware, next part is to code. First file is email_conf. It consists of 6 configuration parameters:
MAILGUN_API_KEY:
Create you account on mailgun. On successfully creating, login you will see dashboard, go to Sending > Overview.
How would you like to send you emails?
Select API then in language select python,
You will get you mailgun api key.
SANDBOX_URL:
When you will login to your account in mailgun, dashboard will open like the one in picture.
The portion marked in red is your sandbox_url
SENDER_EMAIL:
Your_variable@sandbox_url
RECIPIENT_EMAIL:
Your email.
API_KEY:
Link your WiFi to bolt cloud then login to cloud.boltiot.com On the left panel, choose API and you will see the api key as 'XXXXXXXX....' To read it, click on eye.
DEVICE_ID:
When you will login to your bolt account on cloud.boltiot.com, you will see following page:
ID: is your Device id.
Now save the changes in your email_conf.py file and create a new file as light_monitor.py and write the code.
EXPLANATION OF CODE:
The very first task is to import the desired files.
Then we will set the limit so that if the intensity of light falling on the LDR becomes low then significant actions can be taken easily.
In order to check if the intensity is low or not, we will have to extract the information from the LDR. To do so we write following command:
response = mybolt.analogRead('A0')
and check if the LED is ON by:
response = mybolt.digitalRead('0')
Our LED is connected to the digital pin 0, so we will perform digitalRead and digitalWrite for it and LDR is connected to ADC pin 'A0' therefore we will perform analogRead and analogWrite for it.
Once we extract the value we check if the value is low then the limit and if our LED is OFF. If both the conditions are satisfied then we move forward and turn on the LED by following:
response = mybolt.digitalWrite('0', 'HIGH')
and then send mail to the concerned authority that the lights were turned on by following :
response = mailer.send_email("Subject of mail", "Body of mail")
*It is important to include subject, if we try to skip the subject this line will show error.
We monitor the data every 3600 sec or 1 hour. But you can change that value if you want by following:
time.sleep('Time-Duration)
*time-duration should be in seconds.
On successful completion, we get following output:
Comments