• Tags
  • Documentation
  • Order
  • Register
  • Login
Duet3D Logo Duet3D
  • Tags
  • Documentation
  • Order
  • Register
  • Login

Boilerplate HTTP connection code for Python-based UI

Scheduled Pinned Locked Moved
Duet Web Control
python web ui python ui websockets
5
11
1.2k
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.
  • undefined
    Danal
    last edited by Danal 31 Oct 2019, 03:55

    Duet/RepRap V2 firmware (i.e. the printer) uses HTTP polling. Duet/RepRap V3 uses sockets. DWC V2 (i.e. the browser) code has a module that senses which is which as it initially attempts to connect, and uses the correct protocol. (DWC V1 uses only HTTP and therefore only connect to V2 firmware).

    See: https://github.com/chrishamm/DuetWebControl/tree/master/src/store/machine/connector

    If you want to do more than look at source, be aware that browser development consoles deal very poorly with minified files, and the distribution copy of DWC is minified. Therefore, if you wish to use run time breakpoints, examine data, etc, the sequence is roughly:

    1. clone the DuetWebControl repository to your hard drive (git clone).

    2. Edit the "vue.config.js" file to have " optimization: { minimize: false }," just after the "performance:" line

    3. Execute "npm run build". This takes quite a while.

    It will build a 'dist' directory that contains NON minified application. Upload the whole zip to your Duet via the "upload system files" button in DWC. (don't unzip it). And then... you can load the webpage in your browser, open the developer console (F12 in Chrome) and stop/trace the app, etc, etc.

    Delta / Kossel printer fanatic

    1 Reply Last reply Reply Quote 1
    • undefined
      Danal
      last edited by Danal 31 Oct 2019, 04:03

      P.S. the non-minified app will still be web-packed into one very large javascript file. I've found that Chrome crashes if you edit that in browser and try to save it (so that your edits take effect). To be clear, breakpoints, data watches, everything except save, that all works correctly for the non-minified file. So it is still the best way to trace things.

      At the moment, I haven't found a browser than handles it any better.

      Therefore, if you wish to actually change something (like inserting console.log statements), it is best to edit the very large app .js file, push it to the duet, and reload the browser (be sure you have development console open when you reload, as this bypasses all caching).

      Directly editing the big app .js avoids re-running 'npm run build' which takes 5 to 20 min depending on your computer.

      And... if you happen to be running Duet 3 hardware, you can do all of the above, all the builds, edits, pushes, etc, on the Rpi that is part of Duet 3 architecture. Quite nifty really.

      If Duet 2 hardware, you'll still have to edit on a PC and upload all or part of the WWW directory to the Duet 2.

      Delta / Kossel printer fanatic

      1 Reply Last reply Reply Quote 1
      • undefined
        poofjunior
        last edited by 31 Oct 2019, 06:40

        Thanks @Danal for the quick reply. I was definitely using RepRap 2 with the old interface. I'm now up-to-date with the RepRap 3 (beta) firmware and DWC 2.

        Running this code

        import socketio
        sio = socketio.Client()
        sio.connect("ws://192.168.1.2/machine", transports="websocket")
        

        now errors out with:

        websocket._exceptions.WebSocketBadStatusException: Handshake status 404 page not found<br>Check that the SD card is mounted and has the correct files in its /www folder
        

        which looks closer. Ok, so now for some noob questions. Am I making the connection correctly? Attempting to connect to ws://192.168.1.2/machine is what my browser is connected to. Do I need to send the reprap password along in this transaction somehow?

        Thanks again!

        1 Reply Last reply Reply Quote 0
        • undefined
          gtj0
          last edited by 31 Oct 2019, 17:14

          Use the "websocket" module instead of socketio. You'll also need to send "OK" back on the websocket to get the next message.

          #!/usr/bin/env python
          
          #
          # Hello, REST API! Requires the websocket-client library.
          #
          
          import websocket
          import sys
          import json
          import urllib
          
          def main(argv):
          	ws = websocket.create_connection(
          		"ws://%s/machine" % sys.argv[1])
          	try:
          		print ("Ready.")
          		msg_str = ws.recv()
          		while msg_str is not None:
          			msg_json = json.loads(msg_str)
          			print(json.dumps(msg_json, sort_keys=True,
          				indent=2, separators=(',', ': ')))
          			ws.send("OK\n")
          			msg_str = ws.recv()
          	except KeyboardInterrupt:
          		print ("*** Closing")
          	except websocket.WebSocketConnectionClosedException:
          		print ("*** Closed")
          	ws.close()
          
          
          if __name__ == "__main__":
          	sys.exit(main(sys.argv) or 0)
          
          1 Reply Last reply Reply Quote 0
          • undefined
            poofjunior
            last edited by 31 Oct 2019, 18:30

            Thanks @gtj0. Does this code work for you?

            I get the error:

            websocket._exceptions.WebSocketBadStatusException: Handshake status 200 OK
            

            when running it on "ws://192.168.1.2/machine"

            This error happens on this line of execution:

            ws = websocket.create_connection("ws://192.168.1.2/machine") 
            

            which prevents me from being able to reply back with the OK.

            Is there any other sort of "handshaking" that the webui does before making this connection?

            1 Reply Last reply Reply Quote 0
            • undefined
              gtj0
              last edited by 31 Oct 2019, 19:36

              Wait, I may have misunderstood something. You're using RRF3 on a Duet2? If so, then the websocket won't work. That's only for a Duet3 with the Duet Software Framework on a single board computer. Sorry about that. I saw you asking about websocket and didn't read the previous posts thoroughly.

              Your original approach should work fine on a Duet2 regardless of the version of RRF you're using. You don't really have to use rr_connect at all for your application.

              There's an explanation of the rr_* commands at
              https://github.com/chrishamm/DuetWebControl/tree/legacy

              1 Reply Last reply Reply Quote 0
              • undefined
                poofjunior
                last edited by 31 Oct 2019, 20:31

                Ahhh--that makes sense now! Ok, I will revert to my original plan of using HTTP requests.

                Thanks a ton for clearing this up.

                Just curious, do folks know if there's any plan to port websocket support to the Duet Ethernet/Wifi? (Perhaps there are hardware limitations that make this nice-to-have feature difficult in practice?) I just want to make sure I do development on something lasting.

                undefined 1 Reply Last reply 31 Oct 2019, 23:26 Reply Quote 0
                • undefined
                  gtj0 @poofjunior
                  last edited by 31 Oct 2019, 23:26

                  @poofjunior said in Boilerplate HTTP connection code for Python-based UI:

                  Ahhh--that makes sense now! Ok, I will revert to my original plan of using HTTP requests.

                  Thanks a ton for clearing this up.

                  Just curious, do folks know if there's any plan to port websocket support to the Duet Ethernet/Wifi? (Perhaps there are hardware limitations that make this nice-to-have feature difficult in practice?) I just want to make sure I do development on something lasting.

                  There definitely are limitations on the Duet2 which would make this unlikely. Having said that, @dc42 did say that it might be possible at some future point to re-purpose one of the SPI busses on the Duet2 to be used with the Duet Software Framework running on an SBC. I emphasize might.

                  I posed a follow-on question here...
                  https://forum.duet3d.com/topic/12322/duet2-spi-and-dsf
                  Unfortunately, for me, "next week" turned into "next month" 🙂

                  1 Reply Last reply Reply Quote 0
                  • Gevorgunundefined
                    Gevorgun
                    last edited by 6 Mar 2022, 22:54

                    Thanks for the idea, man. I'll try to correct my mistakes.

                    1 Reply Last reply Reply Quote 0
                    • Zentroundefined
                      Zentro
                      last edited by 7 Mar 2022, 00:53

                      I'm very poorly versed in python. Perhaps due to my ignorance of all the subtleties of programs written in python, I was constantly deleting files I needed or writing dumb code.

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post
                      Unless otherwise noted, all forum content is licensed under CC-BY-SA