Get HTTP how to compare values
-
i want to control a toggle switch online.
error i got:
line 105 :
a = int(a)
my code:
while True: a = urequest.get('http://things.ubidots.com/api/v1.6/devices/pycom/Switch/lv?token=') a = int(a) #post HTTP value = acq.reading("P13") post_var("pycom", current) #get HTTP b = urequest.get('http://things.ubidots.com/api/v1.6/devices/pycom/Switch/lv?token=') b = int(b) if a != b: a=b p_out.toggle() print("toggle") else: print("same") time.sleep(5)
from the http link, i will get either 1.0 or 0.0 only. I want to compare if it is different value that means the switch is triggered then it will toggle the switch.
if I don't change it to int it will always different and always toggle.
Can someone help? how can i make it compare the response after get http
Thanks.
-
@rvandam alright, thank for your help! :)
-
@vicky_ First you should buildin some validation if your GET requests did succeed. With a.status or b.status you can check the status. Status 200 is ok.
I am not familiar with the post_var function. You should check if there is some validation there as well.
-
@rvandam finally it print same! I think that means it can compare a & b. Thank you!
but it stuck at there after posting one value and geting one value to compare. Do you have any solution for this? It works well if only post Http, the delay i put 5s but now is already more than 5s.
sometimes i get this error: OSError:-1
-
@vicky_ try float(a.text) instead of float(a)
-
@rvandam ohh because i didnt not put the token number. here is the full links http://things.ubidots.com/api/v1.6/devices/pycom/Switch/lv?token=A1E-YH9cBXkax47bJPraezMrcc2ityAtZq you should see 0.0 now because the switch is off from the ubidots
-
@vicky_ I tried the link from your example, and I get an 403 error. Can you give an output from the urequest?
-
@rvandam Thanks for your help.
when i use float(a) i get this
when i use int(a[1]) because the first number is important and i get this
-
1.1 or 1.0 are not integers. Try converting them to floats first. float(a).
If only the last number is important (0 or 1) you could do something like this int(a[2])