Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Get HTTP how to compare values

    Scheduled Pinned Locked Moved LoPy
    lopyhttp gethttp
    9 Posts 2 Posters 1.5k Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • V Offline
      Vicky_
      last edited by Vicky_

      i want to control a toggle switch online.

      error i got:

      alt text

      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 Reply Last reply Reply Quote 0
      • R Offline
        rvandam
        last edited by

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

        V 1 Reply Last reply Reply Quote 0
        • V Offline
          Vicky_ @rvandam
          last edited by

          @rvandam Thanks for your help.

          when i use float(a) i get this

          alt text

          when i use int(a[1]) because the first number is important and i get this
          alt text

          R 1 Reply Last reply Reply Quote 0
          • R Offline
            rvandam @Vicky_
            last edited by

            @vicky_ I tried the link from your example, and I get an 403 error. Can you give an output from the urequest?

            V 1 Reply Last reply Reply Quote 0
            • V Offline
              Vicky_ @rvandam
              last edited by Vicky_

              @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

              R 1 Reply Last reply Reply Quote 0
              • R Offline
                rvandam @Vicky_
                last edited by

                @vicky_ try float(a.text) instead of float(a)

                V 1 Reply Last reply Reply Quote 0
                • V Offline
                  Vicky_ @rvandam
                  last edited by Vicky_

                  @rvandam finally it print same! I think that means it can compare a & b. Thank you!

                  alt text

                  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

                  R 1 Reply Last reply Reply Quote 0
                  • R Offline
                    rvandam @Vicky_
                    last edited by

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

                    V 1 Reply Last reply Reply Quote 0
                    • V Offline
                      Vicky_ @rvandam
                      last edited by

                      @rvandam alright, thank for your help! :)

                      1 Reply Last reply Reply Quote 0

                      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
                      • 1 / 1
                      • First post
                        Last post

                      Pycom on Twitter

                      Pycom