repl.rst 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. Getting a MicroPython REPL prompt
  2. =================================
  3. REPL stands for Read Evaluate Print Loop, and is the name given to the
  4. interactive MicroPython prompt that you can access on the WiPy. Using
  5. the REPL is by far the easiest way to test out your code and run commands.
  6. You can use the REPL in addition to writing scripts in ``main.py``.
  7. .. _wipy_uart:
  8. To use the REPL, you must connect to the WiPy either via :ref:`telnet <wipy_telnet>`,
  9. or with a USB to serial converter wired to one of the two UARTs on the
  10. WiPy. To enable REPL duplication on UART0 (the one accessible via the expansion board)
  11. do::
  12. >>> from machine import UART
  13. >>> import os
  14. >>> uart = UART(0, 115200)
  15. >>> os.dupterm(uart)
  16. Place this piece of code inside your `boot.py` so that it's done automatically after
  17. reset.
  18. Windows
  19. -------
  20. First you need to install the FTDI drivers for the expansion board's USB to serial
  21. converter. Then you need a terminal software. The best option is to download the
  22. free program PuTTY: `putty.exe <http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html>`_.
  23. **In order to get to the telnet REPL:**
  24. Using putty, select ``Telnet`` as connection type, leave the default port (23)
  25. and enter the IP address of your WiPy (192.168.1.1 when in ``WLAN.AP`` mode),
  26. then click open.
  27. **In order to get to the REPL UART:**
  28. Using your serial program you must connect to the COM port that you found in the
  29. previous step. With PuTTY, click on "Session" in the left-hand panel, then click
  30. the "Serial" radio button on the right, then enter you COM port (eg COM4) in the
  31. "Serial Line" box. Finally, click the "Open" button.
  32. Mac OS X
  33. --------
  34. Open a terminal and run::
  35. $ telnet 192.168.1.1
  36. or::
  37. $ screen /dev/tty.usbmodem* 115200
  38. 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:
  39. - create ``~/.screenrc``
  40. - add ``bind q quit``
  41. This will allow you to quit ``screen`` by hitting CTRL-A Q.
  42. Linux
  43. -----
  44. Open a terminal and run::
  45. $ telnet 192.168.1.1
  46. or::
  47. $ screen /dev/ttyUSB0 115200
  48. You can also try ``picocom`` or ``minicom`` instead of screen. You may have to
  49. use ``/dev/ttyUSB01`` or a higher number for ``ttyUSB``. And, you may need to give
  50. yourself the correct permissions to access this devices (eg group ``uucp`` or ``dialout``,
  51. or use sudo).
  52. Using the REPL prompt
  53. ---------------------
  54. Now let's try running some MicroPython code directly on the WiPy.
  55. With your serial program open (PuTTY, screen, picocom, etc) you may see a blank
  56. screen with a flashing cursor. Press Enter and you should be presented with a
  57. MicroPython prompt, i.e. ``>>>``. Let's make sure it is working with the obligatory test::
  58. >>> print("hello WiPy!")
  59. hello WiPy!
  60. In the above, you should not type in the ``>>>`` characters. They are there to
  61. indicate that you should type the text after it at the prompt. In the end, once
  62. you have entered the text ``print("hello WiPy!")`` and pressed Enter, the output
  63. on your screen should look like it does above.
  64. If you already know some Python you can now try some basic commands here.
  65. If any of this is not working you can try either a hard reset or a soft reset;
  66. see below.
  67. Go ahead and try typing in some other commands. For example::
  68. >>> from machine import Pin
  69. >>> import wipy
  70. >>> wipy.heartbeat(False) # disable the heartbeat
  71. >>> led = Pin('GP25', mode=Pin.OUT)
  72. >>> led(1)
  73. >>> led(0)
  74. >>> led.toggle()
  75. >>> 1 + 2
  76. 3
  77. >>> 4 // 2
  78. 2
  79. >>> 20 * 'py'
  80. 'pypypypypypypypypypypypypypypypypypypypy'
  81. Resetting the board
  82. -------------------
  83. If something goes wrong, you can reset the board in two ways. The first is to press CTRL-D
  84. at the MicroPython prompt, which performs a soft reset. You will see a message something like::
  85. >>>
  86. PYB: soft reboot
  87. MicroPython v1.4.6-146-g1d8b5e5 on 2015-10-21; WiPy with CC3200
  88. Type "help()" for more information.
  89. >>>
  90. If that isn't working you can perform a hard reset (turn-it-off-and-on-again) by pressing the
  91. RST switch (the small black button next to the heartbeat LED). During telnet, this will end
  92. your session, disconnecting whatever program that you used to connect to the WiPy.