I'm Harsha Manoj, this is a project specified in my Internshala IoT training program powered by BoltIoT. This Capstone Project assignment will cover my entire IoT training with different modules like Building an IoT Monitoring System, Controlling Devices Over the Internet, Cloud, APIs and Alerts through Bolt module.
In the capstone project for the IoT training, we require you to build a proof of concept of the lighting system of a refrigerator which uses data from the Light Dependent Resistor (LDR) and a push button as well as features of the Bolt IoT cloud. You need to use the light intensity and button state to collect light intensity and button data to decide the state of the fridge:
a) Door open: Bright and button released
b) Door closed: Dark and button pressed
c) Door half open: Dim and button released
In your code, send an e-mail with the current state of the door when the state changes. Additionally, irritate the user once every 10 seconds if the door is open (state a and c). Additionally, you should set the intensity of a single indicator LED which clearly indicates the above three states.
Bonus challenge: Machine LearningUse TensorFlow (or similar) library for Machine Learning by Google to build a classifier which dynamically adapts the thresholds for the LDR for classification of the brightness data (for state a and c) into the above environment categories instead of using hard-coded values. Thus, the system should function for different ambient lighting conditions, independent of the way you are using for triggering the LDR. (Hint: Look into Anomaly Detection for IoT).
Gathering all required componentsThese are the components required
1. Bolt WiFi Module
2. LDR (Light Dependent Resistor)
3. PushButton
4. Resistor 10K
5. LED
1. Make sure you have not powered on your Bolt Module while connecting the circuit. This will ensure that in case we make any mistake, it will not short circuit your device. Switch off the power if it is connected.
2. Connect one end of the LDR to the A0 (analog) pin of the Bolt device and other ends of the LDR to the 3.3V pin of the Bolt as shown in the image below.
3. Connect the 10K ohm resistor between the GND and A0 pin of the Bolt so that LDR and the resistor form a series connection.
4. Connect one end of the push button to the 1 (digital) pin of the Bolt device and other ends of the LDR to the 3.3V pin of the Bolt as shown in the image below.
5. Connect the 10K ohm resistor between the GND and 1 pin of the Bolt so that push button and the resistor form a series connection.
6. Connect positive end of LED to the 0 (digital) pin of the Bolt device through 330 ohm resistor as shown in schematic.
7. Connect the other end of LED to the GND.
8. I use breadboard for making the circuits.
From the given problem statement, I connected LDR, push-button and LED to Bolt Cloud module. The connections are clearly mentioned in schematics. I did this entire project in python(3.5.4). You can run Python files on either Digital Ocean or on Ubuntu server virtual machine. Here you can learn how to install python 3 and setup local programming environment on Ubuntu OS. After successful installation of Python 3 on your Digital Ocean Droplet or Ubuntu server, first I installed BoltIoT library using pip.
sudo pip3 install boltiot
After installing the requirements, create an account in Mail Gun. It provides email API to send emails up to 10,000 per month for free. It will provide private API key and sandbox authentication id. Create a credentials.py with following code and replace with your credentials.
MAILGUN_API_KEY = 'your Mailgun private API'
SANDBOX_URL = 'your sandbox url'
SENDER_EMAIL = 'test@your sandbox url'
RECIPIENT_EMAIL = 'your mail id'
API_KEY = 'Bolt API key'
DEVICE_ID = 'Your Device ID'
We need to import following libraries in python.
from boltiot import Bolt, Email
import credentials
import json, time
Testing LDR:
ldr_response = mybolt.analogRead('A0')
data = json.loads(ldr_response)
print(data['value'])
ldr_value = int(ldr_data['value'])
Testing Pushbutton:
pb_response = mybolt.digitalRead('1')
pb_data = json.loads(pb_response)
print(pb_data['value'])
pb_value = int(pb_data['value'])
Step 3: Anomaly Detection- Using Machine LearningFor the Anomaly detection, I prepared a data-set sensorvalues.csv where I saved LDR sensor readings and door states according to problem description. I used Decision trees algorithm for prediction of door state depends on real-time LDR values. Know more about decision trees for better understanding of this use case here. To implement these algorithms and make them work you need to install following dependencies. Run these commands in your terminal window after successfully installing Python 3.
sudo pip3 install pandas
sudo pip3 install numpy
sudo pip3 install scikit-learn
There is a basic tutorial on implementing a Machine Learning classifier in python with scikit-learn. Go through this tutorial to understand how to code a ML classifier.
I've been using Scikit Learn library for couple of months for most of the Machine Learning algorithms and it was doing great. So I decided to use sklearn in this project. Here is the code for machine learning prediction of refrigerator door state using Decision trees. In this project, I wrote ML classification code in dtree.py
import dtree
present_state = dtree.MachineLearning_model(ldr_value)
Step 4: Core logicIt will send emails through Mail Gun API when door state changes form close to open and vice versa. I've also placed LED indication, responsive to change in LDR brightness intensity value.
If push button is pressed - Door state = closed
if push button open - Door state = Predicted state from ML classifier
if previous_state != present_state:
try:
if pb_value == 1:
print("Door is Closed")
led_state = mybolt.analogWrite('0','0')
response = mailer.send_email("Alert","The Current door state is: CLOSE")
if present_state == 0 and pb_value == 0:
print("Door is half open")
led_state = mybolt.analogWrite('0','75')
if present_state == 1 and pb_value == 0:
print("Door is open")
led_state = mybolt.analogWrite('0','255')
response = mailer.send_email("Alert","The Current door state is: OPEN")
previous_state = present_state
except Exception as e:
print("Error",e)
time.sleep(10)
Step 5: Receiving EmailsIt is used in cold storage units like Drugs manufacturing industries to have proper measures on refrigerators where drugs are stored and this project helps them to have proper observations on the storage activities.
Comments