micropython: add micropython component
This commit is contained in:
17
components/language/micropython/docs/wipy/tutorial/blynk.rst
Normal file
17
components/language/micropython/docs/wipy/tutorial/blynk.rst
Normal file
@@ -0,0 +1,17 @@
|
||||
Getting started with Blynk and the WiPy
|
||||
---------------------------------------
|
||||
|
||||
Blynk provides iOS and Android apps to control any hardware over the Internet
|
||||
or directly using Bluetooth. You can easily build graphic interfaces for all
|
||||
your projects by simply dragging and dropping widgets, right on your smartphone.
|
||||
|
||||
Before anything else, make sure that your WiPy is running
|
||||
the latest software, check :ref:`OTA How-To <wipy_firmware_upgrade>` for instructions.
|
||||
|
||||
1. Get the `Blynk library <https://github.com/vshymanskyy/blynk-library-python/blob/master/BlynkLib.py>`_ and put it in ``/flash/lib/`` via FTP.
|
||||
2. Get the `Blynk example for WiPy <https://github.com/vshymanskyy/blynk-library-python/blob/master/examples/hardware/PyCom_WiPy.py>`_, edit the network settings, and afterwards
|
||||
upload it to ``/flash/`` via FTP as well.
|
||||
3. Follow the instructions on each example to setup the Blynk dashboard on your smartphone or tablet.
|
||||
4. Give it a try, for instance::
|
||||
|
||||
>>> execfile('sync_virtual.py')
|
18
components/language/micropython/docs/wipy/tutorial/index.rst
Normal file
18
components/language/micropython/docs/wipy/tutorial/index.rst
Normal file
@@ -0,0 +1,18 @@
|
||||
.. _wipy_tutorial:
|
||||
|
||||
WiPy tutorials and examples
|
||||
===========================
|
||||
|
||||
Before starting, make sure that you are running the latest firmware,
|
||||
for instructions see :ref:`OTA How-To <wipy_firmware_upgrade>`.
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
:numbered:
|
||||
|
||||
intro.rst
|
||||
repl.rst
|
||||
blynk.rst
|
||||
wlan.rst
|
||||
timer.rst
|
||||
reset.rst
|
64
components/language/micropython/docs/wipy/tutorial/intro.rst
Normal file
64
components/language/micropython/docs/wipy/tutorial/intro.rst
Normal file
@@ -0,0 +1,64 @@
|
||||
Introduction to the WiPy
|
||||
========================
|
||||
|
||||
To get the most out of your WiPy, there are a few basic things to
|
||||
understand about how it works.
|
||||
|
||||
Caring for your WiPy and expansion board
|
||||
----------------------------------------
|
||||
|
||||
Because the WiPy/expansion board does not have a housing it needs a bit of care:
|
||||
|
||||
- Be gentle when plugging/unplugging the USB cable. Whilst the USB connector
|
||||
is well soldered and is relatively strong, if it breaks off it can be very
|
||||
difficult to fix.
|
||||
|
||||
- Static electricity can shock the components on the WiPy and destroy them.
|
||||
If you experience a lot of static electricity in your area (eg dry and cold
|
||||
climates), take extra care not to shock the WiPy. If your WiPy came
|
||||
in a ESD bag, then this bag is the best way to store and carry the
|
||||
WiPy as it will protect it against static discharges.
|
||||
|
||||
As long as you take care of the hardware, you should be okay. It's almost
|
||||
impossible to break the software on the WiPy, so feel free to play around
|
||||
with writing code as much as you like. If the filesystem gets corrupt, see
|
||||
below on how to reset it. In the worst case you might need to do a safe boot,
|
||||
which is explained in detail in :ref:`wipy_boot_modes`.
|
||||
|
||||
Plugging into the expansion board and powering on
|
||||
-------------------------------------------------
|
||||
|
||||
The expansion board can power the WiPy via USB. The WiPy comes with a sticker
|
||||
on top of the RF shield that labels all pins, and this should match the label
|
||||
numbers on the expansion board headers. When plugging it in, the WiPy antenna
|
||||
will end up on top of the SD card connector of the expansion board. A video
|
||||
showing how to do this can be found `here on YouTube <https://www.youtube.com/watch?v=47D9MZ9zFQw>`_.
|
||||
|
||||
Expansion board hardware guide
|
||||
------------------------------
|
||||
|
||||
The document explaining the hardware details of the expansion board can be found
|
||||
`in this PDF <https://github.com/wipy/wipy/blob/master/docs/User_manual_exp_board.pdf>`_.
|
||||
|
||||
Powering by an external power source
|
||||
------------------------------------
|
||||
|
||||
The WiPy can be powered by a battery or other external power source.
|
||||
|
||||
**Be sure to connect the positive lead of the power supply to VIN, and
|
||||
ground to GND. There is no polarity protection on the WiPy so you
|
||||
must be careful when connecting anything to VIN.**
|
||||
|
||||
- When powering via ``VIN``:
|
||||
|
||||
**The input voltage must be between 3.6V and 5.5V.**
|
||||
|
||||
- When powering via ``3V3``:
|
||||
|
||||
**The input voltage must be exactly 3V3, ripple free and from a supply capable
|
||||
of sourcing at least 300mA of current**
|
||||
|
||||
Performing firmware upgrades
|
||||
----------------------------
|
||||
|
||||
For detailed instructions see :ref:`OTA How-To <wipy_firmware_upgrade>`.
|
130
components/language/micropython/docs/wipy/tutorial/repl.rst
Normal file
130
components/language/micropython/docs/wipy/tutorial/repl.rst
Normal file
@@ -0,0 +1,130 @@
|
||||
Getting a MicroPython REPL prompt
|
||||
=================================
|
||||
|
||||
REPL stands for Read Evaluate Print Loop, and is the name given to the
|
||||
interactive MicroPython prompt that you can access on the WiPy. Using
|
||||
the REPL is by far the easiest way to test out your code and run commands.
|
||||
You can use the REPL in addition to writing scripts in ``main.py``.
|
||||
|
||||
.. _wipy_uart:
|
||||
|
||||
To use the REPL, you must connect to the WiPy either via :ref:`telnet <wipy_telnet>`,
|
||||
or with a USB to serial converter wired to one of the two UARTs on the
|
||||
WiPy. To enable REPL duplication on UART0 (the one accessible via the expansion board)
|
||||
do::
|
||||
|
||||
>>> from machine import UART
|
||||
>>> import os
|
||||
>>> uart = UART(0, 115200)
|
||||
>>> os.dupterm(uart)
|
||||
|
||||
Place this piece of code inside your ``boot.py`` so that it's done automatically after
|
||||
reset.
|
||||
|
||||
Windows
|
||||
-------
|
||||
|
||||
First you need to install the FTDI drivers for the expansion board's USB to serial
|
||||
converter. Then you need a terminal software. The best option is to download the
|
||||
free program PuTTY: `putty.exe <http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html>`_.
|
||||
|
||||
**In order to get to the telnet REPL:**
|
||||
|
||||
Using putty, select ``Telnet`` as connection type, leave the default port (23)
|
||||
and enter the IP address of your WiPy (192.168.1.1 when in ``WLAN.AP`` mode),
|
||||
then click open.
|
||||
|
||||
**In order to get to the REPL UART:**
|
||||
|
||||
Using your serial program you must connect to the COM port that you found in the
|
||||
previous step. With PuTTY, click on "Session" in the left-hand panel, then click
|
||||
the "Serial" radio button on the right, then enter you COM port (eg COM4) in the
|
||||
"Serial Line" box. Finally, click the "Open" button.
|
||||
|
||||
Mac OS X
|
||||
--------
|
||||
|
||||
Open a terminal and run::
|
||||
|
||||
$ telnet 192.168.1.1
|
||||
|
||||
or::
|
||||
|
||||
$ screen /dev/tty.usbmodem* 115200
|
||||
|
||||
When you are finished and want to exit ``screen``, type CTRL-A CTRL-\\. If your keyboard does not have a \\-key (i.e. you need an obscure combination for \\ like ALT-SHIFT-7) you can remap the ``quit`` command:
|
||||
|
||||
- create ``~/.screenrc``
|
||||
- add ``bind q quit``
|
||||
|
||||
This will allow you to quit ``screen`` by hitting CTRL-A Q.
|
||||
|
||||
Linux
|
||||
-----
|
||||
|
||||
Open a terminal and run::
|
||||
|
||||
$ telnet 192.168.1.1
|
||||
|
||||
or::
|
||||
|
||||
$ screen /dev/ttyUSB0 115200
|
||||
|
||||
You can also try ``picocom`` or ``minicom`` instead of screen. You may have to
|
||||
use ``/dev/ttyUSB01`` or a higher number for ``ttyUSB``. And, you may need to give
|
||||
yourself the correct permissions to access this devices (eg group ``uucp`` or ``dialout``,
|
||||
or use sudo).
|
||||
|
||||
Using the REPL prompt
|
||||
---------------------
|
||||
|
||||
Now let's try running some MicroPython code directly on the WiPy.
|
||||
|
||||
With your serial program open (PuTTY, screen, picocom, etc) you may see a blank
|
||||
screen with a flashing cursor. Press Enter and you should be presented with a
|
||||
MicroPython prompt, i.e. ``>>>``. Let's make sure it is working with the obligatory test::
|
||||
|
||||
>>> print("hello WiPy!")
|
||||
hello WiPy!
|
||||
|
||||
In the above, you should not type in the ``>>>`` characters. They are there to
|
||||
indicate that you should type the text after it at the prompt. In the end, once
|
||||
you have entered the text ``print("hello WiPy!")`` and pressed Enter, the output
|
||||
on your screen should look like it does above.
|
||||
|
||||
If you already know some Python you can now try some basic commands here.
|
||||
|
||||
If any of this is not working you can try either a hard reset or a soft reset;
|
||||
see below.
|
||||
|
||||
Go ahead and try typing in some other commands. For example::
|
||||
|
||||
>>> from machine import Pin
|
||||
>>> import wipy
|
||||
>>> wipy.heartbeat(False) # disable the heartbeat
|
||||
>>> led = Pin('GP25', mode=Pin.OUT)
|
||||
>>> led(1)
|
||||
>>> led(0)
|
||||
>>> led.toggle()
|
||||
>>> 1 + 2
|
||||
3
|
||||
>>> 4 // 2
|
||||
2
|
||||
>>> 20 * 'py'
|
||||
'pypypypypypypypypypypypypypypypypypypypy'
|
||||
|
||||
Resetting the board
|
||||
-------------------
|
||||
|
||||
If something goes wrong, you can reset the board in two ways. The first is to press CTRL-D
|
||||
at the MicroPython prompt, which performs a soft reset. You will see a message something like::
|
||||
|
||||
>>>
|
||||
MPY: soft reboot
|
||||
MicroPython v1.4.6-146-g1d8b5e5 on 2015-10-21; WiPy with CC3200
|
||||
Type "help()" for more information.
|
||||
>>>
|
||||
|
||||
If that isn't working you can perform a hard reset (turn-it-off-and-on-again) by pressing the
|
||||
RST switch (the small black button next to the heartbeat LED). During telnet, this will end
|
||||
your session, disconnecting whatever program that you used to connect to the WiPy.
|
54
components/language/micropython/docs/wipy/tutorial/reset.rst
Normal file
54
components/language/micropython/docs/wipy/tutorial/reset.rst
Normal file
@@ -0,0 +1,54 @@
|
||||
Reset and boot modes
|
||||
====================
|
||||
|
||||
There are soft resets and hard resets.
|
||||
|
||||
- A soft reset simply clears the state of the MicroPython virtual machine,
|
||||
but leaves hardware peripherals unaffected. To do a soft reset, simply press
|
||||
**Ctrl+D** on the REPL, or within a script do::
|
||||
|
||||
import sys
|
||||
sys.exit()
|
||||
|
||||
- A hard reset is the same as performing a power cycle to the board. In order to
|
||||
hard reset the WiPy, press the switch on the board or::
|
||||
|
||||
import machine
|
||||
machine.reset()
|
||||
|
||||
Safe boot
|
||||
---------
|
||||
|
||||
If something goes wrong with your WiPy, don't panic! It is almost
|
||||
impossible for you to break the WiPy by programming the wrong thing.
|
||||
|
||||
The first thing to try is to boot in safe mode: this temporarily skips
|
||||
execution of ``boot.py`` and ``main.py`` and gives default WLAN settings.
|
||||
|
||||
If you have problems with the filesystem you can :ref:`format the internal flash
|
||||
drive <wipy_factory_reset>`.
|
||||
|
||||
To boot in safe mode, follow the detailed instructions described :ref:`here <wipy_boot_modes>`.
|
||||
|
||||
In safe mode, the ``boot.py`` and ``main.py`` files are not executed, and so
|
||||
the WiPy boots up with default settings. This means you now have access
|
||||
to the filesystem, and you can edit ``boot.py`` and ``main.py`` to fix any problems.
|
||||
|
||||
Entering safe mode is temporary, and does not make any changes to the
|
||||
files on the WiPy.
|
||||
|
||||
.. _wipy_factory_reset:
|
||||
|
||||
Factory reset the filesystem
|
||||
----------------------------
|
||||
|
||||
If you WiPy's filesystem gets corrupted (very unlikely, but possible), you
|
||||
can format it very easily by doing::
|
||||
|
||||
>>> import os
|
||||
>>> os.mkfs('/flash')
|
||||
|
||||
Resetting the filesystem deletes all files on the internal WiPy storage
|
||||
(not the SD card), and restores the files ``boot.py`` and ``main.py`` back
|
||||
to their original state after the next reset.
|
||||
|
70
components/language/micropython/docs/wipy/tutorial/timer.rst
Normal file
70
components/language/micropython/docs/wipy/tutorial/timer.rst
Normal file
@@ -0,0 +1,70 @@
|
||||
Hardware timers
|
||||
===============
|
||||
|
||||
Timers can be used for a great variety of tasks, calling a function periodically,
|
||||
counting events, and generating a PWM signal are among the most common use cases.
|
||||
Each timer consists of two 16-bit channels and this channels can be tied together to
|
||||
form one 32-bit timer. The operating mode needs to be configured per timer, but then
|
||||
the period (or the frequency) can be independently configured on each channel.
|
||||
By using the callback method, the timer event can call a Python function.
|
||||
|
||||
Example usage to toggle an LED at a fixed frequency::
|
||||
|
||||
from machine import Timer
|
||||
from machine import Pin
|
||||
led = Pin('GP16', mode=Pin.OUT) # enable GP16 as output to drive the LED
|
||||
tim = Timer(3) # create a timer object using timer 3
|
||||
tim.init(mode=Timer.PERIODIC) # initialize it in periodic mode
|
||||
tim_ch = tim.channel(Timer.A, freq=5) # configure channel A at a frequency of 5Hz
|
||||
tim_ch.irq(handler=lambda t:led.toggle(), trigger=Timer.TIMEOUT) # toggle a LED on every cycle of the timer
|
||||
|
||||
Example using named function for the callback::
|
||||
|
||||
from machine import Timer
|
||||
from machine import Pin
|
||||
tim = Timer(1, mode=Timer.PERIODIC, width=32)
|
||||
tim_a = tim.channel(Timer.A | Timer.B, freq=1) # 1 Hz frequency requires a 32 bit timer
|
||||
|
||||
led = Pin('GP16', mode=Pin.OUT) # enable GP16 as output to drive the LED
|
||||
|
||||
def tick(timer): # we will receive the timer object when being called
|
||||
global led
|
||||
led.toggle() # toggle the LED
|
||||
|
||||
tim_a.irq(handler=tick, trigger=Timer.TIMEOUT) # create the interrupt
|
||||
|
||||
Further examples::
|
||||
|
||||
from machine import Timer
|
||||
tim1 = Timer(1, mode=Timer.ONE_SHOT) # initialize it in one shot mode
|
||||
tim2 = Timer(2, mode=Timer.PWM) # initialize it in PWM mode
|
||||
tim1_ch = tim1.channel(Timer.A, freq=10, polarity=Timer.POSITIVE) # start the event counter with a frequency of 10Hz and triggered by positive edges
|
||||
tim2_ch = tim2.channel(Timer.B, freq=10000, duty_cycle=5000) # start the PWM on channel B with a 50% duty cycle
|
||||
tim2_ch.freq(20) # set the frequency (can also get)
|
||||
tim2_ch.duty_cycle(3010) # set the duty cycle to 30.1% (can also get)
|
||||
tim2_ch.duty_cycle(3020, Timer.NEGATIVE) # set the duty cycle to 30.2% and change the polarity to negative
|
||||
tim2_ch.period(2000000) # change the period to 2 seconds
|
||||
|
||||
|
||||
Additional constants for Timer class
|
||||
------------------------------------
|
||||
|
||||
.. data:: Timer.PWM
|
||||
|
||||
PWM timer operating mode.
|
||||
|
||||
.. data:: Timer.A
|
||||
.. data:: Timer.B
|
||||
|
||||
Selects the timer channel. Must be ORed (``Timer.A`` | ``Timer.B``) when
|
||||
using a 32-bit timer.
|
||||
|
||||
.. data:: Timer.POSITIVE
|
||||
.. data:: Timer.NEGATIVE
|
||||
|
||||
Timer channel polarity selection (only relevant in PWM mode).
|
||||
|
||||
.. data:: Timer.TIMEOUT
|
||||
.. data:: Timer.MATCH
|
||||
|
||||
Timer channel IRQ triggers.
|
71
components/language/micropython/docs/wipy/tutorial/wlan.rst
Normal file
71
components/language/micropython/docs/wipy/tutorial/wlan.rst
Normal file
@@ -0,0 +1,71 @@
|
||||
WLAN step by step
|
||||
=================
|
||||
|
||||
The WLAN is a system feature of the WiPy, therefore it is always enabled
|
||||
(even while in ``machine.SLEEP``), except when deepsleep mode is entered.
|
||||
|
||||
In order to retrieve the current WLAN instance, do::
|
||||
|
||||
>>> from network import WLAN
|
||||
>>> wlan = WLAN() # we call the constructor without params
|
||||
|
||||
You can check the current mode (which is always ``WLAN.AP`` after power up)::
|
||||
|
||||
>>> wlan.mode()
|
||||
|
||||
.. warning::
|
||||
When you change the WLAN mode following the instructions below, your WLAN
|
||||
connection to the WiPy will be broken. This means you will not be able
|
||||
to run these commands interactively over the WLAN.
|
||||
|
||||
There are two ways around this::
|
||||
1. put this setup code into your :ref:`boot.py file<wipy_filesystem>` so that it gets executed automatically after reset.
|
||||
2. :ref:`duplicate the REPL on UART <wipy_uart>`, so that you can run commands via USB.
|
||||
|
||||
Connecting to your home router
|
||||
------------------------------
|
||||
|
||||
The WLAN network card always boots in ``WLAN.AP`` mode, so we must first configure
|
||||
it as a station::
|
||||
|
||||
from network import WLAN
|
||||
wlan = WLAN(mode=WLAN.STA)
|
||||
|
||||
|
||||
Now you can proceed to scan for networks::
|
||||
|
||||
nets = wlan.scan()
|
||||
for net in nets:
|
||||
if net.ssid == 'mywifi':
|
||||
print('Network found!')
|
||||
wlan.connect(net.ssid, auth=(net.sec, 'mywifikey'), timeout=5000)
|
||||
while not wlan.isconnected():
|
||||
machine.idle() # save power while waiting
|
||||
print('WLAN connection succeeded!')
|
||||
break
|
||||
|
||||
Assigning a static IP address when booting
|
||||
------------------------------------------
|
||||
|
||||
If you want your WiPy to connect to your home router after boot-up, and with a fixed
|
||||
IP address so that you can access it via telnet or FTP, use the following script as /flash/boot.py::
|
||||
|
||||
import machine
|
||||
from network import WLAN
|
||||
wlan = WLAN() # get current object, without changing the mode
|
||||
|
||||
if machine.reset_cause() != machine.SOFT_RESET:
|
||||
wlan.init(WLAN.STA)
|
||||
# configuration below MUST match your home router settings!!
|
||||
wlan.ifconfig(config=('192.168.178.107', '255.255.255.0', '192.168.178.1', '8.8.8.8'))
|
||||
|
||||
if not wlan.isconnected():
|
||||
# change the line below to match your network ssid, security and password
|
||||
wlan.connect('mywifi', auth=(WLAN.WPA2, 'mywifikey'), timeout=5000)
|
||||
while not wlan.isconnected():
|
||||
machine.idle() # save power while waiting
|
||||
|
||||
.. note::
|
||||
|
||||
Notice how we check for the reset cause and the connection status, this is crucial in order
|
||||
to be able to soft reset the WiPy during a telnet session without breaking the connection.
|
Reference in New Issue
Block a user