How to include a module

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
john7
Posts: 10
Joined: Mon Mar 16, 2020 11:09 am

How to include a module

Post by john7 » Mon Mar 16, 2020 11:19 am

in wifi.py I have

Code: Select all

import network

class WIFI:
    def do_connect():
    #import network
    station = network.WLAN(network.STA_IF)
    station.active(True)
    if not station.isconnected():
        print('connecting to network...')
        station.connect('Niron', 'projects1234')
        while not station.isconnected():
            pass
    print('network config:', station.ifconfig())
in main.py

Code: Select all

#import wifi 
from wifi import WIFI
I get an error
ImportError: no module named 'wifi'
What should I do?

User avatar
OlivierLenoir
Posts: 126
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: How to include a module

Post by OlivierLenoir » Mon Mar 16, 2020 4:08 pm

You forget to indent below def do_connect():.

john7
Posts: 10
Joined: Mon Mar 16, 2020 11:09 am

Re: How to include a module

Post by john7 » Mon Mar 16, 2020 4:41 pm

OlivierLenoir wrote:
Mon Mar 16, 2020 4:08 pm
You forget to indent below def do_connect():.
It's copy/paste problem. In the code I have all indents. Besides the compiler alerts in case of bad indentation.

I use the Thonny IDE in case if it's important.

User avatar
tve
Posts: 216
Joined: Wed Jan 01, 2020 10:12 pm
Location: Santa Barbara, CA
Contact:

Re: How to include a module

Post by tve » Mon Mar 16, 2020 7:35 pm

The error gets eaten, it's very annoying. Connect to the REPL and try to import wifi manually, then you will see the error that is preventing import.

User avatar
OlivierLenoir
Posts: 126
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: How to include a module

Post by OlivierLenoir » Mon Mar 16, 2020 7:38 pm

Can you try adding self in def do_connect(self):?

I've made the following test in repl.

Code: Select all

MicroPython v1.12 on 2019-12-20; ESP32 module with ESP32
Type "help()" for more information.
>>> 
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== import network
=== 
=== class WIFI:
===     def do_connect(self):
===         station = network.WLAN(network.STA_IF)
===         station.active(True)
===         if not station.isconnected():
===             print('connecting to network...')
===             station.connect('Niron', 'projects1234')
===             while not station.isconnected():
===                 pass
===         print('network config:', station.ifconfig())
>>> c = WIFI()
>>> c.do_connect()
I (16280) wifi: wifi driver task: 3ffe2f38, prio:23, stack:3584, core=0
I (32778) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (32788) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (32818) wifi: wifi firmware version: aeed694
I (32818) wifi: config NVS flash: enabled
I (32818) wifi: config nano formating: disabled
I (32818) wifi: Init dynamic tx buffer num: 32
I (32828) wifi: Init data frame dynamic rx buffer num: 32
I (32828) wifi: Init management frame dynamic rx buffer num: 32
I (32838) wifi: Init management short buffer num: 32
I (32838) wifi: Init static rx buffer size: 1600
I (32848) wifi: Init static rx buffer num: 10
I (32848) wifi: Init dynamic rx buffer num: 32
I (32948) phy: phy_version: 4102, 2fa7a43, Jul 15 2019, 13:06:06, 0, 0
I (32948) wifi: mode : sta (24:6f:28:17:ee:3c)
I (32948) wifi: STA_START
connecting to network...
I (35008) wifi: STA_DISCONNECTED, reason:201
no AP found
I (37058) wifi: STA_DISCONNECTED, reason:201
no AP found

User avatar
OlivierLenoir
Posts: 126
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: How to include a module

Post by OlivierLenoir » Mon Mar 16, 2020 7:44 pm

I'm connected when I put my personnel wifi network parameter.

Code: Select all

I (32594) network: CONNECTED
I (35534) event: sta ip: 192.168.100.83, mask: 255.255.255.0, gw: 192.168.100.254
I (35534) network: GOT_IP
network config: ('192.168.100.83', '255.255.255.0', '192.168.100.254', '192.168.100.254')

User avatar
OlivierLenoir
Posts: 126
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: How to include a module

Post by OlivierLenoir » Mon Mar 16, 2020 8:03 pm

I should consider the following code to be able to provide SSID and password.

Code: Select all

import network

class WIFI(object):

    def __init__(self, ssid, passwd):
        self.ssid = ssid
        self.passwd = passwd

    def do_connect(self):
        self.station = network.WLAN(network.STA_IF)
        self.station.active(True)
        if not self.station.isconnected():
            print('connecting to network...')
            self.station.connect(self.ssid, self.passwd)
            while not self.station.isconnected():
                pass
        print('network config:', self.station.ifconfig())

conn = WIFI('Niron', 'projects1234')
conn.do_connect()

john7
Posts: 10
Joined: Mon Mar 16, 2020 11:09 am

Re: How to include a module

Post by john7 » Tue Mar 17, 2020 5:57 am

OlivierLenoir wrote:
Mon Mar 16, 2020 8:03 pm
I should consider the following code to be able to provide SSID and password.
the code is working if I put it in main.py. The problem to import from another file.

john7
Posts: 10
Joined: Mon Mar 16, 2020 11:09 am

Re: How to include a module

Post by john7 » Tue Mar 17, 2020 5:59 am

tve wrote:
Mon Mar 16, 2020 7:35 pm
The error gets eaten, it's very annoying. Connect to the REPL and try to import wifi manually, then you will see the error that is preventing import.
As I understand Thonny looks in installation directory when it sees an import directive. May be I should provide the full path to the file?

User avatar
OlivierLenoir
Posts: 126
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: How to include a module

Post by OlivierLenoir » Tue Mar 17, 2020 8:46 am

john7 wrote:
Tue Mar 17, 2020 5:57 am
the code is working if I put it in main.py. The problem to import from another file.
  1. Create file wifi.py on your computer with the here bellow code
  2. Connect to your board rshell -p /dev/ttyUSB0
  3. Use rshell to copy wifi.py on your board cp wifi.py /pyboard/
  4. From rshell start repl
  5. You can now import your module from wifi import WIFI
  6. ...

Code: Select all

olivier@ole02-t510:~/Documents/MicroPython/Forum$rshell -p /dev/ttyUSB0
[i]Using buffer-size of 32
Connecting to /dev/ttyUSB0 (buffer-size 32)...
Trying to connect to REPL  connected
Testing if ubinascii.unhexlify exists ... Y
Retrieving root directories ... /boot.py/
Setting time ... Mar 17, 2020 09:33:27
Evaluating board_name ... pyboard
Retrieving time epoch ... Jan 01, 2000
Welcome to rshell. Use Control-D (or the exit command) to exit rshell.[/i]
/home/olivier/Documents/MicroPython/Forum> cp wifi.py /pyboard/
/home/olivier/Documents/MicroPython/Forum> repl
Entering REPL. Use Control-X to exit.
>
MicroPython v1.12 on 2019-12-20; ESP32 module with ESP32
Type "help()" for more information.
>>> from wifi import WIFI
>>>conn = WIFI('Niron', 'projects1234')
>>>conn.do_connect()
File wifi.py

Code: Select all

import network

class WIFI(object):

    def __init__(self, ssid, passwd):
        self.ssid = ssid
        self.passwd = passwd

    def do_connect(self):
        self.station = network.WLAN(network.STA_IF)
        self.station.active(True)
        if not self.station.isconnected():
            print('connecting to network...')
            self.station.connect(self.ssid, self.passwd)
            while not self.station.isconnected():
                pass
        print('network config:', self.station.ifconfig())

Post Reply