Upload files to "Kode Arduino IDE"

This commit is contained in:
akbarwijaya1701 2024-07-25 11:31:55 +07:00
commit 3ebbaaaefc
2 changed files with 300 additions and 0 deletions

View File

@ -0,0 +1,180 @@
#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);
}

View File

@ -0,0 +1,120 @@
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
#include <HardwareSerial.h>
// Define SoftwareSerial for sensor communication
SoftwareSerial mySerial(25, 26); // RX, TX
// Initialize data array and distance variable
unsigned char data[4] = {};
float distance;
// Initialize I2C LCD display with address 0x27, 16 columns, and 2 rows
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define buzzer pin
const int buzzerPin = 12;
// Initialize HardwareSerial for LoRa E32 on UART2
HardwareSerial LoRaSerial(2); // Use UART2
void setup() {
// Initialize serial communication for debugging
Serial.begin(57600);
mySerial.begin(9600);
// 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
// Print initial message to Serial Monitor
Serial.println("Sender is ready. Sending data...");
}
void loop() {
// Read data from sensor
do {
for (int i = 0; i < 4; i++) {
data[i] = mySerial.read();
}
} while (mySerial.read() == 0xff);
mySerial.flush();
if (data[0] == 0xff) {
int sum = (data[0] + data[1] + data[2]) & 0x00FF;
if (sum == data[3]) {
distance = (data[1] << 8) + data[2];
unsigned long sendTime = millis(); // Capture the timestamp
int snr = random(10, 20); // Simulate SNR
int rssi = random(-100, -30); // Simulate RSSI
if (distance > 280) {
// Print distance to Serial Monitor
Serial.print("Jarak: ");
Serial.print(distance / 10);
Serial.println(" cm");
// Display distance on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Jarak: ");
lcd.print(distance / 10);
lcd.print(" cm");
// Send distance via LoRa
LoRaSerial.print("Jarak: ");
LoRaSerial.print(distance / 10);
LoRaSerial.print(" cm,SNR:");
LoRaSerial.print(snr);
LoRaSerial.print(",RSSI:");
LoRaSerial.print(rssi);
LoRaSerial.print(",TIME:");
LoRaSerial.println(sendTime);
// Turn off the buzzer
digitalWrite(buzzerPin, LOW);
} else {
// Print warning to Serial Monitor
Serial.println("WARNING: Bahaya! Air terlalu tinggi!");
// Display warning on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WARNING: Bahaya!");
lcd.setCursor(0, 1);
lcd.print("Air Tinggi");
// Send warning via LoRa
LoRaSerial.print("WARNING: Bahaya! Air terlalu tinggi!,SNR:");
LoRaSerial.print(snr);
LoRaSerial.print(",RSSI:");
LoRaSerial.print(rssi);
LoRaSerial.print(",TIME:");
LoRaSerial.println(sendTime);
// Turn on the buzzer
digitalWrite(buzzerPin, HIGH);
}
} else {
// Print error to Serial Monitor
Serial.println("ERROR");
// Display error on LCD
lcd.clear();
lcd.print("ERROR");
// Send error via LoRa
LoRaSerial.println("ERROR");
}
}
// Brief delay between measurements
delay(150);
}