For the project (Project 9: Sending an SMS when Temperature Crosses Threshold) , I made python program exactly same as given in training but i got an error in try block.The error is like “syntax error”. I am attaching required screenshot for the same. Kindly help!!
Try these codes -
#API_KEY,This is your Bolt Cloud account API key API_KEY = 'your api-key' #DEVICE_ID,This is the ID of your Bolt device DEVICE_ID = 'device-id' #You can find SID in your Twilio Dashboard SID = 'twilio api-key or sid' #You can find on your Twilio Dashboard AUTH_TOKEN = 'auth-token' #This is the no. generated by Twilio. You can find this on your Twilio Dashboard FROM_NUMBER = 'from-number' #This is your number. Make sure you are adding +91 in beginning TO_NUMBER = '+91 to-number'
import conf from boltiot import Bolt, Sms, Email import requests # for making HTTP requests import json # library for handling JSON data import time # module for sleep operation minimum_limit = 235 maximum_limit = 238 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) print("Sensor value is: " + str(data['value'])) try: sensor_value = int(data['value']) if sensor_value > maximum_limit or sensor_value < minimum_limit: print("Making request to Twilio to send a SMS") response = sms.send_sms("The Current temperature sensor value is " +str((100*sensor_value)/1024) + " degree celsious") print("Response received from Twilio is: " + str(response)) print("Status of SMS at Twilio is :" + str(response.status)) except Exception as e: print ("Error occured: Below are the details") print (e) time.sleep(10)
Hope this will solve your problem.
Copied same code you provided here but still facing same error.
. This is the message i am getting while executing the code
If there is syntax error it means there is mistake in typing syntax or in the spelling of it. You have given that there is error in 19th line. Check your 19th line of code correct it .
But I checked 19th line and it is same as given in training programme. Please help me to remove the error.
19th line is try: If you find nay error in that line please let me know
here I marked the arrow. Don’t give that much space between two lines. Take the try: exactly below the print line and run your code
Make sure that you have provided proper indentation i.e. a gap of one tab or four spaces.
Please share the screenshot of your code after changing it.
To debug this issue -
- Check if your code is indented properly.
- Configure your nano editor so that whenever you enter tabs, it will convert it into spaces. Check this link https://askubuntu.com/questions/40732/how-do-i-get-spaces-instead-of-tabs-in-nano
- Since you are using the Digitalocean, you can use the sublime editor to edit your code. Check this thread Downloading/Editing digital oceans files form your computer
- Once you open your file in the sublime editor, it will show you the spaces and tabs. Check the below screenshot.