mirror of
https://github.com/sascha-hemi/pycom-documentation.git
synced 2026-03-21 09:04:12 +01:00
Restructure, rev1
* Products
-> updated with new products
-> added accessories
* Getting started
-> re-structured getting started guide
-> removed some of the advanced stuff
* Tutorials / Examples
-> added categories
-> added new basic tutorials Sleep, GPIO and Pring
-> added WiFi ap / sta tutorial
-> added wifi Scan MAC tutorial
* Firmware API
-> added pysense pytrack pygate categories here
* Datasheets
-> added CE FCC and RoHS documents
-> added pysense2 and pytrack 2 templates
* Update firmware
-> new section, added all methods of updating the firmware
* License
-> put license in its own section
general remarks:
-> updated the layout / theme
no more red code text
codeblocks actually work now
-> general layout updates, removed the old html structures (mostly)
This commit is contained in:
0
content/tutorials/basic/_index.md
Normal file
0
content/tutorials/basic/_index.md
Normal file
42
content/tutorials/basic/gpio.md
Normal file
42
content/tutorials/basic/gpio.md
Normal file
@@ -0,0 +1,42 @@
|
||||
---
|
||||
title: "GPIO"
|
||||
aliases:
|
||||
- tutorials/basic/gpio.html
|
||||
- tutorials/basic/gpio.md
|
||||
- chapter/tutorials/basic/gpio
|
||||
---
|
||||
The module has several spare General Purpose Input-Output (GPIO) pins available for you to use with your own sensors and actuators.
|
||||
|
||||
## Output
|
||||
Controlling the GPIO pins of the modules is rather easy. In the example below, we can let the orange LED on the expansion board blink.
|
||||
|
||||
```python
|
||||
from machine import Pin
|
||||
import time
|
||||
led = Pin('P9', mode=Pin.OUT)
|
||||
|
||||
while True:
|
||||
print("high")
|
||||
led.value(1)
|
||||
time.sleep(1)
|
||||
print("low")
|
||||
led.value(0)
|
||||
time.sleep(1)
|
||||
```
|
||||
## Input
|
||||
|
||||
Sometimes, it would be useful to know the state of a pin. For example, you could use the button on the xpansion board to toggle the led
|
||||
|
||||
```python
|
||||
from machine import Pin
|
||||
import time
|
||||
led = Pin('P9', mode = Pin.OUT)
|
||||
button = Pin('P10', mode = Pin.IN)
|
||||
|
||||
while True:
|
||||
if(button() == 1):
|
||||
led.value(1)
|
||||
else:
|
||||
led.value(0)
|
||||
```
|
||||
|
||||
14
content/tutorials/basic/print.md
Normal file
14
content/tutorials/basic/print.md
Normal file
@@ -0,0 +1,14 @@
|
||||
---
|
||||
title: "Print"
|
||||
aliases:
|
||||
- tutorials/all/print.html
|
||||
- tutorials/all/print.md
|
||||
- chapter/tutorials/basic/print
|
||||
---
|
||||
|
||||
Using the `print()` statements in your python script is quite easy. But did you know you can also concatinate strigns and variables inline? If you are formiliar with C, it's functionality is similar to the `printf()` function, but with `\n` always included.
|
||||
|
||||
```python
|
||||
import machine
|
||||
print("hello world: " + str(machine.rng()) + " random number" )
|
||||
```
|
||||
51
content/tutorials/basic/repl.md
Normal file
51
content/tutorials/basic/repl.md
Normal file
@@ -0,0 +1,51 @@
|
||||
---
|
||||
title: "REPL"
|
||||
aliases:
|
||||
- tutorials/all/repl.html
|
||||
- tutorials/all/repl.md
|
||||
- chapter/tutorials/all/repl
|
||||
---
|
||||
|
||||
Using the Pymakr Plugin, open and connect a device or use serial terminal (PuTTY, screen, picocom, etc). Upon connecting, there should be a blank screen with a flashing cursor. Press Enter and a MicroPython prompt should appear, i.e. `>>>`. Let's make sure it is working with the obligatory test:
|
||||
|
||||
```python
|
||||
>>> print("Hello LoPy!")
|
||||
Hello LoPy!
|
||||
```
|
||||
|
||||
In the example above, the `>>>` characters should not be typed. They are there to indicate that the text should be placed after the prompt. Once the text has been entered `print("Hello LoPy!")` and pressed `Enter`, the output should appear on screen, identical to the example above.
|
||||
|
||||
Basic Python commands can be tested out in a similar fashion.
|
||||
|
||||
If this is not working, try either a hard reset or a soft reset; see below.
|
||||
|
||||
Here are some other example, utilising the device's hardware features:
|
||||
|
||||
```python
|
||||
>>> from machine import Pin
|
||||
>>> led = Pin('G16', mode=Pin.OUT, value=1)
|
||||
>>> led(0)
|
||||
>>> led(1)
|
||||
>>> led.toggle()
|
||||
>>> 1 + 2
|
||||
3
|
||||
>>> 5 / 2
|
||||
2.5
|
||||
>>> 20 * 'py'
|
||||
'pypypypypypypypypypypypypypypypypypypypy'
|
||||
```
|
||||
|
||||
## Resetting the Device
|
||||
|
||||
If something goes wrong, the device can be reset with two methods. The first is to press `CTRL-D` at the MicroPython prompt, which will perform a soft reset. A message, as following, will appear:
|
||||
|
||||
```python
|
||||
>>>
|
||||
PYB: soft reboot
|
||||
MicroPython v1.4.6-146-g1d8b5e5 on 2016-10-21; LoPy with ESP32
|
||||
Type "help()" for more information.
|
||||
>>>
|
||||
```
|
||||
|
||||
If that still isn't working a hard reset can be performed (power-off/on) by pressing the `RST` switch (the small black button next to the RGB LED). Using telnet, this will end the session, disconnecting the program that was used to connect to the Pycom Device.
|
||||
|
||||
39
content/tutorials/basic/rgbled.md
Normal file
39
content/tutorials/basic/rgbled.md
Normal file
@@ -0,0 +1,39 @@
|
||||
---
|
||||
title: "RGB LED"
|
||||
aliases:
|
||||
- tutorials/all/rgbled.html
|
||||
- tutorials/all/rgbled.md
|
||||
- chapter/tutorials/all/rgbled
|
||||
---
|
||||
|
||||
By default the heartbeat LED flashes in blue colour once every 4s to signal that the system is alive. This can be overridden through the `pycom` module.
|
||||
|
||||
```python
|
||||
import pycom
|
||||
|
||||
pycom.heartbeat(False)
|
||||
pycom.rgbled(0xff00) # turn on the RGB LED in green colour
|
||||
```
|
||||
|
||||
The heartbeat LED is also used to indicate that an error was detected.
|
||||
|
||||
The following piece of code uses the RGB LED to make a traffic light that runs for 10 cycles.
|
||||
|
||||
```python
|
||||
import pycom
|
||||
import time
|
||||
|
||||
pycom.heartbeat(False)
|
||||
for cycles in range(10): # stop after 10 cycles
|
||||
pycom.rgbled(0x007f00) # green
|
||||
time.sleep(5)
|
||||
pycom.rgbled(0x7f7f00) # yellow
|
||||
time.sleep(1.5)
|
||||
pycom.rgbled(0x7f0000) # red
|
||||
time.sleep(4)
|
||||
```
|
||||
|
||||
Here is the expected result:
|
||||
|
||||

