---
# System prepended metadata

title: Case Study 1 - Modul 3

---

# Case Study 1 - Modul 3

Kelompok:7

Anggota Kelompok:

1. Nugroho Ulil Abshar (2306229310)
2. Syahmi Hamdani (2306220532)
3. Muhammad Hilmi Al-Muttaqi Budiyanto (2306267082)
4. Wilman Saragih (2306161776)
5. Andhika Fadlan Wijanarko (2306267164)
6. Javana Muahmmad Dzaki (2306161826)
7. Muhammad Rifat Faqih (2306250762)
8. Salahuddin Zidane Alghifari (2206028200)
---

Kode yang dijalankan

```cpp
#include <SPI.h>
#include <LoRa.h>

const int csPin = 5;  
const int resetPin = 14; 
const int irqPin = 2;   

float frequency = 433.0E6;
uint8_t syncWord = 0xE0; 
int txPower = 17;        
int spreadingFactor = 7;  
long signalBandwidth = 125E3; 
int codingRate = 5;       
int preambleLength = 8;   
bool crcEnabled = true;   
bool invertIQSignals = false; 
bool IQEnabled = true;   
int Gain = 0;

void setup() {
  Serial.begin(115200);
  while (!Serial);
  initLoRa();
  displayMenu();
}

void loop() {
  if (Serial.available() > 0) {
    char choice = Serial.read();
    processMenuChoice(choice);
  }
} 

void initLoRa() {
  LoRa.setPins(csPin, resetPin, irqPin);

  if (!LoRa.begin(frequency)) {
    Serial.println("Inisialisasi LoRa gagal. Periksa koneksi.");
    while (true);
  }

  LoRa.setTxPower(txPower);
  LoRa.setSpreadingFactor(spreadingFactor);
  LoRa.setSignalBandwidth(signalBandwidth);
  LoRa.setCodingRate4(codingRate);
  LoRa.setPreambleLength(preambleLength);
  LoRa.enableCrc();
  LoRa.setSyncWord(syncWord);

  LoRa.receive();

  LoRa.onReceive(onReceiveCallback);

  Serial.println("Inisialisasi LoRa berhasil.");
}

void displayMenu() {
  Serial.println("\n--- Menu Komunikasi LoRa ---");
  Serial.println("a. Inisialisasi LoRa");
  Serial.println("b. Ubah Sync Word");
  Serial.println("c. Ubah Transmission Power");
  Serial.println("d. Ubah Spreading Factor");
  Serial.println("e. Ubah Signal Bandwidth");
  Serial.println("f. Ubah Coding Rate");
  Serial.println("g. Ubah Preamble Length");
  Serial.println("h. Enable/Disable CRC");
  Serial.println("i. Enable/Disable Invert IQ");
  Serial.println("j. Atur Gain");
  Serial.println("k. Atur Idle");
  Serial.println("l. Atur Sleep");
  Serial.println("m. Kirim Pesan");
  Serial.println("Pilih opsi (a-k): ");
}

void processMenuChoice(char choice) {
  switch(choice) {
    case 'a': 
      initLoRa(); 
      break;
    case 'b': 
      changeSyncWord(); 
      break;
    case 'c': 
      changeTxPower(); 
      break;
    case 'd': 
      changeSpreadingFactor(); 
      break;
    case 'e': 
      changeSignalBandwidth(); 
      break;
    case 'f': 
      changeCodingRate(); 
      break;
    case 'g': 
      changePreambleLength(); 
      break;
    case 'h': 
      toggleCRC(); 
      break;
    case 'i': 
      toggleIQ(); 
      break;
    case 'j': 
      setGain(); 
      break;
    case 'k': 
      setIdle(); 
      break;
    case 'l': 
      setSleep(); 
      break;
    case 'm': 
      sendMessage(); 
      break;
    case '\n':
    case '\r':
      break;
    default:
      Serial.println("Pilihan tidak valid. Silakan coba lagi.");
      displayMenu();
  }
}

// --- Helper Function for Input to reduce repetition ---
String getSerialInput(String prompt) {
  // 1. Clear buffer
  while (Serial.available()) Serial.read();
  
  // 2. Show Prompt
  Serial.println(prompt);
  
  // 3. Wait for input
  while (!Serial.available());
  
  // 4. Read and Trim
  String input = Serial.readStringUntil('\n');
  input.trim();
  return input;
}

void changeSyncWord() {
  String input = getSerialInput("Masukkan Sync Word (0-255):");
  if (input.length() > 0) {
    syncWord = input.toInt();
    LoRa.setSyncWord(syncWord);
    Serial.print("Sync Word diubah menjadi: 0x"); 
    Serial.println(syncWord, HEX);
  }
  LoRa.receive(); // Enable receive mode
  displayMenu();
}

void changeTxPower() {
  String input = getSerialInput("Masukkan Transmission Power (2-20 dBm):");
  if (input.length() > 0) {
    txPower = input.toInt();
    LoRa.setTxPower(txPower);
    Serial.print("Transmission Power diubah menjadi: "); 
    Serial.print(txPower);
    Serial.println(" dBm");
  }
  LoRa.receive(); // Enable receive mode
  displayMenu();
}

void changeSpreadingFactor() {
  String input = getSerialInput("Masukkan Spreading Factor (6-12):");
  if (input.length() > 0) {
    spreadingFactor = input.toInt();
    LoRa.setSpreadingFactor(spreadingFactor);
    Serial.print("Spreading Factor diubah menjadi: "); 
    Serial.println(spreadingFactor);
  }
  LoRa.receive(); // Enable receive mode
  displayMenu();
}

void changeSignalBandwidth() {
  String input = getSerialInput("Masukkan Signal Bandwidth (Hz):");
  if (input.length() > 0) {
    signalBandwidth = input.toFloat(); // Use toFloat for bandwidth
    LoRa.setSignalBandwidth(signalBandwidth);
    Serial.print("Signal Bandwidth diubah menjadi: "); 
    Serial.print(signalBandwidth);
    Serial.println(" Hz");
  }
  LoRa.receive(); // Enable receive mode
  displayMenu();
}

void changeCodingRate() {
  String input = getSerialInput("Masukkan Coding Rate (5-8):");
  if (input.length() > 0) {
    codingRate = input.toInt();
    LoRa.setCodingRate4(codingRate);
    Serial.print("Coding Rate diubah menjadi: 4/"); 
    Serial.println(codingRate);
  }
  LoRa.receive(); // Enable receive mode
  displayMenu();
}

void changePreambleLength() {
  String input = getSerialInput("Masukkan Preamble Length (6-65535):");
  if (input.length() > 0) {
    preambleLength = input.toInt();
    LoRa.setPreambleLength(preambleLength);
    Serial.print("Preamble Length diubah menjadi: "); 
    Serial.println(preambleLength);
  }
  LoRa.receive(); // Enable receive mode
  displayMenu();
}

void toggleCRC() {
  crcEnabled = !crcEnabled;
  if (crcEnabled) {
    LoRa.enableCrc();
    Serial.println("CRC diaktifkan");
  } else {
    // Note: Some libraries do not support disabling CRC explicitly after init
    LoRa.disableCrc(); 
    Serial.println("CRC dinonaktifkan");
  }
  LoRa.receive(); // Enable receive mode
  displayMenu();
}

void toggleIQ() {
  IQEnabled = !IQEnabled;
  if (IQEnabled) {
    LoRa.enableInvertIQ();
    Serial.println("Invert IQ diaktifkan");
  } else {
    LoRa.disableInvertIQ();
    Serial.println("Invert IQ dinonaktifkan");
  }
  LoRa.receive(); // Enable receive mode
  displayMenu();
}

void setGain() {
  String input = getSerialInput("Masukkan Gain (0 - 6):");
  if (input.length() > 0) {
    Gain = input.toInt();
    // LoRa.setGain(Gain); // UNCOMMENT IF YOUR LIBRARY SUPPORTS THIS
    // Standard arduino-LoRa does not have setGain (it uses AGC). 
    // If compiling fails, keep this commented out.
    
    Serial.print("Gain diatur ke (Virtual): "); 
    Serial.println(Gain);
  }
  LoRa.receive(); // Enable receive mode
  displayMenu();
}

void setIdle() {
  LoRa.idle();
  Serial.println("Radio diatur ke mode Idle (Receive berhenti)");
  displayMenu();
}

void setSleep() {
  LoRa.sleep();
  Serial.println("Radio diatur ke mode Sleep (Receive berhenti)");
  displayMenu();
}

void sendMessage() {
  while (Serial.available()) {
    Serial.read();
  }

  Serial.println("Masukkan pesan yang akan dikirim:");
  
  while (!Serial.available());
  
  String message = Serial.readStringUntil('\n');
  message.trim(); // Removes leading/trailing whitespace or \r characters

  if (message.length() > 0) {
    LoRa.beginPacket();
    LoRa.print(message);
    LoRa.endPacket(); // Transmission ends here, radio goes to IDLE
    
    Serial.print("Mengirim pesan: ");
    Serial.println(message);
  } else {
    Serial.println("Pesan kosong, tidak dikirim.");
  }

  LoRa.receive(); 
  
  displayMenu();
}
void onReceiveCallback(int packetSize) {
  Serial.println(packetSize);
  if (packetSize == 0) return;

  String incoming = "";
  while (LoRa.available()) {
    incoming += (char)LoRa.read();
  }

  Serial.println("\n--- Paket Diterima ---");
  Serial.println("Payload: " + incoming);
  Serial.println("RSSI: " + String(LoRa.packetRssi()));
  Serial.println("SNR: " + String(LoRa.packetSnr()));
  Serial.println("-------------------");

  displayMenu();
}

```

