Site icon Renzo Mischianti

LORA E220 error using U8g2Lib

Hello Mr. Renzo, please help me. My name is Hansaran, and I have interest in electronics. I often read your articles about LoRa. I'm a beginner with LoRa and ESP32 modules, because usually I use Arduino for my projects. I want to create a LoRa communication device to monitor someone. The monitoring data will be sent to a gateway using the LoRa E220 module. I have never used LoRa before. I'm making this device to monitor my father, who is a sulfur miner. The mining is done manually, and there is no communication network available, which makes me worry about his condition. I hope this device can be completed and can be useful for my father. Sorry for the long explanation. When I try to integrate the "sendfixedtransmission" example into my existing program, I have an error like this.
Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
Core  1 register dump:
PC      : 0x400d632b  PS      : 0x00060a30  A0      : 0x800d6413  A1      : 0x3ffc5b70  
A2      : 0x3ffc2044  A3      : 0x000000c1  A4      : 0x00000000  A5      : 0x00000008  
A6      : 0x3ffb7c80  A7      : 0x00000000  A8      : 0x800d6304  A9      : 0x3ffc5b60  
A10     : 0x00000000  A11     : 0x000003e8  A12     : 0x00000064  A13     : 0x00000067  
A14     : 0x00060a20  A15     : 0x00000001  SAR     : 0x0000000a  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000000  LBEG    : 0x4008474d  LEND    : 0x40084755  LCOUNT  : 0x00000027  

Backtrace: 0x400d6328:0x3ffc5b70 0x400d6410:0x3ffc5ba0 0x400d2ba6:0x3ffc5be0 0x400d8b0a:0x3ffc5c40
ELF file SHA256: 31e1dff54c44b265
Rebooting...
I've read articles about this error but still don't understand it. https://stackoverflow.com/questions/63399901/how-to-fix-guru-meditation-error-core-1-paniced-loadprohibited-error It turned out that the buffer(declared globally) that I used for receiving messages was appending new messages instead of overwriting the pre-existing contents in it. So, an illegal memory-write attempt was made and the device panicked and rebooted. I've tried sending the fixed transmission example before and it worked. Please, I need your help. Here is my code:
#include "U8g2lib.h"
#include <Wire.h>
#include "RTClib.h"
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include "Arduino.h"
#include "LoRa_E220.h"

U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0); // //OLED SSD1306 128x64
LoRa_E220 e220ttl(&Serial2, 15, 33, 32); //  RX AUX M0 M1

// 'icon_sync', 16x16px
const unsigned char bitmap_icon_sinkronisasi [] PROGMEM = {
  0x00, 0x00, 0xe0, 0x01, 0x10, 0x38, 0x88, 0x18, 0x84, 0x28, 0x82, 0x20, 0x82, 0x40, 0x82, 0x40,
  0xc2, 0x4f, 0xc2, 0x40, 0x02, 0x40, 0x0a, 0x20, 0x0c, 0x10, 0x0e, 0x08, 0xc0, 0x07, 0x00, 0x00,
};
// 'icon_gps1', 16x16px
const unsigned char bitmap_icon_node_sensor_1 [] PROGMEM = {
  0x00, 0x00, 0xc0, 0x0f, 0x00, 0x10, 0x80, 0x27, 0x00, 0x48, 0x00, 0x53, 0x60, 0x54, 0xe0, 0x54,
  0xe0, 0x51, 0xe0, 0x03, 0xe0, 0x03, 0x50, 0x40, 0xf8, 0x60, 0x04, 0x41, 0xfe, 0x43, 0x02, 0xe2,
};
// 'icon_gps2', 16x16px
const unsigned char bitmap_icon_node_sensor_2 [] PROGMEM = {
  0x00, 0x00, 0xc0, 0x0f, 0x00, 0x10, 0x80, 0x27, 0x00, 0x48, 0x00, 0x53, 0x60, 0x54, 0xe0, 0x54,
  0xe0, 0x51, 0xe0, 0x03, 0xe0, 0x03, 0x50, 0x60, 0xf8, 0x90, 0x04, 0x41, 0xfe, 0x23, 0x02, 0xf2,
};
// 'icon kembang api', 16x16px
const unsigned char bitmap_icon_sleep [] PROGMEM = {
  0x00, 0x00, 0x00, 0x10, 0x00, 0x29, 0x08, 0x10, 0x08, 0x00, 0x36, 0x00,
  0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 0x00, 0x63, 0x00, 0x00, 0x00, 0x08,
  0x20, 0x08, 0x50, 0x00, 0x20, 0x00, 0x00, 0x00,
};
// 'icon jam', 16x16px
const unsigned char bitmap_icon_set_waktu [] PROGMEM = {
  0xe0, 0x07, 0x18, 0x18, 0x84, 0x24, 0x8a, 0x40, 0x82, 0x50, 0x81, 0x80, 0xc1, 0x81, 0x45, 0xae,
  0x41, 0x82, 0x81, 0x81, 0x05, 0xa0, 0x02, 0x40, 0x12, 0x48, 0x04, 0x21, 0x18, 0x18, 0xe0, 0x07,
};
// 'icon_knob_over_oled', 16x16px

// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 384)
const unsigned char* bitmap_icons[5] = {
  bitmap_icon_sinkronisasi,
  bitmap_icon_node_sensor_1,
  bitmap_icon_node_sensor_2,
  bitmap_icon_sleep,
  bitmap_icon_set_waktu,
};

// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 8320)
// 'scrollbar_background', 8x64px
const unsigned char bitmap_scrollbar_background [] PROGMEM = {
  0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
  0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
  0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
  0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
  0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40,
  0x00, 0x40, 0x00, 0x00,
};

// 'item_sel_outline', 128x21px
const unsigned char bitmap_item_sel_outline [] PROGMEM = {
  0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  0xFF, 0xFF, 0xFF, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x02, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x0C, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x02, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x0C, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x02, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x0C, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x02, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x0C, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x02, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x0C, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x02, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x0C, 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0xF8, 0xFF, 0xFF, 0xFF,
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x03,
};

// ------------------ end generated bitmaps from image2cpp ---------------------------------

const int NUM_ITEMS = 5; // number of items in the list and also the number of screenshots and screenshots with QR codes (other screens)
const int MAX_ITEM_LENGTH = 20; // maximum characters for the item name

char menu_items [NUM_ITEMS] [MAX_ITEM_LENGTH] = {  // array with item names
  { "Sinkronisasi" },
  { "Node Sensor 1" },
  { "Node Sensor 2" },
  { "Sleep" },
  { "Set. Waktu" },
};
// note - when changing the order of items above, make sure the other arrays referencing bitmaps
// also have the same order, for example array "bitmap_icons" for icons, and other arrays for screenshots and QR codes
// TOMBOL MENU
#define BUTTON_UP_PIN 27 // pin for UP button 
#define BUTTON_SELECT_PIN 25 // pin for SELECT button
#define BUTTON_DOWN_PIN 26 // pin for DOWN button
int button_up_clicked = 0; // only perform action when button is clicked, and wait until another press
int button_select_clicked = 0; // same as above
int button_down_clicked = 0; // same as above
int item_selected = 0; // which item in the menu is selected
int item_sel_previous; // previous item - used in the menu screen to draw the item before the selected one
int item_sel_next; // next item - used in the menu screen to draw next item after the selected one
int current_screen = 0;   // 0 = menu, 1 = screenshot, 2 = qr

//ATUR WAKTU
int tombol_up = 0; // only perform action when button is clicked, and wait until another press
int tombol_ok = 0; // same as above
int tombol_down = 0; // same as above
int item_terpilih = 0; // which item in the menu is selected
int aksi_sekarang = 0;   // 0 = menu, 1 = screenshot, 2 = qr
int tombol_ok_sinkron = 0;
int kurang = 0;
int dino;

// RTC DS1307
RTC_DS1307 rtc;
//uint8_t set_tahun, set_bulan, set_tanggal, set_jam, set_menit, set_detik;
uint8_t  bulan, tanggal, jam, menit, detik;
uint16_t tahun;
String hari, jam_up, menit_up, detik_up, sync_time;
unsigned long delay_rtc;
String timstep;
char daysOfTheWeek[7][12] = {
  "Sunday",
  "Monday",
  "Tuesday",
  "Wednesday",
  "Thursday",
  "Friday",
  "Saturday"
};

