MicroPython动手做(33)——物联网之天气预报-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

123
返回列表 发新帖
楼主: eagler8

MicroPython动手做(33)——物联网之天气预报

[复制链接]
 楼主| 发表于 2020-6-20 15:11 | 显示全部楼层
9、使用三轴传感器选择城市天气预报

向前倾斜为安徽合肥,向后为福建福州
向左为上海,向右为北京


[mw_shl_code=arduino,true]#MicroPython动手做(33)——物联网之天气预报
#使用三轴传感器选择城市天气预报

from mpython import *
import urequests
import network
import ntptime
import music
import json


brightness=9

weather_serveraddr = "http://server.mindplus.top"

weather_appid = "31982666"

weather_appsecret = "E6MtBcxQ"

def weather_getWeather(_weather, _city):
  if weather_serveraddr=="http://www.tianqiapi.com":
    nowResult = urequests.get(weather_serveraddr+"/api/?appid="+weather_appid+"&appsecret="+weather_appsecret+"&version=v6&cityid="+_city)
    json=nowResult.json()
    nowResult.close()
    return json[_weather]
  else:
    nowResult = urequests.get(weather_serveraddr+"/api/weather/?appid="+weather_appid+"&appsecret="+weather_appsecret+"&version=v6&cityid="+_city)
    json=nowResult.json()
    nowResult.close()
    return json[_weather]


my_wifi = wifi()
my_wifi.connectWiFi("zh","zy1567")
while not (my_wifi.sta.isconnected()):
  pass
