P
Thanks @crumble and @timh for your answers!
I've changed my code, because I want to implement a WiFi Direct between my two Lopy4 ( so one act like AP, and the other like STA). I have changed my code as you advised me:
Server Main
import usocket as socket
import machine
from network import WLAN
wlan = WLAN() # get current object, without changing the mode
if machine.reset_cause() != machine.SOFT_RESET:
wlan.init(mode=WLAN.AP, ssid='Lopy-wlan', auth=(WLAN.WPA2,'www.pycom.io'), channel=10, antenna=WLAN.INT_ANT)
myconfig = wlan.ifconfig()
lst_myconfig = list(myconfig)
lst_myconfig[0] = '192.168.0.20'
myconfig = tuple(lst_myconfig)
wlan.ifconfig(config=myconfig)
print("Wlan configuration", myconfig)
s = socket.socket()
s.bind((lst_myconfig[0],2000))
s.listen(5)
c, addr = s.accept() # Establish connection with client.
print ('Got connection from', addr)
c.send(b'Thank you for connecting') # Send bytes
c.close()
Client Main
from network import WLAN
import pycom
import machine
import socket
ssid1 = "Lopy-wlan"
password = "www.pycom.io"
wlan = WLAN(mode=WLAN.STA) #station
nets = wlan.scan()
s = socket.socket() #create socket
for net in nets:
if net.ssid == ssid1:
print("Found network: " + net.ssid)
wlan.connect(net.ssid, auth=(net.sec, password), timeout=5000) #connected to lopy AP
while not wlan.isconnected():
machine.idle() # save energy
if wlan.isconnected() == True:
print('WLAN connection succeeded!')
s.connect(socket.getaddrinfo('192.168.0.20',2000)[0][-1])
b = bytes("Hello",'utf-8')
s.sendall(b) #send bytes
buffer = s.recv(64) #receive bytes
print(buffer) #print bytes received
s.close()
I've upload Server code on my Lopy server, then I run Client code on my Lopy client, but when I try to connect the socket ( with s.connect(...)) I have OSError: -1.
I've seached on google but I did not find anything.
What can it be?