Error in the code of sensing and email when temperature crosses threshold

import email_conf
from boltiot import Email, Bolt
import json, time

minimum_limit = 300 #the minimum threshold of light value
maximum_limit = 600 #the maximum threshold of light value

mybolt = Bolt(email_conf.API_KEY, email_conf.DEVICE_ID)
mailer = Email(email_conf.MAILGUN_API_KEY, email_conf.SANDBOX_URL, email_conf.SENDER_EMAIL, email_conf.RECIPIENT_EMAIL)

while True:
print (“Reading sensor value”)

data = json.loads(response) 
print ("Sensor value is: " + str(data['value']))
    sensor_value = int(data['value']) 
    if sensor_value > maximum_limit or sensor_value < minimum_limit:
        print("Making request to Mailgun to send an email")
        response = mailer.send_email("Alert", "The Current temperature sensor value is " +str(sensor_value))
        response_text = json.loads(response.text)
        print("Response received from Mailgun is: " + str(response_text['message']))
except Exception as e: 
    print ("Error occured: Below are the details")
    print (e)

The code that you should write in the while statement after print (“Reading sensor value”) -
At the last you should add the sleep time as - time.sleep(10)

Thanx It is helpful the code ran

Hello, @indronil65090
Your code is possibly showing error because you didn’t read/take any response from the analog pin A0, but you are using it in next step - data = json.loads(response) .
Your code is incomplete.
PLease insert – response = mybolt.analogRead(‘A0’)
after the line - print (“Reading sensor value”).
Also ensure proper indentation in your code, indentation is very important in python.
This should solve your error.

Hi @indronil65090 ,

In the above section, response is not initialised. Please initailise it by reading the sensor value, for example response = mybolt.analogRead(‘A0’)

Correct code:

while True:
    print (“Reading sensor value”)
    response=mybolt.analogRead('A0')#can be any method
    data = json.loads(response) 

Also, make sure your indentation is correct.