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.
-
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])
-
@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

-
@vicky_ I tried the link from your example, and I get an 403 error. Can you give an output from the urequest?
-
@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_ try float(a.text) instead of float(a)
-
@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_ 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 alright, thank for your help! :)
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login