Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <otadrive_esp.h>
- #include <WiFi.h>
- #include <TensorFlowLite_ESP32.h>
- const char* FW_VERSION = "1.0.0";
- const char* TOKEN = "TOKEN";
- const char* SSID_WIFI = "SSID";
- const char* KATA_SANDI_WIFI = "PASSWORD";
- unsigned long previousMillis = 0;
- bool Terhubung_WiFi = false;
- // Inisialisasi model TensorFlow Lite
- tflite::ErrorReporter pelaporKesalahan;
- const tflite::Model* model = tflite::GetModel(g_model);
- tflite::MicroInterpreter interpreter(model, pelaporKesalahan);
- TfLiteTensor* tensorInput = interpreter.input(0);
- TfLiteTensor* tensorOutput = interpreter.output(0);
- // Inisialisasi OTA
- OTADRIVE otaDrive;
- // Fungsi tangani kesalahan
- void penanganan_kesalahan(const char* pesan) {
- Serial.print("Ups! Ada masalah: ");
- Serial.println(pesan);
- if (strcmp(pesan, "Gagal update") == 0) {
- otaDrive.rollbackFirmware(); // Jika gagal update firmware, kembali ke versi sebelumnya
- }
- }
- void onProsesUpdate(int progress, int totalt) {
- Serial.print("*");
- }
- void menghubungkan_WiFi() {
- WiFi.begin(SSID_WIFI, KATA_SANDI_WIFI);
- Serial.print("Menghubungkan ke WiFi");
- for (int i = 0; i < 20 && WiFi.status() != WL_CONNECTED; ++i) {
- delay(500);
- Serial.print(".");
- }
- if (WiFi.status() == WL_CONNECTED) {
- Serial.println("\nWiFi terhubung!");
- Terhubung_WiFi = true;
- } else {
- penanganan_kesalahan("Koneksi WiFi gagal");
- }
- }
- void setup() {
- Serial.begin(115200);
- pinMode(LED_BUILTIN, OUTPUT);
- // Inisialisasi OTA
- otaDrive.setInfo(TOKEN, String("v@") + FW_VERSION);
- otaDrive.onUpdateFirmwareProgress(onProsesUpdate);
- // Inisialisasi TensorFlow Lite interpreter
- interpreter.AllocateTensors();
- }
- void Jalankan_Sinkronisasi() {
- if (!Terhubung_WiFi) {
- menghubungkan_WiFi();
- if (!Terhubung_WiFi) {
- penanganan_kesalahan("Gagal sambung ke WiFi");
- return;
- }
- }
- if (otaDrive.isUpdateAvailable()) {
- Serial.println("Ada update OTA tersedia. Mulai update...");
- if (otaDrive.updateFirmware()) {
- Serial.println("Update selesai");
- } else {
- Serial.println("Update gagal");
- penanganan_kesalahan("Update gagal");
- }
- } else {
- Serial.println("Tidak ada update OTA yang tersedia");
- }
- }
- void loop() {
- unsigned long waktu = millis();
- if (waktu - previousMillis >= 2200) {
- previousMillis = waktu;
- digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
- float dataInput[3] = {
- WiFi.RSSI(),
- otaDrive.getUpdateSize(),
- waktu - previousMillis
- };
- memcpy(tensorInput->data.f, dataInput, sizeof(dataInput));
- interpreter.Invoke();
- float dataOutput[1];
- memcpy(dataOutput, tensorOutput->data.f, sizeof(dataOutput));
- if (dataOutput[0] > 0.5) {
- Jalankan_Sinkronisasi();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement