Temperature Monitoring PYTHON CODE

@rahul.singh1 @shoeb.ahmed @yeshwant.naik @vinayak.joshi

In the temperature monitoring python code, this is the code to obtain the current value of the sensor on our computer screen:

print ("Sensor value is: " + str(data[‘value’]))

How can we transform this code so that we get the temperature value in degree celsius instead of sensor value on our screen? I try dividing the value by 10.24 in the code but it did not give me the result. How to write that logic in the code so as to obtain temperature value instead of sensor value.

To get the temperature value in degree Celsius
first store that sensor value in a variable then use the given formula
temp = (100sensor_value)/1024
For example:
data = json.loads(response)
sensor_value = int(data[‘value’])
temp = (100
sensor_value)/1024 # in this line sensor value is converted to °C
print(“Current temperature value is: “+str(temp)+” °C”)

This may solve your query. Any further assistance you may inform.

@akshaykumar.kumar198 first you need to assign a variable where you’ll store the sensor value and to calculate the temperature use the following example.
sensor_value=int(data[‘value’])
temperature=(100*sensor_value)/1024
print(temperature)
will give you the sensor_value in celsius.

2 Likes

@nazirahammed98 Thank You. It solved my query.

1 Like

hi @akshaykumar.kumar198 can you share the code? actually I’m getting error in that code and I’m bit confused about which line is wrong. thank you :slight_smile:

What is wrong in this code? I have created Temperature monitoring system in python but in this code I got some error. any one can tell me what is wrong in that code?
@nazirahammed98
@yeshwant.naik
@vishalvats2000

@Harsh while running this code was your bolt module online?

NO but if I connect my bolt over internet then it will give me error? I’m just test my code whether it right or wrong. Can you just tell me my code is right or wrong?

@harshika1406.vats if i remove this line – data = json.loads(response) then it will affect on my code? bcoz I’m confused in json

Yes @Harsh. As you will get a JSON object as a response from bolt cloud having the sensor value. So you have to process that json object using the loads method of json library. I am trying to find the error in your program, will inform you in sometime

2 Likes

@Harsh the only error which I see in the code is in the line 10. You have wrongly concatenated the strings in the print statement. The print statement should look like this:

print (“Current temperature value is: " + str(temperature) + " C”)

Hii @vishalvats2000 not solved yet. it gives me same error. what is invalid literal for int() with base 10?

Even after running the code with the device being online?

NO @vishalvats2000 I’m just verify my code whether it is right or wrong

Brother @Harsh , the value string in your case will be a null string it will not contain any string because at the moment your device is not online and thus the int function can’t convert the “” to an integer.

Please do run the program after connecting the bolt module with the proper hardware connections

Also I can confirm the same, In your code which has a total of 20 lines, comment out every line after line 7. After line 7, write the following statement only:

print(data)

and then send the screenshot of the output

ok @vishalvats2000 i will try. thank you :slight_smile:

1 Like

Great, first of all do correct that print statement and then connect your bolt module to a power source and make sure you have made proper hardware connections. The code will work fine :slight_smile:

1 Like