// SD Card
unsigned long delaysd;
unsigned long delaytampil;
String simpan_waktu, simpan_node1, simpan_node2;
String hrate1, suhu1, lon1, lat1, alt1, hrate2, suhu2, lon2, lat2, alt2;

// LORA
#define NODESENSOR1 2 //NODE SENSOR 1
#define NODESENSOR2 4 //NODE SENSOR 1
#define ENABLE_RSSI true
String pesan_n1, pesan_n2, pesan;

//int detik;
//----------------------------------- File SD -----------------------------------------
//SDCARD READ FILES
void readFile(fs::FS &fs, const char * path) {
  //Serial.printf("Reading file: %s\n", path);

  File file = fs.open(path);
  if (!file) {
    //Serial.println("Failed to open file for reading");
    return;
  }

  Serial.println("Read from file: ");
  while (file.available()) {
    Serial.write(file.read());
  }
  file.close();
}
//SDCARD WRITE FILES
void writeFile(fs::FS &fs, const char * path, const char * message) {
  File file = fs.open(path, FILE_WRITE);
  if (!file) {
    return;
  }
  if (file.print(message)) {
  } else {
  }
  file.close();
}
//SDCARD APPEND
void appendFile(fs::FS &fs, const char * path, const char * message) {
  File file = fs.open(path, FILE_APPEND);
  if (!file) {
    return;
  }
  if (file.print(message)) {
  } else {
  }
  file.close();
}
//SDCARD RENAME FILES
void renameFile(fs::FS &fs, const char * path1, const char * path2) {
  if (fs.rename(path1, path2)) {
  } else {
  }
}
//SDCARD DELETE FILES
void deleteFile(fs::FS &fs, const char * path) {
  if (fs.remove(path)) {
  } else {
  }
}
// ------------------------------------ Menu 1--------------------------------------------

// -----------------------------------------------------------------------------------------------------------
void sinkronisasi(void) {
  //u8g2.setFont(u8g2_font_4x6_tf);
  //u8g2.setFont(u8g2_font_u8glib_4_tf);
  //u8g2.setFont(u8g2_font_6x10_mf);
  //u8g2.drawStr(20, 32 + 11 + 15, timstep.c_str());

  u8g2.setFont(u8g2_font_artosserif8_8r);
  u8g2.drawStr(0, 20, "Pesan diterima :");
  u8g2.drawStr(0, 30, pesan.c_str());
  //u8g2.drawStr(0, 40, "Siap Untuk Sinkronisasi");
}
void node1(void) {
  u8g2.setFont(u8g2_font_amstrad_cpc_extended_8f);/////////////Font Bagus
  u8g2.drawStr(0, 20, "Long : 1234567");
  //u8g2.setFont(u8g2_font_u8glib_4_tf);
  u8g2.drawStr(0, 29, "Lati : 1234567");
  //u8g2.setFont(u8g2_font_6x10_mf);
  u8g2.drawStr(0, 38, "Alti : 123 mdpl");
  //u8g2.setFont(u8g2_font_6x10_mf);
  u8g2.drawStr(0, 47, "Wind : 123 km/j");
}
void node2(void) {
  u8g2.setFont(u8g2_font_artosserif8_8r);
  u8g2.drawStr(0, 20, "Long : 1234567");
  //u8g2.setFont(u8g2_font_u8glib_4_tf);
  u8g2.drawStr(0, 29, "Lati : 1234567");
  //u8g2.setFont(u8g2_font_6x10_mf);
  u8g2.drawStr(0, 38, "Alti : 123 mdpl");
  //u8g2.setFont(u8g2_font_6x10_mf);
  u8g2.drawStr(0, 47, "Wind : 123 km/j");
}
void turu(void) {
  u8g2.setFont(u8g2_font_6x10_mf);
  u8g2.drawStr(0, 20, "Tekan Lagi");
  u8g2.drawStr(0, 30, "Untuk Mode ");
  u8g2.drawStr(0, 40, "Tidur ");
}
void setwaktu(void) {
  String dis_waktu = String(jam_up) + ":" + String(menit_up) + ":" + String(detik_up);
  String dis_tanggal = String(tanggal) + "/" + String(bulan) + "/" + String(tahun);
  //String dis_timstep = String(timstep);

  u8g2.setFont(u8g2_font_logisoso20_tf);
  u8g2.drawStr(15, 21, dis_waktu.c_str());
  u8g2.setFont(u8g2_font_amstrad_cpc_extended_8f);
  u8g2.drawStr(25, 32, hari.c_str());
  u8g2.drawStr(25, 32 + 11, dis_tanggal.c_str());
  u8g2.drawStr(20, 32 + 11 + 15, timstep.c_str());
}
void gelap(void) {
  //u8g2.setFont(u8g2_font_artosserif8_8r);
  //u8g2.drawStr(0, 18, "Long : 1234567");
}

