TKK_E32210490/Kode Arduino IDE/Receiver_Code.ino

180 lines
4.9 KiB
C++

#define DEBUG
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <ThingerESP32.h>
#include <HardwareSerial.h>
// Initialize I2C LCD display with address 0x27, 16 columns, and 2 rows
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Initialize HardwareSerial for LoRa E32 on UART2
HardwareSerial LoRaSerial(2); // Use UART2
// Define buzzer pin
const int buzzerPin = 12;
// Thinger.io credentials
#define USERNAME "akbarwijaya1701"
#define DEVICE_ID "TAD3"
#define DEVICE_CREDENTIAL "GN&bnQsadgJEKjPL"
// WiFi credentials
const char* ssid = "rodijayapap";
const char* pass = "ps4jember";
// Variable to store received data
String receivedData = "Waiting for data";
// Variables to store SNR, RSSI, and Delay
float SNR = 0.0;
int RSSI = 0;
float delayTime = 0.0; // Use float to store delay in seconds
// Thinger.io instance
ThingerESP32 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
void setup() {
// Initialize serial communication for debugging
Serial.begin(57600);
// Initialize LCD
lcd.init();
lcd.backlight();
// Set buzzer pin as output
pinMode(buzzerPin, OUTPUT);
// Initialize LoRa E32 communication on UART2
LoRaSerial.begin(9600, SERIAL_8N1, 16, 17); // Baud rate 9600, 8 data bits, no parity, 1 stop bit
// Connect to Wi-Fi
WiFi.begin(ssid, pass);
Serial.print("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("Connected to WiFi");
// Initialize Thinger.io
thing.add_wifi(ssid, pass);
// Define Thinger.io resources
thing["receivedData"] >> [](pson& out){
out = receivedData;
};
thing["buzzer"] << [](pson& in){
if (in.is_empty()) {
in = digitalRead(buzzerPin);
} else {
digitalWrite(buzzerPin, in ? HIGH : LOW);
}
};
// Define Thinger.io resources for SNR, RSSI, and Delay
thing["SNR"] >> [](pson& out){
out = SNR;
};
thing["RSSI"] >> [](pson& out){
out = RSSI;
};
thing["delayTime"] >> [](pson& out){
out = delayTime;
};
// Print initial message on LCD
lcd.setCursor(0, 0);
lcd.print("Waiting for data");
// Print initial message to Serial Monitor
Serial.println("Receiver is ready. Waiting for data...");
}
void loop() {
thing.handle();
if (LoRaSerial.available()) {
String data = LoRaSerial.readStringUntil('\n');
unsigned long receiveTime = millis(); // Capture the timestamp
// Print received data to Serial Monitor
Serial.print("Received: ");
Serial.println(data);
// Check if the data contains SNR, RSSI, and TIME
int snrIndex = data.indexOf("SNR:");
int rssiIndex = data.indexOf("RSSI:");
int timeIndex = data.indexOf("TIME:");
if (snrIndex != -1 && rssiIndex != -1 && timeIndex != -1) {
String snrString = data.substring(snrIndex + 4, data.indexOf(',', snrIndex));
String rssiString = data.substring(rssiIndex + 5, data.indexOf(',', rssiIndex));
String timeString = data.substring(timeIndex + 5);
// Print parsed values to Serial Monitor
Serial.print("SNR string: ");
Serial.println(snrString);
Serial.print("RSSI string: ");
Serial.println(rssiString);
Serial.print("Time string: ");
Serial.println(timeString);
SNR = snrString.toFloat();
RSSI = rssiString.toInt();
unsigned long sendTime = timeString.toInt();
delayTime = (receiveTime - sendTime) / 1000.0; // Convert delay to seconds
// Remove additional information from receivedData
receivedData = data.substring(0, snrIndex - 1);
// Display received data on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(receivedData.substring(0, 16)); // Display first 16 characters on the first row
if (receivedData.length() > 16) {
lcd.setCursor(0, 1);
lcd.print(receivedData.substring(16, 32)); // Display next 16 characters on the second row if available
}
// Print SNR, RSSI, and Delay to Serial Monitor
Serial.print("SNR: ");
Serial.println(SNR);
Serial.print("RSSI: ");
Serial.println(RSSI);
Serial.print("Delay: ");
Serial.println(delayTime);
} else {
receivedData = data;
// Display received data on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(receivedData.substring(0, 16)); // Display first 16 characters on the first row
if (receivedData.length() > 16) {
lcd.setCursor(0, 1);
lcd.print(receivedData.substring(16, 32)); // Display next 16 characters on the second row if available
}
}
// Check for distance warning message
if (receivedData.indexOf("WARNING: Bahaya!") != -1) {
// Turn on the buzzer
digitalWrite(buzzerPin, HIGH);
} else {
// Turn off the buzzer
digitalWrite(buzzerPin, LOW);
}
} else {
// No data received, print to Serial Monitor for debugging
Serial.println("No data received from LoRa.");
}
// Brief delay to avoid overwhelming the loop
delay(1000);
}