I’m running a weather station using 2 Raspberry Pi. One gets all the measurements (temp, humidity, barometric pressure, wind speed, wind direction) and saves it to a text file that is then sent to my other Pi. The other Pi reads from the text file and sends everything to Ubidots. Normally I can send the data fine and see it on my dashboard, but whenever I get a 0 for wind speed, for example, none of the values send to Ubidots. Is there a way to get the data to send even if I get 0 for one of my values?
Here’s my code:
from ubidots import ApiClient
#Create an "API" object
api = ApiClient("be12602f1ef0d5a99465e42c5bc94128f1c9f736")
#Create a "Variable" object
temperature = api.get_variable("565e11f276254239d1a05ebc")
humidity = api.get_variable("565e14f47625424142543dc0")
barometric_pressure = api.get_variable("565e14e676254241983b6e04")
wind_speed = api.get_variable("565e14b27625423fa10595c8")
wind_direction = api.get_variable("565e14d17625423fc96fc463")
#Here is where you usually put the code to capture the data, either through your GPIO pins
f = open("/home/pi/WeatherStats.txt","r")
pressu = f.read(3)
tempu = f.read(4)
humu = f.read(5)
windsu = f.read(14)
winddu = f.read(5)
f.close()
#Write the value to your variable in Ubidots
temperature.save_value({'value':tempu})
humidity.save_value({'value':humu})
barometric_pressure.save_value({'value':pressu})
wind_speed.save_value({'value':windsu})
wind_direction.save_value({'value':winddu})
First try to detect where the program is breaking; is it in the read file?
windsu = f.read(14)
or when sending the variables?
wind_speed.save_value({'value':windsu})
Try printing windsu out to the console to see what it actually gets when it’s zero. Probably it’s a strange character instead of a zero.
Also, I like to send my variables in a single command. This saves you all the variables declaration and every individual save_value line:
from ubidots import ApiClient
#Create an "API" object
api = ApiClient("SECRET")
#Here is where you usually put the code to capture the data, either through your GPIO pins
f = open("/home/pi/WeatherStats.txt","r")
pressu = f.read(3)
tempu = f.read(4)
humu = f.read(5)
windsu = f.read(14)
winddu = f.read(5)
f.close()
#Write to Ubidots
api.save_collection([{'variable': '565e11f276254239d1a05ebc', 'value': tempu}, {'variable': '565e14f47625424142543dc0', 'value':humu}, {'variable': '565e14e676254241983b6e04', 'value': pressu}, {'variable': '565e14b27625423fa10595c8', 'value': windsu}, {'variable': '565e14d17625423fc96fc463', 'value': winddu}])
The thing is, the program isn’t breaking at all. When I receive a 0 for windspeed it’s actually supposed to be 0 because the wind does not blow when I’m testing it inside. It won’t let me send a value of 0 to ubidots though.