2023年1月9日 星期一

Python 天氣 --> Node-Red MQTT --> Line Notifly

Python 天氣 --> Node-Red MQTT --> Line Notifly



import requests

import paho.mqtt.client as mqtt #import the client1

import time

publish=''

j=1

def on_message(client, userdata, message):

    print("message received " ,str(message.payload.decode("utf-8")))

    print("message topic=",message.topic)

    print("message qos=",message.qos)

    print("message retain flag=",message.retain)

########################################

broker_address="broker.mqtt-dashboard.com"

#broker_address="iot.eclipse.org"


print("creating new instance")

client = mqtt.Client("P1") #create new instance

client.on_message=on_message #attach function to callback

print("connecting to broker")

client.connect(broker_address) #connect to broke


# url = '一般天氣預報 - 今明 36 小時天氣預報 JSON 連結'

url = "https://opendata.cwb.gov.tw/fileapi/v1/opendataapi/F-C0032-001?Authorization=rdec-key-123-45678-011121314&format=JSON"

data = requests.get(url)   # 取得 JSON 檔案的內容為文字

data_json = data.json()    # 轉換成 JSON 格式

location = data_json['cwbopendata']['dataset']['location']

client.loop_start() #start the loop

for i in location:

    city = i['locationName']    # 縣市名稱

    wx8 = i['weatherElement'][0]['time'][0]['parameter']['parameterName']    # 天氣現象

    maxt8 = i['weatherElement'][1]['time'][0]['parameter']['parameterName']  # 最高溫

    mint8 = i['weatherElement'][2]['time'][0]['parameter']['parameterName']  # 最低溫

    ci8 = i['weatherElement'][3]['time'][0]['parameter']['parameterName']    # 舒適度

    pop8 = i['weatherElement'][4]['time'][0]['parameter']['parameterName']   # 降雨機率

    print(f'{j} {city}未來 8 小時{wx8},最高溫 {maxt8} 度,最低溫 {mint8} 度,降雨機率 {pop8} %')

    publish=(f'{j} {city}未來 8 小時{wx8},最高溫 {maxt8} 度,最低溫 {mint8} 度,降雨機率 {pop8} %')

    j=j+1


    print("Subscribing to topic","alex9ufo/weather")

    client.subscribe("alex9ufo/weather")

    

    client.publish("alex9ufo/weather",publish)

    time.sleep(10) # wait

    client.loop_stop() #stop the loop

>>> %Run -c $EDITOR_CONTENT

creating new instance

connecting to broker

1 臺北市未來 8 小時陰時多雲短暫雨,最高溫 20 度,最低溫 18 度,降雨機率 70 %

Subscribing to topic alex9ufo/weather

message received  1 臺北市未來 8 小時陰時多雲短暫雨,最高溫 20 度,最低溫 18 度,降雨機率 70 %

message topic= alex9ufo/weather

message qos= 0

message retain flag= 0

2 新北市未來 8 小時多雲時陰短暫雨,最高溫 21 度,最低溫 18 度,降雨機率 60 %

Subscribing to topic alex9ufo/weather

3 桃園市未來 8 小時多雲時陰短暫雨,最高溫 20 度,最低溫 18 度,降雨機率 50 %

Subscribing to topic alex9ufo/weather

4 臺中市未來 8 小時多雲短暫雨,最高溫 20 度,最低溫 18 度,降雨機率 30 %

Subscribing to topic alex9ufo/weather

5 臺南市未來 8 小時多雲時晴,最高溫 20 度,最低溫 18 度,降雨機率 20 %

Subscribing to topic alex9ufo/weather

6 高雄市未來 8 小時多雲時晴,最高溫 23 度,最低溫 19 度,降雨機率 20 %

Subscribing to topic alex9ufo/weather

7 基隆市未來 8 小時陰時多雲短暫雨,最高溫 20 度,最低溫 18 度,降雨機率 80 %

Subscribing to topic alex9ufo/weather

8 新竹縣未來 8 小時多雲短暫雨,最高溫 19 度,最低溫 17 度,降雨機率 30 %

Subscribing to topic alex9ufo/weather

9 新竹市未來 8 小時多雲短暫雨,最高溫 18 度,最低溫 17 度,降雨機率 30 %

Subscribing to topic alex9ufo/weather

10 苗栗縣未來 8 小時多雲短暫雨,最高溫 19 度,最低溫 18 度,降雨機率 30 %

Subscribing to topic alex9ufo/weather

11 彰化縣未來 8 小時多雲短暫雨,最高溫 19 度,最低溫 18 度,降雨機率 30 %

Subscribing to topic alex9ufo/weather

12 南投縣未來 8 小時多雲,最高溫 21 度,最低溫 17 度,降雨機率 20 %

Subscribing to topic alex9ufo/weather

13 雲林縣未來 8 小時多雲,最高溫 20 度,最低溫 17 度,降雨機率 20 %

