Vikas Verma
Published © GPL3+

Auto ON/OFF Light

Light turns On and Off automatically depending on surrounding light's intensity.

BeginnerFull instructions provided2 hours419
Auto ON/OFF Light

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
USB Cable, Micro USB Type B Plug
USB Cable, Micro USB Type B Plug
×1
LDR, 5 Mohm
LDR, 5 Mohm
×1
5 mm LED: Yellow
5 mm LED: Yellow
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Resistor 330 ohm
Resistor 330 ohm
×1
Male/Male Jumper Wires
×3
Male/Female Jumper Wires
Male/Female Jumper Wires
×2
PHPoC Bread Board
PHPoC Bread Board
×1

Software apps and online services

Oracle VM virtualbox
Or any other means by which you can install Linux operating system

Story

Read more

Schematics

step 1

connect one terminal of LDR with 3.3 V pin and other terminal with A0 pin using jumper wires.

step 2

connect one terminal o a 10K ohm resistor with A0 pin and other terminal to the ground (GND)

step 3

Ground the negative terminal of the LED

step 4

connect a 330 ohm resistor to the positive terminal of LED

step 5

Now connecting the other terminal of the resistor to pin 0

Code

configuration file

Python
this will be imported to the main code in order to use the API key, threshold value and device id of the bolt WiFi module
"""Configuration file conf.py"""
bolt_api_key = "XXXX"               # This is your Bolt Cloud API Key
device_id = "XXXX"                  # This is the device ID and will be                                              similar to BOLTXXXX where XXXX is some                                         numbers
threshold = 200                     # intensity below which LED turns ON

main code

Python
The code executes an infinite while loop. It receives the sensor value from pin A0 (if some error occurs the request is unsuccessful). The value is compared with the threshold. If it less than the threshold, LED which is connected to pin 0 is set to HIGH (ON) else it is set to LOW (OFF).

NOTE : TO EXIT AN INFINITE LOOP WE CAN USE CTRL+C TO TERMINATE THE PROGRAM.
import json                     # library for handling JSON data
import time                     # module for sleep operation

from boltiot import Bolt        # importing Bolt from boltiot module
import conf                     # config file

mybolt = Bolt(conf.bolt_api_key, conf.device_id)

def get_sensor_value_from_pin(pin):
    """Returns the sensor value. Returns -999 if request fails"""
    try:
        response = mybolt.analogRead(pin)
        data = json.loads(response)
        if data["success"] != 1:
            print("Request not successfull")
            print("This is the response->", data)
            return -999
        sensor_value = int(data["value"])
        return sensor_value
    except Exception as e:
        print("Something went wrong when returning the sensor value")
        print(e)
        return -999



while True:
    # Step 1
    sensor_value = get_sensor_value_from_pin("A0")    
    print("The current sensor value is:", sensor_value)
    
    # Step 2
    if sensor_value == -999:
        print("Request was unsuccessfull. Skipping.")
        time.sleep(10)
        continue
    
    # Step 3
    if sensor_value <= conf.threshold:
        mybolt.digitalWrite('0' , 'HIGH')
          
    else :
        mybolt.digitalWritr('0' , 'LOW')  

    # Step 4
    time.sleep(10)

Credits

Vikas Verma
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.