LED as a indicator with buzzer as alarm

I need a python code. The Story is as follows.

I need to interface the python code to the sms sending code. The concept is, i want to enter the threshold temperature manually. If the sensor temperature is above the threshold value, the RED LED( I am using) should turn on for 10sec. If the sensor temperature is less than the threshold value, the GREEN LED ( I am using) should turn on for 10 sec and reset the code and repeat the steps from beginning.

@ykoffice6920 Here is the code for your logic. If you want the alarm also, then connect the buzzer to pin 2 in parallel with the RED LED.

Note: Connect the LEDs to the pins in series with the resistor but connect the buzzer without resistor that is positive terminal to GPIO pin and negative terminal to GND.

import conf
from boltiot import Bolt, Sms
import json, time

threshold = 32 


mybolt = Bolt(conf.API_KEY, conf.DEVICE_ID)
sms = Sms(conf.SID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)


while True: 
    print ("Reading sensor value")
    response = mybolt.analogRead('A0') 
    data = json.loads(response) 
    sensor_value = int(data['value'])
    temp = (100*sensor_value)/1024
    print("Tempeature value is: " + str(temp) + "°C" )
    try: 
        
        if temp < threshold:
            
            print("Switching the GREEN LED ON")
            print("Making request to Twilio to send a SMS")
            response = sms.send_sms("The Current temperature temperature is " +str(temp)+ "°C")
            print("Response received from Twilio is: " + str(response))
            print("Status of SMS at Twilio is :" + str(response.status))

            mybolt.digitalWrite('1','HIGH')               #Connect the GREEN LED to pin 1
            time.sleep(10)
            mybolt.digitalWrite('1','LOW')

        if temp > threshold:
            
            print("Switching the RED LED ON")
            print("Making request to Twilio to send a SMS")
            response = sms.send_sms("The Current temperature temperature is " +str(temp)+ "°C")
            print("Response received from Twilio is: " + str(response))
            print("Status of SMS at Twilio is :" + str(response.status))

            mybolt.digitalWrite('2','HIGH')               #Connect the RED LED to pin 2
            time.sleep(10)
            mybolt.digitalWrite('2','LOW')

    except Exception as e:
        print ("Error occured: Below are the details")
        print (e)
    time.sleep(10)
2 Likes

Hi @ykoffice6920
Above program provided by @akshaykumar.kumar198 is correct but there is only one correction in the code and that is to import Sms from boltiot because we are obatining it from Bolt Python Library. So the line should be:
from boltiot import Bolt, Sms

1 Like

@suryanshchoudhary12 It will not have any effect actually. Either way is correct!