程式碼:
https://gist.github.com/clive520/a088792c33914caa3693d0daf08a049c
#include <WiFi.h> #include <WiFiMulti.h> WiFiMulti wifiMulti; #define LED_BUILTIN 2 // 連接到WIFI時,亮內建的LED //連接LINE #include <WiFiClientSecure.h> WiFiClientSecure Secure_client; String LINE_TOKEN; void setup() { LINE_TOKEN = "tvfUV1c09uK0MP0000000000000000jXVH46ZvhE"; //輸入LINE的權杖 pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN,LOW) ; Serial.begin(9600); delay(10); //連接WIFI 連接多個WIFI wifiMulti.addAP("clive_lef", "12345678"); wifiMulti.addAP("clive_1F", "12345678"); wifiMulti.addAP("Free", "12341234"); wifiMulti.addAP("book", "12345678"); Serial.println("Connecting Wifi..."); if(wifiMulti.run() == WL_CONNECTED) { //連接多個WIFI delay(50); Serial.println(""); Serial.print("Successful Connecting to Access Point:"); Serial.println(WiFi.SSID()); Serial.print("SSID Signal strength:"); Serial.println(WiFi.RSSI());//WiFi.RSSI()訊號強度 Serial.println("WiFi connected"); Serial.print("IP address: "); Serial.println(WiFi.localIP());//WiFi.localIP()連接到的IP for (int count = 0; count < 5; count++) { //成功時亮內建的LED,閃爍5次 digitalWrite(LED_BUILTIN, HIGH) ; delay(500); digitalWrite(LED_BUILTIN, LOW) ; delay(500); } } //連接WIFI結束 } void loop() { TO_LINE("打入要傳送LINE的訊息");//傳送LINE的訊息 delay(60000); } //副程式 //傳訊息到LINE void TO_LINE(String x) { if (Secure_client.connect("notify-api.line.me",443)) { String data = "message="+String(x); Secure_client.println("POST /api/notify HTTP/1.1"); Secure_client.println("Host: notify-api.line.me"); Secure_client.println("Authorization: Bearer " + LINE_TOKEN); Secure_client.println("Content-Type: application/x-www-form-urlencoded"); Secure_client.print("Content-Length: "); Secure_client.println(data.length()); Secure_client.println(); Secure_client.println(data); Secure_client.stop(); } }
沒有留言:
張貼留言