基于Micropython的天气显示-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 5293|回复: 0

[项目] 基于Micropython的天气显示

[复制链接]
发表于 2019-4-16 14:55 | 显示全部楼层 |阅读模式
看到大家做的项目,感觉自己做的好简单
项目介绍:

使用STM32+ESP8266的方案来获取天气信息,并且在LCD上进行显示。
(好尴尬,这个项目就一行解释完了)


流程说明:
STM32和ESP8266都是采用micropython官方提供的固件。
STM32负责LCD的显示。
ESP8266负责获取天气信息。
两者的桥梁是通过UART进行通信。由ESP8266将获取到的天气信息发送给STM32进行显示
(强行扯了几行,显得没有那么短


项目视频:


配套代码:
STM32: STM32开发板代码.rar (416.49 KB, 下载次数: 12)
ESP8266: ESP8266开发板代码.rar (1.17 KB, 下载次数: 17)

项目详细的制作过程说明见:
https://www.arduino.cn/forum.php?mod=viewthread&tid=85731&page=1&extra=#pid461293
更下项目的进程哈!

项目的思路很简单,就是从网上获取天气信息,然后进行显示!

很尽量把过程写清楚,方便大家的交流和学习。
首先说明下使用的硬件信息: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-prompt


STM32代码部分
Micropython的LCD显示器的驱动参考自:
https://github.com/ropod7/pyboard_drive/tree/master/ILI9341
采用的是ILI9341的显示驱动的显示屏。LCD显示的代码实现比较简单,就是各个元素的显示位置需要调整下。

LCD显示的时候需要用到天气图标,在openweathermap找到了相应的图标,所以就直接用爬虫下载了,有点多,懒得点。。哈
(小程序,不是很规范,仅供参考)[mw_shl_code=python,true]# encoding: utf-8

import requests
import os
from lxml import etree

url = "https://openweathermap.org/weather-conditions"

headers ={
    '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, headers=headers)
#print(response.content)
data = response.content

html = etree.HTML(data)
icon_table = html.xpath("//table[@class='table 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,然后选择文件,存储为,或者是使用快捷键ctrl+shift+s。
然后在弹出的存储为窗口中,选择保存文件格式为bmp,格式的下拉框第三个选项。
然后会弹出BMP选项,点击高级模式按钮,在左边的选项中选择:R5 G6 B5,确定即可。


STM32下载micropython固件,驱动LCD,并且接受UART中断数据进行显示的代码如下:
display.py
[mw_shl_code=python,true]# endcoding:utf-8
import os
import gc

import pyb, micropython

from lcd import LCD, Chars, ILI, imgdir, cachedir, imgcachepath
from colors import *
from machine import UART


l = LCD()
c = l.initCh(color=BLACK, font='Vera_15', bgcolor=WHITE)
uart = UART(4, 9600)                        
uart.init(9600, bits=8, parity=None, stop=1)

def draw_bg():
    l.portrait = False # 修改为横屏显示
    l.fillMonocolor( WHITE )
    l.drawRect(170, 10, 140, 100, MAROON, border=1)
    l.drawRect(170, 130, 140, 100, MAROON, border=1)
    l.drawRect(10, 130, 140, 100, MAROON, border=1)
    c.printLn('Weather Forcast', 10, 15, scale=1)
    c.printLn('TEMPERATURE', 175, 20, scale=1)
    c.printLn('HUMIDITY', 200, 140, scale=1)
    c.printLn('WINDSPEED', 30, 140, scale=1)
   
    c.printLn('°C', 270, 60, scale=2)
    c.printLn('%', 270, 180, scale=2)
    c.printLn('m/s', 95, 180, scale=2)

def display_data(s):
    l.portrait = False # 修改为横屏显示
    data = s.split(',')
    l.drawRect(200, 60, 68, 25, WHITE, border=0) # 清空temp显示区域
    l.drawRect(200, 180, 68, 25, WHITE, border=0)# 清空humdity显示区域
    l.drawRect(30, 180, 60, 25, WHITE, border=0) # 清空windspeed显示区域
    l.drawRect(30, 30, 110, 90, WHITE, border=0) # 清空icon显示区域
    c.printLn(data[0][:5], 200, 60, scale=2) # 温度
    if data[1] == "----":
        c.printLn(data[1], 200, 180, scale=2)# 湿度
    else:
        c.printLn(data[1], 220, 180, scale=2)# 湿度
    c.printLn(data[2], 30, 180, scale=2) # 风速
    if data[3] != '----':
        img = data[3][:3]+'.bmp'
        l.renderBmp(img, pos=(40, 120))
    else:
        c.printLn(data[3], 55, 60, scale=2)

def irq_func(uart_o):
    try:
        data = str(uart_o.read(), 'utf8')
    except:
        return
    print('----->>>>: ', data)
    if (data == 'connecting'):
        l.portrait = False # 修改为横屏显示
        l.drawRect(30, 30, 110, 90, WHITE, border=0)
        c.printLn('connecting', 35, 60, scale=1)
    elif data.startswith('data'):
        data = data[5:]
        display_data(data)
   
uart.irq(trigger=UART.IRQ_RXIDLE, handler=irq_func)

def init_display():
    draw_bg()
    display_data('----,----,----,----') # 初始化显示
[/mw_shl_code]
main.py
[mw_shl_code=python,true]# -*- coding: utf-8 -*-

from display import *
from machine import UART

uart = UART(4, 9600)                        
uart.init(9600, bits=8, parity=None, stop=1)

init_display() # 初始化完成后再开启UART
uart.irq(trigger=UART.IRQ_RXIDLE, handler=irq_func)[/mw_shl_code]


ESP8266代码部分
WiFi模块就只要简单ESP8266模块来完成就可以了。定时获取天气信息(这里我设置了1分钟更新一次,对于天气更新的话,可以不这么频繁)并且发送给STM32的代码如下:config.py
[mw_shl_code=python,true]# encoding:utf-8
import os
import network
import socket
import json
from machine import UART
import time

uart=UART(0,9600)
uart.init(9600, bits=8, parity=None, stop=1)

def get_weather_info():
    key = 'openweatermap注册账号,然后获取这个key值'
    info = b''
    weather_data = 'data:'
    s = socket.socket()
    addr = socket.getaddrinfo('api.openweathermap.org', 80)
    s.connect(addr[0][4])
    s.send(b'GET http://api.openweathermap.org/data/2.5/weather?q=Putian,CN&appid=%s HTTP/1.0\r\n\r\n' % key)
    while True:
        data = s.recv(100)
        if data:
            info += data
        else:
            break
    s.close()
    data = info.split(b'\r\n\r\n')[-1]
    data = json.loads(data)
    temp = data['main']['temp'] - 273.15
    humidity = 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_data
   
def send_data(data):
    uart.write(data)

def do_connect():
    send_data("\r\n")
    connect = 'connecting'
    send_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_code=arduino,true]from config import *
import os
from machine import Timer

def send_data_period(tim):
    send_data(get_weather_info())

tim = Timer(-1)
tim.init(period=60000, mode=Timer.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的固件来完成,对空间和内存有一定要求,可以试试)





您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-9-20 22:36 , Processed in 0.073959 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表