|
| 1 | +Getting a MicroPython REPL prompt |
| 2 | +================================= |
| 3 | + |
| 4 | +REPL stands for Read Evaluate Print Loop, and is the name given to the |
| 5 | +interactive MicroPython prompt that you can access on the WiPy. Using |
| 6 | +the REPL is by far the easiest way to test out your code and run commands. |
| 7 | +You can use the REPL in addition to writing scripts in ``main.py``. |
| 8 | + |
| 9 | +To use the REPL, you must connect to the WiPy either via :ref:`telnet <wipy_telnet>`, |
| 10 | +or with a USB to serial converter wired to the one the two UARTs on the |
| 11 | +WiPy. To enable REPL duplication on UART0 (the one accesible via the expansion board) |
| 12 | +do:: |
| 13 | + |
| 14 | + >>> from machine import UART |
| 15 | + >>> import os |
| 16 | + >>> uart = UART(0, 115200) |
| 17 | + >>> o.dupterm(uart) |
| 18 | + |
| 19 | +Place this piece of code inside your `boot.py` so that it's done automatically after |
| 20 | +reset. |
| 21 | + |
| 22 | +Windows |
| 23 | +------- |
| 24 | + |
| 25 | +You need to install the pyboard driver to use the serial USB device. |
| 26 | +The driver is on the pyboard's USB flash drive, and is called ``pybcdc.inf``. |
| 27 | + |
| 28 | +To install this driver you need to go to Device Manager |
| 29 | +for your computer, find the pyboard in the list of devices (it should have |
| 30 | +a warning sign next to it because it's not working yet), right click on |
| 31 | +the pyboard device, select Properties, then Install Driver. You need to |
| 32 | +then select the option to find the driver manually (don't use Windows auto update), |
| 33 | +navigate to the pyboard's USB drive, and select that. It should then install. |
| 34 | +After installing, go back to the Device Manager to find the installed pyboard, |
| 35 | +and see which COM port it is (eg COM4). |
| 36 | +More comprehensive instructions can be found in the |
| 37 | +`Guide for pyboard on Windows (PDF) <http://micropython.org/resources/Micro-Python-Windows-setup.pdf>`_. |
| 38 | +Please consult this guide if you are having problems installing the driver. |
| 39 | + |
| 40 | +The best option is to download the free program PuTTY: |
| 41 | +`putty.exe <http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html>`_. |
| 42 | + |
| 43 | +**In order to get to the telnet REPL:** |
| 44 | + |
| 45 | +Using putty, select ``Telnet`` as connection type, leave the default port (23) |
| 46 | +and enter the IP address of your WiPy (192.168.1.1 when in ``WLAN.AP`` mode), |
| 47 | +then click open. |
| 48 | + |
| 49 | +**In order to get to the REPL UART:** |
| 50 | + |
| 51 | +Using your serial program you must connect to the COM port that you found in the |
| 52 | +previous step. With PuTTY, click on "Session" in the left-hand panel, then click |
| 53 | +the "Serial" radio button on the right, then enter you COM port (eg COM4) in the |
| 54 | +"Serial Line" box. Finally, click the "Open" button. |
| 55 | + |
| 56 | +Mac OS X |
| 57 | +-------- |
| 58 | + |
| 59 | +Open a terminal and run:: |
| 60 | + |
| 61 | + $ telnet 192.168.1.1 |
| 62 | + |
| 63 | +or:: |
| 64 | + |
| 65 | + $ screen /dev/tty.usbmodem* 115200 |
| 66 | + |
| 67 | +When you are finished and want to exit screen, type CTRL-A CTRL-\\. |
| 68 | + |
| 69 | +Linux |
| 70 | +----- |
| 71 | + |
| 72 | +Open a terminal and run:: |
| 73 | + |
| 74 | + $ telnet 192.168.1.1 |
| 75 | + |
| 76 | +or:: |
| 77 | + |
| 78 | + $ screen /dev/ttyUSB0 115200 |
| 79 | + |
| 80 | +You can also try ``picocom`` or ``minicom`` instead of screen. You may have to |
| 81 | +use ``/dev/ttyUSB01`` or a higher number for ``ttyUSB``. And, you may need to give |
| 82 | +yourself the correct permissions to access this devices (eg group ``uucp`` or ``dialout``, |
| 83 | +or use sudo). |
| 84 | + |
| 85 | +Using the REPL prompt |
| 86 | +--------------------- |
| 87 | + |
| 88 | +Now let's try running some MicroPython code directly on the WiPy. |
| 89 | + |
| 90 | +With your serial program open (PuTTY, screen, picocom, etc) you may see a blank |
| 91 | +screen with a flashing cursor. Press Enter and you should be presented with a |
| 92 | +MicroPython prompt, i.e. ``>>>``. Let's make sure it is working with the obligatory test:: |
| 93 | + |
| 94 | + >>> print("hello WiPy!") |
| 95 | + hello WiPy! |
| 96 | + |
| 97 | +In the above, you should not type in the ``>>>`` characters. They are there to |
| 98 | +indicate that you should type the text after it at the prompt. In the end, once |
| 99 | +you have entered the text ``print("hello pyboard!")`` and pressed Enter, the output |
| 100 | +on your screen should look like it does above. |
| 101 | + |
| 102 | +If you already know some python you can now try some basic commands here. |
| 103 | + |
| 104 | +If any of this is not working you can try either a hard reset or a soft reset; |
| 105 | +see below. |
| 106 | + |
| 107 | +Go ahead and try typing in some other commands. For example:: |
| 108 | + |
| 109 | + >>> from machine import Pin |
| 110 | + >>> import wipy |
| 111 | + >>> wipy.heartbeat(False) # disable the heartbeat |
| 112 | + >>> led = Pin('GP25', mode=Pin.OUT) |
| 113 | + >>> led(1) |
| 114 | + >>> led(0) |
| 115 | + >>> led.toggle() |
| 116 | + >>> 1 + 2 |
| 117 | + 3 |
| 118 | + >>> 4 // 2 |
| 119 | + 2 |
| 120 | + >>> 20 * 'py' |
| 121 | + 'pypypypypypypypypypypypypypypypypypypypy' |
| 122 | + |
| 123 | +Resetting the board |
| 124 | +------------------- |
| 125 | + |
| 126 | +If something goes wrong, you can reset the board in two ways. The first is to press CTRL-D |
| 127 | +at the MicroPython prompt, which performs a soft reset. You will see a message something like:: |
| 128 | + |
| 129 | + >>> |
| 130 | + PYB: soft reboot |
| 131 | + MicroPython v1.4.6-146-g1d8b5e5 on 2015-10-21; WiPy with CC3200 |
| 132 | + Type "help()" for more information. |
| 133 | + >>> |
| 134 | + |
| 135 | +If that isn't working you can perform a hard reset (turn-it-off-and-on-again) by pressing the |
| 136 | +RST switch (the small black button next to the heartbeat LED). During telnet, this will end |
| 137 | +your session, disconnecting whatever program that you used to connect to the WiPy. |
0 commit comments