MicroPython-On-ESP8266——自动连接WIFI与开启webrepl_进击的大米-程序员宝宝_esp8266自动连接
技术标签: 开发板 物联网 MicroPython
MicroPython-On-ESP8266——自动连接WIFI与开启webrepl
1. 启动即连接wifi
esp8266芯片的核心作为即为作为网络连接的桥梁,前面咱们也学习了使用network
库将开发板当作热点或者连接到现有网络中。后面需要使用urequests模块作网络请求什么的,都是需要先连接到网络才行。
1.1. 启动方法
micropython固件包刷到开发板上后,里面是有一个boot.py
文件的,而且这个文件不能删除。
开发板上电启动后,会先执行boot.py
中的代码,然后才继续执行main.py
中代码。后期学习过程中,主要的业务逻辑都会写在main文件中,那咱们就可以把连接wifi代码段放到boot文件中,这样就可以专心调度业务功能了。
之前学习网络模块时,有学习到连接现有wifi的功能
1 2 3 4 5 6 7 8 9 10 11 |
<span class="token keyword">def</span> <span class="token function">do_connect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> sta_if <span class="token operator">=</span> network<span class="token punctuation">.</span>WLAN<span class="token punctuation">(</span>network<span class="token punctuation">.</span>STA_IF<span class="token punctuation">)</span> sta_if<span class="token punctuation">.</span>active<span class="token punctuation">(</span><span class="token boolean">False</span><span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token keyword">not</span> sta_if<span class="token punctuation">.</span>isconnected<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'connecting to network...'</span><span class="token punctuation">)</span> sta_if<span class="token punctuation">.</span>active<span class="token punctuation">(</span><span class="token boolean">True</span><span class="token punctuation">)</span> sta_if<span class="token punctuation">.</span>connect<span class="token punctuation">(</span><span class="token string">'essid'</span><span class="token punctuation">,</span> <span class="token string">'password'</span><span class="token punctuation">)</span> <span class="token comment"># 修改为自己wifi密码</span> <span class="token keyword">while</span> <span class="token keyword">not</span> sta_if<span class="token punctuation">.</span>isconnected<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">pass</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'network config:'</span><span class="token punctuation">,</span> sta_if<span class="token punctuation">.</span>ifconfig<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> |
那只要把这段代码放到boot.py
中并启动do_connect函数即可了。
1.2. 配置boot.py文件
查后面的webrepl资料时,找到一个别人配置的boot.py文件,封装得比较好,拿来学习学习。
原地址:https://www.jianshu.com/p/c2ddd4fd05be
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<span class="token keyword">def</span> <span class="token function">do_connect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">import</span> json <span class="token keyword">import</span> network <span class="token comment"># 尝试读取配置文件wifi_confi.json,这里我们以json的方式来存储WIFI配置</span> <span class="token comment"># wifi_config.json在根目录下</span> <span class="token comment"># 若不是初次运行,则将文件中的内容读取并加载到字典变量 config</span> <span class="token keyword">try</span><span class="token punctuation">:</span> <span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">'wifi_config.json'</span><span class="token punctuation">,</span><span class="token string">'r'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> f<span class="token punctuation">:</span> config <span class="token operator">=</span> json<span class="token punctuation">.</span>loads<span class="token punctuation">(</span>f<span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment"># 若初次运行,则将进入excpet,执行配置文件的创建 </span> <span class="token keyword">except</span><span class="token punctuation">:</span> essid <span class="token operator">=</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">'wifi name:'</span><span class="token punctuation">)</span> <span class="token comment"># 输入essid</span> password <span class="token operator">=</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token string">'wifi passwrod:'</span><span class="token punctuation">)</span> <span class="token comment"># 输入password</span> config <span class="token operator">=</span> <span class="token builtin">dict</span><span class="token punctuation">(</span>essid<span class="token operator">=</span>essid<span class="token punctuation">,</span> password<span class="token operator">=</span>password<span class="token punctuation">)</span> <span class="token comment"># 创建字典</span> <span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">'wifi_config.json'</span><span class="token punctuation">,</span><span class="token string">'w'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> f<span class="token punctuation">:</span> f<span class="token punctuation">.</span>write<span class="token punctuation">(</span>json<span class="token punctuation">.</span>dumps<span class="token punctuation">(</span>config<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment"># 将字典序列化为json字符串,存入wifi_config.json</span> <span class="token comment">#以下为正常的WIFI连接流程 </span> wifi <span class="token operator">=</span> network<span class="token punctuation">.</span>WLAN<span class="token punctuation">(</span>network<span class="token punctuation">.</span>STA_IF<span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token keyword">not</span> wifi<span class="token punctuation">.</span>isconnected<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'connecting to network...'</span><span class="token punctuation">)</span> wifi<span class="token punctuation">.</span>active<span class="token punctuation">(</span><span class="token boolean">True</span><span class="token punctuation">)</span> wifi<span class="token punctuation">.</span>connect<span class="token punctuation">(</span>config<span class="token punctuation">[</span><span class="token string">'essid'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> config<span class="token punctuation">[</span><span class="token string">'password'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">import</span> time time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span> <span class="token comment">#一般睡个5-10秒,应该绰绰有余</span> <span class="token keyword">if</span> <span class="token keyword">not</span> wifi<span class="token punctuation">.</span>isconnected<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> wifi<span class="token punctuation">.</span>active<span class="token punctuation">(</span><span class="token boolean">False</span><span class="token punctuation">)</span> <span class="token comment">#关掉连接,免得repl死循环输出</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'wifi connection error, please reconnect'</span><span class="token punctuation">)</span> <span class="token keyword">import</span> os <span class="token comment"># 连续输错essid和password会导致wifi_config.json不存在</span> <span class="token keyword">try</span><span class="token punctuation">:</span> os<span class="token punctuation">.</span>remove<span class="token punctuation">(</span><span class="token string">'wifi_config.json'</span><span class="token punctuation">)</span> <span class="token comment"># 删除配置文件</span> <span class="token keyword">except</span><span class="token punctuation">:</span> <span class="token keyword">pass</span> do_connect<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment"># 重新连接</span> <span class="token keyword">else</span><span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'network config:'</span><span class="token punctuation">,</span> wifi<span class="token punctuation">.</span>ifconfig<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span> do_connect<span class="token punctuation">(</span><span class="token punctuation">)</span> |
把wifi和密码放到了外部json配置文件中,首次使用需要在REPL中输入,后面有了配置文件就会自己连接到网络了。
2. 使用webrepl
前面学习过程中,调试和上传程序代码这些的都是在REPL中进行的,实际上micropython还提供了一种webrepl调试方法,在开发板连接到网络后,再可开启该功能,就可以通过websocket方式与开发板交互了。这样省去每次都还要连接串口USB线来调试的麻烦。
2.1. 初始化webrepl
要开启webrepl功能,首先还是得连线后在REPL下初始化该模块
1 2 |
<span class="token keyword">import</span> webrepl_set |
在REPL中执行这条语句,系统自会提示安装webrepl服务,需要配置一个网络连接的密码,自己记得就行了。
2.2. 启动webrepl服务
初始化好以后就可以启动webrepl了,咱们还是在REPL中继续执行
1 2 3 |
<span class="token keyword">import</span> webrepl webrepl<span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span> |
webrepl.start()
方法在启动服务时,需要这里可以在参数里指定port端口(默认是8266)和password密码的。
2.3. 连接到webrepl
服务启来了,那咱们就可以使用websocket来连接和交互了,这里使用官方部署的在线环境客户端就行。
进入地址: http://micropython.org/webrepl
进入后,填写正确的地址和端口(刚才已经把port改为了8888,需要对应),connect后输入密码就会提示连接成功了。
当然了,需要本机与开发板连接到了同一个wifi网络才可以正常访问得到。
因为咱们在连接wifi网络时使用的是STA_IF模式,所以这里访问webrepl时需要用路由给分配的内网ip,192.168.4.1这个就不能用来连接了。
2.3. 设置开发板启动即开启webrepl
前面已经在boot.py
启动文件中配置好了开发板启动即连接到wifi网络,那要再开启webrepl也就容易了,把启动服务指令加到连接网络后面就行
1 2 3 4 |
<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span> do_connect<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">import</span> webrepl webrepl<span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span> |
转载请注明:徐自远的乱七八糟小站 » MicroPython-On-ESP8266——自动连接WIFI与开启webrepl_进击的大米-程序员宝宝_esp8266自动连接