LoPy to LoPy communication over WiFi
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 )
- list item you very much in advance,
- list item you very much in advance,
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.
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
Are you sure that it is AP?
PS. zip file ling is invalid
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.
For extra convenience, here are the relevant files' contents:
''' 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
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 ...