micropython支持中文啦
目前micropython官方版本固件是不支持中文的,经过我们2周加班加点的努力,终于可以支持中文啦中文支持包含一下几个方面
1. string类型的变量直接可用中文
比如
a="中文"
print(a)
2. 文件可以使用中文注释
比如
#这个程序完成了XXXX功能
3. 网络资源获取到中文字符串的时候,可以直接打印
比如
本例程会提供一个天气预报的例子,请看完全部教程
4. 可以通过Framebuf将汉字打印到液晶上
比如
oled.text("愿Micropython越来越强大",0,0)
本帖子转贴自
http://www.dfrobot.com.cn/community/thread-26740-1-1.html
学习了一下http的get和post,用get请求完成了一个获取天气的应用,供大家参考学习一.注册网站
https://www.seniverse.com
使用它的免费服务,一天可以请求400次,基本天气信息都有了
二. 注册完成后会有API-KEY,请保存下来
http://www.dfrobot.com.cn/community/data/attachment/forum/201708/08/172057c09lhl9jvo0fvsoj.png
三. 准备材料
1. firebeetle-esp32或firebeetle-esp8266主板
2. uPyCraft 0.24版
四 开始实验
1.将uPy_Lib目录下的urequests.py拖动到板子的lib目录下(注意这个文件岛主做了更改,官方原版不支持params)
2.将uPy_Lib目录下的ssd1306.py拖动到板子的lib目录下
3. 使用下面这个代码,保存成py文件,下载到板子上并运行
import urequests as requests
from machine import Pin,I2C,SPI
import time
import network
import ujson
import ssd1306
import HZK
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=100000)
spi = SPI(baudrate=100000, polarity=1, phase=0, sck=Pin(18), mosi=Pin(23), miso=Pin(19))
cs = Pin(25,Pin.OUT)
HZK.init(spi,cs)
lcd=ssd1306.SSD1306_I2C(128,64,i2c)
API_KEY = 'mggxhv8zbogqbmqj'
UID = "UF239A86CE"
LOCATION = 'chengdu'
URL = 'https://api.seniverse.com/v3/weather/now.json'
UNIT = 'c'
LANGUAGE = 'zh-Hans'
SSID="YOURSSID"
PASSWORD="YOURPASSWORD"
wlan=None
s=None
def connectWifi(ssid,passwd):
global wlan
wlan=network.WLAN(network.STA_IF)
wlan.active(True)
wlan.disconnect()
wlan.connect(ssid,passwd)
while(wlan.ifconfig()=='0.0.0.0'):
time.sleep(1)
return True
def fetchWeather(location):
result = requests.get(URL, params={
'key': API_KEY,
'location': location,
'language': LANGUAGE,
'unit': UNIT
})
return result.text
if __name__ == '__main__':
connectWifi(SSID,PASSWORD)
result = fetchWeather(LOCATION)
print(result)
j=ujson.loads(result)
print("\r\n\r\n")
print(j['results']['location']['name'])
print(j['results']['now']['text'])
print(j['results']['now']['temperature'])
addr=j['results']['location']['name']
weather=j['results']['now']['text']
temperature=j['results']['now']['temperature']
lcd.text("地点:%s"%addr,5,0)
lcd.text("天气:%s"%weather,5,16)
lcd.text("温度:%s"%temperature,5,32)
lcd.show()
五.实验效果
http://www.dfrobot.com.cn/community/data/attachment/forum/201708/08/175504x0aj49n0t252nizj.png
http://www.dfrobot.com.cn/community/data/attachment/forum/201708/08/175615tgogbssopsmgbkml.png
这样就能看到大成都的天气啦!
:P:P:P:P:P:P:P:P:P:P:P:P:P:P:P:P:P:Pimport urequests as requests
from machine import Pin,I2C,SPI
import time
import network
import ujson
import ssd1306
import HZK
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=100000)
spi = SPI(baudrate=100000, polarity=1, phase=0, sck=Pin(18), mosi=Pin(23), miso=Pin(19))
cs = Pin(25,Pin.OUT)
HZK.init(spi,cs)
lcd=ssd1306.SSD1306_I2C(128,64,i2c)
API_KEY = 'mggxhv8zbogqbmqj'
UID = "UF239A86CE"
LOCATION = 'chengdu'
URL = 'https://api.seniverse.com/v3/weather/now.json'
UNIT = 'c'
LANGUAGE = 'zh-Hans'
SSID="YOURSSID"
PASSWORD="YOURPASSWORD"
wlan=None
s=None
def connectWifi(ssid,passwd):
global wlan
wlan=network.WLAN(network.STA_IF)
wlan.active(True)
wlan.disconnect()
wlan.connect(ssid,passwd)
while(wlan.ifconfig()=='0.0.0.0'):
time.sleep(1)
return True
def fetchWeather(location):
result = requests.get(URL, params={
'key': API_KEY,
'location': location,
'language': LANGUAGE,
'unit': UNIT
})
return result.text
if __name__ == '__main__':
connectWifi(SSID,PASSWORD)
result = fetchWeather(LOCATION)
print(result)
j=ujson.loads(result)
print("\r\n\r\n")
print(j['results']['location']['name'])
print(j['results']['now']['text'])
print(j['results']['now']['temperature'])
addr=j['results']['location']['name']
weather=j['results']['now']['text']
temperature=j['results']['now']['temperature']
lcd.text("地点:%s"%addr,5,0)
lcd.text("天气:%s"%weather,5,16)
lcd.text("温度:%s"%temperature,5,32)
lcd.show() ???????????????????????????????????????????????????????????????????????
页:
[1]