This is a project that will completely fullfill your lighting needs. This system uses a Light Dependent Resistor ( LDR ) whose resistance changes according to the light intensity of the surroundings. The data from the LDR is collected and sent to the Bolt Cloud by the Bolt Wifi Module. The data is then analysed and the light is turned on if the value received is below the preset threshold. It will also send an SMS to the registered phone number if such an incident occurs.
This is a project that will completely fullfill your lighting needs. This system uses a Light Dependent Resistor ( LDR ) whose resistance changes according to the light intensity of the surroundings. The data from the LDR is collected and sent to the Bolt Cloud by the Bolt Wifi Module. The data is then analysed and the light is turned on if the value received is below the preset threshold. It will also send an SMS to the phone number which is registered with twilio account if such an incident occurs.
The system will automatically turn off the light when the light intensity increases above the threshold and an SMS regarding the same will also be sent.
The bonus feature of this project is that this system can detect any anomaly in the light intensity of the surroundings. The system is designed in such a way that if the light intensity in the surroundings increases or decreases suddenly, the system will detect it as an anomaly and send an SMS regarding the same.
Hardware ConnectionsThe connections for the LDR are as follows:-
- Connect one leg of the LDR to the 3V3 pin and other leg to the A0 pin of the Bolt Wifi module.
- Connect one leg of the 10k resistor to the GND pin and other leg of the to the A0 pin of the Bolt Wifi module.
In the above image, the GREEN , PURPLE and BLUE connectors are connected to A0, 3V3 and GND pins respectively.
The connections for the LED are as follows :-
- Connect the longer pin (anode) of the LED to the digital pin 1 of the Bolt Wifi module
- Connect the shorter pin (cathode) of the LED to the digital pin 2 of the Bolt Wifi module.
STEP-1
INSTALL pycharm compiler to run the program, you can watch this video for step by step procedure.
STEP-2
Then open Command prompt in your system and follow following steps to install Bolt python Library:-
1. Check Python is installed or not.
C:\WINDOWS\system32>python --version
Python 3.8.02. Check pip is installed or not
C:\WINDOWS\system32>pip --version
pip 19.3.1 from c:\program files\python38\lib\site-packages\pip (python 3.8)3. To install pip
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py4. Then run the following:
python get-pip.py5. To install boltiot library
pip install boltiotSTEP 3 : Getting the API key and Device ID of the Bolt Wifi module
After creating an account on the Bolt Cloud, you can access your API key and Device ID from the API tab and the Devices tab respectively of your Bolt Cloud dashboard. Copy both the API key and Device ID of your Bolt device and store it somewhere securely.
STEP 4 : Creating an account on Twilio
In order to send SMS when the light intensity crosses the threshold, we will be using Twilio. So we have to create a free account on Twilio for the same.
Given below is the link for creating an account on Twilio:
https://www.twilio.com/try-twilio
Refer the following video for any clarification on how to create your Twilio account:
Before Coding:-Before writing the main code, we have to save the credentials such as the API key, Device ID, Twilio SSID, etc. in a separate python file. This file is then included in the main code. Given below is an example of the same :
SSID = 'XXXXXXXXXXXXXXXX' #Replace the X with the SSID found on your Twilio dashboard
AUTH_TOKEN = 'XXXXXXXXXXXXXX' #Replace X with auth token from your Twilio dashboard
TO_NUMBER = 'XXXXXXXXXX' #Replace X with the registered phone number
FROM_NUMBER = 'XXXXXXXXX' #Replace X with your Twilio phone number
DEVICE_ID = 'BOLTXXXXXX' #Replace this with your Bolt Device ID
API_KEY = 'XXXXXXXXXXXXXX' #Replace X with the API key of your Bolt DeviceAlgorithm For Coding:-
- Fetch the latest value of the LDR sensor.
- Compute the Z-score for anomaly detection.
- Check whether the sensor value is beyond the upper and lower bound calculated using the Z-score. If yes, send an SMS saying that an anomaly was detected.
- If it was not an anomaly, check whether the sensor value is below the threshold. If yes, turn on the LED and send an SMS regarding the same.
- Otherwise check whether the sensor value is above the threshold. If yes, turn off the LED and send an SMS regarding the same.
- Wait for 10 seconds and repeat all the steps.
This technique consists of subtracting the mean of the column from each value in a column, and then dividing the result by the standard deviation of the column.The result of standardization is that the features will be re-scaled so that they’ll have the properties of a standard normal distribution. In summary, the Z-Score (also called the standard score) represents the number of standard deviations with which the value of an observation point or data differ than the mean value of what is observed. The formula to calculate the Z-Score is given below:
where Mn represents the Mean and r represents the frame size
where Zn represents the Z-Score, C is the multiplication factor and r is the frame size
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.
- 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.
Then we will make some variables to store important data also we will call all the details from conf.py file in order to send sms from twilio account.
Now we will define a function get_sensor_value(pin) this will give the intensity of light falling on LDR.
It takes one parameter(pin) and makes a request to the Bolt Cloud to fetch the latest sensor value from the mentioned pin. The line `mybolt.analogRead()` returns the value for that pin.
The response returned by the Bolt Cloud needs to be converted into a JSON object for ease of use. The line `data = json.loads(response)` converts the response from the Bolt Cloud into a JSON object.
Before returning the value of the response, we need to check if the request was made successfully. The Bolt Cloud returns a status of 1 if the request was made successfully and anything else apart from 1 means that the request has failed. The line `if data["success"] != "1":` checks for a valid response.
Functions : def turn_on_led() and def turn_off_led() will switch our led on and off whenever the intensity of light is above or below the threshold.
def compute_bounds(history_data, frame_size, factor):- This will calculate the z-score with the help of formulas mentioned above.
Since we want to continuously monitor the temperature reading, we will enclose our logic to fetch, compare and send the SMS inside an infinite loop using the `while True:` statement. An infinite loop is a special loop which executes its code continuously since its exit condition is never going to be valid. To exit the loop, we will need to forcibly exit the code by holding CTRL + C.
Now our program will start from while loop , firstly it will fetch the intensity of light falling on LDR with the help of function get_sensor_value(pin) and print the intensity on the output screen , then it will check for anomaly for about 100 seconds. then if any anomaly is detected by the sensor it will send sms to the registered mobile number. If not then it will compare the intensity with threshold if it is above or below the threshold then it will switch off and on led respectively and also send a sms to the registered number.














Comments