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 = (100sensor_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.
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
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?
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
@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”)
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:
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