Page 1 of 1

Python3 network module seems not to have WLAN attribute

Posted: Sat Jun 01, 2019 7:57 pm
by RobinMosedale
I'm successfully setting up esp32's to connect to my internal Wifi network, and turning to the Pi I' attempting to do the same before progressing to socket comms.


after installing the network module and attempting:
import network
station=network.WLAN(network.STA_IF)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'WLAN'
>>>

having listed the help on 'network' it confirms no such attribute to the object.

I'm surprised, this seems to be standard practice elsewhere.

How do I set up a connection to WIFI under python3 or python2.7 if WLAN isn't included?

Re: Python3 network module seems not to have WLAN attribute

Posted: Sat Jun 01, 2019 8:36 pm
by DougieLawson
Rename your python program to ANY name other than network.py.

Re: Python3 network module seems not to have WLAN attribute

Posted: Sat Jun 01, 2019 8:43 pm
by RobinMosedale
I'm under REPL interactive line by line input as i have done with esp32.

Perhaps I've downloaded the wrong module 'network'

If someone could point me to a reliable source for the network module I'd be most grateful

Re: Python3 network module seems not to have WLAN attribute

Posted: Sat Jun 01, 2019 9:51 pm
by DougieLawson
I think the docs you're using don't match the module you're importing

Code: Select all