|
||||
|
||||
113
content/tutorials/basic/sleep.md
Normal file
113
content/tutorials/basic/sleep.md
Normal file
@@ -0,0 +1,113 @@
|
||||
|
||||
---
|
||||
title: "Sleep"
|
||||
aliases:
|
||||
- tutorials/all/sleep.html
|
||||
- tutorials/all/sleep.md
|
||||
- chapter/tutorials/basic/sleep
|
||||
---
|
||||
|
||||
There are several methods to make your device sleep. First we cover the basic sleep. Similar to `delay()` used in Arduino, sleep will yield your program until the time is over. Important is that the all microcontroller functions keep running. Also the LoRa, SigFox and LTE modems can be used directly (without re-attaching) after regular sleep.
|
||||
|
||||
```python
|
||||
import time
|
||||
|
||||
time.sleep(1) #sleep 1 second
|
||||
|
||||
time.sleep_ms(10) #sleep 10 milliseconds
|
||||
|
||||
time.sleep_us(10) #sleep 10 microseconds
|
||||
```
|
||||
|
||||
Similar to `yield()`, in micropython we use
|
||||
```python
|
||||
import machine
|
||||
machine.idle()
|
||||
```
|
||||
|
||||
### Power saving
|
||||
To save power, we can also put the controller into sleep modes using the following examples.
|
||||
|
||||
#### Light sleep
|
||||
|
||||
The `machine.sleep()` command will put the controller into a light sleep mode. WiFi and BLE are switched off, but the main CPU and RAM are still running. the LoRa, SigFox and LTE modems are stopped as well and have to be re-initialized after wakeup. The controller will continue running the code after waking up. GPIO states are also conserved. Setting the second argument to `True` will restore the WiFi and BLE after wakeup.
|
||||
|
||||
```python
|
||||
import machine
|
||||
import time
|
||||
print("this will be printed before: " + str(time.ticks_ms()))
|
||||
machine.sleep(1000*10, True)
|
||||
print("this will be printed after 10 seconds: " str(time.ticks_ms()))
|
||||
```
|
||||
|
||||
#### Deep sleep
|
||||
Deepsleep disables, next to the lightsleep, the main CPU and RAM. This leaves only a low power coprocessor and RTC timer running. After waking up, the board will start again at `boot.py`, just like with pressing the reset button. The CPU counter (`time.ticks()`) will continue to count however!
|
||||
You can also leave the brackets empty to sleep indefinetely, until the reset button is pressed, the power is removed, or an external wake up signal (interrupt) is provided. Be aware that the LTE modem. ***
|
||||
|
||||
|
||||
```python
|
||||
import machine
|
||||
print("Wake up")
|
||||
machine.deepsleep(1000) #deepsleep 1 second
|
||||
print("this will never get printed!")
|
||||
```
|
||||
|
||||
#### Wake up reason
|
||||
|
||||
Sometimes, we want to know the reason the board woke up, to differentiate the difference between pressing the reset button and waking up from sleep. We can also determine the time left on the sleep timer. Try the example below:
|
||||
|
||||
```python
|
||||
import machine
|
||||
import time
|
||||
(wake_reason, gpio_list) = machine.wake_reason()
|
||||
print("Device running for: " + str(time.ticks_ms()) + "ms")
|
||||
print("Remaining sleep time: " + str(machine.remaining_sleep_time()) + "ms" )
|
||||
if wake_reason == machine.PWRON_WAKE:
|
||||
print("Woke up by reset button")
|
||||
elif wake_reason == machine.PIN_WAKE:
|
||||
print("Woke up by external pin (external interrupt)")
|
||||
print(*gpio_list, sep=", ")
|
||||
elif wake_reason == machine.RTC_WAKE:
|
||||
print("Woke up by RTC (timer ran out)")
|
||||
elif wake_reason == machine.ULP_WAKE:
|
||||
print("Woke up by ULP (capacitive touch)")
|
||||
|
||||
machine.pin_sleep_wakeup(('P3', 'P4'), mode=machine.WAKEUP_ANY_HIGH, enable_pull=True)
|
||||
|
||||
machine.deepsleep(1000*60) #sleep for 1 minute
|
||||
print("This will never be printed")
|
||||
```
|
||||
|
||||
>Note `pybytes.deepsleep()` is fundamentally the same function as `machine.deepsleep()`, but will gracefully stop the pybytes platform functionality.
|
||||
|
||||
#### Other methods
|
||||
|
||||
The expansionboards (Pysense 2.0 X, and Pytrack 2.0 X, DeepSleep shield) use a different mechanism to put the controller to sleep. A separate controller on the expansion board will put the main controller to sleep. This will actually cut all power from the module for the set amount of time, hard resetting it. Cutting power to the expansion board will work as well. Using this method, we can still recover the wake up reason and remaining sleep time. The example below works was written for a Pysense, but works on any of the boards by changing the first lines
|
||||
|
||||
```python
|
||||
from pysense import Pysense
|
||||
py = Pysense()
|
||||
py.setup_sleep(10) # set sleep time of 10 seconds
|
||||
py.go_to_sleep()
|
||||
print("this will never be printed")
|
||||
```
|
||||
Using this method, we can also wake the board using the accelerometer and external pin `P6` by rising (`True`) or falling (`False`) edge
|
||||
|
||||
```python
|
||||
from pysense import Pysense
|
||||
from LIS2HH12 import LIS2HH12
|
||||
|
||||
py = Pysense()
|
||||
acc = LIS2HH12()
|
||||
|
||||
# enable activity and also inactivity interrupts, using the default callback handler
|
||||
py.setup_int_wake_up(True, True)
|
||||
|
||||
# set the acceleration threshold to 2000mG (2G) and the min duration to 200ms
|
||||
acc.enable_activity_interrupt(2000, 200)
|
||||
|
||||
py.set_int_pin_wake_up(True) #wake up on rising edge on pin 6 of the expansion header
|
||||
|
||||
py.go_to_sleep()# the device will sleep indefinitely, until pin 6 goes high, or the accelerometer is triggered
|
||||
```
|
||||
LTE Power saving mode (PSM)
|
||||
Reference in New Issue
Block a user