Run file via MicroPython prompt

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
iron2414
Posts: 6
Joined: Tue Oct 18, 2016 7:29 pm

Run file via MicroPython prompt

Post by iron2414 » Wed Oct 19, 2016 8:40 am

I have a WiPy board (1.3) and i'd like to connect to my home network with it, so i could send data via socket to my home server without any internet connection. I have this board almost a year ago, but i have the freetime to use it just now. When i bought it i found some methods in the documentation that i could run any *py file. (For example "run myScript.py" or something like that) and i can't seem to find it anymore, not even the 1.3 documentation for WiPy. Here's my code for connecting to the router:

wlan = WLAN(mode=WLAN.STA)
# go for fixed IP settings
wlan.ifconfig(config=('dhcp'))
wlan.scan() # scan for available networks
wlan.connect(ssid='mySsid', auth=(WLAN.WPA2, 'somePassword'))
while not wlan.isconnected():
pass

I don't want to put the code into main.py or boot.py because i'm not always using the WiPy at home, and i can connect to it only with Wi-Fi, so it would be trouble some when i'm not at my home network.

PS: I'm new to the forum, nice to meet you all. Searched for this already, but couldn't really find anything, so i hope its okay to ask that. :)

User avatar
platforma
Posts: 258
Joined: Thu May 28, 2015 5:08 pm
Location: Japan

Re: Run file via MicroPython prompt

Post by platforma » Wed Oct 19, 2016 10:02 am

Hi and welcome to the forum! Make yourself at home :)

In order to run your scripts on WiPy, you need to have them uploaded onto the board. There's a number of ways of doing this, but using the FTP client is the easiest in my opinion. See http://docs.micropython.org/en/latest/w ... ftp-access for details.

Once you uploaded your myScript.py you can use "import myScript" directly from your main.py or from REPL. You can import selectively by using "from myScript import myClass", if you wish. The import call will load your code in and all classes/functions will be available.

Hope this answers your question!

dwight.hubbard
Posts: 38
Joined: Mon May 16, 2016 6:35 pm

Re: Run file via MicroPython prompt

Post by dwight.hubbard » Tue Nov 01, 2016 3:46 pm

Put the code to configure the device in a module in the lib directory. Then import and run the function to configure it from the repl.

Sent from my SM-G920T using Tapatalk

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Run file via MicroPython prompt

Post by dhylands » Wed Nov 02, 2016 2:12 am

You can also run files by using pyboard.py found in the tools directory:
https://github.com/micropython/micropyt ... pyboard.py

You can run:

Code: Select all

pyboard.py IP-address somefile.py

Post Reply