//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| SET WAKTU |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

void set_waktu() {
  DateTime now = rtc.now();

  if ((digitalRead(BUTTON_DOWN_PIN) == LOW) && (tombol_ok_sinkron == 0)) { // select button clicked, jump between screens
    tombol_ok_sinkron = 1; // set button to clicked to only perform the action once
    if (aksi_sekarang == 0) {
      aksi_sekarang = 1;
    }
    else if (aksi_sekarang == 1) {
      aksi_sekarang = 2;
    }
    else if (aksi_sekarang == 2) {
      aksi_sekarang = 3;
    }
    else if (aksi_sekarang == 3) {
      aksi_sekarang = 4;
    }
    else if (aksi_sekarang == 4) {
      aksi_sekarang = 5;
    }
    else if (aksi_sekarang == 5) {
      aksi_sekarang = 6;
    }
    else if (aksi_sekarang == 6) {
      aksi_sekarang = 7;
    }
    else {
      aksi_sekarang = 0;
    }
  }
  if ((digitalRead(BUTTON_DOWN_PIN) == HIGH) && (tombol_ok_sinkron == 1)) { // unclick
    tombol_ok_sinkron = 0;
  }
  //u8g2.clearBuffer();  // clear buffer for storing display content in RAM
  if (aksi_sekarang == 0) { // MENU SCREEN
    tahun = now.year();
    bulan = now.month();
    tanggal = now.day();
    jam = now.hour();
    menit = now.minute();
    detik = now.second();
    timstep = now.timestamp();
    hari = String(daysOfTheWeek[now.dayOfTheWeek()]);
    jam_up = String(jam);
    menit_up = String(menit);
    detik_up = String(detik);
    if (jam < 10) {
      jam_up = "0" + String(jam);
    }
    if (menit < 10) {
      menit_up = "0" + String(menit);
    }
    if (detik < 10) {
      detik_up = "0" + String(detik);
    }
    setwaktu();
  }
  //GANTI TANGGAL
  else if (aksi_sekarang == 1) {
    if ((digitalRead(BUTTON_UP_PIN) == LOW) && (tombol_up == 0)) { // up button clicked - jump to previous menu item
      tombol_up = 1; // set button to clicked to only perform the action once
      tanggal++;
      if (tanggal > 31) {
        tanggal = 1;
      }
    }
    if ((digitalRead(BUTTON_UP_PIN) == HIGH) && (tombol_up == 1)) { // unclick
      tombol_up = 0;
    }
    setwaktu();
  }

  //GANTI BULAN
  else if (aksi_sekarang == 2) {
    if ((digitalRead(BUTTON_UP_PIN) == LOW) && (tombol_up == 0)) { // up button clicked - jump to previous menu item
      tombol_up = 1; // set button to clicked to only perform the action once
      bulan ++;
      if (bulan > 12) {
        bulan = 1;
      }
    }
    if ((digitalRead(BUTTON_UP_PIN) == HIGH) && (tombol_up == 1)) { // unclick
      tombol_up = 0;
    }
    setwaktu();
  }

  //GANTI TAHUN
  else if (aksi_sekarang == 3) {
    if ((digitalRead(BUTTON_UP_PIN) == LOW) && (tombol_up == 0)) { // up button clicked - jump to previous menu item
      tombol_up = 1; // set button to clicked to only perform the action once
      tahun ++;
      if (tahun > 2030) {
        tahun = 2020;
      }
    }
    if ((digitalRead(BUTTON_UP_PIN) == HIGH) && (tombol_up == 1)) { // unclick
      tombol_up = 0;
    }
    setwaktu();
  }

  //GANTI JAM
  else if (aksi_sekarang == 4) {
    if ((digitalRead(BUTTON_UP_PIN) == LOW) && (tombol_up == 0)) { // up button clicked - jump to previous menu item
      tombol_up = 1; // set button to clicked to only perform the action once
      jam ++;
      if (jam > 23) {
        jam = 1;
      }
      jam_up = String(jam);
    }

    if ((digitalRead(BUTTON_UP_PIN) == HIGH) && (tombol_up == 1)) { // unclick
      tombol_up = 0;
    }
    setwaktu();
  }
  //GANTI MENIT
  else if (aksi_sekarang == 5) {
    if ((digitalRead(BUTTON_UP_PIN) == LOW) && (tombol_up == 0)) { // up button clicked - jump to previous menu item
      tombol_up = 1; // set button to clicked to only perform the action once
      menit ++;
      if (menit > 59) {
        menit = 1;
      }
      menit_up = String(menit);
    }

    if ((digitalRead(BUTTON_UP_PIN) == HIGH) && (tombol_up == 1)) { // unclick
      tombol_up = 0;
    }
    setwaktu();
  }
  //GANTI DETIK
  else if (aksi_sekarang == 6) {
    if ((digitalRead(BUTTON_UP_PIN) == LOW) && (tombol_up == 0)) { // up button clicked - jump to previous menu item
      tombol_up = 1; // set button to clicked to only perform the action once
      detik ++;
      if (detik > 59) {
        detik = 1;
      }
      detik_up = String(detik);
    }

    if ((digitalRead(BUTTON_UP_PIN) == HIGH) && (tombol_up == 1)) { // unclick
      tombol_up = 0;
    }
    setwaktu();
  }

  else if (aksi_sekarang == 7) {
    //rtc.setTime();  // SS,MM,HH,Date,Month,Year
    rtc.adjust(DateTime(tahun, bulan, tanggal, jam, menit, detik));
    setwaktu();
    aksi_sekarang = 0;
  }
  //setwaktu();
}

