Error in code for, Sending an Email when Temperature Crosses Threshold


#1

Also Sir, found indentation errors in the code, the actual code mentioned in the course is as below:
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:
response = mybolt.analogRead(‘A0’)
data = json.loads(response)
print (data[‘value’])
try:
sensor_value = int(data[‘value’])
print (sensor_value)
if sensor_value > maximum_limit or sensor_value < minimum_limit:
response = mailer.send_email(“Alert”, "The Current temperature sensor value is " +str(sensor_value))
except Exception as e:
print (“Error”,e)
time.sleep(10)
the code which i have writen is as shown in the image below,for whic im als getting indentation error;

please do can you guide me…


#2

@pednekarruchika Seems that the issue is due to you trying to use both spaces and tabs in the same sentence. Different editors interpret the tab differently. Some take a single tab to be of 3 spaces while some take it to be of 4 spaces.
I suggest, that you copy the code in a text editor like Sublime text and replace all tabs with spaces and then try running it.


Error in code of,Sending an SMS when Temperature Crosses Threshold!
#3

Sir, i tried doing that, but now getting error for the other line.
If possible can you view my code,Using team viewer and sort it out for me?? for both the codes of temperature sensor as well as email …


#4

Hi @pednekarruchika,

Please write your code again and do not use tabs anywhere in your program. Use 4 spaces to indent your program.
Do let me know in case you need further assistance.

import email_conf
from boltiot import Email, Bolt
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:
    response = mybolt.analogRead('A0')
    data = json.loads(response)
    print(data['value'])
    try:
        sensor_value = int(data['value'])
        print(sensor_value)
        if sensor_value > maximum_limit or sensor_value < minimum_limit:
            response = mailer.send_email("Alert", "The Current temperature sensor value is " + str(sensor_value))
    except Exception as e:
        print("Error", e)
        time.sleep(10)

Check this screenshot :


#5

Sir,tried with the spaces you put up in SS,but im sorry im still unable to get.
Attached with the error i found…!! Sir,this error could be due to the installation time ??


#6

Hi @pednekarruchika

Sorry, There was a mistake from my side.

The code will be

from boltiot import Email, Bolt

I am also updating the code in the code snippet.

Do let me know in case you need further assistance.


#7

Ha no Sir, it isn’t your mistake, I am only going wrong somewhere, but can’t figure out d issue. After doing d changes u told,its still d same.


#8

My apologies, but being a complete newbie at this… This is the python code or script on your server side, right? What does the code look like for your bolt device. I’m guessing it watches the temperature and when it it crosses threshold, it makes a request to your sever to run this file? would you mind sharing your code for the bolt device?
Thanks
Kelly


#9

Hi @ksprayberry,

The Bolt device does not watch the Temperature and it does not triggers alerts if it crosses the threshold. The Bolt just pushes the data to the cloud when the request for the data is made and you have flexibility of controlling the alerts and threshold etc from the server side. The above Python code would run on your server. It fetches the value of pin A0 using the analogRead function of the Bolt Python library and then checks if the Temperature has crossed the threshold or not and accordingly sends out the alerts.

Let me know if this answers your question or in case you have any doubts. I will be happy to help.


#10

Ok thanks! I did a little more studying on the training course and picked up how it works. The python code polls the sensor and bolt device for the data and if it is over the temperature it sends the message. I just need to go back and figure out how to use flask or how to set the process to run on my server without interaction.

Thanks!


#11

Sir, its becoming very difficult to communicate this way.
Would be happy working as an Intern,where I can communicate directly with the experts.