Send a file from one Lopy to another Lopy



  • It seems that finally they are between lopys and connect because the server prints this:

    0_1523461807143_Captura de pantalla 2018-04-11 17.49.50.png

    Now the client seems to give me another error but this one I do not know what it means:
    0_1523461909712_Captura de pantalla 2018-04-11 17.50.58.png

    OSError: (Errno 113) EHOSTUNREACH

    ¿?



  • Well nothing for more that I try things I can not send anything. I keep writing to see if anyone can help me.

    This is the code that runs the server Lopy. About this code I do not know if there will be problems because as I can not connect, I do not know if something fails. But create the wifi network in which the client connects correctly as you can see in the client capture:

    def iniciarFTP():
        try:
            import network
            from network import WLAN
            wlan = network.WLAN(mode=network.WLAN.STA)
            wlan.init(mode=WLAN.AP, ssid='Gateway1', auth=(WLAN.WPA2,'witeklab@2018'), channel=7, antenna=WLAN.INT_ANT)
            from network import Server
            server = Server(login=('micro', 'python'), timeout=600)
            server.timeout(300)
            server.timeout()
            print(server.isrunning())
            return True
        except:
            return False
    
    #Iniciamos servidor:
    iniciarFTP()
    
    #Ponemos a la escucha:
    import socket
    
    f = open('recieved.txt', 'w')
    eggs = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    eggs.bind(('', 8206))
    eggs.listen(5)
    while True:
        channel, details = eggs.accept()
        print ('connected')
        got = channel.recv(4096)
        f.write(got)
        channel.send("recieved!")
        channel.close()
    

    This is the code of the Lopy client in which I get an error on line 47.
    What I do is look for the Wi-Fi network that the Lopy server generates and connect me as a client. So far so good. The next thing I try is with the ip obtained from the connection I assign to the variable 'HOST' the ip of the Lopy server and the PORT because it is listening. But when doing 's.connect (ADDR)' gives error:
    0_1523460319728_Captura de pantalla 2018-04-11 17.17.46.png

    complete code of the client Lopy:

    from network import WLAN
    
    import socket
    import sys
    
    
    def buscarWifi(ssid):
        #Ponemos el wifi en modo estacion:
        wlan = WLAN(mode=WLAN.STA)
    
        #Buscamos wifi:
        redes = wlan.scan()
        print('Redes disponibles: ',len(redes))
    
        for red in redes:
            if red.ssid == ssid:
                wlan.connect(red.ssid, auth=(red.sec,'witeklab@2018'), timeout=5000)
                while not wlan.isconnected():
                    machine.idle() # save power while waiting
                print('WLAN connection succeeded!')
                return True,wlan
                break
            else:
                return False,wlan
    ok,w = buscarWifi('Gateway1')
    if ok:
        print('ok')
        ip = w.ifconfig()
        print('ip destino = ',ip[0])
        print(w.ifconfig())
        print(w.isconnected())
    
        HOST = ip[0]
        PORT = 8206
        ADDR = (HOST,PORT)
        BUFSIZE = 4096
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect(ADDR)    
        f = open("prueba.txt")
        linea = f.readline()
        while linea != "":
            print(linea)
            linea = f.readline()
            s.send(linea)
        f.close()
        s.close()
    
    else:
        print('Connection failed!')
    

    I have tried many more ways to do it but almost all of them gave me another type of errors. For example I also found an adaptation of the python FTP library for micro Python but it did not work either ... At last and the caba I just want to do the same thing that Filezilla does when you connect to the Wi-Fi of a Lopy that is sending you a file .. .

    If someone can help me? thanks in advance, regards.



  • Up, some good Samaritan who gives me a hand with the code ...

    Is that there is no sure way that if it surpasses what I am doing in each moment I would get to get it out, better or worse but I would take it out. But there is no way ...

    If I do this on the server:

    def iniciarFTP():
        try:
            import network
            from network import WLAN
            wlan = network.WLAN(mode=network.WLAN.STA)
            wlan.init(mode=WLAN.AP, ssid='Gateway1', auth=(WLAN.WPA2,'witeklab@2018'), channel=7, antenna=WLAN.INT_ANT)
            from network import Server
            server = Server(login=('micro', 'python'), timeout=600)
            server.timeout(300)
            server.timeout()
            print(server.isrunning())
            return True
        except:
            return False
    
    iniciarFTP()
    

    and in the client I connect like this:

    from network import WLAN
    wlan = WLAN(mode=WLAN.STA)
    
    nets = wlan.scan()
    for net in nets:
        print (net.ssid)
        if net.ssid == 'Gateway1':
            print('Network found!')
            wlan.connect(net.ssid, auth=(net.sec, 'witeklab@2018'), timeout=5000)
            while not wlan.isconnected():
                machine.idle() # save power while waiting
            print('WLAN connection succeeded!')
            wlan.ifconfig()
            break
    

    I just need the socket type code to send a file.txt but for more than try it gives me error with the name of the server or a thousand other things ...

    I can not get it out, if someone is bored a moment and can write the code of the client and server to send and receive via wifi would be very grateful because now I just want to send them to that space. XD

    please please :(



  • @seb
    I have searched a lot and I have not come across any example that will finally help me to know how to send a file by socket.

    I know it's a lot to ask but ...Please, do you think it would be possible for you to write me two examples of how to send it and how to receive it from lopy to lopy with micro python using socket?

    I have searched a lot and I have not come across any example that will finally help me to know how to send a file by socket.

    I know it's a lot to ask but ... Do you think it would be possible for you to write me two examples of how to send it and how to receive it from lopy to lopy with micro python using socket?

    thank you very much!



  • @zceld

    Look into the "select" library or using threads if you want to do things concurrently



  • @seb said in Send a file from one Lopy to another Lopy:

    Just look up any standard python example of sending files and this will most likely port directly over to micropython. The basic idea is that you open the file you want to send, read it line by line and then send it line by line. On the other end you just need to read and write to a file.
    Looking at the code you have posted above I have two comments:

    Make the socket blocking before sending data because you might be closing the socket before the data has actually been sent if you do it non-blocking.
    I would recommend sending the file line by line instead of reading the whole file into the bytes variabile. You might find with large files you will run out of RAM because of this.

    But ... if the destination Lopy is executing an infinite loop carrying out some processes, is it possible to send it over wifi without having to listen, or anything like FTP?



  • Just look up any standard python example of sending files and this will most likely port directly over to micropython. The basic idea is that you open the file you want to send, read it line by line and then send it line by line. On the other end you just need to read and write to a file.

    Looking at the code you have posted above I have two comments:

    1. Make the socket blocking before sending data because you might be closing the socket before the data has actually been sent if you do it non-blocking.
    2. I would recommend sending the file line by line instead of reading the whole file into the bytes variabile. You might find with large files you will run out of RAM because of this.

 

Pycom on Twitter