Restart Heltec LoRa
สวัสดีครับ วันนี้มาสะสมเทคนิค ปลีกย่อยกันอีกเล็กน้อยครับ นั่นคือ การ Restart เจ้าบอร์ด IOT จำพวก esp8266, esp32 และ Heltec Lora esp32
ทำไมเราต้องใช้วิธีนี้ คำตอบคือ การทำงานของบอร์ด มีโอกาสที่จะเกิด error จากสาเหตุที่ไม่ทราบได้ ดังนั้น เราอาจจะให้บอร์ด ทำการ restart เพื่อให้มันพักผ่อน หน่วยความจำ เพื่อให้โปรแกรมกลับมาทำงานกันใหม่ เช่นเรากำหนดให้มันทำงานทุก 10000 รอบ หรือ เราจะตั้งเวลาไว้ก็ได้ หรือจะ รีโมทเข้ามาก็ได้
ในวันนี้เราจะให้มันนับรอบถอยหลัง จาก 20 รอบ แล้ว รีเซ็ทตัวเอง
ขั้นตอน
- กำหนดตัวแปลนับถอยหลัง int restart = 20 ในบรรทัด 33
- โปรแกรม Restart_loop() 274
- เรียกใช้งานที่บรรทัด 132
เมื่อโปรแกรมทำงานไป ก็จะนับถอยหลัง จนครบ 20 รอบตามที่กำหนดไว้ โปรแกรมก็จะเริ่มมาทำงานใหม่
ในแง่ความปลอดภัยของระบบที่นำไปใช้งาน ก็ถือว่าแทบไม่มีผลกระทบให้เกิดความเสียหาย เมื่อเทียบกับการปล่อยให้ระบบเดินไปจนถึงจุดจบโดยที่เราไม่สามารถทราบว่าจะเกิดขึ้นเมื่อไหร่ วิธีนี้ก็จะทำให้ระบบมีเสถียรภาพดีกว่าการไม่มี restart เลย
ไว้ตอนหน้าเราจะมาลองกับบอร์ดอื่นๆ กันต่อครับ
/////////////// Demo for ESP32 /////////////////////// #include <ArduinoJson.h> #include <WiFi.h> #include <Arduino.h> #include <Ticker.h> #include <Wire.h> #include <math.h> #include <ArduinoJson.h> #include <Arduino_JSON.h> ///============ #define LED 0 //=================== #include <Adafruit_Sensor.h> //=========== const char* ssid = "Arsenal2019_2.4GHz "; const char* password = "xxxxxxxx"; //===== const char* host = "otrixiot.com"; char* code = "xxxxxxxx"; char* dID = "xxxxx"; float data1,data2,data3,data4,data5,data6,data7,data8,data9,data10 ; float data11,data12,data13,data14,data15,data16,data17,data18,data19,data20 ; String led1 = "00:00"; String led2 = "00:00"; String led3 = "1"; String R1,R2,R3,R4,R5,R6,R7,R8 ; String RR1,RR2,RR3,RR4,RR5,RR6,RR7,RR8 ; //int counter = 1; int i ; int restart = 20; int sentcount = 0; String url ; int Relay1 = 12 ; int Relay2 = 2 ; // String iddevice = dID; String cccode = code; String ccode = code; long lastMillis = 100; ////////////////////////////////////////////////////////// void Aboutme() { Serial.println("File name : GFM "); } ///////////////////////////////////////////////////////// void WiFisetup() { Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); delay(1500); if (WiFi.status() != WL_CONNECTED) { delay(500); Serial.println("Failed to connected ssdi1 and WiFi setup "); } else{ Serial.println(""); Serial.println("WiFi connected OK"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } } bool state = true; void WiFiForwardSetup() { // We start by connecting to a WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void setup() { //WIFI Kit series V1 not support Vext control Serial.begin(115200); WiFisetup(); //WiFiForwardSetup(); } void loop() { data1 = random(5.5,7.5); data2 = random(17.5,19.5); data3 = random(19.5,21.5); data4 = random(21.5,23.5); data5 = random(33.5,35.5); data6 = random(45.5,47.5); data7 = random(17.5,19.5); data8 = random(19.5,21.5); data9 = random(21.5,23.5); data10 = random(23.5,25.5); data11 = random(25.5,27.5); data12 = random(27.5,29.5); data13 = random(29.5,31.5); data14 = random(31.5,33.5); data15 = random(33.5,35.5); data16 = random(35.5,37.5); data17 = random(37.5,39.5); data18 = random(39.5,41.5); data19 = random(41.5,43.5); data20 = random(43.5,45.5); if (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Failed to connected and will restart WiFi setup "); WiFi.disconnect(); WiFi.begin(ssid, password); WiFiForwardSetup(); } Forward_loop(); read_master_status_FromServer(); Restart_loop(); } /////////////////////////////////// int value = 0; void Forward_loop() { ++value; Serial.print("connecting to server Otrixiot .... "); Serial.println(host); // Use WiFiClient class to create TCP connections WiFiClient client; const int httpPort = 80; if (!client.connect(host, httpPort)) { Serial.println("connection failed"); return; } // We now create a URI for the request String url = "/api/insertData?device_id=" + String(iddevice)+"&code="+String(ccode)+"&data1=" +String(data1) +"&data2=" + String(data2)+"&data3=" +String(data3)+"&data4=" +String(data4)+"&data5=" +String(data5) +"&data6=" +String(data6)+"&data7=" +String(data7)+"&data8=" +String(data8)+"&data9=" +String(data9) +"&data10=" +String(data10)+"&data11=" +String(data11)+"&data12=" +String(data12)+"&data13=" +String(data13) +"&data14=" +String(data14)+"&data15=" +String(data15)+"&data16=" +String(data16)+"&data17=" +String(data17) +"&data18=" +String(data18)+"&data19=" +String(data19)+"&data20=" +String(data20); Serial.print("Requesting URL: "); Serial.println(url); // This will send the request to the server client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); unsigned long timeout = millis(); while (client.available() == 0) { if (millis() - timeout > 5000) { Serial.println(">>> Client Timeout !"); client.stop(); return; } } // Read all the lines of the reply from server and print them to Serial while(client.available()) { String line = client.readStringUntil('\r'); //Serial.print(line); } Serial.println(); Serial.println("closing connection"); delay(1000); } //// read control ==== void read_master_status_FromServer() { Serial.print("Reading control command from Server ... connecting to "); Serial.println(host); WiFiClient client; delay(1500); if (client.connect(host, 80)) { Serial.println("reconnecting..."); url = "/api/getRealyStatus/"+String(dID)+"/"+String(code)+"/"+"abZYrshRYR243askdSKSKSK5646dkfmTURDsand"; Serial.print("Requesting URL: "); //Serial.println(url); // comment to prevent hacker client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); delay(2000); String section="header"; while(client.available()) { //Serial.println("Connection available "); String line = client.readStringUntil('\r'); //Serial.print(line); // we’ll parse the HTML body here if (section=="header") { // headers.. if (line=="\n") { // skips the empty space at the beginning section="json"; } } else if (section=="json") { // print the good stuff section="ignore"; String result = line.substring(1); // Parse JSON int size = result.length() + 1; char json[size]; result.toCharArray(json, size); StaticJsonDocument <2000> doc; DeserializationError error = deserializeJson(doc, json); if (error) return; Serial.println("parseObject() OK ..."); //Serial.println(result); // show all json found String master_state = doc["result"]["masterStatus"]; String led1 = doc["result"]["relayStatus"]["data6_H"]; String led2 = doc["result"]["relayStatus"]["data6_L"]; String led3 = doc["result"]["relayStatus"]["data3_status"]; String RR1 = doc["result"]["relayStatus"]["data1_status"]; String RR2 = doc["result"]["relayStatus"]["data2_status"]; String RR3 = doc["result"]["relayStatus"]["data3_status"]; String RR4 = doc["result"]["relayStatus"]["data4_status"]; String RR5 = doc["result"]["relayStatus"]["data5_status"]; String RR6 = doc["result"]["relayStatus"]["data6_status"]; // Timer relay 6 in platform String RR7 = doc["result"]["relayStatus"]["data7_status"]; // Timer relay 7 in platform String RR8 = doc["result"]["relayStatus"]["data8_status"]; // Timer relay 8 in platform R1 = RR1; R2 = RR2; R3 = RR3; R4 = RR4; R5 = RR5; R6 = RR6; R7 = RR7; R8 = RR8; Serial.println("Relay : "+String(R1)+" "+String(R2)+" "+String(R3)+" "+String(R4)+" "+String(R5)+" "+String(R6)+" "+String(R7)+" "+String(R8)); Serial.print("Relay 1 := ");Serial.println(led1); Serial.print("Master state = ");Serial.println(master_state); Serial.print("R8 for Relay : ");Serial.println(R8); Serial.print("Pump Start : ");Serial.println(led1); Serial.print("Pump Stop : ");Serial.println(led2); Serial.print("Led3 : ");Serial.println(led3); Serial.print("Master Status : ");Serial.println(master_state); Serial.print("Timer Relay R5 : ");Serial.println(R5); Serial.println("Relay : "+String(R1)+" "+String(R2)+" "+String(R3)+" "+String(R4)+" "+String(R5)+" "+String(R6)+" "+String(R7)+" "+String(R8)); delay(1000); } // if found json } // end while client available } // end if host connected else { // if you couldn't make a connection: Serial.println("connection failed read server 1"); } } void Restart_loop() { Serial.println(restart); if (restart == 0) { Serial.println("Restart..."); ESP.restart(); // restart } restart--; // ลดค่าตัวแปร restart delay(1000); } ////////////////////////