Pastikan Sync Word pada transmitter dan receiver sama dan saling koordinasi dengan kelompok lain agar tidak saling mengganggu.

Dengan kelompok pasangan kalian, satunya menjadi tim A yang akan tetap di tempat, sedangkan yang satunya akan menjadi tim B yang akan bergerak. Pada setiap titik, lakukan 3 kali pengiriman data dengan TX Power (10, 14, 18 dBm). Catat RSSI dan SNR pada setiap penerimaan data.

### 1\. Titik pertama adalah jarak 1 meja, tim A tetap di tempat, tim B berjalan ke meja sebelah. (20 poin)

Dokumentasi:
![632158-min](https://hackmd.io/_uploads/BJ4XkoUbWg.jpg)



Serial Monitor
![Salah satu foto serial monitor](https://hackmd.io/_uploads/B1e7Ac8Wbe.png)


| TX Power | RSII | SNR | 
| :---- | :---- | :---- |
| 3.3 | -50 | 8.25 |
| 3.3 | -103 | -12 |

### 2\. Titik kedua adalah jarak setengah panjang ruangan, tim A tetap di tempat, tim B berjalan sampai setengah panjang ruangan. (20 poin)

![632168-min](https://hackmd.io/_uploads/BkPVZjLWWe.jpg)

![image](https://hackmd.io/_uploads/HySDZo8bZx.png)



| TX Power | RSII | SNR | 
| :---- | :---- | :---- |
| 3.3 | -80 | 9.25 |
| - | - | - |

### 3\. Titik ketiga adalah wall obstacle, tim B berjalan ke belakang tembok (lorong antar tempat praktikum dan bagian biomedis) (20 poin)

![632167-min](https://hackmd.io/_uploads/ByynxjIZbg.jpg)

![image](https://hackmd.io/_uploads/HkOoZiL-Zl.png)




| TX Power | RSII | SNR | 
| :---- | :---- | :---- |
| 3.3 | -77 | 9 |
| - | - | - |

### 4\. Titik keempat adalah tempat parkiran, tim A berjalan ke jendela lantai 7, tim B berjalan ke parkiran. Pastikan kedua tim dapat salimg melihat. (20 poin)

Parkiran dapat dilihat pada gambar berikut:

![345448-min](https://hackmd.io/_uploads/HkVmGs8Z-x.jpg)


![Salah satu foto serial monitor]()

| TX Power | RSII | SNR | 
| :---- | :---- | :---- | (20 poin)
| - | - | - |
| - | - | - |

### 5\. Berikan foto bukti menyerahkan antenna LoRA pada asisten. (20 poin)

![Foto penyerahan]()