微信网站可以免费做么,广州seo推广营销,想卖产品怎么推广宣传,开发定制软件公司本帖最后由 michael_llh 于 2019-4-17 10:06 编辑更下项目的进程哈#xff01;项目的思路很简单#xff0c;就是从网上获取天气信息#xff0c;然后进行显示#xff01;很尽量把过程写清楚#xff0c;方便大家的交流和学习。首先说明下使用的硬件信息#xff1a;Micropyt…本帖最后由 michael_llh 于 2019-4-17 10:06 编辑更下项目的进程哈项目的思路很简单就是从网上获取天气信息然后进行显示很尽量把过程写清楚方便大家的交流和学习。首先说明下使用的硬件信息Micropython的开发板LCD显示屏WiFi模块ESP8266思路是这样LCD显示屏由Micropython开发板进行控制显示然后WiFi模块从网上下载天气信息并且将信息发送给Micropython开发板进行显示。其实是可以单单使用WiFi模块就可以完成这个项目的不需要使用到Micropython开发板的但是单单使用WiFi模块来做的话需要使用ESP32这个模块因为8266这个模块内存比较小驱动LCD显示屏需要较大的内存。手上没有ESP32的模块就只能做个变通了也能顺便锻炼下两个模块的通信问题哈。Micropython的准备问题首先我们需要烧写STM32和ESP8266的micropython固件固件可以直接在官网下载到。STM32开发板我使用的是micropython的开发板这个应该直接淘宝可以买到ESP8266使用的是NodeMCU这个板子买回来烧写成micropython的。关于烧写方法大家可以直接参考官网的说明很详细了,具体参考http://www.micropython.org/download也可以看看我之前的说明https://www.arduino.cn/thread-49997-1-1.html关于代码的上传这里说明下因为STM32的开发板是带有SD卡槽的所以我是将所有的代码拷贝到SD卡槽中。这里因为micropython实现SD的驱动并且把开发板模拟成一个读卡器插上电脑就会显示一个SD卡的盘直接拷贝进去就可以了。对于ESP8266比较麻烦因为这个板子只有两个UART然后UART1的一个引脚被板子上SPI芯片驱动的引脚占用了所以UART1没有办法使用而UART0又是我们需要连接电脑进行调试用的。所以需要采用官方提供的webrepl的方法来进行板子的调试和文件上传关于webrepl的使用也可以直接参考官网的说明http://docs.micropython.org/en/latest/esp8266/quickref.html#webrepl-web-browser-interactive-promptSTM32代码部分Micropython的LCD显示器的驱动参考自https://github.com/ropod7/pyboard_drive/tree/master/ILI9341采用的是ILI9341的显示驱动的显示屏。LCD显示的代码实现比较简单就是各个元素的显示位置需要调整下。LCD显示的时候需要用到天气图标在openweathermap找到了相应的图标所以就直接用爬虫下载了有点多懒得点。。哈(小程序不是很规范仅供参考)[mw_shl_codepython,true]# encoding: utf-8import requestsimport osfrom lxml import etreeurl https://openweathermap.org/weather-conditionsheaders {User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36}icon_addr ./weather_icon/if not os.path.exists(icon_addr):os.mkdir(icon_addr)response requests.get(url, headersheaders)#print(response.content)data response.contenthtml etree.HTML(data)icon_table html.xpath(//table[classtable table-bordered])[0]# print(etree.tostring(icon_table).decode(utf-8))trs icon_table.xpath(.//tr)[1:]for tr in trs:td tr.xpath(./td/img/src)for icon_url in td:respnse requests.get(icon_url)file_addr icon_addr icon_url[-7:]with open(file_addr, wb) as fp:fp.write(respnse.content)print(success save icon: %s % icon_url[-7:])[/mw_shl_code]网上能下载到的icon基本都是png svg格式的bmp的几乎没有再加上LCD显示图片格式有一定要求所以只能自己再转换下了。(But下载下来的图片太小了放大后很糊表示看着很难受所以我自己替换了几个部分没有找到合适的就将就用了)使用的是Photoshop然后选择文件存储为或者是使用快捷键ctrlshifts。然后在弹出的存储为窗口中选择保存文件格式为bmp格式的下拉框第三个选项。然后会弹出BMP选项点击高级模式按钮在左边的选项中选择R5 G6 B5确定即可。STM32下载micropython固件驱动LCD并且接受UART中断数据进行显示的代码如下display.py[mw_shl_codepython,true]# endcoding:utf-8import osimport gcimport pyb, micropythonfrom lcd import LCD, Chars, ILI, imgdir, cachedir, imgcachepathfrom colors import *from machine import UARTl LCD()c l.initCh(colorBLACK, fontVera_15, bgcolorWHITE)uart UART(4, 9600)uart.init(9600, bits8, parityNone, stop1)def draw_bg():l.portrait False # 修改为横屏显示l.fillMonocolor( WHITE )l.drawRect(170, 10, 140, 100, MAROON, border1)l.drawRect(170, 130, 140, 100, MAROON, border1)l.drawRect(10, 130, 140, 100, MAROON, border1)c.printLn(Weather Forcast, 10, 15, scale1)c.printLn(TEMPERATURE, 175, 20, scale1)c.printLn(HUMIDITY, 200, 140, scale1)c.printLn(WINDSPEED, 30, 140, scale1)c.printLn(°C, 270, 60, scale2)c.printLn(%, 270, 180, scale2)c.printLn(m/s, 95, 180, scale2)def display_data(s):l.portrait False # 修改为横屏显示data s.split(,)l.drawRect(200, 60, 68, 25, WHITE, border0) # 清空temp显示区域l.drawRect(200, 180, 68, 25, WHITE, border0)# 清空humdity显示区域l.drawRect(30, 180, 60, 25, WHITE, border0) # 清空windspeed显示区域l.drawRect(30, 30, 110, 90, WHITE, border0) # 清空icon显示区域c.printLn(data[0][:5], 200, 60, scale2) # 温度if data[1] ----:c.printLn(data[1], 200, 180, scale2)# 湿度else:c.printLn(data[1], 220, 180, scale2)# 湿度c.printLn(data[2], 30, 180, scale2) # 风速if data[3] ! ----:img data[3][:3].bmpl.renderBmp(img, pos(40, 120))else:c.printLn(data[3], 55, 60, scale2)def irq_func(uart_o):try:data str(uart_o.read(), utf8)except:returnprint(-----: , data)if (data connecting):l.portrait False # 修改为横屏显示l.drawRect(30, 30, 110, 90, WHITE, border0)c.printLn(connecting, 35, 60, scale1)elif data.startswith(data):data data[5:]display_data(data)uart.irq(triggerUART.IRQ_RXIDLE, handlerirq_func)def init_display():draw_bg()display_data(----,----,----,----) # 初始化显示[/mw_shl_code]main.py[mw_shl_codepython,true]# -*- coding: utf-8 -*-from display import *from machine import UARTuart UART(4, 9600)uart.init(9600, bits8, parityNone, stop1)init_display() # 初始化完成后再开启UARTuart.irq(triggerUART.IRQ_RXIDLE, handlerirq_func)[/mw_shl_code]ESP8266代码部分WiFi模块就只要简单ESP8266模块来完成就可以了。定时获取天气信息(这里我设置了1分钟更新一次对于天气更新的话可以不这么频繁)并且发送给STM32的代码如下config.py[mw_shl_codepython,true]# encoding:utf-8import osimport networkimport socketimport jsonfrom machine import UARTimport timeuartUART(0,9600)uart.init(9600, bits8, parityNone, stop1)def get_weather_info():key openweatermap注册账号然后获取这个key值info bweather_data data:s socket.socket()addr socket.getaddrinfo(api.openweathermap.org, 80)s.connect(addr[0][4])s.send(bGET http://api.openweathermap.org/data/2.5/weather?qPutian,CNappid%s HTTP/1.0\r\n\r\n % key)while True:data s.recv(100)if data:info dataelse:breaks.close()data info.split(b\r\n\r\n)[-1]data json.loads(data)temp data[main][temp] - 273.15humidity data[main][humidity]wind data[wind][speed]icon data[weather][0][icon]# weather_data.append({temp:temp, humidity:humidity, wind:wind})weather_data str(temp)weather_data ,weather_data str(humidity)weather_data ,weather_data str(wind)weather_data ,weather_data str(icon)return weather_datadef send_data(data):uart.write(data)def do_connect():send_data(\r\n)connect connectingsend_data(connect)time.sleep(1)wlan network.WLAN(network.STA_IF)wlan.active(True)if not wlan.isconnected():#print(connecting to network...)wlan.connect(Xiaomi_196E, 1234567890)while not wlan.isconnected():send_data(connect)time.sleep(1)print(network config:, wlan.ifconfig())[/mw_shl_code]main.py[mw_shl_codearduino,true]from config import *import osfrom machine import Timerdef send_data_period(tim):send_data(get_weather_info())tim Timer(-1)tim.init(period60000, modeTimer.PERIODIC, callback send_data_period)do_connect()time.sleep(1)send_data(get_weather_info())[/mw_shl_code]项目可以深入的地方1. ILI9341这个显示屏的话还支持触屏但是我代码中给的库还没有支持所以这个可以继续深入支持触屏的话还可以做很多好玩的事情。2. 代码中将对应的WiFi连接信息和获取信息的地点写在代码中了这个可以继续优化加上前面显示屏的例子可以进行在屏幕进行输入选择3. 项目中用到了STM32和ESP8266两块芯片其实可以直接使用ESP32单独完成的或者是单独的ESP8266(8266不一定的可以使用micropython的固件来完成对空间和内存有一定要求可以试试)