>>> dir(network)
['BUFFER_SIZE', 'BinaryConnection', 'Connection', 'PORT', 'SERVER_HOST', 'TextConnection', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_clientOpen', '_close', '_listenerThreadBody', '_receive', '_send', '_serverAccept', '_serverWait', '_startListenerThread', '_stopListenerThread', 'call', 'conn', 'hangUp', 'isConnected', 'say', 'socket', 'thread', 'time', 'trace', 'wait', 'whenHungUp']
>>>
http://blog.whaleygeek.co.uk/raspberry- ... in-python/

https://pypi.org/project/network/

Re: Python3 network module seems not to have WLAN attribute

Posted: Sat Jun 01, 2019 10:25 pm
by RobinMosedale
Thanks Dougie, I'll do a thorough check tomorrow

Re: Python3 network module seems not to have WLAN attribute

Posted: Sun Jun 02, 2019 11:51 am
by RobinMosedale
Downloaded the indicated module, inspected, and that doesn't contain the standard WLAN objects either.

I am merely at this stage establishing a station and connecting to the Wifi. Nothing more at this stage.



I'm puzzled.
Nearly all the python examples that come up on searches use these objects 'WLAN, with STA_IF parameters), yet
1. There's no 'network' module in standard distros.
2. module network.py isn't in the standard repository
3. The one's Pip3'd down similarly don't include any WLAN reference.

These are used to log on and establish the connection before setting up the sockets.
Perhaps Raspberry pi doesn't let you do that since it's variant of LINUX insists on control

Neither can I find raspberry pi examples of simply detecting, logging on and establishing Wifi link.

Some serious opacity and timewasting here.

Perhaps I should ignore the wifi capture and just assume connection to the correct access point via Linux and just bash on with setting up sockets?

Pretty fundamental simple stuff.

Re: Python3 network module seems not to have WLAN attribute

Posted: Sun Jun 02, 2019 2:27 pm
by DougieLawson
Post a link to the instructions/tutorial you are following. Those docs clearly don't match what you've installed and how you're trying to use it.

We can't help without that after the Great Crystal Ball Disaster of 1989.

Re: Python3 network module seems not to have WLAN attribute

Posted: Sun Jun 02, 2019 3:09 pm
by B.Goode
How do I set up a connection to WIFI under python3 or python2.7 if WLAN isn't included?


I don't mean to tell you your approach is wrong, but I'll risk asking "why do you need to?"


Establishing a system-wide association with a WiFi Access Point from the Raspbian Operating System, either from the Desktop or a shell (command line) prompt is well documented by the Raspberry Pi Foundation. https://www.raspberrypi.org/documentati ... /README.md

Re: Python3 network module seems not to have WLAN attribute

Posted: Sun Jun 02, 2019 7:58 pm
by RobinMosedale
Here is the code:-

Code: Select all

import network
import time
station=network.WLAN(network.STA_IF)
station.active(True)
station.connect("TP-LINK_EB850C", "ju87bstuka")
while (not (station.isconnected())):
        print("waiting to Connect")
        print (station.ifconfig())
        time.sleep(1)
station.ifconfig()
print ("Connected")
Context:-
This is the initial stage of a monitoring and control system.
There are 5 esp32's performing inner-loop monitoring and control.
The raspberry pi is outer-loop control.
The esp's confine their activity to low level interrupt handling, enabling and disabling interrupts from IR sensors dependent on their physical location. They also control low level functions concerning speed and error recognition. They communicate back to the raspberry pi which performs overall mode, control and health monitoring.
The Raspberry pi sets the mode of operation, monitors the low level speed and position data, and control sequencing of commands to its own actuators (of whatever form) and the esp simple network. It also deals with configuration and calibration.
The esp32's all act as clients to the Raspberry pi server.
However, the top node esp32 operates as a WIFI access point, and all remaining esp32's and raspberry pi must log onto that secure private network. There is no access to external LANs. It is a private LAN.

The above code establishes a connection with the esp32 wifi access point. The station 'connect' parameters are merely examples.

This is a monitoring and control system.
It is autonomous, and must restart after glitch or power fail.
The raspberry pi being a general purpose machine at other times, logs on to one of its listed Wifi access points for normal internet and Lan connection.
For this application it must be directed to log off any of the WAN/LAN connections and connect to the ESP32's control system's isolated access point. Here enumerated as 'Control-net'

The above code runs as expected on the esp32 (micropython) with python module 'network' imported and the attributes as above.
This is output:-
waiting to Connect
('0.0.0.0', '0.0.0.0', '0.0.0.0', '0.0.0.0')
waiting to Connect
('0.0.0.0', '0.0.0.0', '0.0.0.0', '0.0.0.0')
waiting to Connect
('0.0.0.0', '0.0.0.0', '0.0.0.0', '0.0.0.0')
waiting to Connect
('0.0.0.0', '0.0.0.0', '0.0.0.0', '0.0.0.0')
waiting to Connect
('0.0.0.0', '0.0.0.0', '0.0.0.0', '0.0.0.0')
waiting to Connect
('0.0.0.0', '0.0.0.0', '0.0.0.0', '0.0.0.0')
waiting to Connect
('0.0.0.0', '0.0.0.0', '0.0.0.0', '0.0.0.0')
waiting to Connect
('0.0.0.0', '0.0.0.0', '0.0.0.0', '0.0.0.0')
waiting to Connect
('0.0.0.0', '0.0.0.0', '0.0.0.0', '0.0.0.0')
waiting to Connect
('0.0.0.0', '0.0.0.0', '0.0.0.0', '192.168.1.254')
Connected


All good and as desired.
After the connections are established, the sockets are instantiated, used, closed in the normal manner.

This is the first stage in setting up the private control network. It's very very simple. All the esp's and the raspberry pi need to perform this initial step.
Similar control is needed for the raspberry pi. It appears that there are no such wlan control objects in the raspberry module 'network'.
Am I right?

If so, I'll chuck the Pi away and use something more predicative.

Best wishes,
Robin Mosedale

Re: Python3 network module seems not to have WLAN attribute

Posted: Sun Jun 02, 2019 11:14 pm
by DirkS
RobinMosedale wrote:
Sun Jun 02, 2019 7:58 pm
Note
The indentation around:-
"print("waiting to Connect")
print (station.ifconfig())
time.sleep(1)
"
........................... keeps being removed by this webpage no matter whether I substitute spaces or not. Be assured they are 'Python' indented
Put your code in a 'code block' to preserve formatting
viewtopic.php?f=32&t=84477

Re: Python3 network module seems not to have WLAN attribute

Posted: Mon Jun 03, 2019 9:55 am
by markkuk
You are tring to run MicroPython code on a RPi. The network module of MicroPython is not a part of the standard Python language and it's not available (or necessary) on Raspbian Linux. Setting up a WLAN connection is handled entirely by the operating system: https://www.raspberrypi.org/documentati ... /README.md. Your Python program on the Pi should use either the socket API or some higher level network library.

Re: Python3 network module seems not to have WLAN attribute

Posted: Mon Jun 03, 2019 12:09 pm
by RobinMosedale
DirkS wrote:
Sun Jun 02, 2019 11:14 pm
RobinMosedale wrote:
Sun Jun 02, 2019 7:58 pm
Note
The indentation around:-
"print("waiting to Connect")
print (station.ifconfig())
time.sleep(1)
"
........................... keeps being removed by this webpage no matter whether I substitute spaces or not. Be assured they are 'Python' indented
Put your code in a 'code block' to preserve formatting
viewtopic.php?f=32&t=84477
Thank you, corrected.

Robin

Re: Python3 network module seems not to have WLAN attribute

Posted: Mon Jun 03, 2019 12:19 pm
by RobinMosedale
markkuk wrote:
Mon Jun 03, 2019 9:55 am
You are tring to run MicroPython code on a RPi. The network module of MicroPython is not a part of the standard Python language and it's not available (or necessary) on Raspbian Linux. Setting up a WLAN connection is handled entirely by the operating system: https://www.raspberrypi.org/documentati ... /README.md. Your Python program on the Pi should use either the socket API or some higher level network library.
Thank you for taking the trouble to respond to this post.
"You are tring to run MicroPython code on a RPi. "

Incorrect. There is no variant of micropython for raspberry pi.
This is esp32 code. It is directly similar to the standard code that commonly runs python on many other platforms. It seems just deficient for the RPi, and that confirmation is what I'm asking.

"The network module of MicroPython is not a part of the standard Python language and it's not available (or necessary) on Raspbian Linux. "

I figured that out; see the posts. Not necessary?

"Setting up a WLAN connection is handled entirely by the operating system: https://www.raspberrypi.org/documentati ... /README.md. "

As suspected, that there is no WLAN functionality for RPi, as there is in most other ports.

I'll either use scripts, or OS calls from Python


"Your Python program on the Pi should use either the socket API or some higher level network library."

Read the post.

Thanks anyway.

May port to a more appropriate platform, or use use the OS calls
Robin

Re: Python3 network module seems not to have WLAN attribute

Posted: Thu Jun 27, 2019 10:50 am
by blimpyway
Any platform with a full-fledged linux will not have python libraries to connect/disconnect wifi access points. You need to either use OS specific configuration for a permanent connection or command line interface for dynamic connect/disconnect.

Since uPython on ESP12/32 does not have an underlying OS, it has to have a non-standard python module (import network) which provides this OS-level functionality on ESP hardware.

That's what markkuk meant when he said you should not use MicroPython code on Pi