Advertisement
mikroavr

blynk_watchdog

Aug 11th, 2023
1,243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define RXD2 26
  2. #define TXD2 27
  3.  
  4. #define PKEY 14
  5. #define RST 12
  6.  
  7. #define TINY_GSM_MODEM_SIM7600
  8. #define BLYNK_TEMPLATE_ID "TMPLE1aPZJfE"
  9. #define BLYNK_TEMPLATE_NAME "Solar_Tracker"
  10. #define BLYNK_AUTH_TOKEN "aoZ95-IDwV16oVP3N9TChD36QmmPWMRz"
  11.  
  12. #define BLYNK_PRINT Serial
  13. #define SerialMon Serial
  14. #define SerialAT Serial2
  15. #include <TinyGsmClient.h>
  16. #include <BlynkSimpleTinyGSM.h>
  17. TinyGsm modem(SerialAT);
  18.  
  19. unsigned long cur_time_res, old_time_res;
  20. unsigned long cur_time, old_time;
  21. bool hold = 0;
  22.  
  23. #define TINY_GSM_MODEM_SIM7600
  24. #if !defined(TINY_GSM_RX_BUFFER)
  25. #define TINY_GSM_RX_BUFFER 650
  26. #endif
  27. #define TINY_GSM_DEBUG SerialMon
  28. // set GSM PIN, if any
  29. #define GSM_PIN ""
  30.  
  31. // Your GPRS credentials, if any
  32. char apn[]  = "Internet";
  33. char user[] = "";
  34. char pass[] = "";
  35.  
  36. int counter = 0;
  37. int count_con = 0;
  38.  
  39. #define WATCHDOG_TIMEOUT_S 20 // restart jika tidak ada respon 5detik
  40. hw_timer_t * watchDogTimer = NULL;
  41.  
  42. void IRAM_ATTR watchDogInterrupt() {
  43.   Serial.println("reboot");
  44.   ESP.restart();
  45. }
  46.  
  47. void watchDogRefresh()
  48. {
  49.   timerWrite(watchDogTimer, 0);                    //reset timer (feed watchdog)
  50. }
  51.  
  52. void setup() {
  53.   // put your setup code here, to run once:
  54.  
  55.   delay(100);
  56.   Serial.begin(115200);
  57.   SerialAT.begin(115200, SERIAL_8N1, RXD2, TXD2);
  58.  
  59.   pinMode(RST, OUTPUT);
  60.   pinMode(PKEY, OUTPUT);
  61.  
  62.   digitalWrite(PKEY, LOW);
  63.   digitalWrite(RST, LOW);
  64.   delay(1000);
  65.   digitalWrite(PKEY, HIGH);
  66.   digitalWrite(RST, HIGH);
  67.   delay(1000);
  68.   digitalWrite(PKEY, LOW);
  69.   digitalWrite(RST, LOW);
  70.   delay(1000);
  71.  
  72.   SerialMon.println("Initializing modem...");
  73.   modem.restart();
  74.   String modemInfo = modem.getModemInfo();
  75.   Serial.print("Modem Info: ");
  76.   Serial.println(modemInfo);
  77.  
  78.   Blynk.begin(BLYNK_AUTH_TOKEN, modem, apn, user, pass);
  79.   delay(100);
  80.  
  81. }
  82.  
  83. void loop() {
  84.   // put your main code here, to run repeatedly:
  85.   Blynk.run();
  86.  
  87.   cur_time_res = millis();
  88.   if (cur_time_res - old_time_res >= 1000) {
  89.     watchDogRefresh();
  90.  
  91.     old_time_res = millis();
  92.   }
  93.  
  94.  
  95.  
  96.   cur_time = millis();
  97.   if (cur_time - old_time >= 4000) {
  98.     //baca_pzem();
  99.     Blynk.virtualWrite(V3, counter);
  100.     counter ++;
  101.     if (counter == 1500) {
  102.       counter = 0;
  103.     }
  104.     bool check_con = Blynk.connected();
  105.     Serial.print("connected: " );
  106.     Serial.println(check_con);
  107.     if (check_con == false) {
  108.       count_con++;
  109.       if (count_con == 20) {
  110.         Serial.print("disconnect: ");
  111.         Serial.println(count_con);
  112.         reset_sim();
  113.         wakeup_sim();
  114.       }
  115.     }
  116.     old_time = millis();
  117.   }
  118. }
  119.  
  120. void reset_sim() {
  121.   digitalWrite(RST, HIGH);
  122. }
  123.  
  124. void wakeup_sim() {
  125.   SerialMon.println("wakeup sim7600");
  126.   digitalWrite(PKEY, LOW);
  127.   digitalWrite(RST, LOW);
  128.   delay(1000);
  129.   digitalWrite(PKEY, HIGH);
  130.   digitalWrite(RST, HIGH);
  131.   delay(1000);
  132.   digitalWrite(PKEY, LOW);
  133.   digitalWrite(RST, LOW);
  134.   delay(1000);
  135.   wRespon(15000);
  136. }
  137.  
  138. void wRespon(long waktu) {
  139.   cur_time_res = millis();
  140.   old_time_res = cur_time_res;
  141.   while (cur_time_res - old_time_res < waktu ) {
  142.     cur_time_res = millis();
  143.     while (SerialAT.available() > 0) {
  144.       SerialMon.print(SerialAT.readString());
  145.     }
  146.   }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement