Difference between revisions of "Tutorial AutoRun"

From Sketching with Hardware at LMU Wiki
Jump to navigation Jump to search
(Created page with "= Run your MicroPyton code directly at Boot-up of ESP32/ESP8266 = In this part of the tutorial, we talk about the boot process and how to start your own code at boot-up time....")
 
Line 9: Line 9:
 
* you have written a main.py that allows to select whether to boot into your code or not
 
* you have written a main.py that allows to select whether to boot into your code or not
  
 +
== Required Module and Files ==
 +
To enable autorun you have to create a file that is called 'main.py' that is in the main directory. There are different ways to do this:
 +
* write the code you want to execute into the file main.py
 +
* write your code as a module that can be executed by calling a function. call this function from main.
 +
 +
== Examples of main.py ==
 +
 +
== Code Example: code in main.py ==
 +
<syntaxhighlight lang="python" line='line'>
 +
# main.py - example how to have the code directly in the main.py file
 +
# here an example for blinking an LED
 +
from machine import Pin
 +
from time import sleep
 +
p25 = Pin(25, Pin.OUT)
 +
 +
while True:
 +
    pin25.on()
 +
    sleep(1)
 +
    pin25.off()
 +
    sleep(0.5)
 +
</syntaxhighlight>
 +
 +
== Code Example: function in moduele and called from main.py ==
 +
<syntaxhighlight lang="python" line='line'>
 +
# main.py - example how to call a function (with your code) from the main.py file
 +
# we assume you have a function mymain() defined in a module mycode.py
 +
import mycode
 +
mycode.mymain()
 +
</syntaxhighlight>
  
 
= Instructional Videos =
 
= Instructional Videos =

Revision as of 09:49, 17 August 2020

Run your MicroPyton code directly at Boot-up of ESP32/ESP8266

In this part of the tutorial, we talk about the boot process and how to start your own code at boot-up time. For this, we show how to use 'main.py' as entry point. We have an example how to use a Pin/switch to select whether to boot into your code or to not start a program.

We also look at how to use the uPyCraft IDE when autorun code, basically how to connect with Putty (or any terminal program) to remove main.py to be able to connect again with uPyCraft IDE.

Success criteria

  • you understand the boot process works (boot.py then main.py)
  • you have written some code and configured the ESP32 so that it is executed at boot time automatically
  • you have written a main.py that allows to select whether to boot into your code or not

Required Module and Files

To enable autorun you have to create a file that is called 'main.py' that is in the main directory. There are different ways to do this:

  • write the code you want to execute into the file main.py
  • write your code as a module that can be executed by calling a function. call this function from main.

Examples of main.py

Code Example: code in main.py

 1 # main.py - example how to have the code directly in the main.py file
 2 # here an example for blinking an LED
 3 from machine import Pin 
 4 from time import sleep
 5 p25 = Pin(25, Pin.OUT)
 6 
 7 while True:
 8     pin25.on()
 9     sleep(1)
10     pin25.off()
11     sleep(0.5)

Code Example: function in moduele and called from main.py

1 # main.py - example how to call a function (with your code) from the main.py file
2 # we assume you have a function mymain() defined in a module mycode.py
3 import mycode
4 mycode.mymain()

Instructional Videos

How to Autorun MicroPython code

This shows how to use main.py as entry point to shart your MicroPython code. It looks at how to stop a running program with a terminal program (e.g. Putty). We introduce how to add a boot selector switch.

There is a video on Youtube (29:23) that shows the software: https://youtu.be/kzgUN_h1bqw