I raise the plot to see if you can help me. Is the next: I'm linking two devices that work with micro-python language and have chips to use Wi-Fi as scheduled. Well basically the idea is to develop a program that on the server chip (hereafter chipS) Wi-Fi is activated in AP (Access Point) mode as a server so that the client chip (hereinafter chipC) connects to the and this one sends a file.txt to him by means of socket since to realize a coexion type FTP there is no library of this one in micropython. Although it's funny because from Windows with the Filezilla client if I can connect to the chipS and upload a file. That's basically what I want to do but with the chipC.
Finally, if someone knows how to use an FTP library adapted to micropython and explains how it is used, I also accept it.
Well at the moment I am trying with socket but I do not know what is wrong or how it would be done, for examples that I have read I have made the following programming but it also gives me errors.
In the case of chipS the code used is:
import socket # Import socket module
import network
import gc
from network import WLAN
from network import Server
def iniciarFTP():
try:
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)
server = Server(login=('micro', 'python'), timeout=600)
#server.timeout(300)
#print(server.isrunning())
print(wlan.ifconfig())
return True
except:
return False
#Iniciamos servidor:
iniciarFTP()
host = ''
port = 60000 # Reserve a port for your service.
s = socket.socket() # Create a socket object
gc.collect()
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
print ('Server listening....')
while True:
conn, addr = s.accept() # Establish connection with client.
print ('Got connection from', addr)
data = conn.recv(256)
print('Server received', repr(data))
with open('received_file.txt', 'wb') as f:
print ('file opened')
while True:
print('receiving data...')
s.setblocking(True)
print('Memoria->',str(gc.mem_free()))
data = s.recv(256)
print('M despues->',str(gc.mem_free()))
print('data=%s', (data))
if not data:
break
# write data to a file
f.write(data)
f.close()
print('Successfully get the file')
s.close()
print('connection closed')
conn.send('Thank you for connecting')
conn.close()
gc.collect()
And upon receiving the client's connection it indicates' Got connection from ('192.168.4.2', 49429) But the client has already failed:
This is the code used in the chipC:
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 c in range(len(redes)):
#print(c,'->',redes[c])
#if ssid in redes[c]: //Otra opción de encontrar el ssid
#print("Encontrado en:",redes[c])
#return True
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:
ip = w.ifconfig()
#(ip, subnet, gateway, dns)
print('ip destino = ',ip[2]) #ip del servidor wifi que tambien es la ip al que le quiero enviar el socket/archivo
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = ip[2]
port = 60000
client.connect((host, port)) ## <-- Failure
filepath = 'prueba.txt'
with open(filepath) as fp:
line = fp.readline()
while line:
print("Line {}".format(line.strip()))
line = fp.readline()
client.send(line)
client.close()
To add comment that the chips are: Lopy Pycom company and that both this test and others tambén I have raised in their official forum but have not helped me to see if here and in Spanish I have more luck, likewise I leave the link: link