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

    LoPy to LoPy communication over WiFi

    Scheduled Pinned Locked Moved LoPy
    lopywifi
    6 Posts 2 Posters 2.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.
    • J Offline
      JosSchr
      last edited by

      Hello everyone,

      I'm trying to make one LoPy send data to another one over WiFi, but I'm hitting several obstacles on the road ...

      I have a file in my project, networking.py, that contains a function ( establishWiFiConnection() ) that initializes the WiFi processor so my LoPy works as an access point. The WLAN object is stored in a class for convenience in addressing it. But whenever I call this function, I lose my connection to the LoPy and I have to do a hard reset to connect to it again. When i call this function in main.py, I don't see the LoPy's network anymore on my computer and I have to safe boot my LoPy to regain access.

      You can download my zipped code Ihere. ( Seeing as my code ( which is not long ) is spread over multiple files I think this is more convenient for everyone )

      Thank

      • list item you very much in advance,
        Joshua
      J 1 Reply Last reply Reply Quote 0
      • J Offline
        JosSchr @JosSchr
        last edited by

        For extra convenience, here are the relevant files' contents:

        Networking.py:

        ''' This module contains functionalliy that handles the details of connecting the LoPy to LoRa and Wi Fi networks
        
            It provides the following functions
             - enableLoRaCommunication ( blocking ): 
                   Joins the LoRa network with the LoRa authentification data specified in the MEHO2NetworkConfiguration class.
        
                   Params:
                       blocking: boolean that specifies if program should block until LoRa network is joined ( optional )
                   Return value: none
                   
             - establishWiFiConnection ( blocking ):
                   Joins the WiFi network with the WiFi connection settings specified in the MEHO2NetworkConfiguration class.
                   
                   Params:
                       blocking: boolean that specifies if program should block until it is connected to the WiFi network ( optional )
                   Return value: none
                   
             - endWiFiConnection:
                   Closes the connection with the WiFi network currently connected to.
                   
                   Params: none
                   Return value: none
                   
             - changeLoRaConfiguration ( newLoRaAuthentificationMode, newLoRaAuthentificationData )
                   Changes the settings for joining LoRa networks on the fly
                   
                   When one of the parameters is not defined, its default setting as defined in the MEHO2NetworkConfiguration class
                   is conserved
                   
                   Params:
                       newLoRaAuthentificationMode: one of the LoRa activation mode constants defined in the LoPy's network.LoRa class ( optional )
                       newLoRaAuthentificationData: tuple containing your device EUI and the key of your LoRa network in that order ( optional )
                   Return value: none
              
              - changeWiFiConfiguration ( newWiFiNetworkingMode, newSSID, newAuthentificationInfo, newConnectingIsBlocking )
                    Changes the settings for connecting to WiFi networks on the fly
                    
                   When one of the parameters is not defined, its default setting as defined in the MEHO2NetworkConfiguration class
                   is conserved
                   
                   Params:
                       newWiFiNetworkingMode: one of the WiFi networking mode constants defined in the LoPy's network.WLAN class
                       newSSID: the SSID of the new network to join ( optional )
                       newAuthentificationInfo: the credentials of the new network to join ( optional )
                       newConnectingIsBlocking: specifies whether establishWiFiConnection should block while waiting on ( optional )
                           being connected to the new network ( optional )
                   Return value: none
                   
            Adjust the variables inside the MEHO2NetworkConfiguration class to provide your needed initial network configuration
            
            Note that after calling a configuration changing function, you should end and than reestablish the connection for there
            to be an effect
            Beware for Python 3. It could also work with Python 2, but we didn't test this! DO NOT COUNT ON IT!
            Written by Yuyu Lin and Joshua Schroijen
        '''
        
        import network
        import time
        import binascii
        
        class MEHO2NetworkConfiguration:
            ''' WiFi configuration
            '''
            WiFiNetworkingMode = network.WLAN.AP
            SSID = "lopy-wlan-8432"
            authentificationInfo = ( network.WLAN.WPA2, "www.pycom.io" )
            connectingIsBlocking = True # Beware - when using a non-blocking mode, check if sendData ( ) succeeded!
        
            ''' LoRa configuation
            '''
            joiningIsBlocking = True # Beware - when using a non-blocking mode, check if sendData ( ) succeeded!
            # This is a device specific authentification configuration! Be careful when changing it!
            LoRaAuthentificationMode = network.LoRa.OTAA
            LoRaAuthentificationData = ( binascii.unhexlify ( "74c542453857974a" ),                  # This number is the LoRa EUI
                                         binascii.unhexlify ( "2e15afc94a7ef69ae61a814737c38699" ) ) # This number is the Lora Key
        
        class MEHO2NetworkingGlobals:
            LoRaConnection = None # This variable will hold the instance of the LoRa connection this module helps manage
            WLANConnection = None # This variable will hold the instance of the WLAN connection this module helps manage
        
        ''' Use this function to join the LoRa network
            After calling this function, you can set up and use the gateway client when sending data over LoRa
        '''
        def enableLoRaCommunication ( blocking = None ):
            MEHO2NetworkingGlobals.LoRaConnection = network.LoRa ( mode = network.LoRa.LORAWAN )
            MEHO2NetworkingGlobals.LoRaConnection.join ( activation = MEHO2NetworkConfiguration.LoRaAuthentificationMode, auth = MEHO2NetworkConfiguration.LoRaAuthentificationData, timeout = 0 )
        
            if ( blocking == None ):
                blocking = MEHO2NetworkConfiguration.joiningIsBlocking
        
            if ( blocking == True ):
                while not MEHO2NetworkingGlobals.LoRaConnection.has_joined ( ):
                    time.sleep_ms ( 50 )
        
        ''' Use this function to establish a connection to the WiFi network
            After calling this function, you can set up and use the gateway cleitn when sending data over WiFi
        '''
        def establishWiFiConnection ( blocking = None ): # Beware - when using a non-blocking mode, check if SendData ( ) succeeded!
            if ( MEHO2NetworkConfiguration.WiFiNetworkingMode == network.WLAN.STA ):
                MEHO2NetworkingGlobals.WLANConnection = network.WLAN ( mode = MEHO2NetworkConfiguration.WiFiNetworkingMode )
                MEHO2NetworkingGlobals.WLANConnection.connect ( ssid = MEHO2NetworkConfiguration.SSID,                \
                                                                auth = MEHO2NetworkConfiguration.authentificationInfo )
        
                if ( blocking == None ):
                    blocking = MEHO2NetworkConfiguration.connectingIsBlocking
        
                if ( blocking == True ):
                    while not MEHO2NetworkingGlobals.WLANConnection.isconnected ( ):
                        time.sleep_ms ( 50 )
        
            else:
                MEHO2NetworkingGlobals.WLANConnection = network.WLAN ( mode = MEHO2NetworkConfiguration.WiFiNetworkingMode )
                MEHO2NetworkingGlobals.WLANConnection.init ( ssid = MEHO2NetworkConfiguration.SSID,                \
                                                             auth = MEHO2NetworkConfiguration.authentificationInfo,
                                                             channel = 1 )                                                     )
        
        def printWLANInformation ( ):
            networkInformation = MEHO2NetworkingGlobals.WLANConnection.ifconfig ( )
            print ( "Successfully connected to the network. Networking information: \n" )
            print ( "IP address\t%s\n" % ( networkInformation [ 0 ] ) )
            print ( "Subnet mask\t%s\n" % ( networkInformation [ 1 ] ) )
            print ( "Gateway\t%s\n" % ( networkInformation [ 2 ] ) )
            print ( "DNS server\t%s\n" % ( networkInformation [ 3 ] ) )
        
        def endWiFiConnection ( ):
            MEHO2NetworkingGlobals.WLANConnection.disconnect ( )
        
        def changeLoRaConfiguration ( newLoRaAuthentificationMode = MEHO2NetworkConfiguration.LoRaAuthentificationMode,
                                      newLoRaAuthentificationData = MEHO2NetworkConfiguration.LoRaAuthentificationData ):
            MEHO2NetworkConfiguration.LoRaAuthentificationMode = newLoRaAuthentificationMode
            MEHO2NetworkConfiguration.LoRaAuthentificationData = newLoRaAuthentificationData
        
        def changeWiFiConfiguration ( newWiFiNetworkingMode=MEHO2NetworkConfiguration.WiFiNetworkingMode,
                                      newSSID=MEHO2NetworkConfiguration.SSID,
                                      newAuthentificationInfo=MEHO2NetworkConfiguration.authentificationInfo,
                                      newConnectingIsBlocking=MEHO2NetworkConfiguration.connectingIsBlocking):
            MEHO2NetworkConfiguration.WiFiNetworkingMode = newWiFiNetworkingMode
            MEHO2NetworkConfiguration.SSID = newSSID
            MEHO2NetworkConfiguration.authentificationInfo = newAuthentificationInfo
            MEHO2NetworkConfiguration.connectingIsBlocking = newConnectingIsBlocking
        

        Main.py:

        import networking
        import adc
        import gatewayclient
        import gatewayserver
        import usocket
        
        networking.establishWiFiConnection ( )
        networking.enableLoRaCommunication ( )
        adc.setupADC ( )
        gatewayclient.initializeGatewayClient ( )
        gatewayserver.initializeGatewayServer ( )
        
        while True:
            dataToForward = gatewayserver.receiveData ( )
            gatewayclient.sendData ( dataToForward )    
            
        gatewayserver.closeGatewayServer ( )
        gatewayclient.closeGatewayClient ( )
        networking.endWiFiConnection ( )
        

        Again, it's the networking.establishWiFiConnection ( ) call that appears to fail ...

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

          Also, checking if machine.reset_cause is not equal to machine.SOFT_RESET before reinitializing the WiFi processor does nothing. All jumpers are in place. The UART connection over a regular USB cable doesn't work either.

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

            @JosSchr
            what is MEHO2NetworkConfiguration.WiFiNetworkingMode value
            Are you sure that it is AP?
            PS. zip file ling is invalid

            think about people first, then computers

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

              @livius
              Hi, Livius! Thank you very much for your response!

              As can be seen on line 64 of networking.py, it is network.WLAN.AP:

                  WiFiNetworkingMode = network.WLAN.AP
              
              1 Reply Last reply Reply Quote 0
              • J Offline
                JosSchr @JosSchr
                last edited by JosSchr

                After doing a lot of extra experimentation, I found the problem to be solvable by moving the WiFi setup code ( so the wlan.init() calls ) to boot.py and by including the mode parameter in the wlan.init() call instead of doing a preliminary call to wlan.mode().

                There are some hickups with connecting to the LoPy through the new configuration on Windows, but it seems to work fine on Linux.

                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