注册-进入控制台-申请天气接口(按照次数收费)


wincc
全局脚本-vbs-editor-标准模块添加代码(如下)下面是7日数据的


Sub GetWeather7Days() Dim http, url, response, i Dim dateStr, week, weather, lowest, highest, wind, windsc Dim block, key, city key = "1289916280853cfeb46036fa5b94ad3b" city = "济南" url = "https://apis.tianapi.com/tianqi/index?key=1289916280853cfeb46036fa5b94ad3b&city=101120101&type=7" Set http = CreateObject("MSXML2.XMLHTTP") http.Open "GET", url, False http.Send If http.Status = 200 Then response = http.ResponseText For i = 1 To 7 dateStr = ExtractJsonField(response, """date"":""", i) week = ExtractJsonField(response, """week"":""", i) weather = ExtractJsonField(response, """weather"":""", i) lowest = ExtractJsonField(response, """lowest"":""", i) highest = ExtractJsonField(response, """highest"":""", i) wind = ExtractJsonField(response, """wind"":""", i) windsc = ExtractJsonField(response, """windsc"":""", i) block = dateStr & " " & week & " " & weather & " " & lowest & "~" & highest & " " & wind & windsc Select Case i Case 1: HMIRuntime.ActiveScreen.ScreenItems("Day1").Text = block Case 2: HMIRuntime.ActiveScreen.ScreenItems("Day2").Text = block Case 3: HMIRuntime.ActiveScreen.ScreenItems("Day3").Text = block Case 4: HMIRuntime.ActiveScreen.ScreenItems("Day4").Text = block Case 5: HMIRuntime.ActiveScreen.ScreenItems("Day5").Text = block Case 6: HMIRuntime.ActiveScreen.ScreenItems("Day6").Text = block Case 7: HMIRuntime.ActiveScreen.ScreenItems("Day7").Text = block End Select Next Else For i = 1 To 7 HMIRuntime.ActiveScreen.ScreenItems("Day" & i).Text = "连接失败" Next End If End Sub Function ExtractJsonField(str, field, index) Dim pos, count, iStart, iEnd count = 0 pos = 1 Do pos = InStr(pos, str, field) If pos = 0 Then ExtractJsonField = "N/A" Exit Function End If count = count + 1 If count = index Then iStart = pos + Len(field) iEnd = InStr(iStart, str, """") ExtractJsonField = Mid(str, iStart, iEnd - iStart) Exit Function End If pos = pos + Len(field) Loop End Function
动作里添加触发每分钟执行一次 我是定时每天早上5点触发(脚本如下)
Option Explicit Function action Dim nowTime, nowHour, nowMin, todayStr, tagLastRun nowTime = Now nowHour = Hour(nowTime) nowMin = Minute(nowTime) todayStr = CStr(Date) ' 建议使用内存变量存储上次运行日期,假设变量名为 "LastWeatherRun" Set tagLastRun = HMIRuntime.Tags("LastWeatherRun") ' 每天 5:00 执行一次 If nowHour = 5 And nowMin = 0 Then If tagLastRun.Read <> todayStr Then Call GetWeather7Days() tagLastRun.Write todayStr End If End If End Function
页面做静态文本 对象名称 分别Day1,Day2,Day3-7,分别对应7日的天气 (见下图)