Subscribing to topic alex9ufo/weather

14 嘉義縣未來 8 小時多雲,最高溫 20 度,最低溫 18 度,降雨機率 20 %

Subscribing to topic alex9ufo/weather

15 嘉義市未來 8 小時多雲,最高溫 21 度,最低溫 17 度,降雨機率 20 %

Subscribing to topic alex9ufo/weather

16 屏東縣未來 8 小時晴時多雲,最高溫 24 度,最低溫 19 度,降雨機率 20 %

Subscribing to topic alex9ufo/weather

17 宜蘭縣未來 8 小時陰短暫雨,最高溫 20 度,最低溫 17 度,降雨機率 80 %

Subscribing to topic alex9ufo/weather

18 花蓮縣未來 8 小時陰短暫雨,最高溫 20 度,最低溫 18 度,降雨機率 80 %

Subscribing to topic alex9ufo/weather

19 臺東縣未來 8 小時陰時多雲短暫雨,最高溫 21 度,最低溫 19 度,降雨機率 60 %

Subscribing to topic alex9ufo/weather

20 澎湖縣未來 8 小時多雲時陰短暫雨,最高溫 18 度,最低溫 17 度,降雨機率 30 %

Subscribing to topic alex9ufo/weather

21 金門縣未來 8 小時陰時多雲短暫雨,最高溫 17 度,最低溫 16 度,降雨機率 30 %

Subscribing to topic alex9ufo/weather

22 連江縣未來 8 小時陰短暫雨,最高溫 14 度,最低溫 13 度,降雨機率 30 %

Subscribing to topic alex9ufo/weather

>>> 



[{"id":"f1240a9f515684cc","type":"inject","z":"29a9d63b197c467b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":150,"y":100,"wires":[["aef5d1c2db77479d"]]},{"id":"aef5d1c2db77479d","type":"http request","z":"29a9d63b197c467b","name":"temp request","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://opendata.cwb.gov.tw/fileapi/v1/opendataapi/O-A0001-001?Authorization=CWB-40C25FFF-1224-4250-B9D9-3735AAE17DBF&downloadType=WEB&format=JSON","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":330,"y":100,"wires":[["90a8c0e89dfc30e3"]]},{"id":"90a8c0e89dfc30e3","type":"json","z":"29a9d63b197c467b","name":"","property":"payload","action":"","pretty":false,"x":470,"y":100,"wires":[["07a67f1be408f083"]]},{"id":"07a67f1be408f083","type":"debug","z":"29a9d63b197c467b","name":"debug 48","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":100,"wires":[]},{"id":"58ba84310f8e0524","type":"mqtt in","z":"29a9d63b197c467b","name":"","topic":"alex9ufo/weather","qos":"2","datatype":"auto-detect","broker":"841df58d.ee5e98","nl":false,"rap":true,"rh":0,"inputs":0,"x":180,"y":180,"wires":[["6c6271690eaaf77a","fb24e42ed91f6523"]]},{"id":"6c6271690eaaf77a","type":"debug","z":"29a9d63b197c467b","name":"debug 50","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":340,"y":180,"wires":[]},{"id":"fb24e42ed91f6523","type":"function","z":"29a9d63b197c467b","name":"Format timestamp","func":"var date = new Date();\nvar h = date.getHours();\nvar m = date.getMinutes();\nvar s = date.getSeconds();\nif(h<10){\n    h = '0'+h;\n}\nif(m<10){\n    m = '0' + m;\n}\nif(s<10){\n    s = '0' + s;\n}\nmsg.payload = msg.payload + ' --> Time:(' + h + ':' + m + ':' + s + ')' ;\n\nreturn msg;","outputs":1,"noerr":0,"x":150,"y":280,"wires":[["08047167226b2747"]]},{"id":"08047167226b2747","type":"function","z":"29a9d63b197c467b","name":"Set Line API ","func":"msg.headers = {'content-type':'application/x-www-form-urlencoded','Authorization':'Bearer A4wwPNh2WqB7dlfeQyyIAwtggn1kfZSI5LkkCdia1gB'};\nmsg.payload = {\"message\":msg.payload};\nreturn msg;\n\n//oR7KdXvK1eobRr2sRRgsl4PMq23DjDlhfUs96SyUBZu","outputs":1,"noerr":0,"x":330,"y":280,"wires":[["1f4d2676f1654319"]]},{"id":"1f4d2676f1654319","type":"http request","z":"29a9d63b197c467b","name":"","method":"POST","ret":"txt","paytoqs":false,"url":"https://notify-api.line.me/api/notify","tls":"","persist":false,"proxy":"","authType":"","x":500,"y":280,"wires":[["eeba3672b9db5840"]]},{"id":"eeba3672b9db5840","type":"debug","z":"29a9d63b197c467b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":650,"y":280,"wires":[]},{"id":"841df58d.ee5e98","type":"mqtt-broker","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]





沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...