rgb[1] = (0*brightness//9, 102*brightness//9, 0*brightness//9)
rgb.write()
music.pitch(165, 50)
ntptime.settime(8, "ntp.ntsc.ac.cn")
while True:
  if (accelerometer.get_x() < -0.3):
    music.pitch(196, 50)
    oled.fill(0)
    oled.DispChar("安徽 合肥", 0, (1-1)*16, 1)
    oled.DispChar(weather_getWeather("wea", "101220101"), 0, (2-1)*16, 1)
    oled.DispChar((str("最低温度:") + str(weather_getWeather("tem2", "101220101"))), 0, (3-1)*16, 1)
    oled.DispChar((str("最高温度:") + str(weather_getWeather("tem1", "101220101"))), 0, (4-1)*16, 1)
    oled.show()
  if (accelerometer.get_x() > 0.3):
    music.pitch(262, 50)
    oled.fill(0)
    oled.DispChar("福建 福州", 0, (1-1)*16, 1)
    oled.DispChar(weather_getWeather("wea", "101230101"), 0, (2-1)*16, 1)
    oled.DispChar((str("最低温度:") + str(weather_getWeather("tem2", "101230101"))), 0, (3-1)*16, 1)
    oled.DispChar((str("最高温度:") + str(weather_getWeather("tem1", "101230101"))), 0, (4-1)*16, 1)
    oled.show()
  if (accelerometer.get_y() > 0.3):
    music.pitch(392, 50)
    oled.fill(0)
    oled.DispChar("上海", 0, (1-1)*16, 1)
    oled.DispChar(weather_getWeather("wea", "101020100"), 0, (2-1)*16, 1)
    oled.DispChar((str("最低温度:") + str(weather_getWeather("tem2", "101020100"))), 0, (3-1)*16, 1)
    oled.DispChar((str("最高温度:") + str(weather_getWeather("tem1", "101020100"))), 0, (4-1)*16, 1)
    oled.show()
  if (accelerometer.get_y() < -0.3):
    music.pitch(659, 50)
    oled.fill(0)
    oled.DispChar("北京", 0, (1-1)*16, 1)
    oled.DispChar(weather_getWeather("wea", "101010100"), 0, (2-1)*16, 1)
    oled.DispChar((str("最低温度:") + str(weather_getWeather("tem2", "101010100"))), 0, (3-1)*16, 1)
    oled.DispChar((str("最高温度:") + str(weather_getWeather("tem1", "101010100"))), 0, (4-1)*16, 1)
    oled.show()[/mw_shl_code]
 楼主| 发表于 2020-6-20 15:19 | 显示全部楼层
本帖最后由 eagler8 于 2020-6-20 15:26 编辑

Mind+  实验图形编程

23.jpg
 楼主| 发表于 2020-6-20 15:46 | 显示全部楼层
10、全球天气API平台

链接:http://www.tianqiapi.com

24.jpg
25.jpg
 楼主| 发表于 2020-6-20 15:58 | 显示全部楼层
注册全球天气API平台账号

链接:http://www.tianqiapi.com/user/register

26.jpg
 楼主| 发表于 2020-6-20 16:05 | 显示全部楼层
登录全球天气API平台

链接:http://www.tianqiapi.com/user/index

激活账号,获取APPID和APPSecret(密钥)

27 (1).jpg

 楼主| 发表于 2020-6-20 18:42 | 显示全部楼层
11、六位触摸按键选择城市天气预报

P 上海   Y 福州   T 北京   H 重庆   O 深圳  N 济南

[mw_shl_code=arduino,true]#MicroPython动手做(33)——物联网之天气预报
#六位触摸按键选择城市天气预报

from machine import Timer
from mpython import *
import urequests
import network
import ntptime
import music
import json


touch_threshold = {'P': 400, 'Y': 400, 'T': 400, 'H': 400, 'O': 400, 'N': 400}

tim12 = Timer(12)
_status_p = _status_y = _status_t = _status_h = _status_o = _status_n = 0
def on_touchpad_P_pressed():pass
def on_touchpad_P_unpressed():pass
def on_touchpad_Y_pressed():pass
def on_touchpad_Y_unpressed():pass
def on_touchpad_T_pressed():pass
def on_touchpad_T_unpressed():pass
def on_touchpad_H_pressed():pass
def on_touchpad_H_unpressed():pass
def on_touchpad_O_pressed():pass
def on_touchpad_O_unpressed():pass
def on_touchpad_N_pressed():pass
def on_touchpad_N_unpressed():pass

def timer12_tick(_):
  global _status_p, _status_y, _status_t, _status_h, _status_o, _status_n
  try:
    touchPad_P.read();pass
  except:
    return
  if touchPad_P.read() < touch_threshold['P']:
    if 1 != _status_p:_status_p = 1;on_touchpad_P_pressed()
  elif 0 != _status_p:_status_p = 0;on_touchpad_P_unpressed()
  if touchPad_Y.read() < touch_threshold['Y']:
    if 1 != _status_y:_status_y = 1;on_touchpad_Y_pressed()
  elif 0 != _status_y:_status_y = 0;on_touchpad_Y_unpressed()
  if touchPad_T.read() < touch_threshold['T']:
    if 1 != _status_t:_status_t = 1;on_touchpad_T_pressed()
  elif 0 != _status_t:_status_t = 0;on_touchpad_T_unpressed()
  if touchPad_H.read() < touch_threshold['H']:
    if 1 != _status_h:_status_h = 1;on_touchpad_H_pressed()
  elif 0 != _status_h:_status_h = 0;on_touchpad_H_unpressed()
  if touchPad_O.read() < touch_threshold['O']:
    if 1 != _status_o:_status_o = 1;on_touchpad_O_pressed()
  elif 0 != _status_o:_status_o = 0;on_touchpad_O_unpressed()
  if touchPad_N.read() < touch_threshold['N']:
    if 1 != _status_n:_status_n = 1;on_touchpad_N_pressed()
  elif 0 != _status_n:_status_n = 0;on_touchpad_N_unpressed()

def weather_getWeather(_weather, _city):
  if weather_serveraddr=="http://www.tianqiapi.com":
    nowResult = urequests.get(weather_serveraddr+"/api/?appid="+weather_appid+"&appsecret="+weather_appsecret+"&version=v6&cityid="+_city)
    json=nowResult.json()
    nowResult.close()
    return json[_weather]
  else:
    nowResult = urequests.get(weather_serveraddr+"/api/weather/?appid="+weather_appid+"&appsecret="+weather_appsecret+"&version=v6&cityid="+_city)
    json=nowResult.json()
    nowResult.close()
    return json[_weather]

brightness=9

weather_serveraddr = "http://www.tianqiapi.com"

weather_appid = "85215611"

weather_appsecret = "Kx8r5ZCY "

# 事件回调函数
def on_touchpad_P_pressed():
  global g_my_variable
  music.pitch(147, 500)
  oled.fill(0)
  oled.DispChar("上海", 0, (1-1)*16, 1)
  oled.DispChar(weather_getWeather("wea", "101020100"), 0, (2-1)*16, 1)
  oled.DispChar((str("最低温度:") + str(weather_getWeather("tem2", "101020100"))), 0, (3-1)*16, 1)
  oled.DispChar((str("最高温度:") + str(weather_getWeather("tem1", "101020100"))), 0, (4-1)*16, 1)
  oled.show()
def on_touchpad_H_pressed():
  global g_my_variable
  music.pitch(175, 500)
  oled.fill(0)
  oled.DispChar("重庆", 0, (1-1)*16, 1)
  oled.DispChar(weather_getWeather("wea", "101040100"), 0, (2-1)*16, 1)
  oled.DispChar((str("最低温度:") + str(weather_getWeather("tem2", "101040100"))), 0, (3-1)*16, 1)
  oled.DispChar((str("最高温度:") + str(weather_getWeather("tem1", "101040100"))), 0, (4-1)*16, 1)
  oled.show()
def on_touchpad_Y_pressed():
  global g_my_variable
  music.pitch(247, 500)
  oled.fill(0)
  oled.DispChar("福州", 0, (1-1)*16, 1)
  oled.DispChar((str("天气情况:") + str(weather_getWeather("wea", "101230101"))), 0, (2-1)*16, 1)
  oled.DispChar((str("最低温度:") + str(weather_getWeather("tem2", "101230101"))), 0, (3-1)*16, 1)
  oled.DispChar((str("最高温度:") + str(weather_getWeather("tem1", "101230101"))), 0, (4-1)*16, 1)
  oled.show()
def on_touchpad_O_pressed():
  global g_my_variable
  music.pitch(196, 500)
  oled.fill(0)
  oled.DispChar("深圳", 0, (1-1)*16, 1)
  oled.DispChar(weather_getWeather("wea", "101280601"), 0, (2-1)*16, 1)
  oled.DispChar((str("最低温度:") + str(weather_getWeather("tem2", "101280601"))), 0, (3-1)*16, 1)
  oled.DispChar((str("最高温度:") + str(weather_getWeather("tem1", "101280601"))), 0, (4-1)*16, 1)
  oled.show()
def on_touchpad_T_pressed():
  global g_my_variable
  music.pitch(165, 500)
  oled.fill(0)
  oled.DispChar("北京", 0, (1-1)*16, 1)
  oled.DispChar(weather_getWeather("wea", "101010100"), 0, (2-1)*16, 1)
  oled.DispChar((str("最低温度:") + str(weather_getWeather("tem2", "101010100"))), 0, (3-1)*16, 1)
  oled.DispChar((str("最高温度:") + str(weather_getWeather("tem1", "101010100"))), 0, (4-1)*16, 1)
  oled.show()
def on_touchpad_N_pressed():
  global g_my_variable
  music.pitch(220, 500)
  oled.fill(0)
  oled.DispChar("济南", 0, (1-1)*16, 1)
  oled.DispChar(weather_getWeather("wea", "101120101"), 0, (2-1)*16, 1)
  oled.DispChar((str("最低温度:") + str(weather_getWeather("tem2", "101120101"))), 0, (3-1)*16, 1)
  oled.DispChar((str("最高温度:") + str(weather_getWeather("tem1", "101120101"))), 0, (4-1)*16, 1)
  oled.show()


tim12.init(period=100, mode=Timer.PERIODIC, callback=timer12_tick)
my_wifi = wifi()
my_wifi.connectWiFi("zh","zy1567")
while not (my_wifi.sta.isconnected()):
  pass
rgb[1] = (0*brightness//9, 102*brightness//9, 0*brightness//9)
rgb.write()
music.pitch(131, 50)
ntptime.settime(8, "ntp.ntsc.ac.cn")
oled.fill(0)
oled.DispChar("触摸按键选城市", 0, (1-1)*16, 1)
oled.DispChar("P 上海   Y 福州", 0, (2-1)*16, 1)
oled.DispChar("T 北京   H 重庆", 0, (3-1)*16, 1)
oled.DispChar("O 深圳  N 济南", 0, (4-1)*16, 1)
oled.show()[/mw_shl_code]
 楼主| 发表于 2020-6-20 18:49 | 显示全部楼层
Mind+  实验图形编程

28.jpg

29.jpg
 楼主| 发表于 2020-6-20 18:53 | 显示全部楼层
30.jpg

37.jpg
 楼主| 发表于 2020-6-20 18:57 | 显示全部楼层
六位触摸按键选择城市天气预报

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

本版积分规则

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

GMT+8, 2024-12-28 01:09 , Processed in 0.097637 second(s), 16 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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