Wifi and Lora Mesh on Lopy4

  • Hello,

    I have 6 Lopy4s to build a LoRa mesh.

    I used this code :

    It's the code of the simple pymesh. It works very well, my lopy4s are able to connect to the mesh and send pings and messages to each other.

    Now, I would like to implement a WLAN socket. I want my smartphone to send data using wifi to the lopy, and I need the module to receive this wifi data and send it to the LoRa mesh.

    I also need to take the received data from the LoRa mesh and send it to the smartphone via Wifi.

    I tried to actualise the variable "msg" (the message) when the wifi socket receives data but it doesn't work. The ping is sent but nothing else is done.

    Here is my code :


    Ifrom network import LoRa
    from network import WLAN
    import socket
    import time
    import utime
    import ubinascii
    import pycom
    import machine
    import network
    import _thread
    from loramesh import Loramesh
    print('===============  setup as Wifi access point  ==============')
    wlan = network.WLAN()
    print('===============  Wifi initialisation  =====================')
    print('===============  initialisation completed  ================')
    wifi_port = 9999
    wifi_ip = wlan.ifconfig()[0]
    soc=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    print("===============  LoRA init  ===============================")
    lora = LoRa(mode=LoRa.LORA, region=LoRa.EU868, bandwidth=LoRa.BW_125KHZ, sf=7)
    MAC = str(ubinascii.hexlify(lora.mac()))[2:-1]
    print("LoRa MAC: %s"%MAC)
    mesh = Loramesh(lora)
    # waiting until it connected to Mesh network
    while True:
        print("%d: State %s, single %s"%(time.time(), mesh.cli('state'), mesh.cli('singleton')))
        if not mesh.is_connected():
        print('Neighbors found: %s'%mesh.neighbors())
    # create UDP socket
    s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
    myport = 1234
    # handler responisble for receiving packets on UDP Pymesh socket
    def receive_pack():
        # listen for incomming packets
        while True:
            rcv_data, rcv_addr = s.recvfrom(128)
            if len(rcv_data) == 0:
            rcv_ip = rcv_addr[0]
            rcv_port = rcv_addr[1]
            print('Incomming %d bytes from %s (port %d)'%(len(rcv_data), rcv_ip, rcv_port))
            # could send some ACK pack:
            if rcv_data.startswith("Hello"):
                    s.sendto('ACK ' + MAC + ' ' + str(rcv_data)[2:-1], (rcv_ip, rcv_port))
                except Exception:
                    print("ACK failed")
            mesh.blink(7, .3)
    pack_num = 1
    msg = "Hello World! from fipy2, pack: "
    ip = mesh.ip()
    # infinite main loop
    while True:
        print("%d: State %s, single %s, IP %s"%(time.time(), mesh.cli('state'), mesh.cli('singleton'), mesh.ip()))
        # check if topology changes, maybe RLOC IPv6 changed
        new_ip = mesh.ip()
        if ip != new_ip:
            print("IP changed from: %s to %s"%(ip, new_ip))
            ip = new_ip
        # update neighbors list
        neigbors = mesh.neighbors_ip()
        print("%d neighbors, IPv6 list: %s"%(len(neigbors), neigbors))
        # send PING and UDP packets to all neighbors
        for neighbor in neigbors:
            if mesh.ping(neighbor) > 0:
                print('Ping OK from neighbor %s'%neighbor)
                mesh.blink(10, .1)
                print('Ping not received from neighbor %s'%neighbor)
            pack_num = pack_num + 1
                s_data, s_addr = soc.recvfrom(128)
                if len(s_data) != 0:
                    s_ip = s_addr[0]
                    s_port = s_addr[1]
                    print('Incomming %d bytes from %s (port %d)'%(len(s_data), s_ip, s_port))
                    msg = s_data
                s.sendto(msg + str(pack_num), (neighbor, myport))
                print('Sent message to %s'%(neighbor))
            except Exception:
            time.sleep(20 + machine.rng()%20)
        # random sleep time
        time.sleep(30 + machine.rng()%30)

    Can you please tell me what I am doing wrong please ? I've been on it for 2 weeks now and I really struggle with it.

    Thank you very much and have a nice day,


  • @rfinkers Haven't used Atom in a while, and never used on PC (on macOS Copy is Command-C which is different from Ctrl-C, hence no issue in that respect), so I can't really say. But in the worst case you should be able to use the Edit menu?

  • @jcaron Right click in the REPL is not working...?

  • @rfinkers there are plenty of other ways to copy-paste. Right click, menu...

  • @jcaron said in Wifi and Lora Mesh on Lopy4:

    It's always better to copy-paste as text rather than screenshots.

    When you press CTRL-C in the REPL it will stop the all the running code...

  • @CamGro It's always better to copy-paste as text rather than screenshots. That makes it searchable and allows copy-paste, it's much better.

    You should make sure you incoming packet log is different for UDP and LoRa, it would make reading the logs easier.

    Have you tried doing a test with just the UDP socket to test that part? Are you sure whatever you use to send the UDP packet actually works and sends to the correct IP and port?

  • @Martinnn I tried to use the picture option but it didn't work obviously. Sorry for that.

    Here are the pictures :

    fipy 2



    And lopy3 :



  • @CamGro The links you provided load (along with your tiny images) a whole truckload of junk. Why not paste the images here directly?

  • Hello and thank you very much for your answer !

    For now, I just wanted to send the message coming from the wifi socket to all the neighbors in the mesh. Each neighbor has a smartphone so every neighbor's smartphone should read what is sent in the mesh.

    But for now I am just testing it on two modules : one fipy (fipy2) and one lopy (lopy3).

    Here are the results I got on the console. I was sending data on the wifi socket but the modules did not seem to realize it.

    for fipy2 :





    for lopy 3 :

    lopy3 1/2


    lopy3 2/2


    So the default message is sent using the LoRa mesh and received on the other end, because a ACK is sent back.

    However, the message is not actualized with the wifi data.

    Thank you and have a nice day,


  • @CamGro can you share the logs printed by your code?

    Which node do you want to send the UDP packets to? As it stands, it seems your code will sent any received packet to whichever neighbour it is currently looping through, I'm not sure that's what you intended to do.

    Also, you probably want to set the UDP socket to non-blocking.

Log in to reply

Pycom on Twitter