//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| END SET WAKTU ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void printParameters(struct Configuration configuration);
void printModuleInformation(struct ModuleInformation moduleInformation);
// -----------------------------------------------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  //LORA
  ResponseStructContainer c;
  c = e220ttl.getConfiguration();
  // It's important get configuration pointer before all other operation
  Configuration configuration = *(Configuration*) c.data;
  Serial.println(c.status.getResponseDescription());
  Serial.println(c.status.code);
  printParameters(configuration);
  ////////////////////////////////////////////////////////////////////////////////////////
  //----------------------- FIXED RECEIVER RSSI -----------------------
  configuration.ADDL = 0x03;
  configuration.ADDH = 0x00;
  configuration.CHAN = 23;

  configuration.SPED.uartBaudRate = UART_BPS_9600;
  configuration.SPED.airDataRate = AIR_DATA_RATE_010_24;
  configuration.SPED.uartParity = MODE_00_8N1;

  configuration.OPTION.subPacketSetting = SPS_200_00;
  configuration.OPTION.RSSIAmbientNoise = RSSI_AMBIENT_NOISE_DISABLED;
  configuration.OPTION.transmissionPower = POWER_22;

  configuration.TRANSMISSION_MODE.enableRSSI = RSSI_ENABLED;
  configuration.TRANSMISSION_MODE.fixedTransmission = FT_FIXED_TRANSMISSION;
  configuration.TRANSMISSION_MODE.enableLBT = LBT_DISABLED;
  configuration.TRANSMISSION_MODE.WORPeriod = WOR_2000_011;

  ResponseStatus rs = e220ttl.setConfiguration(configuration, WRITE_CFG_PWR_DWN_SAVE);
  //Serial.println(rs.getResponseDescription());
 // Serial.println(rs.code);
  c.close();
  ////////////////////////////////////////////////////////////////////////////////////////

  u8g2.setColorIndex(1);  // set the color to white
  u8g2.begin();
  u8g2.setBitmapMode(1);

  //RTC
  rtc.begin();

  // define pins for buttons
  // INPUT_PULLUP means the button is HIGH when not pressed, and LOW when pressed
  // since it´s connected between some pin and GND
  pinMode(BUTTON_UP_PIN, INPUT_PULLUP); // up button
  pinMode(BUTTON_SELECT_PIN, INPUT_PULLUP); // select button
  pinMode(BUTTON_DOWN_PIN, INPUT_PULLUP); // down button

  // MicroSD Card
  if (!SD.begin()) {
    Serial.println("Card Mount Failed");
    return;
  }
  //deleteFile(SD, "/datalog.txt");
  File file = SD.open("/databasis.txt");
  if (!file) {
    Serial.println("File doesn't exist");
    Serial.println("Creating file...");
    writeFile(SD, "/databasis.txt", "Apasih gajelas banget \r\n");
  }
  else {
    Serial.println("File already exists");
  }
  appendFile(SD, "/databasis.txt", "| Tanggal | Hrate | Suhu | Latitude | Longitude | Altitude|\r\n");
  appendFile(SD, "/databasis.txt", "===========================================================\r\n");
  readFile(SD, "/databasis.txt");
  file.close();

  delay_rtc = millis();
  delaysd = millis();
}

