Invalid literal for int() with base 10: ' ' and arduino error

I am trying to make smart irrigation system using dht11 sensor and water level sensor with bolt iot and arduino.I am getting errors in arduino code and python code .Could anyone help me??
This is my python code .Plzz check and tell me whether it is correct or not.I have also attached screenshot of errors .

import json, requests, time

from boltiot import Bolt

import requests # for making HTTP requests

import json # library for handling JSON data

import time # module for sleep operation

from boltiot import Bolt # importing Bolt from boltiot module

“”“Configurations for home_automation.py”""

Bolt IoT Credentials

api_key = “************************” #API Key of Bolt Cloud

device_id = “BOLT******” #Device ID

Telegram Credentials

telegram_chat_id = “@smart_irrigation01

telegram_bot_id = “bot5099747110:AAEgtaDanIrrNnVpUdZ9joI5l55JsS3FlTg”

URL = “https://api.telegram.org/” + telegram_bot_id

threshold_value = 40

mybolt = Bolt(api_key, device_id)

def get_levelsensor_value_from_pin(pin):

"""Returns the sensor value. Returns -999 if request fails"""

try:

    responseA=mybolt.serialBegin(9600)

    responseB=mybolt.serialWrite('getAnalogdata')

    response = mybolt.serialRead('10')

    print(response)

    data = json.loads(response)

    if data["success"] != 1:

        print("Request not successfull")

        print("This is the response->", data["value"])

        return -999

    levelsensor_value = int(data["value"])

    return levelsensor_value

except Exception as e:

    print("Something went wrong when returning the sensor value")

    print(e)

    return -999

def send_telegram_message(message):

"""Sends message via Telegram"""

print("Sending telegram message .....")

url =  URL+ "/sendMessage"

data = {

    "chat_id": telegram_chat_id,

    "text": message

}

try:

    response = requests.request("POST",

        url,

        params=data)

    print("This is the telegram response=>")

    print(response.text)

    telegram_data = json.loads(response.text)

    return telegram_data["ok"]

except Exception as e:

     print("An error occurred in sending message via Telegram")

     print(e)

     return False

while True:

# Step 1 : Check Device Status

print("Checking device Status .....")

response = check_device_status()

if response != True:

    time.sleep(10)

levelsensor_value = get_levelsensor_value_from_pin('A1')

print("The level sensor value is:", levelsensor_value)

time.sleep(10)

continue


if levelsensor_value == -999:

            print("Request was unsuccessfull. levelsensor Skipping.")

            time.sleep(10)

            continue

if levelsensor_value >= threshold_value:

            message = "Water level reached its level.Turning off the Motor"

            print(message)

            send_telegram_message(message)
            time.sleep(10)

data.append(levelsensor_value)

time.sleep(10)

Hi @rishabhpatle06,

Which Arduino board are you using for this project?

I am using arduino uno .Is the code wirrtten right?

could anyone please help me here ??

@rishabhpatle06 Can you please try uploading sample led blinking code on your Arduino board? Let me know if you face any errors. Here is the code for your reference

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

@shobhit.kumawat sir,

I have checked this code its running perfectly .After that i made some modification in the code but I am not getting telegram alert when water goes beyond threshold value and i also want to turn off motor when sensor value goes beyond threshold.

This is the python code i am using …

URL = "https://api.telegram.org/" + telegram_bot_id
threshold_value = 400
mybolt = Bolt(api_key, device_id)

def get_levelsensor_value_from_pin(pin):
    """Returns the sensor value. Returns -999 if request fails"""
    try:
        responseA=mybolt.serialBegin(9600)
        mybolt.serialWrite("GetAnalogData")
        responseB =mybolt.serialWrite("GetAnalogData")
        response = mybolt.serialRead(10)
        print(response)
        data = json.loads(response)
        if data['success'] != '1' 
            print("This is the response-> ", data)
            return -999
        levelsensor_value = int(data["value"])
        return levelsensor_value
    except Exception as e:
        print("Something went wrong when returning the sensor value")
        print(e)
        return -999
    
def send_telegram_message(message):
    """Sends message via Telegram"""
    print("Sending telegram message .....")
    url = URL + "/sendMessage"
    data = { "chat_id": telegram_chat_id,
             "text": message     
    }
    try:
        response = requests.request(
            "POST",
            url,
            params=data)
        print("This is the Telegram URL")
        print(url)
        print("This is the Telegram response")
        print(response.text)
        telegram_data = json.loads(response.text)
        return telegram_data["ok"]
    except Exception as e:
        print("An error occurred in sending the alert message via Telegram")
        print(e)
        return False
    
while True:
           
            print("reading sensor value")
            levelsensor_value = get_levelsensor_value_from_pin('A5')
            time.sleep(5)
            continue
            if levelsensor_value >= 400:
                   message = "Water level reached its level.Turning off the Motor"
                   print(message)
                   send_telegram_message(message)
                   time.sleep(5)
                   if levelsensor_value == -999:
                     print("Request was unsuccessfull. levelsensor Skipping.")
                     time.sleep(10)
                     continue           
            time.sleep(10)

Hi @rishabhpatle06,
In your python code, please remove continue keyword in the last while loop on the 4th line as it would prevent the code below continue to be executed.

while True:
           
            print("reading sensor value")
            levelsensor_value = get_levelsensor_value_from_pin('A5')
            time.sleep(5)
            #removed continue keyword

Do let me know if you need any further help.

yaa its working now . I want to add button through which i can manually turn on or off the motor .i have connected motor to arduino using relay .So is it possible to add button in th bolt cloud so that i can handle that manually???

Hi @rishabhpatle06,

Please refer to https://docs.boltiot.com/docs/controlling-output-devices-glowing-an-led#dual-button for code snippets of different types of buttons. You can use these buttons to control your relay using the Bolt Cloud. Just paste this code into your project configuration.

I have tried this but the button is not working.What should i do now ?

Below is my arduino and bolt cloud code ,plzz look into it and tell me if it require any changes.


@rishabhpatle06 Have you connected the relay to the Digital pins of Bolt Module or they are connected on Arduino?

I have connected relay to ardiuno.

@raghav.srivastava Can you please help him out as the inbuilt code snippet will not work for External Boards?

1 Like

Hi @rishabhpatle06 ,
I would recommend you directly connect the relay to a digital pin on Bolt Module directly instead of Arduino since you are already reading data from sensors connected to Arduino, it would overcomplicate your design… As I can see you have already written the code to activate the relay on pin 0 of your Bolt Module, so just connect your relay to pin 0 of your Bolt Module and your buttons would work perfectly. Do let me know if you need any further help.