edwinjosegeorge
Published

Online Bell system

Bell system that rings on predefined intervals

IntermediateProtip2 hours590
Online Bell system

Things used in this project

Story

Read more

Schematics

bolt_Vp0zN116th.jpg

Code

Online bell code

Python
The project make use of python's multi threading feature to control the bell and interact with the use at the same time. You can add or remove alarm time anytime you want!. Before running the code, make sure that you go through the BoltIot training for better understanding regarding how IOT works. This code make use of Boltiot module, Twilio sms service and few other concepts regarding python programming. Before running the code, you should install the libraries to connect the boltiot device. Here I have save my bolt wifi module's details in a file call info. which is later imported to the main code.
Here we are controlling the relay module using the botliot. Well, some relay module can be inverting, while other, non-inverting. Check you relay module before deploying the project. My, for instance was inverting type. Just replace 'LOW' with 'HIGH' and 'HIGH' with 'LOW' in the code if you are using non-inverting relay module.
import threading
from datetime import timedelta
import time,info,json
from boltiot import Bolt, Sms

alarm = list()
mybolt = Bolt(info.API_KEY, info.DEVICE_ID)
sms = Sms(info.SID, info.AUTH_TOKEN, info.TO_NUMBER, info.FROM_NUMBER)
mybolt.digitalWrite('0','HIGH')

def create_alarm():
    hr = int(input('Enter the alarm hour [0-23]: '))
    while(0>hr or hr>23):
        hr = int(input('Enter a valid hour [0-23]: '))
    mi = int(input('Enter the alarm minute : '))
    while(0>mi or mi>59):
        mi = int(input('Enter a valid minute [0-59]: '))
    d = timedelta(hours = hr, minutes = mi)

    if d not in alarm:
        alarm.append(d)

def delete_alarm():
    hr = int(input('Enter the alarm hour [0-23]: '))
    while(0>hr or hr>23):
        hr = int(input('Enter a valid hour [0-23]: '))
    mi = int(input('Enter the alarm minute [0-59] : '))
    while(0>mi or mi>59):
        mi = int(input('Enter a valid minute [0-59]: '))
    d = timedelta(hours = hr, minutes = mi)
    if d in alarm:
        alarm.remove(d)

def view_alarm():
    if len(alarm)==0:
        print('No alarm. Create new alarm.')
        return None
    print('hour minute')

    alarm.sort()
    for t in alarm:
        h = int(t.seconds/3600)
        m = t.seconds/60 - 60*h
        print(" %2d    %2d"%(h,m))

def get_inputs():
    choice = 0
    while choice!=4:
        print("\n\nAlarm Menu\n1.Add new alarm\n2.View alarm\n3.Delete an alarm\n4.Stop and exit")
        choice = int(input("Enter your choice <1-4> : "))
        if(choice == 1):
            create_alarm()
        elif(choice == 2):
            view_alarm()
        elif(choice == 3):
            delete_alarm()
    print("Please wait...")

A = threading.Thread(target = get_inputs)
A.start()
while(A.isAlive()):
    s = list(map(int,time.ctime().split()[3].split(':')))
    n = timedelta(hours = s[0], minutes = s[1])
    if n in alarm:
        data = json.loads(mybolt.isAlive())
        if data['success']==1:
            sms.send_sms("Online Bell system.\nDevice online. The bell rings now.")
            mybolt.digitalWrite('0','LOW')
            time.sleep(4)
            mybolt.digitalWrite('0','HIGH')
        else:
            sms.send_sms("Online Bell system.\nDevice offline. The system skips bell. Check the internet connectivity.")

        s = list(map(int,time.ctime().split()[3].split(':')))
        time.sleep(60-s[2])
    else:
        time.sleep(1)

Credits

edwinjosegeorge

edwinjosegeorge

1 project • 0 followers

Comments