void loop() {

  // when pin 13 is LOW (DEMO_PIN), enable demo mode
  // this could be done either by using a switch
  // or simply by connecting the wire between pin 13 and GND
  // (those pins are next to each other)
  if (current_screen == 0) { // MENU SCREEN

    // up and down buttons only work for the menu screen
    if ((digitalRead(BUTTON_UP_PIN) == LOW) && (button_up_clicked == 0)) { // up button clicked - jump to previous menu item
      item_selected = item_selected - 1; // select previous item
      button_up_clicked = 1; // set button to clicked to only perform the action once
      if (item_selected < 0) { // if first item was selected, jump to last item
        item_selected = NUM_ITEMS - 1;
      }
    }
    else if ((digitalRead(BUTTON_DOWN_PIN) == LOW) && (button_down_clicked == 0)) { // down button clicked - jump to next menu item
      item_selected = item_selected + 1; // select next item
      button_down_clicked = 1; // set button to clicked to only perform the action once
      if (item_selected >= NUM_ITEMS) { // last item was selected, jump to first menu item
        item_selected = 0;
      }
    }

    if ((digitalRead(BUTTON_UP_PIN) == HIGH) && (button_up_clicked == 1)) { // unclick
      button_up_clicked = 0;
    }
    if ((digitalRead(BUTTON_DOWN_PIN) == HIGH) && (button_down_clicked == 1)) { // unclick
      button_down_clicked = 0;
    }

  }

  if ((digitalRead(BUTTON_SELECT_PIN) == LOW) && (button_select_clicked == 0)) { // select button clicked, jump between screens
    button_select_clicked = 1; // set button to clicked to only perform the action once
    if (current_screen == 0) {
      current_screen = 1; // menu items screen --> screenshots screen
    }
    else if (current_screen == 1) {
      current_screen = 2; // screenshots screen --> qr codes screen
    }
    else {
      current_screen = 0; // qr codes screen --> menu items screen
    }
  }
  if ((digitalRead(BUTTON_SELECT_PIN) == HIGH) && (button_select_clicked == 1)) { // unclick
    button_select_clicked = 0;
  }

  // set correct values for the previous and next items
  item_sel_previous = item_selected - 1;
  if (item_sel_previous < 0) {
    item_sel_previous = NUM_ITEMS - 1; // previous item would be below first = make it the last
  }
  item_sel_next = item_selected + 1;
  if (item_sel_next >= NUM_ITEMS) {
    item_sel_next = 0; // next item would be after last = make it the first
  }

  u8g2.clearBuffer();  // clear buffer for storing display content in RAM

  if (current_screen == 0) { // MENU SCREEN

    // selected item background
    u8g2.drawXBMP(0, 22, 128, 21, bitmap_item_sel_outline);

    // draw previous item as icon + label
    u8g2.setFont(u8g_font_7x14);
    u8g2.drawStr(25, 15, menu_items[item_sel_previous]);
    u8g2.drawXBMP( 4, 2, 16, 16, bitmap_icons[item_sel_previous]);

    // draw selected item as icon + label in bold font
    u8g2.setFont(u8g_font_7x14B);
    u8g2.drawStr(25, 15 + 20 + 2, menu_items[item_selected]);
    u8g2.drawXBMP( 4, 24, 16, 16, bitmap_icons[item_selected]);

    // draw next item as icon + label
    u8g2.setFont(u8g_font_7x14);
    u8g2.drawStr(25, 15 + 20 + 20 + 2 + 2, menu_items[item_sel_next]);
    u8g2.drawXBMP( 4, 46, 16, 16, bitmap_icons[item_sel_next]);

    // draw scrollbar background
    u8g2.drawXBMP(128 - 8, 0, 8, 64, bitmap_scrollbar_background);

    // draw scrollbar handle
    u8g2.drawBox(125, 64 / NUM_ITEMS * item_selected, 3, 64 / NUM_ITEMS);

    // draw upir logo
    //u8g2.drawXBMP(128-16-4, 64-4, 16, 4, upir_logo);

  }
  else if (current_screen == 1) { // SCREENSHOTS SCREEN
    if (item_selected == 0) {
      sinkronisasi();
    }
    else if (item_selected == 1) {
      node1();
    }
    else if (item_selected == 2) {
      node2();
    }
    else if (item_selected == 3) {
      turu();
    }
    else if (item_selected == 4) {
      set_waktu();
    }
  }
  else if (current_screen == 2) { // QR SCREEN
    if (item_selected != 3) {
      current_screen = 0;
    }
    else {
      //do nothing
    }
  }
  // Baca Pesan
  baca_pesan();

  // Kirim pesan Serial Monitor
  kirim_pesan();

  //Simpan setiap 10 detik
  if (millis() - delaysd > 10000) {
    //appendFile(SD, "/databasis.txt", simpan.c_str());
    simpan_data();
    detik = detik + 10;
    delaysd = millis();
  }

  u8g2.sendBuffer();
}

