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

    Sending data from LoPy to a webserver

    Scheduled Pinned Locked Moved LoPy
    lopyraspberry piwifi send datadata
    13 Posts 3 Posters 5.1k Views 4 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.
    • J Offline
      JeromeMaquoi
      last edited by

      Hello!
      I have a sensor connected to a LoPy and I want to send the sensor data to a webserver (a raspberry pi 3) via WiFi. Is that possible if I connect the webserver to the WiFi of the LoPy? If so, how can I send the data? I've heard about "rest" API for the raspberry but I don't know if it can help.

      Thank you for your answers,

      Jérôme Maquoi

      L 1 Reply Last reply Reply Quote 0
      • L Offline
        livius @JeromeMaquoi
        last edited by

        @jeromemaquoi
        Think in another direction. As you say you have webserver on rasp pi. Then connect your lopy to it throught wifi.
        https://docs.pycom.io/chapter/tutorials/all/wlan.html
        and
        then create socket
        https://docs.pycom.io/chapter/firmwareapi/micropython/usocket.html
        and send data in format you need

        think about people first, then computers

        1 Reply Last reply Reply Quote 0
        • T Offline
          this.wiederkehr
          last edited by

          I'd do it the other way round: Connect the lopy to your local wlan.

          you might use the urequests lib from here https://github.com/micropython/micropython-lib/tree/master/urequests

          Then use e.g. http post requests to send the data:

          import urequests
          sensorreading = 'whatever'
          res = urequests.post('http://yourserver/endpoint', json={'my_sensor': sensorreading})
          res.close()
          if (res.status_code - 200) < 100: # check that response code is in the 200 range
              print('done')
          else:
              print('there was some error')
          

          This will send a post http request to the given url and the sensor reading as json.

          If your are doing this over wan or on an untrusted wlan you have to consider security!

          A completely different approach would be to use mqtt. Example here: https://docs.pycom.io/chapter/tutorials/all/mqtt.html
          (needs an mqtt broker)

          J 2 Replies Last reply Reply Quote 0
          • J Offline
            JeromeMaquoi @this.wiederkehr
            last edited by

            @this-wiederkehr @livius Ok I understood I could connect my LoPy to the webserver via WiFi. But can I do it without stopping to transmit Wifi with the LoPy?

            T 1 Reply Last reply Reply Quote 0
            • T Offline
              this.wiederkehr @JeromeMaquoi
              last edited by

              @JeromeMaquoi

              I thought it is not possible to use the lopy as both, station(connecting to an existing wlan) and ap (access point providing a wlan) simultaneously... but in fact it "seems" to be possible, tough I have not tested. And some restrictions apply (same channel of ap and sta)

              https://www.esp32.com/viewtopic.php?t=1192#p5285
              https://docs.pycom.io/chapter/firmwareapi/pycom/network/wlan.html

              setting the mode as to WLAN.STA_AP seems to make this possible ...

              1 Reply Last reply Reply Quote 0
              • J Offline
                JeromeMaquoi @this.wiederkehr
                last edited by

                @this-wiederkehr Hello again,

                I used the urequests lib that you gave me a month ago. Unfortunately I have this error. Can you help me?

                0_1511895323070_Capture du 2017-11-27 19:13:26.png
                Here is the 24 line:
                0_1511895434372_Ligne 24.png

                Also, I have another question. How can I catch those data from my web server?

                Best regards,

                Jérôme Maquoi

                1 Reply Last reply Reply Quote 0
                • T Offline
                  this.wiederkehr
                  last edited by

                  127.0.0.1 refers to localhost. you have to use the real ip of the server.

                  See the example on how to get the response. https://github.com/micropython/micropython-lib/blob/master/urequests/example_xively.py

                  J 1 Reply Last reply Reply Quote 0
                  • J Offline
                    JeromeMaquoi @this.wiederkehr
                    last edited by

                    @this-wiederkehr Hello,
                    I tested the code with the real ip address and it works! But I don't understand how it works. When I make "urequests.post", where is the data sent? And how can I catch it from the server side? I did'nt understand your response about that.

                    Jérôme Maquoi

                    J 1 Reply Last reply Reply Quote 0
                    • J Offline
                      JeromeMaquoi @JeromeMaquoi
                      last edited by

                      Someone can help me?

                      L 1 Reply Last reply Reply Quote 0
                      • L Offline
                        livius @JeromeMaquoi
                        last edited by livius

                        @jeromemaquoi
                        real sample tell more
                        this is the same as when you open your browser and put address in the bar
                        when you write http://www.pycom.io (if you write only www.pycom.io browser assume http:// at start and also try https)
                        when you press enter browser send request to the server on default port which is 80 for http and 443 for https
                        name www.pycom.io is translated to ip 18.220.239.183


                        now "same" is for urequests.post
                        if you write

                        urequests.post("http://123.123.123.123/", data = data)
                        

                        request is send to destination server 123.123.123.123 to port 80
                        and waiting for response from that server.
                        If on that server there is nothing listening on port 80 then you got timeout

                        then to recive something you must have on destination server socket which listen on port 80

                        think about people first, then computers

                        J 1 Reply Last reply Reply Quote 0
                        • T Offline
                          this.wiederkehr
                          last edited by

                          Your question is very broad ....

                          The above code sends a post request with the sensor data as a json payload. Each server framework provides a way to access that data however the way to do it depends on the framework.

                          I see that you have used php, as I have no idea about php I'm quoting stackoverflow for a way to access json in the payload with php:

                          $data = json_decode(file_get_contents('php://input'), true);

                          https://stackoverflow.com/a/18867369/4273834

                          1 Reply Last reply Reply Quote 0
                          • J Offline
                            JeromeMaquoi @livius
                            last edited by

                            @livius Hi, thanks for the answer. It's clearer for me now.
                            So if I understood, when I run this code and I have the word "done" printed in the console, does that mean that the server is listening on port 80? Or does that mean that the data is just correctly sent?

                            0_1513179237435_urequests.PNG

                            L 1 Reply Last reply Reply Quote 0
                            • L Offline
                              livius @JeromeMaquoi
                              last edited by

                              @jeromemaquoi
                              yes, you got 'done' if on the destination side port is open and something is listen

                              think about people first, then computers

                              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