Socket connect address
-
Re: Sending data via WiFi
If I want to connect to my home router, which address should I enter here: socket.connect(('207.58.139.247', 80))?
-
@michalt38 OK. Then you'll need two steps:
First, you have to connect the WiPy to the network. That's done with wlan.connect, as given in my sample scripts.
Second: you have to write a little program for you WiPy and your PC that connects through sockets and sends the data. The IP adresses used are those of the WiPy and the PC, not the one of the router. You have to define which entity connects (socket.connect() ) and which entity accepts (socket.accept()).
For initial testing, you can use for the PC side netcat (nc), which is a simple raw socket send/receive program.
Examples for the WIPy side are here:
https://docs.python.org/3/howto/sockets.html
or here:
https://www.tutorialspoint.com/python3/python_networking.htm
-
@robert-hh I have to write a program on a PC that can recive data from WiPy through WiFi and I need to send some example data from WiPy to PC.
-
@michalt38
Hello, it seems that things are still a little bit mixed up. There are two kinds of connect we seem to talk about:
a) connect to a network. That's the one you do with the wlan.connect(). Once it succeeds ( and it should it the script returned ('192.168.1.141', '255.255.255.0', '192.168.1.1', '8.8.8.8'), then your little micro is connected to the network created by your router, like you PC or other devices are. You should be able to get a response with ping 192.168.1.141 from it. Once your device is connected to the network, you can access any other entity on your LAN or in the Internet.
c) connect to a service. That's done with socket.connect(), which connects you to another device which has issued a socket.accept(), like a server does. Routers normally do not offer such a service, but they may. For connection, you need the IP-address of the device and it port number of the service.
Now my question is: what is your intention?
-
@michalt38 said in Socket connect address:
I got ('192.168.1.141', '255.255.255.0', '192.168.1.1', '8.8.8.8')
192.168.1.141 is ip configured for your Wipy in network
'255.255.255.0' is net mask
192.168.1.1 is your router
'8.8.8.8' is DNSAnd questions are:
- Is your router IP 192.168.1.1 ?
- is 192.168.1.141 free ip in your network?
- is '255.255.255.0' valid mask for your network - problaby yes but..
-
@robert-hh
I got ('192.168.1.141', '255.255.255.0', '192.168.1.1', '8.8.8.8')
But why I got error 111? Should I configure this socket on router first?
-
@michalt38
You could use something like:import network import time # setup as a station wlan = network.WLAN(mode=network.WLAN.STA) wlan.connect(ssid="your ssid", auth=(network.WLAN.WPA2, "your router password")) while not wlan.isconnected(): time.sleep_ms(1000) print(".", end="") print(wlan.ifconfig())
-
@livius
I've got OSError: 111. What does it mean?
-
@michalt38
this command is not for connecting to router
only to connect one socket with another opened on the end address here 207.58.139.247 and on port here is : 80to connect to router use
wlan.connect(net.ssid, auth=(net.sec, 'mywifikey'), timeout=5000)
details:
https://docs.pycom.io/pycom_esp32/pycom_esp32/tutorial/includes/wlan.html