void kirim_pesan() {
  if (Serial.available()) {
    String input = Serial.readString();
    ResponseStatus rs = e220ttl.sendFixedMessage(0, NODESENSOR1, 23, input);
    // Check If there is some problem of succesfully send
   // Serial.println(rs.getResponseDescription());
  }
}

void baca_pesan() {
 if (e220ttl.available() > 1) {
    // read the String message
#ifdef ENABLE_RSSI
    ResponseContainer rc = e220ttl.receiveMessageRSSI();
#else
    ResponseContainer rc = e220ttl.receiveMessage();
#endif
    // Is something goes wrong print error
    if (rc.status.code != 1) {
      Serial.println(rc.status.getResponseDescription());
    } else {
      Print the data received
      Serial.println(rc.status.getResponseDescription());
      Serial.println(rc.data);
#ifdef ENABLE_RSSI
      Serial.print("RSSI: "); Serial.println(rc.rssi, DEC);
#endif
    }
    pesan = String(rc.data);
  }
}

void simpan_data() {
  DateTime now = rtc.now();
  simpan_waktu = String(daysOfTheWeek[now.dayOfTheWeek()]) + ", " + String(now.hour()) + ":" + String(now.minute()) + "|" + String(now.second()) + "\r\n";
  simpan_node1 = String("Node 1") + "|" + hrate1 + "|" + suhu1 + "|" + lon1 + "|" + lat1 + "|" + alt1 + "\r\n";
  simpan_node2 = String("Node 2") + "|" + hrate2 + "|" + suhu2 + "|" + lon2 + "|" + lat2 + "|" + alt2 + "\r\n";
  appendFile(SD, "/databasis.txt", simpan_waktu.c_str());
  appendFile(SD, "/databasis.txt", simpan_node1.c_str());
  appendFile(SD, "/databasis.txt", simpan_node2.c_str());
}

