回到首页 返回首页
回到顶部 回到顶部
返回上一页 返回上一页

【PinPong】PinPong Board显示网络天气——关于WIFI功能测试的一个案例四个版本 简单

头像 宙仔 2021.05.06 368 2

值得先说明的一些事情:(也不知道有没有理解错误~)

(1)pinpong库是一套控制开源硬件主控板的Python库,简而言之pinpong是python库;

(2)pinpong主要实现硬件与Python的交互,仅仅是交互,而非代码刷入!

这个《pinpong board显示网络天气》的案例,是我对pinpong认识的过程。

一、第一版。

(一)思路:(乍看没啥问题哈)

project-image

(二)各功能实现代码

1.Pinpong Board链接WiFi的代码:

代码在pinpong库的例程里面有,我就不贴文字,上图~

project-image
project-image

WiFi指示灯亮绿灯,Oled屏显示IP地址,WiFi已成功连上了~

2.天气数据的获取

(1)中国天气网http://www.weather.com.cn/

数据请求网址:http://www.weather.com.cn/data/sk/101281301.html

PS:我这里查询了“清远”的天气情况,如果要查其他城市,可以登录中国天气网找到相应的城市代码,将请求链接中最后一串数字替换即可,如广州:101280101。

这里需要用到requests和json库,若与进行程序后提示缺少库文件,运行cmd,键入:

pip install requests

pip install json

数据获取回来之后,是一段json代码,因此,需要对其进行解析,可自行百度,不一一累述。代码如图:

project-image

3.oled屏幕显示天气。

pinpong Board的OLED显示屏显示的像素大小为128*64,每字符占高8*16像素。

一开始没留意,代码中直接显示城市的中文字符,运行报错,估计是板载oled在python模式下不支持中文!直接上拼音通过。

project-image
代码
import time
import requests
import json
from pinpong.board import Board,Pin,WIFI
from pinpong.libs.dfrobot_ssd1306 import SSD1306_I2C

Board("uno").begin()               

oled = SSD1306_I2C(width=128,height=64)
obj = WIFI()

ssid="wifiname"
password="wifipassword"
obj.set_ssid(ssid)
obj.set_password(password)
obj.connect_wifi()

print("Waiting for WIFI connection...")
time.sleep(1)

while True:
    ip,port = obj.get_ip_port()
    if ip != None:
        print("ip:{}port:{}".format(ip,port))
        break
    time.sleep(1)
    
ip = "ip:" + str(ip)
port = "port:" + str(port)

oled.text(ip,0,10)
oled.show()

url = 'http://www.weather.com.cn/data/sk/101281301.html'
response = requests.get(url)
response.encoding = 'utf-8'
res = json.loads(response.text)
city = res['weatherinfo']['city']
temp = res['weatherinfo']['temp']
shidu =res['weatherinfo']['SD']
time = res['weatherinfo']['time']

oled.fill(0)
oled.text("QingYuan Weather",0,0)
oled.text("Wendu:" + temp,0,16)
oled.text("Shidu:" + shidu,0,32)
oled.text("time:" + time,0,48)
oled.show()

案例这就完了?不,还有后续~~~

二、第二版。

纯粹是好玩~~

还记得开始的时候,我特别提醒的那两点吗?

既然直接用Python代码获取了网络天气数据,那是否可以不需要将PinPong Board开启WiFi连接,直接将其刷到oled显示屏?

也不知道如何将案例一刷入的WiFi连接清除,唯有用笨笨的方法,将密码改错,运行代码,此时看到WiFi指示灯变红了,嘿嘿。

为了区别,查询了广州的天气数据:http://www.weather.com.cn/data/sk/101280101.html

代码将WiFi部分去掉,点击运行!

project-image
project-image

好了,最初的想法还是可行的,接下来还有~~

三、第三版

这第三代码和第一版的代码是一样的,为什么是第三版?现在,我们把PC断网,记得一定要断网!

好了,运行第一版的代码,测试一下PC断网状态下,程序能否通过板载WiFi连接到中国天气网并获取天气的数据?

1.PC断网状态下,运行程序,板载WiFi成功连接!

project-image

2.PC断网状态下,板载WiFi成功连接路由,但拿不到数据了……

project-image

四、继续第四版——PinPong Board无线刷代码

这回怎么玩?

1.我们先输入PinPong Board关于连接WiFi的代码~~这里仅仅是WiFi连接。

代码沿用第一版的WiFi连接代码~~刷进入之后,我们可以看到WiFi指示灯变绿,oled显示屏显示pinpong board的ip地址,这时候,我们一定一定要把这个ip地址记录下来,接下来的程序会用到~

2.现在将PinPong Board从电脑的USB接口拔出来,利用手机充电电源来供电,板子开启之后,WiFi的指示灯从蓝色变成了绿色,这表示,板子已经和WiFi连接成功了! 这会不会是PinPong Board唯一能够驻留在板子中的Python代码???

3.好咯,现在我们通过WiFi连接的情况下,将我们的python代码运行到PinPong中,这里必须必须要强调的,PinPong  Board和电脑必须在同一网段~~~

4.贴个代码

代码
import requests
import json
from pinpong.libs.dfrobot_ssd1306 import SSD1306_I2C
from pinpong.board import Board,Pin


ip = "192.168.1.122"  #网络配置中OLEB屏上显示的ip
port = 8081    #网络配置中OLEB屏上显示的port

Board(ip, port)
oled = SSD1306_I2C(width=128,height=64)

url = 'http://www.weather.com.cn/data/sk/101281301.html'
response = requests.get(url)
response.encoding = 'utf-8'
res = json.loads(response.text)
city = res['weatherinfo']['city']
temp = res['weatherinfo']['temp']
shidu =res['weatherinfo']['SD']
time = res['weatherinfo']['time']

oled.fill(0)
oled.text("QingYuan Weather",0,0)
oled.text("Wendu:" + temp,0,16)
oled.text("Shidu:" + shidu,0,32)
oled.text("time:" + time,0,48)
oled.show()
project-image
project-image

最后,四个案例简单的小结:

1.PinPong Board仅仅是Python的一个库,可以实现Python与开源硬件的交互。

2.Python程序中涉及到的数据处理,可以在Python所在的系统(PC,树莓派,虚谷等)中处理!

3.WiFi功能,目前仅用于无线状态下刷代码的功能!

不正之处,望各位大佬们指正~~~

评论

user-avatar
  • 宙仔

    宙仔2021.05.08

    是乱码的,要转码 response.encoding = 'utf-8'

    0
    • gray6666

      gray66662021.05.07

      数据请求网址我这里打开咋是乱码 {"weatherinfo":{"city":"娓呰繙","cityid":"101281301","temp":"26.1","WD":"鍗楅","WS":"灏忎簬3绾�","SD":"86%","AP":"996.3hPa","njd":"鏆傛棤瀹炲喌","WSE":"<3","time":"17:50","sm":"2.6","isRadar":"0","Radar":""}}

      0