主控板通过ESP8266(01) 实现网络通讯(非AT方式)

一、ESP8266刷Micropython固件

1.1 物品准备

在这里插入图片描述

1.1.1 ESP8266-01或ESP8266-01S

在这里插入图片描述

1.1.2 18650电池盒DC头

锂电池2节18650带线 2节7.4V 电池盒 串联充电
在这里插入图片描述
在这里插入图片描述

1.1.3 多路DC-DC电压转换模块电源

多路DC-DC电压转换模块电源 12V转3.3/5/12V AMS1117
在这里插入图片描述

1.1.4 面包板

在这里插入图片描述

1.1.5 USB-TTL

在这里插入图片描述

1.1.6 USB转串口驱动下载

https://download.youkuaiyun.com/download/cwdelphi/16139417

1.1.7 刷机固件下载

https://micropython.org/download/esp8266/
注意,只能选1M的!!!
Daily builds, 1M of flash
https://micropython.org/resources/firmware/esp8266-1m-20210202-v1.14.bin
在这里插入图片描述

1.1.8 刷机软件下载

https://docs.ai-thinker.com/_media/flash_download_tool_v3.8.5_1.zip
在这里插入图片描述

1.2 ESP8266针脚图

在这里插入图片描述

1.3 ESP8266刷机接线

在这里插入图片描述
刷机接线图:

外接电源面包板
3V33V3
GNDGND
ESP8266引脚USB-TTL刷机器
RXDTXD
TXDRXD
ESP8266引脚面包板
3V33V3
GNDGND
GPIO0GND

正常工作接线图:

外接电源面包板
3V33V3
GNDGND
ESP8266引脚USB-TTL刷机器
RXDTXD
TXDRXD
ESP8266引脚面包板
3V33V3
GNDGND

在这里插入图片描述
参数设置按上图,刷机过程中,ESP8266的电源引脚要插拔两次。

二、ESP8266网络程序编写示例

2.1连接WIFI

import network
import time

def connect_wlan(essid, password): 
    wifi = network.WLAN(network.STA_IF)  
    if not wifi.isconnected(): 
        print('connecting to network...')
        wifi.active(True) 
        wifi.connect(essid, password) 
        time.sleep(10) #一般睡个5-10秒,应该绰绰有余      
 
        if not wifi.isconnected():
            wifi.active(False) #关掉连接,免得repl死循环输出
            print('wifi connection error, please reconnect')
            connect_wlan(essid, password) # 重新连接
        else:
            print('network config:', wifi.ifconfig())
    else:
        print('network config:', wifi.ifconfig())
        
if __name__ == '__main__':
    connect_wlan(essid = 'XX',password = 'XX')

三、ESP8266和主控板之间的串口通讯

3.1 主控板代码

from machine import UART,Pin
import uasyncio as asyncio

async def uart2_write_thread(u):
    print('uart2_write_thread start')

    count = 1
    while True:
        print('Send: hello {0}'.format(count))
        u.write('hello {0}\n'.format(count))
        count = count + 1
        await asyncio.sleep(1)

async def uart2_read_thread(u):
    print('uart2_read_thread start')
    while True:
        if u.any():
            bin_data = u.readline()
            print('Echo Byte: {}'.format(bin_data))
            print('Echo String: {}'.format(bin_data.decode()))
        else:
            await asyncio.sleep(1)
 
def main():  
    uart2 = UART(2, baudrate=9600, rx=17,tx=16,timeout=10)
    
    loop = asyncio.get_event_loop()
    loop.create_task(uart2_read_thread(uart2))
    loop.create_task(uart2_write_thread(uart2))
    loop.run_forever()
 
if __name__ == '__main__':
    main()

3.2 ESP8266代码

from machine import UART,Pin
import uasyncio as asyncio

async def uart2_write_thread(u):
    print('uart2_write_thread start')

    count = 1
    while True:
        print('Send: hello {0}'.format(count))
        u.write('hello {0}\n'.format(count))
        count = count + 1
        await asyncio.sleep(1)

async def uart2_read_thread(u):
    print('uart2_read_thread start')
    while True:
        if u.any():
            bin_data = u.readline()
            print('Echo Byte: {}'.format(bin_data))
            print('Echo String: {}'.format(bin_data.decode()))
        else:
            await asyncio.sleep(1)
 
def main():  
    uart2 = UART(2, baudrate=9600, rx=17,tx=16,timeout=10)
    
    loop = asyncio.get_event_loop()
    loop.create_task(uart2_read_thread(uart2))
    loop.create_task(uart2_write_thread(uart2))
    loop.run_forever()
 
if __name__ == '__main__':
    main()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值