void printParameters(struct Configuration configuration) {
  Serial.println("----------------------------------------");

  Serial.print(F("HEAD : "));  Serial.print(configuration.COMMAND, HEX);Serial.print(" ");Serial.print(configuration.STARTING_ADDRESS, HEX);Serial.print(" ");Serial.println(configuration.LENGHT, HEX);
  Serial.println(F(" "));
  Serial.print(F("AddH : "));  Serial.println(configuration.ADDH, HEX);
  Serial.print(F("AddL : "));  Serial.println(configuration.ADDL, HEX);
  Serial.println(F(" "));
  Serial.print(F("Chan : "));  Serial.print(configuration.CHAN, DEC); Serial.print(" -> "); Serial.println(configuration.getChannelDescription());
  Serial.println(F(" "));
  Serial.print(F("SpeedParityBit     : "));  Serial.print(configuration.SPED.uartParity, BIN);Serial.print(" -> "); Serial.println(configuration.SPED.getUARTParityDescription());
  Serial.print(F("SpeedUARTDatte     : "));  Serial.print(configuration.SPED.uartBaudRate, BIN);Serial.print(" -> "); Serial.println(configuration.SPED.getUARTBaudRateDescription());
  Serial.print(F("SpeedAirDataRate   : "));  Serial.print(configuration.SPED.airDataRate, BIN);Serial.print(" -> "); Serial.println(configuration.SPED.getAirDataRateDescription());
  Serial.println(F(" "));
  Serial.print(F("OptionSubPacketSett: "));  Serial.print(configuration.OPTION.subPacketSetting, BIN);Serial.print(" -> "); Serial.println(configuration.OPTION.getSubPacketSetting());
  Serial.print(F("OptionTranPower    : "));  Serial.print(configuration.OPTION.transmissionPower, BIN);Serial.print(" -> "); Serial.println(configuration.OPTION.getTransmissionPowerDescription());
  Serial.print(F("OptionRSSIAmbientNo: "));  Serial.print(configuration.OPTION.RSSIAmbientNoise, BIN);Serial.print(" -> "); Serial.println(configuration.OPTION.getRSSIAmbientNoiseEnable());
  Serial.println(F(" "));
  Serial.print(F("TransModeWORPeriod : "));  Serial.print(configuration.TRANSMISSION_MODE.WORPeriod, BIN);Serial.print(" -> "); Serial.println(configuration.TRANSMISSION_MODE.getWORPeriodByParamsDescription());
  Serial.print(F("TransModeEnableLBT : "));  Serial.print(configuration.TRANSMISSION_MODE.enableLBT, BIN);Serial.print(" -> "); Serial.println(configuration.TRANSMISSION_MODE.getLBTEnableByteDescription());
  Serial.print(F("TransModeEnableRSSI: "));  Serial.print(configuration.TRANSMISSION_MODE.enableRSSI, BIN);Serial.print(" -> "); Serial.println(configuration.TRANSMISSION_MODE.getRSSIEnableByteDescription());
  Serial.print(F("TransModeFixedTrans: "));  Serial.print(configuration.TRANSMISSION_MODE.fixedTransmission, BIN);Serial.print(" -> "); Serial.println(configuration.TRANSMISSION_MODE.getFixedTransmissionDescription());
  Serial.println("----------------------------------------");
}
void printModuleInformation(struct ModuleInformation moduleInformation) {
  Serial.println("----------------------------------------");
  Serial.print(F("HEAD: "));  Serial.print(moduleInformation.COMMAND, HEX);Serial.print(" ");Serial.print(moduleInformation.STARTING_ADDRESS, HEX);Serial.print(" ");Serial.println(moduleInformation.LENGHT, DEC);
  Serial.print(F("Model no.: "));  Serial.println(moduleInformation.model, HEX);
  Serial.print(F("Version  : "));  Serial.println(moduleInformation.version, HEX);
  Serial.print(F("Features : "));  Serial.println(moduleInformation.features, HEX);
  Serial.println("----------------------------------------");
}
I'm sorry if my code appears messy.
Exit mobile version