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

    Socket having to close and reopen

    Scheduled Pinned Locked Moved LoPy
    socketrpiwificonnectionlopy
    2 Posts 2 Posters 1.3k 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.
    • sorenS Offline
      soren
      last edited by

      Hello,

      I have a Raspberry pie connected to 4 Lopy's over wifi, and a java tcp server program running on the rpi with sockets (with different port) for each lopy.

      My problem is that I have to close the connection after sending a bytearray, and the repon it again when I need to send the next bytearray. The time it takes for it to reconnect to the socket is not really feasible for the system.
      Is it supposed to work like this with sockets, or am I doing something wrong?

      The server code is here: https://github.com/ITU-PerCom-2017/10.-Coop-Predictive-Equipment-Maintenance/blob/master/java/src/TCPServer.java

      And the lopy code is here:

      
      from network import Bluetooth
      from network import WLAN
      import socket
      #from network import socket
      import pycom
      import time
      import machine
      import binascii
      from machine import Timer
      pycom.heartbeat(False)
      pycom.rgbled(0x000000) # turn off led
      
      lopyID = 4 # set this unique to each lopy
      print('started! lopy id: ', lopyID)
      #for breaking out of while(True) loops. Removed when program is finished
      chrono = Timer.Chrono() #
      chrono.start()
      
      wlan = WLAN(mode=WLAN.STA)
      nets = wlan.scan()
      
      
      
      pycom.rgbled(0x7f0770)
      time.sleep(0.5)
      pycom.rgbled(0x000000) # turn off led
      time.sleep(1)
      pycom.rgbled(0x7f0770)
      time.sleep(0.5)
      pycom.rgbled(0x000000) # turn off led
      time.sleep(1)
      pycom.rgbled(0x7f0770)
      time.sleep(0.5)
      pycom.rgbled(0x000000) # turn off led
      time.sleep(1)
      pycom.rgbled(0x7f0770)
      time.sleep(0.5)
      pycom.rgbled(0x000000) # turn off led
      time.sleep(1)
      pycom.rgbled(0x7f0000) # red
      #time.sleep(0.5)
      #pycom.rgbled(0x000000) # turn off led
      #time.sleep(1)
      
      pycom.rgbled(0x7f0000)
      for net in nets: # connect to rpi wifi
           if net.ssid == 'cooppifi':
               print('rpi found!')
               wlan.connect(net.ssid, auth=(net.sec, 'cooppifi2017'), timeout=4000)
               for _ in range(20):
                   time.sleep(1)
                   if wlan.isconnected():
                       print("connected to rpi")
                       break
                   print('.', end='')
               else:
                   print("could not connect to wifi, resetting lopy...")
                   machine.reset()
      
      bluetooth = Bluetooth()
      bluetooth.start_scan(-1)
      adv = None # bluetooth advertisementa
      
      
      time.sleep(2)
      
      def sendToSocket(beaconID, rssi):
          RSSIpositive = rssi * -1
          #newData = beaconID + lopyID + RSSIpositive
          #newData = 255
          s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          s.connect(socket.getaddrinfo('10.42.0.1', 6792)[0][-1])
          pycom.rgbled(0x007f00)
          time.sleep(0.5)
          pycom.rgbled(0x000000) # turn off led
          time.sleep(0.5)
          print('.')
          data = bytearray()
          #data.append(newData)
          data.append(lopyID)
          data.append(beaconID)
          data.append(RSSIpositive)
          print("sending to socket, binary: ", data, data[0])
          #conv = binascii.hexlify(data)
          #print(conv, encoding='utf8')
          s.send(data)
          s.close()
      
      # b'f3bc0b6feb4c' # raw: b'\xf3\xbc\x0bo\xebL'
      # b'edbed48d0b87' # raw: b'\xed\xbe\xd4\x8d\x0b\x87
      # b'c7c383eeaaa4' # raw?
      # b'cd87e6a38dc2' # raw?
      
      pycom.rgbled(0x007f00) # green
      while True:
      
          adv = bluetooth.get_adv()
      
          if adv != None:
              #print('!')
              #print('!!')
              macRaw = adv[0] # binary mac address
      
              macReal = binascii.hexlify(macRaw) # convert to hexadecimal
              if macReal == b'f3bc0b6feb4c':
                  print('beacon A rssi: ', adv[3]) # adv[3] is rssi
                  sendToSocket(beaconID = 1, rssi = adv[3])
              elif macReal == b'edbed48d0b87':
                  print('beacon B rssi: ', adv[3])
                  sendToSocket(beaconID = 2, rssi = adv[3])
              elif macReal == b'c7c383eeaaa4':
                  print('beacon C rssi: ', adv[3])
                  sendToSocket(beaconID = 3, rssi = adv[3])
              elif macReal == b'cd87e6a38dc2':
                  print('beacon D rssi: ', adv[3])
                  sendToSocket(beaconID = 4, rssi = adv[3]) # adv[3]
          #time.sleep(1)
      
          #lap = chrono.read() # for breaking out of loop (remove this part when program is finished)
          #if lap > 30:
          #    print("breaking")
          #    bluetooth.stop_scan()
          #    break
      ```
      L 1 Reply Last reply Reply Quote 0
      • L Offline
        livius @soren
        last edited by

        @soren said in Socket having to close and reopen:
        You close socket on server side then you must close it also on client side(it is closed already but..)
        Question is what problem do you have with the code?
        Is it working, if yes, do you ask if there is some better way?

        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