Advertisement
mudhita_triari

ESP32 OTA...

May 5th, 2024 (edited)
680
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | Source Code | 1 0
  1. #include <Arduino.h>
  2. #include <otadrive_esp.h>
  3. #include <WiFi.h>
  4. #include <TensorFlowLite_ESP32.h>
  5.  
  6. const char* FW_VERSION = "1.0.0";
  7. const char* TOKEN = "TOKEN";
  8. const char* SSID_WIFI = "SSID";
  9. const char* KATA_SANDI_WIFI = "PASSWORD";
  10.  
  11. unsigned long previousMillis = 0;
  12. bool Terhubung_WiFi = false;
  13.  
  14. // Inisialisasi model TensorFlow Lite
  15. tflite::ErrorReporter pelaporKesalahan;
  16. const tflite::Model* model = tflite::GetModel(g_model);
  17. tflite::MicroInterpreter interpreter(model, pelaporKesalahan);
  18. TfLiteTensor* tensorInput = interpreter.input(0);
  19. TfLiteTensor* tensorOutput = interpreter.output(0);
  20.  
  21. // Inisialisasi OTA
  22. OTADRIVE otaDrive;
  23.  
  24. // Fungsi tangani kesalahan
  25. void penanganan_kesalahan(const char* pesan) {
  26.     Serial.print("Ups! Ada masalah: ");
  27.     Serial.println(pesan);
  28.     if (strcmp(pesan, "Gagal update") == 0) {
  29.         otaDrive.rollbackFirmware(); // Jika gagal update firmware, kembali ke versi sebelumnya
  30.     }
  31. }
  32.  
  33. void onProsesUpdate(int progress, int totalt) {
  34.     Serial.print("*");
  35. }
  36.  
  37. void menghubungkan_WiFi() {
  38.     WiFi.begin(SSID_WIFI, KATA_SANDI_WIFI);
  39.     Serial.print("Menghubungkan ke WiFi");
  40.     for (int i = 0; i < 20 && WiFi.status() != WL_CONNECTED; ++i) {
  41.         delay(500);
  42.         Serial.print(".");
  43.     }
  44.     if (WiFi.status() == WL_CONNECTED) {
  45.         Serial.println("\nWiFi terhubung!");
  46.         Terhubung_WiFi = true;
  47.     } else {
  48.         penanganan_kesalahan("Koneksi WiFi gagal");
  49.     }
  50. }
  51.  
  52. void setup() {
  53.     Serial.begin(115200);
  54.     pinMode(LED_BUILTIN, OUTPUT);
  55.  
  56.     // Inisialisasi OTA
  57.     otaDrive.setInfo(TOKEN, String("v@") + FW_VERSION);
  58.     otaDrive.onUpdateFirmwareProgress(onProsesUpdate);
  59.  
  60.     // Inisialisasi TensorFlow Lite interpreter
  61.     interpreter.AllocateTensors();
  62. }
  63.  
  64. void Jalankan_Sinkronisasi() {
  65.     if (!Terhubung_WiFi) {
  66.         menghubungkan_WiFi();
  67.         if (!Terhubung_WiFi) {
  68.             penanganan_kesalahan("Gagal sambung ke WiFi");
  69.             return;
  70.         }
  71.     }
  72.  
  73.     if (otaDrive.isUpdateAvailable()) {
  74.         Serial.println("Ada update OTA tersedia. Mulai update...");
  75.         if (otaDrive.updateFirmware()) {
  76.             Serial.println("Update selesai");
  77.         } else {
  78.             Serial.println("Update gagal");
  79.             penanganan_kesalahan("Update gagal");
  80.         }
  81.     } else {
  82.         Serial.println("Tidak ada update OTA yang tersedia");
  83.     }
  84. }
  85.  
  86. void loop() {
  87.     unsigned long waktu = millis();
  88.     if (waktu - previousMillis >= 2200) {
  89.         previousMillis = waktu;
  90.         digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  91.  
  92.         float dataInput[3] = {
  93.             WiFi.RSSI(),
  94.             otaDrive.getUpdateSize(),
  95.             waktu - previousMillis
  96.         };
  97.  
  98.         memcpy(tensorInput->data.f, dataInput, sizeof(dataInput));
  99.         interpreter.Invoke();
  100.  
  101.         float dataOutput[1];
  102.         memcpy(dataOutput, tensorOutput->data.f, sizeof(dataOutput));
  103.  
  104.         if (dataOutput[0] > 0.5) {
  105.             Jalankan_Sinkronisasi();
  106.         }
  107.     }
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement