# 配置WebREPL

之前的教程通过离线编辑Python代码实现了NyBoard执行序列动作的功能。 但是这样很不方便，每当需要修改代码， 我们需要拔下WiFi模块进行修改，也不能在执行过程中灵活的暂停、修改参数。 究其原因，ESP8266只有一个串口，我们需要使用它与NyBoard进行通信。 好在MicroPython利用ESP提供的WiFi功能实现远程无线Python调试-WebREPL。

[官方文档](https://docs.micropython.org/en/latest/esp8266/tutorial/repl.html#webrepl-a-prompt-over-wifi)

我们在官方文档的基础上，结合模块的特点，撰写了以下教程：

### 1.  启用webREPL

连接设备后，在shell界面输入`import webrepl_setup` ，根据指南输入&#x20;

1. 启动时启用webREPL： **E**
2. 设置密码：你自己的密码（演示为：**1234**）
3. 重复密码&#x20;
4. 重启8266模块：**y**

<figure><img src="/files/DSxuco6x1ic3DNPqBseF" alt=""><figcaption></figcaption></figure>

### 2.  配置WebREPL程序

我们使用下面的demo程序，注意将SSID和密码换成您身边使用的网络信息。

```python
import network
import time
import webrepl

def do_connect():
    
    # WiFi SSID and Password
    wifi_ssid = "YOUR SSID"             # YOUR WiFi SSID
    wifi_password = "YOUR PASSWORD"     # YOUR WiFi PASSWORD

    # Wireless config : Station mode
    station = network.WLAN(network.STA_IF)
    station.active(True)

    # Continually try to connect to WiFi access point
    while not station.isconnected():
    
        # Try to connect to WiFi access point
        print("Connecting...")
        station.connect(wifi_ssid, wifi_password)
        time.sleep(10)

    # Display connection details
    print("Connected!")
    print("My IP Address:", station.ifconfig()[0])
    

if __name__ == "__main__":
    do_connect()
    webrepl.start()
```

启动程序后，会不停连接WiFi网络，一旦连接上，会自动启动设备的WebREPL服务。

```
Connected!
My IP Address: 192.168.xxx.xxx
WebREPL daemon started on ws://192.168.xxx.xxx:8266
Started webrepl in normal mode
```

记住这个IP地址（路由器DHCP自动分配的），配置WebREPL时有用。

### 3. 配置WebREPL服务

我们通过WebREPL来调试Python，原先的串口用来和NyBoard通讯。 所以在`选项`中，将原先的`USB-COMx`接口修改成`WebREPL`。

<figure><img src="/files/kseYBJeQyYO02i8q19Yy" alt=""><figcaption></figcaption></figure>

然后我们填入WebREPL的IP地址、端口及密码，点击`OK`。

<figure><img src="/files/kKoa3gjUBuXeVq2bf2tr" alt=""><figcaption></figcaption></figure>

完成后会连接，当显示`WebREPL Connected`表示连接成功了。

<figure><img src="/files/DOiM4NBSoSLaNIFCNz0I" alt=""><figcaption></figcaption></figure>

我们可以试着运行一些简单的程序，如`blink`。

WebREPL节约了串口，并支持无线调试。缺点是速度较慢（因为存在网络延迟），软件复位等待时间比较久。

### 4. 将串口和调试器分离

现在我们可以使用webREPL来调式程序了，但是我们打开串口监视器会发现，每当我们运行程序，串口会发出一系列的调试内容： 这些海量的字符串会导致我们的NyBoard来不及处理而宕机，如下图所示：

<figure><img src="/files/HV7MaSW3K9A2FFXcYloo" alt=""><figcaption></figcaption></figure>

我们希望在调试程序的时候，串口只输出我们希望输出的指令，而不是这些Debug信息。 打开设备上的`boot.py`文件，将`uos.dupterm(None, 1)` 这行代码取消注释并保存，解除串口和REPL debug的绑定。 再次重启模块，串口调试助手就不再打印debug信息了。

<figure><img src="/files/cloGmr8CLWDUq6DP1m2P" alt=""><figcaption></figcaption></figure>

作为补充，我们可以通过`print()`指令输出debug信息，这些信息会通过WiFi显示在Shell上。

至此，大家可以方便的使用WiFi模块通过webREPL调试Bittle进行基于MicroPython的动作序列编辑。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.petoi.com/chinese/api/micropython-kong-zhi-qi/pei-zhi-webrepl.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
