Simple Lopy to Lopy via WiFi
Hi, I have some trouble to connect my two lopy4 via WiFi.
Here is receiver code:
import socket import pycom pycom.heartbeat(False) s = socket.socket() port = 80 s.bind(('0.0.0.0',port)) print("Bind") s.listen(5) print("Listen") while True: pycom.rgbled(0xFFFFFF) # Blue c, addr = s.accept() print('Connection from :', addr) pycom.rgbled(0x00FF00) # Green data = c.recv(64) print(data) c.send(b'Hello')
And this is sender code
from network import WLAN import pycom import machine import socket #Si connette ad una rete con un certo ssid e una certa password ssid = "****" #ssid of the other lopy password = "*****" #password of the other lopy pycom.heartbeat(False) pycom.rgbled(0xFF0000) # Red led at start wlan = WLAN(mode=WLAN.STA) nets = wlan.scan() s = socket.socket() for net in nets: if net.ssid == ssid: print("Found network: " + net.ssid) wlan.connect(net.ssid, auth=(net.sec, password), timeout=5000) while not wlan.isconnected(): machine.idle() # save energy if wlan.isconnected() == True: print('WLAN connection succeeded!') print(wlan.ifconfig()) while True: b = bytes("Hello",'utf-8') s.sendall(b) #send butes pycom.rgbled(0xFFFF00) buffer = s.recv(64) #receive bytes print(buffer) #print bytes received else: print("WLAN connection error")
When the sender codes arrive at line "s.sendall(b)", it gives me OS ERROR: 128, but I don't understand why. Can anyone help me?
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:
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 = '192.168.0.20' myconfig = tuple(lst_myconfig) wlan.ifconfig(config=myconfig) print("Wlan configuration", myconfig) s = socket.socket() s.bind((lst_myconfig,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()
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)[-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?
The socket is not connected. WiFi communication is point2point by default. So you have to provide a target adress and port.
Set up an own network with fix IP numbers. This will be the easiest way to start. You can switch to broadcast, after you managed the p2p communication.
@p-m-97 Maybe you haven't supplied all of your code, but a few things stand out.
In your sender code you don't actually connect to anything with the socket.
Somewhere you need to call
In addition in your listener code, you don't connect to the wifi network and that should occur before you bind.