Guest User

Untitled

a guest
Jun 19th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.12 KB | None | 0 0
  1. #define BLYNK_PRINT Serial // กำหนด BYNK_PRINT = เท่ากับ Serail
  2. #include <ESP8266WiFi.h> // เรียกใช้ library ESP8266WiFi8266.h เป็น library Wifi ESP8266
  3. #include <BlynkSimpleEsp8266.h> // เรียกใช้ library BlynkSimpleEsp8266.h เป็น library app Blynk
  4. #define SERVER_PORT 80 //กำหนด SERVER_PORT = 80
  5. #include <LiquidCrystal.h> // // เรียกใช้ library LiquidCrystal.h เป็น library จอ LCD
  6. #define LINE_TOKEN "NEO6NR0SJ6huFOOJfE4zcyntIRli0nXgougXSsHJ6SS" // รหัส LINE Notify token
  7. void Weather_display(); //กำหนด ฟังก์ชัน Weather_display
  8. void Line_Notify1(String message1) ; //กำหนด ฟังก์ชัน Line_Notify1
  9. void Line_Notify2(String message2) ; //กำหนด ฟังก์ชัน Line_Notify2
  10. void Line_Notify3(String message3) ; //กำหนด ฟังก์ชัน Line_Notify3
  11. void Line_Notify4(String message4) ; //กำหนด ฟังก์ชัน Line_Notify4
  12. void Line_Notify5(String message5) ; //กำหนด ฟังก์ชัน Line_Notify5
  13. void Line_Notify6(String message6) ; //กำหนด ฟังก์ชัน Line_Notify6
  14. void Line_Notify7(String message7) ; //กำหนด ฟังก์ชัน Line_Notify7
  15. String str_get1 = "GET /data/2.5/weather?q="; // กำหนดค่าคำสั่ง GET
  16. String str_get2 = "&APPID=0da75c9e4dbd2d567f43307b7236f9fb&mode=xml&units=metric HTTP/1.1\r\n"; // กำหนดคำสั่ง HTTP
  17. String str_host = "Host: api.openweathermap.org\r\n\r\n"; // กำหนด Server
  18. String Value ; // กำหนด Value เป็น String
  19. String city ; // กำหนด city เป็น String
  20. String message1 = "%E0%B9%80%E0%B8%A1%E0%B8%86%E0%B8%99%E0%B9%89%E0%B8%AD%E0%B8%A2%20%E0%B8%95%E0%B8%AD%E0%B8%99%E0%B8%A1%E0%B8%B5%E0%B9%81%E0%B8%94%E0%B8%94%E0%B8%99%E0%B9%88%E0%B8%B2%E0%B8%88%E0%B8%B0%E0%B9%81%E0%B8%A3%E0%B8%87%E0%B8%99%E0%B8%B0%20%20%E0%B8%96%E0%B9%89%E0%B8%B2%E0%B8%81%E0%B8%A5%E0%B8%B1%E0%B8%A7%E0%B9%81%E0%B8%94%E0%B8%94%E0%B8%9E%E0%B8%81%E0%B8%A3%E0%B9%88%E0%B8%A1%E0%B9%84%E0%B8%9B%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2%E0%B8%81%E0%B9%87%E0%B9%84%E0%B8%94%E0%B9%89%E0%B8%99%E0%B8%B0";
  21. String message2 = "%E0%B8%81%E0%B9%89%E0%B8%AD%E0%B8%99%E0%B9%80%E0%B8%A1%E0%B8%86%E0%B8%9B%E0%B8%A3%E0%B8%B0%E0%B8%9B%E0%B8%A3%E0%B8%B2%E0%B8%A2%20%E0%B9%83%E0%B8%99%E0%B8%95%E0%B8%AD%E0%B8%99%E0%B9%80%E0%B8%8A%E0%B9%89%E0%B8%B2%E0%B8%96%E0%B9%89%E0%B8%B2%E0%B8%81%E0%B8%A5%E0%B8%B1%E0%B8%A7%E0%B9%81%E0%B8%94%E0%B8%94%E0%B8%81%E0%B9%87%E0%B8%9E%E0%B8%81%E0%B8%A3%E0%B9%88%E0%B8%A1%E0%B9%84%E0%B8%9B%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2";
  22. String message3 = "%E0%B8%81%E0%B9%89%E0%B8%AD%E0%B8%99%E0%B9%80%E0%B8%A1%E0%B8%86%E0%B8%A1%E0%B8%B5%E0%B8%AA%E0%B8%B5%E0%B8%84%E0%B8%A5%E0%B9%89%E0%B8%B3%20%E0%B8%9D%E0%B8%99%E0%B8%AD%E0%B8%B2%E0%B8%88%E0%B8%88%E0%B8%B0%E0%B8%95%E0%B8%81%E0%B8%84%E0%B8%A7%E0%B8%A3%E0%B8%95%E0%B8%B4%E0%B8%94%E0%B8%A3%E0%B9%88%E0%B8%A1%E0%B9%84%E0%B8%9B%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2%E0%B8%81%E0%B9%87%E0%B8%94%E0%B8%B5%E0%B8%99%E0%B8%B0";
  23. String message4 = "%E0%B9%80%E0%B8%A1%E0%B8%86%E0%B8%A1%E0%B8%B7%E0%B8%94%E0%B8%84%E0%B8%A3%E0%B8%B6%E0%B9%89%E0%B8%A1%20%E0%B9%83%E0%B8%AB%E0%B9%89%E0%B8%84%E0%B8%B2%E0%B8%94%E0%B8%A7%E0%B9%88%E0%B8%B2%E0%B8%9D%E0%B8%99%E0%B8%95%E0%B8%81%E0%B9%81%E0%B8%99%E0%B9%88%E0%B8%99%E0%B8%AD%E0%B8%99%20%E0%B8%95%E0%B9%89%E0%B8%AD%E0%B8%87%E0%B8%95%E0%B8%B4%E0%B8%94%E0%B8%A3%E0%B9%88%E0%B8%A1%E0%B9%84%E0%B8%9B%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2%E0%B8%99%E0%B8%B0";
  24. String message5 = "%E0%B8%9D%E0%B8%99%E0%B8%95%E0%B8%81%E0%B8%9B%E0%B8%A3%E0%B8%AD%E0%B8%A2%E0%B9%86%20%E0%B8%AD%E0%B8%A2%E0%B9%88%E0%B8%B2%E0%B8%A5%E0%B8%B7%E0%B8%A1%E0%B9%80%E0%B8%AD%E0%B8%B2%E0%B8%A3%E0%B9%88%E0%B8%A1%E0%B9%84%E0%B8%9B%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2%E0%B8%99%E0%B8%B0";
  25. String message6 = "%E0%B8%9D%E0%B8%99%E0%B8%95%E0%B8%81%E0%B8%9B%E0%B8%B2%E0%B8%99%E0%B8%81%E0%B8%A5%E0%B8%B2%E0%B8%87%20%E0%B9%80%E0%B8%AD%E0%B8%B2%E0%B8%A3%E0%B9%88%E0%B8%A1%E0%B9%84%E0%B8%9B%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2%E0%B8%99%E0%B8%B0";
  26. String message7 = "%E0%B8%9D%E0%B8%99%E0%B8%95%E0%B8%81%E0%B9%81%E0%B8%99%E0%B9%88%E0%B8%99%E0%B8%AD%E0%B8%99%20%E0%B8%95%E0%B9%89%E0%B8%AD%E0%B8%87%E0%B9%80%E0%B8%AD%E0%B8%B2%E0%B8%A3%E0%B9%88%E0%B8%A1%E0%B9%84%E0%B8%9B%E0%B8%99%E0%B8%B0%20%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B9%80%E0%B8%9B%E0%B9%87%E0%B8%99%E0%B8%AB%E0%B9%88%E0%B8%A7%E0%B8%87";
  27. LiquidCrystal lcd(D6, D5, D4, D3, D2, D1); //ฟังก์ชั่นแรกกำหนดPins ที่ใช้ในการเชื่อมต่อ
  28. WiFiServer server(SERVER_PORT); //เปิดใช้งาน TCP Port 80
  29. WiFiClient client; //ประกาศ WiFiClient client
  30. char auth[] = "0aba9c3e70804ef7afb41a9ff816beeb"; // รหัส app Blynk
  31. const char* server_ip = "api.openweathermap.org"; //กำหนดชื่อ Server ที่ต้องการเชื่อมต่อ
  32. const long interval = 30000; //กำหนดค่าตัวแปร ให้ทำงานทุกๆ 30 วินาที
  33. unsigned long previousMillis = 0; //กำหนดตัวแปรเก็บค่า เวลาสุดท้ายที่ทำงาน
  34. unsigned long timer0 = 0 ; // กำหนด timer0 = 0
  35. unsigned long timer1 = 0 ; // กำหนด timer0 = 1
  36.  
  37.  
  38. void setup()
  39. {
  40. lcd.begin(16, 2); // ให้ LCD เริ่มทำงาน
  41. Serial.begin(115200); // กำหนดความเร็วให้รับส่ง-ข้อมูลความเร็ว 115200
  42. Blynk.begin(auth, "PHONE2541", "0812393668"); // ใส่รหัส Blynk และรหัส WiFi
  43. lcd.print("Welcome"); // แสดงผล Welcome ใน จอ LCD
  44. lcd.setCursor(0, 1); // ให้แสดงผลที่ บรรทัดที่ 1
  45. lcd.print("My Project"); // แสดงผล My Project
  46. }
  47. BLYNK_WRITE(V0)
  48. {
  49. Serial.println(param.asStr()); // ให้แสดงค่า ที่พิมพ์ใน Blynk ออก Serail Monitor
  50. Value = param.asStr(); // กำหนดให้ Value = param.asStr
  51. delay(200); // Delay 200
  52. }
  53.  
  54.  
  55. void loop()
  56. {
  57. Blynk.run(); // สั่งให้ Blynk ทำงาน
  58. city = Value ; // กำหนดให้ city = Value
  59. Weather_display();
  60. unsigned long currentMillis = millis(); //อ่านค่าเวลาที่ ESP เริ่มทำงานจนถึงเวลาปัจจุบัน
  61. if (currentMillis - previousMillis >= interval) //ถ้าหากเวลาปัจจุบันลบกับเวลาก่อหน้านี้ มีค่า มากกว่าค่า interval ให้คำสั่งภายใน if ทำงาน
  62. {
  63. previousMillis = currentMillis; //ให้เวลาปัจจุบัน เท่ากับ เวลาก่อนหน้าเพื่อใช้คำนวณเงื่อนไขในรอบถัดไป
  64. Client_Request(); //เรียกใช้ฟังก์ชันClient_Request
  65. }
  66. }
  67.  
  68.  
  69. void Weather_display()
  70. {
  71. while (client.available()) //ตรวจเช็คว่ามีการส่งค่ากลับมาจาก Server หรือไม่
  72. {
  73. String line = client.readStringUntil('\n'); //อ่านค่าที่ Server ตอบกลับมาทีละบรรทัด
  74. String get_data = cut_string(line, "city", "name"); //ตัด string ข้อมูลส่วนชื่อจังหวัด
  75. if (get_data != "NULL") // ถ้า get_data ไม่เท่ากับ ค่าว่างเปล่า ให้คำสั่งภายใน if ทำงาน
  76. {
  77. Serial.println("City"); // ให้แสดง City ใน Serial Monitor
  78. Serial.println(get_data); // ให้แสดงค่า get_data ใน Serial Monitor
  79. lcd.clear(); // clear LCD
  80. lcd.print(get_data); // แสดงค่า get_data ในจอ LCD
  81.  
  82. }
  83. get_data = cut_string(line, "weather", "value"); //ตัด string ข้อมูลส่วน ความดัน
  84. if (get_data != "NULL") // ถ้า get_data ไม่เท่ากับ ค่าว่างเปล่า ให้คำสั่งภายใน if ทำงาน
  85. {
  86. Serial.println("Weather"); // ให้แสดง Weather ใน Serial Monitor
  87. Serial.println(get_data); // ให้แสดงค่า get_data ใน Serial Monitor
  88. lcd.setCursor(0, 1); // ให้แสดงในบรรทัดที่ 1 จอ LCD
  89. lcd.print(get_data); // แสดงค่า get_data ในจอ LCD
  90. if (get_data == "few clouds") // ถ้า get_data ท่ากับ few clouds ให้คำสั่งภายใน if ทำงาน
  91. {
  92. Serial.println("Weather"); // ให้แสดง Weather ใน Serial Monitor
  93. Serial.println(get_data); // ให้แสดงค่า get_data ใน Serial Monitor
  94. lcd.setCursor(0, 1); // ให้แสดงในบรรทัดที่ 1 จอ LCD
  95. lcd.print(get_data); // แสดงค่า get_data ในจอ LCD
  96. Line_Notify1(message1); // เรียกใช้ฟังก์ชัน Line_Notify1
  97. }
  98. if (get_data == "scattered clouds") // ถ้า get_data ท่ากับ scattered clouds ให้คำสั่งภายใน if ทำงาน
  99. {
  100. Serial.println("Weather"); // ให้แสดง Weather ใน Serial Monitor
  101. Serial.println(get_data); // ให้แสดงค่า get_data ใน Serial Monitor
  102. lcd.setCursor(0, 1); // ให้แสดงในบรรทัดที่ 1 จอ LCD
  103. lcd.print(get_data); // แสดงค่า get_data ในจอ LCD
  104. Line_Notify2(message2); // เรียกใช้ฟังก์ชัน Line_Notify2
  105. }
  106. if (get_data == "broken clouds") // ถ้า get_data ท่ากับ broken clouds ให้คำสั่งภายใน if ทำงาน
  107. {
  108. Serial.println("Weather"); // ให้แสดงค่า get_data ใน Serial Monitor
  109. Serial.println(get_data); // ให้แสดงค่า get_data ใน Serial Monitor
  110. lcd.setCursor(0, 1); // ให้แสดงในบรรทัดที่ 1 จอ LCD
  111. lcd.print(get_data); // แสดงค่า get_data ในจอ LCD
  112. Line_Notify3(message3); // เรียกใช้ฟังก์ชัน Line_Notify3
  113. }
  114. if (get_data == "overcast clouds") // ถ้า get_data ท่ากับ overcast clouds ให้คำสั่งภายใน if ทำงาน
  115. {
  116. Serial.println("Weather"); // ให้แสดง Weather ใน Serial Monitor
  117. Serial.println(get_data); // ให้แสดงค่า get_data ใน Serial Monitor
  118. lcd.setCursor(0, 1); // ให้แสดงในบรรทัดที่ 1 จอ LCD
  119. lcd.print(get_data); // แสดงค่า get_data ในจอ LCD
  120. Line_Notify4(message4); // เรียกใช้ฟังก์ชัน Line_Notify4
  121. }
  122. if (get_data == "light rain") // ถ้า get_data ท่ากับ light rain ให้คำสั่งภายใน if ทำงาน
  123. {
  124. Serial.println("Weather"); // ให้แสดง Weather ใน Serial Monitor
  125. Serial.println(get_data); // ให้แสดงค่า get_data ใน Serial Monitor
  126. lcd.setCursor(0, 1); // ให้แสดงในบรรทัดที่ 1 จอ LCD
  127. lcd.print(get_data); // แสดงค่า get_data ในจอ LCD
  128. Line_Notify5(message5); // เรียกใช้ฟังก์ชัน Line_Notify5
  129. }
  130.  
  131. if (get_data == "moderate rain") // ถ้า get_data ท่ากับ moderate rain ให้คำสั่งภายใน if ทำงาน
  132. {
  133. Serial.println("Weather"); // ให้แสดง Weather ใน Serial Monitor
  134. Serial.println(get_data); // ให้แสดงค่า get_data ใน Serial Monitor
  135. lcd.setCursor(0, 1); // ให้แสดงในบรรทัดที่ 1 จอ LCD
  136. lcd.print(get_data); // แสดงค่า get_data ในจอ LCD
  137. Line_Notify6(message6); // เรียกใช้ฟังก์ชัน Line_Notify6
  138. }
  139. if (get_data == "heavy intensity rain") // ถ้า get_data ท่ากับ heavy intensity rain ให้คำสั่งภายใน if ทำงาน
  140. {
  141. Serial.println("Weather"); // ให้แสดง Weather ใน Serial Monitor
  142. Serial.println(get_data); // ให้แสดงค่า get_data ใน Serial Monitor
  143. lcd.setCursor(0, 1); // ให้แสดงในบรรทัดที่ 1 จอ LCD
  144. lcd.print("heavy rain"); // แสดงค่า get_data ในจอ LCD
  145. Line_Notify7(message7); // เรียกใช้ฟังก์ชัน Line_Notify7
  146. }
  147. if (get_data == "light intensity shower rain") // ถ้า get_data ท่ากับ light intensity shower rain ให้คำสั่งภายใน if ทำงาน
  148. {
  149. Serial.println("Weather"); // ให้แสดง Weather ใน Serial Monitor
  150. Serial.println(get_data); // ให้แสดงค่า get_data ใน Serial Monitor
  151. lcd.setCursor(0, 1); // ให้แสดงในบรรทัดที่ 1 จอ LCD
  152. lcd.print("shower rain"); // แสดงค่า get_data ในจอ LCD
  153. Line_Notify7(message7); // เรียกใช้ฟังก์ชัน Line_Notify7
  154. }
  155. }
  156. }
  157. }
  158.  
  159.  
  160. void Client_Request()
  161. {
  162. Serial.println("Connect TCP Server"); // ให้แสดง Connect TCP Server ใน Serial Monitor
  163. int cnt = 0; // ประกาศตัวแปร cnt = 0
  164. while (!client.connect(server_ip, SERVER_PORT)) // เชื่อมต่อกับ Server และรอจนกว่าเชื่อมต่อสำเร็จ
  165. {
  166. Serial.print("."); // แสดง . ใน Serial Monitor
  167. delay(100); // delay เวลา 100
  168. cnt++; // cnt = cnt + 1
  169. if (cnt > 50) // ถ้าหากใช้เวลาเชื่อมต่อเกิน 5 วินาที ให้ออกจาก ฟังก์ชั่น
  170. return; // return กลับไป
  171. }
  172. Serial.println("Success"); // แสดง Success ใน Serial Monitor
  173. delay(500); // delay เวลา 500
  174. client.print(str_get1 + city + str_get2 + str_host); // ส่งคำสั่ง HTTP GET ไปยัง Server
  175. Serial.print(str_get1 + city + str_get2 + str_host); // แสดงค่าคำสั่ง HTTP GET ไปยัง Server ใน Serial Monitor
  176. delay(100); // delay เวลา 100
  177. }
  178. String cut_string(String input, String header, String get_string) // ประกาศตัวแปร cut_string มี Array ที่ประกอบด้วย input header get_string
  179. {
  180. if (input.indexOf(header) != -1) //ตรวจสอบว่าใน input มีข้อความเหมือนใน header หรือไม่
  181. {
  182. int num_get = input.indexOf(get_string, input.indexOf(header)); //หาตำแหน่งของข้อความ get_string ใน input
  183. if (num_get != -1) //ตรวจสอบว่าตำแหน่งที่ได้ไม่ใช่ -1 (ไม่มีข้อความ get_string ใน input)
  184. {
  185. int start_val = input.indexOf("\"", num_get) + 1; // หาตำแหน่งแรกของ “
  186. int stop_val = input.indexOf("\"", start_val); // หาตำแหน่งสุดท้ายของ “
  187. return (input.substring(start_val, stop_val)); //ตัดเอาข้อความระหว่า “แรก และ ”สุดท้าย
  188. }
  189. else // ถ้าเงื่อนไขไม่ตรงให้ทำในคำสั่ง else
  190. {
  191. return ("NULL"); //Return ข้อความ NULL เมื่อไม่ตรงเงื่อนไข
  192. }
  193. }
  194. return ("NULL"); //Return ข้อความ NULL เมื่อไม่ตรงเงื่อนไข
  195. }
  196.  
  197.  
  198. void Line_Notify1(String message1)
  199. {
  200. digitalWrite(D7,HIGH);
  201. WiFiClientSecure client; // ประกาศ WiFiClientSecure client
  202. if (!client.connect("notify-api.line.me", 443)) // ถ้าเชื่อมต่อไม่ได้ให้ไปทำงานในฟังก์ชัน
  203. {
  204. Serial.println("connection failed"); // แสดงค่า connection failedconnection ใน Serial Monitor
  205. return; //Return กลับไป
  206. }
  207. String req = ""; // ประกาศตัวแปร req
  208. req += "POST /api/notify HTTP/1.1\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  209. req += "Host: notify-api.line.me\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  210. req += "Authorization: Bearer " + String(LINE_TOKEN) + "\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  211. req += "Cache-Control: no-cache\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  212. req += "User-Agent: ESP8266\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  213. req += "Connection: close\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  214. req += "Content-Type: application/x-www-form-urlencoded\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  215. req += "Content-Length: " + String(String("message=" + message1).length()) + "\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  216. req += "\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  217. req += "message=" + message1; // req ก่อนหน้านี้มาบวกประโยคกัน
  218. client.print(req); // ส่งข้อความ req ผ่าน Serial Monitor
  219. delay(20); // Delay เวลา 20
  220. while (client.connected()) // ถ้า client.connected ให้ทำงานใน while ถ้าไม่ ออกนอก while
  221. {
  222. String line = client.readStringUntil('\n'); // ประกาศตัวแปร line = client.readStringUntil
  223. if (line == "\r") // ถ้า line = เริ่มบรรทัดหม่ ให้ทำงานใน if
  224. digitalWrite(D7,HIGH);
  225. {
  226. break; // หยุด
  227. }
  228. }
  229. }
  230.  
  231.  
  232. void Line_Notify2(String message2)
  233. {
  234. digitalWrite(D7,HIGH);
  235. WiFiClientSecure client; // ประกาศ WiFiClientSecure client
  236.  
  237. if (!client.connect("notify-api.line.me", 443)) // ถ้าเชื่อมต่อไม่ได้ให้ไปทำงานในฟังก์ชัน
  238. {
  239. Serial.println("connection failed"); // แสดงค่า connection failedconnection ใน Serial Monitor
  240. return; //Return กลับไป
  241. }
  242. String req = ""; // ประกาศตัวแปร req
  243. req += "POST /api/notify HTTP/1.1\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  244. req += "Host: notify-api.line.me\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  245. req += "Authorization: Bearer " + String(LINE_TOKEN) + "\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  246. req += "Cache-Control: no-cache\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  247. req += "User-Agent: ESP8266\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  248. req += "Connection: close\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  249. req += "Content-Type: application/x-www-form-urlencoded\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  250. req += "Content-Length: " + String(String("message=" + message2).length()) + "\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  251. req += "\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  252. req += "message=" + message2; // req ก่อนหน้านี้มาบวกประโยคกัน
  253. client.print(req); // ส่งข้อความ req ผ่าน Serial Monitor
  254. delay(20); // Delay เวลา 20
  255. while (client.connected()) // ถ้า client.connected ให้ทำงานใน while ถ้าไม่ ออกนอก while
  256. {
  257. String line = client.readStringUntil('\n'); // ประกาศตัวแปร line = client.readStringUntil
  258. if (line == "\r") // ถ้า line = เริ่มบรรทัดหม่ ให้ทำงานใน if
  259. digitalWrite(D7,HIGH);
  260. {
  261. break; // หยุดแล้วออกคำสั่ง
  262. }
  263. }
  264. }
  265.  
  266.  
  267. void Line_Notify3(String message3)
  268. {
  269. digitalWrite(D7,HIGH);
  270. WiFiClientSecure client; // ประกาศ WiFiClientSecure client
  271.  
  272. if (!client.connect("notify-api.line.me", 443)) // ถ้าเชื่อมต่อไม่ได้ให้ไปทำงานในฟังก์ชัน
  273. {
  274. Serial.println("connection failed"); // แสดงค่า connection failedconnection ใน Serial Monitor
  275. return; //Return กลับไป
  276. }
  277. String req = ""; // ประกาศตัวแปร req
  278. req += "POST /api/notify HTTP/1.1\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  279. req += "Host: notify-api.line.me\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  280. req += "Authorization: Bearer " + String(LINE_TOKEN) + "\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  281. req += "Cache-Control: no-cache\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  282. req += "User-Agent: ESP8266\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  283. req += "Connection: close\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  284. req += "Content-Type: application/x-www-form-urlencoded\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  285. req += "Content-Length: " + String(String("message=" + message3).length()) + "\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  286. req += "\r\n"; // req ก่อนหน้านี้มาบวกประโยคกัน
  287. req += "message=" + message3; // req ก่อนหน้านี้มาบวกประโยคกัน
  288. client.print(req); // ส่งข้อความ req ผ่าน Serial Monitor
  289. delay(20); // Delay เวลา 20
  290. while (client.connected()) // ถ้า client.connected ให้ทำงานใน while ถ้าไม่ ออกนอก while
  291.  
  292. {
  293. String line = client.readStringUntil('\n'); // ประกาศตัวแปร line = client.readStringUntil
  294. if (line == "\r") // ถ้า line = เริ่มบรรทัดหม่ ให้ทำงานใน if
  295. {
  296. break; // หยุดแล้วออกคำสั่ง
  297. }
  298. }
  299. }
  300.  
  301.  
  302. void Line_Notify4(String message4)
  303. {
  304. digitalWrite(D7,HIGH);
  305. WiFiClientSecure client; // ประกาศ WiFiClientSecure client
  306. if (!client.connect("notify-api.line.me", 443)) // ถ้าเชื่อมต่อไม่ได้ให้ไปทำงานในฟังก์ชัน
  307. {
  308. Serial.println("connection failed"); // แสดงค่า connection failedconnection ใน Serial Monitor
  309. return; //Return กลับไป
  310. }
  311. String req = ""; // ประกาศตัวแปร req
  312. req += "POST /api/notify HTTP/1.1\r\n"; // ประกาศตัวแปร req
  313. req += "Host: notify-api.line.me\r\n"; // ประกาศตัวแปร req
  314. req += "Authorization: Bearer " + String(LINE_TOKEN) + "\r\n"; // ประกาศตัวแปร req
  315. req += "Cache-Control: no-cache\r\n"; // ประกาศตัวแปร req
  316. req += "User-Agent: ESP8266\r\n"; // ประกาศตัวแปร req
  317. req += "Connection: close\r\n"; // ประกาศตัวแปร req
  318. req += "Content-Type: application/x-www-form-urlencoded\r\n"; // ประกาศตัวแปร req
  319. req += "Content-Length: " + String(String("message=" + message4).length()) + "\r\n"; // ประกาศตัวแปร req
  320. req += "\r\n"; // ประกาศตัวแปร req
  321. req += "message=" + message4; // ประกาศตัวแปร req
  322. client.print(req); // ส่งข้อความ req ผ่าน Serial Monitor
  323. delay(20); // Delay เวลา 20
  324. while (client.connected()) // ถ้า client.connected ให้ทำงานใน while ถ้าไม่ ออกนอก while
  325. {
  326. String line = client.readStringUntil('\n'); // ประกาศตัวแปร line = client.readStringUntil
  327. if (line == "\r") // ถ้า line = เริ่มบรรทัดหม่ ให้ทำงานใน if
  328. {
  329. break; // หยุดแล้วออกคำสั่ง
  330. }
  331. }
  332. }
  333.  
  334.  
  335. void Line_Notify5(String message5)
  336. {
  337. digitalWrite(D7,HIGH);
  338. WiFiClientSecure client; // ประกาศ WiFiClientSecure client
  339.  
  340. if (!client.connect("notify-api.line.me", 443)) // ถ้าเชื่อมต่อไม่ได้ให้ไปทำงานในฟังก์ชัน
  341. {
  342. Serial.println("connection failed"); // แสดงค่า connection failedconnection ใน Serial Monitor
  343. return; //Return กลับไป
  344. }
  345. String req = ""; // ประกาศตัวแปร req
  346. req += "POST /api/notify HTTP/1.1\r\n"; // ประกาศตัวแปร req
  347. req += "Host: notify-api.line.me\r\n"; // ประกาศตัวแปร req
  348. req += "Authorization: Bearer " + String(LINE_TOKEN) + "\r\n"; // ประกาศตัวแปร req
  349. req += "Cache-Control: no-cache\r\n"; // ประกาศตัวแปร req
  350. req += "User-Agent: ESP8266\r\n"; // ประกาศตัวแปร req
  351. req += "Connection: close\r\n"; // ประกาศตัวแปร req
  352. req += "Content-Type: application/x-www-form-urlencoded\r\n"; // ประกาศตัวแปร req
  353. req += "Content-Length: " + String(String("message=" + message5).length()) + "\r\n"; // ประกาศตัวแปร req
  354. req += "\r\n"; // ประกาศตัวแปร req
  355. req += "message=" + message5; // ประกาศตัวแปร req
  356. client.print(req); // ส่งข้อความ req ผ่าน Serial Monitor
  357. delay(20); // Delay เวลา
  358. while (client.connected()) // ถ้า client.connected ให้ทำงานใน while ถ้าไม่ ออกนอก while
  359. {
  360. String line = client.readStringUntil('\n'); // ประกาศตัวแปร line = client.readStringUntil
  361. if (line == "\r") // ถ้า line = เริ่มบรรทัดหม่ ให้ทำงานใน if
  362. {
  363. break; // หยุดแล้วออกคำสั่ง
  364. }
  365. }
  366. }
  367.  
  368.  
  369. void Line_Notify6(String message6)
  370. {
  371. digitalWrite(D7,HIGH);
  372. WiFiClientSecure client; // ประกาศ WiFiClientSecure client
  373.  
  374. if (!client.connect("notify-api.line.me", 443)) // ถ้าเชื่อมต่อไม่ได้ให้ไปทำงานในฟังก์ชัน
  375. {
  376. Serial.println("connection failed"); // แสดงค่า connection failedconnection ใน Serial Monitor
  377. return; //Return กลับไป
  378. }
  379. String req = ""; // ประกาศตัวแปร req
  380. req += "POST /api/notify HTTP/1.1\r\n"; // ประกาศตัวแปร req
  381. req += "Host: notify-api.line.me\r\n"; // ประกาศตัวแปร req
  382. req += "Authorization: Bearer " + String(LINE_TOKEN) + "\r\n"; // ประกาศตัวแปร req
  383. req += "Cache-Control: no-cache\r\n"; // ประกาศตัวแปร req
  384. req += "User-Agent: ESP8266\r\n"; // ประกาศตัวแปร req
  385. req += "Connection: close\r\n"; // ประกาศตัวแปร req
  386. req += "Content-Type: application/x-www-form-urlencoded\r\n"; // ประกาศตัวแปร req
  387. req += "Content-Length: " + String(String("message=" + message6).length()) + "\r\n"; // ประกาศตัวแปร req
  388. req += "\r\n"; // ประกาศตัวแปร req
  389. req += "message=" + message6; // ประกาศตัวแปร req
  390. client.print(req); // ส่งข้อความ req ผ่าน Serial Monitor
  391. delay(20); // Delay เวลา
  392. while (client.connected()) // ถ้า client.connected ให้ทำงานใน while ถ้าไม่ ออกนอก while
  393. {
  394. String line = client.readStringUntil('\n'); // ประกาศตัวแปร line = client.readStringUntil
  395. if (line == "\r") // ถ้า line = เริ่มบรรทัดหม่ ให้ทำงานใน if
  396. {
  397. break; // หยุดแล้วออกคำสั่ง
  398. }
  399. }
  400. }
  401.  
  402.  
  403. void Line_Notify7(String message7)
  404. {
  405. digitalWrite(D7,HIGH);
  406. WiFiClientSecure client; // ประกาศ WiFiClientSecure client
  407.  
  408. if (!client.connect("notify-api.line.me", 443)) // ถ้าเชื่อมต่อไม่ได้ให้ไปทำงานในฟังก์ชัน
  409. {
  410. Serial.println("connection failed"); // แสดงค่า connection failedconnection ใน Serial Monitor
  411. return; //Return กลับไป
  412. }
  413. String req = ""; // ประกาศตัวแปร req
  414. req += "POST /api/notify HTTP/1.1\r\n"; // ประกาศตัวแปร req
  415. req += "Host: notify-api.line.me\r\n"; // ประกาศตัวแปร req
  416. req += "Authorization: Bearer " + String(LINE_TOKEN) + "\r\n"; // ประกาศตัวแปร req
  417. req += "Cache-Control: no-cache\r\n"; // ประกาศตัวแปร req
  418. req += "User-Agent: ESP8266\r\n"; // ประกาศตัวแปร req
  419. req += "Connection: close\r\n"; // ประกาศตัวแปร req
  420. req += "Content-Type: application/x-www-form-urlencoded\r\n"; // ประกาศตัวแปร req
  421. req += "Content-Length: " + String(String("message=" + message7).length()) + "\r\n"; // ประกาศตัวแปร req
  422. req += "\r\n"; // ประกาศตัวแปร req
  423. req += "message=" + message7; // ประกาศตัวแปร req
  424. client.print(req); // ส่งข้อความ req ผ่าน Serial Monitor
  425. delay(20); // Delay เวลา
  426. while (client.connected()) // ถ้า client.connected ให้ทำงานใน while ถ้าไม่ ออกนอก while
  427. {
  428. String line = client.readStringUntil('\n'); // ประกาศตัวแปร line = client.readStringUntil
  429. if (line == "\r") // ถ้า line = เริ่มบรรทัดหม่ ให้ทำงานใน if
  430. {
  431. break; // หยุดแล้วออกคำสั่ง
  432. }
  433. }
  434. }
Advertisement
Add Comment
Please, Sign In to add comment