Project : Sending an Email when Temperature Crosses Threshold

Extra data: line 1 column 5 (char 4)

I’m getting this error when i execute temp_email.py
I have recheck my code several times and tried to run it. I’m getting the same result
can u please help me out

Hi , @shashankgowdar02
please go through the training content briefly
the key pairs and timestamps have to be given correctly according to the range and the threshold specified by the user attaching a link which will help you to debug the code and setup Project 9: Sending an SMS when Temperature Crosses Threshold | Bolt :smile:

Hi Bolt team,
My code is showing the following error:
Excepting value: line1 column 1 (char 0)


Your prompt response would be appreciated.

Hi @aaryanbanga1111,

The error typically suggests that there’s an issue with parsing JSON data. It could be related to how you’re attempting to load or parse the JSON response. Double-check your JSON response and the way you are trying to extract values from it using json.loads .

Also please verifiy if you have given these below mentioned details properly

MAILGUN_API_KEY = 'This is the private API key which you can find on your Mailgun Dashboard' 
SANDBOX_URL= 'You can find this on your Mailgun Dashboard' 
SENDER_EMAIL = 'test@' + SANDBOX_URL  # No need to modify this. The sandbox URL is of the format test@YOUR_SANDBOX_URL
RECIPIENT_EMAIL = 'Enter your Email ID Here'
API_KEY = 'This is your Bolt Cloud account API key'
DEVICE_ID = 'This is the ID of your Bolt device' 

You have to replace all the above value with your credentials. You can find the first four value in Mailgun dashboard and the last two in Bolt Cloud dashboard.

Make sure you read each and every step given in this Lecture - Project 10: Sending an Email when Temperature Crosses Threshold | Bolt properly, let me know if you are still facing any issues.

Sir,I check my code again but still facing same issue.

Hi @aaryanbanga1111,

The code should work as expected if you have checked the points that I have mentioned in the message.
Please share the screenshot of the error part it displays when you try to run your code.

“Expecting value: line 1 column 1 (char 0),” typically occurs when trying to parse empty or invalid JSON data. In this case, it might be related to the response you’re getting from analogRead not being in a valid JSON format.

Let’s modify your code to handle this scenario and print the response to understand what’s being received:

import email_conf
from boltiot import Email, Bolt
import json
import 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")
    response = mybolt.analogRead('A0')

    try:
        data = json.loads(response)
        print("Response from Bolt IoT:", data)

        if 'value' in data:
            sensor_value = int(data['value'])
            print("Sensor value is:", sensor_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:", response_text['message'])
        else:
            print("Invalid response from Bolt IoT. Could not extract sensor value.")
    except Exception as e:
        print("Error occurred. Details:")
        print(e)

    time.sleep(10)

This modified code first prints the raw response from analogRead , which might help identify any issues with the response format. If the response is not in valid JSON format, we can further investigate why and adjust the code accordingly.

Hi @aaryanbanga1111,

Apologies for the delay in getting back to you.
The error seems to be with the Mail Gun credentials that you have entered in the email_conf.py file.
Line 1 in your code corresponds to the email_conf.py file configuration. Do cross check if you have entered all the details correctly in that file and everything should work as expected.

For your reference I am mentioning the steps here are well.
As part of this project you will first have to

  1. Login into the server by entering the IP address of your digital ocean droplet. If you have not used Digital Ocean droplet, you can directly login to your Virtual Machine via VirtualBox or VMWare.
  2. After successful login, create a file named email_conf.py which will store all the credentials related to Mailgun. To create a new file type sudo nano email_conf.py in the terminal. After that write below code to save all the credentials in a single file.
MAILGUN_API_KEY = 'This is the private API key which you can find on your Mailgun Dashboard' 
SANDBOX_URL= 'You can find this on your Mailgun Dashboard' 
SENDER_EMAIL = 'test@' + SANDBOX_URL  # No need to modify this. The sandbox URL is of the format test@YOUR_SANDBOX_URL
RECIPIENT_EMAIL = 'Enter your Email ID Here'
API_KEY = 'This is your Bolt Cloud account API key'
DEVICE_ID = 'This is the ID of your Bolt device' 

Note: You have to replace all the above value with your credentials. You can find the first four value in Mailgun dashboard and the last two in Bolt Cloud dashboard.

  1. Now create one more file named temp_email.py. To do so you have to type sudo nano temp_email.py in the terminal. Now we will write main code to collect the data from the Bolt and send Email if it crosses the threshold.

The algorithm is similar to the one where we have sent the SMS if the temperature crosses the threshold. It can be summarised into,

  1. Fetch the latest sensor value from the Bolt device.
  2. Check if the sensor value is in the range specified in our min and max values.
  3. If it is not in range, send the SMS.
  4. Wait for 10 seconds.

Read each and every step given in this lecture properly

@shashankgowdar02 Your created file name and imported file name should be same.For example u create "sudo nano confy.pyyou should be imported asimport conf.py.Otherwise you can enter api_key and sandboxurl and from mail and recipient mail should as in "MAIILGUN"website.Otherwise you can give sender_mail=‘test@’+‘sandbox_url’
Note: sandboxurl must in single or double quotes.