5 Replies Latest reply on Oct 7, 2016 2:29 PM by Intel Corporation

    Connecting to a socket on the Intel Edison module

    zettez

      Greetings.

       

      How would I proceed if I want to connect to a socket which is on our backend from the Intel Edison module? Is it standard python socket scripting or is there any other way that I need to go?

       

      Thanks!

        • 1. Re: Connecting to a socket on the Intel Edison module
          Intel Corporation
          This message was posted on behalf of Intel Corporation

          Hi Zettez,

          Normally in Transmission Control Protocol (TCP) is used with Netcat. Netcat is a simple Unix utility which reads and writes data across network connections, using TCP or UDP protocol (Check this link for more information: http://nc110.sourceforge.net/)

          I personally recommend you to use it, check this link: https://linux.die.net/man/1/nc, it can be very helpful and you can learn how to use it. Try to use it first in your computer with Linux and then try to use it in your Edison. This package should be already included in your Edison Image. 

          I hope you find this information useful.

          Regards, 
          -Leonardo

          • 2. Re: Connecting to a socket on the Intel Edison module
            zettez

            Hey!

             

            Thanks, I will look into it. I need to do this in some scripts, maybe I can invoke Netcat from within my script.

            • 3. Re: Connecting to a socket on the Intel Edison module
              Intel Corporation
              This message was posted on behalf of Intel Corporation

              Hi Zettez,

              Great! Please let us know if it works.

              And don't doubt to ask us if you have issues.

              Regards,
              -Leonardo

              • 4. Re: Connecting to a socket on the Intel Edison module
                zettez

                Hey!

                 

                I successfully connected to our socket by installing websocket-client by typing pip install websocket-client and using this python code (not that pretty but it works) (indentation was scrambled when I copy pasted):

                 

                #!/usr/bin/python
                
                
                import os
                import time
                import json
                from websocket import create_connection
                
                
                def handleAction(action, args):
                  print(action)
                
                  # Do something with the action
                
                while True:
                  time.sleep(8)
                
                
                  try:
                       print("create connection to socket")
                
                
                       socket = create_connection("ws://111.222.333.444:555")
                
                
                       try:
                            while True:
                                 print("reading data..")
                
                
                                 result = socket.recv()
                
                                 print("got data: ")
                                 print(result)
                                 print(type(result))
                
                
                                 data = json.loads(result)
                
                
                                 action = data["action"]
                                 args = data["args"]
                
                                 handleAction(action, args)
                
                
                       except Exception as error:      
                            print("socket receive error")
                            print(error)
                
                
                            socket.close()
                
                
                  except Exception as error:  
                       print("socket error")
                       print(error)
                
                
                       socket.close()
                
                • 5. Re: Connecting to a socket on the Intel Edison module
                  Intel Corporation
                  This message was posted on behalf of Intel Corporation

                  Hi Zettez,

                  That's awesome, we really appreciate that you shared the script.

                  It will be helpful for another users.

                  Thanks.

                  Regards, 
                  -Leonardo