Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MASTER Code
- #include <SoftwareSerial.h>
- #include <LiquidCrystal_I2C.h>
- #include <TimerOne.h>
- #define buzzer_PIN 6
- #define BTN_1_PIN 3
- #define BTN_2_PIN 2
- #define slave_Address 2
- bool LED_1_State = false;
- bool LED_2_State = false;
- unsigned long previousMillis = 0;
- const long interval = 5000;
- byte char_Degree[8] = {B00110, B01001, B01001, B00110, B00000, B00000, B00000, B00000};
- bool BTN_1_Trig = true;
- bool BTN_2_Trig = true;
- SoftwareSerial ReyaxLoRa(5, 4); //--> RX, TX
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- void ReyaxLoRa_Send(int addr, String data_send) {
- String str_Send;
- str_Send = "AT+SEND=" + String(addr) + "," + String(data_send.length()) + "," + data_send + "\r\n";
- ReyaxLoRa.print(str_Send);
- Serial.println();
- Serial.print("Send to Slave : ");
- Serial.print(str_Send);
- Serial.flush();
- }
- void ReyaxLoRa_Receive() {
- if (ReyaxLoRa.available() > 0 ) {
- String rcv_Data_String = ReyaxLoRa.readString();
- if (rcv_Data_String.indexOf("OK") > 0 || rcv_Data_String.indexOf("ERR") > 0) {
- Serial.println();
- Serial.print(F("LoRa Reyax Module Response : "));
- Serial.println(rcv_Data_String);
- Serial.flush();
- return;
- } else {
- // Print received data or messages.
- Serial.println(F("Received from Sender : "));
- Serial.println(rcv_Data_String);
- Serial.flush();
- String _message = getValue(rcv_Data_String, ',', 2);
- String t = getValue(_message, '|', 0);
- String h = getValue(_message, '|', 1);
- String rcv_LED_1_State = getValue(_message, '|', 2);
- String rcv_LED_2_State = getValue(_message, '|', 3);
- String flame = getValue(_message, '|', 4);
- String gas = getValue(_message, '|', 5);
- int gas_status = gas.toInt();
- Serial.println("*******************************");
- Serial.println(gas_status);
- Serial.println("*******************************");
- lcd.clear();
- if (gas_status > 100) {
- digitalWrite(buzzer_PIN, HIGH);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Gas Detected !!");
- lcd.setCursor(0, 1);
- lcd.print(" !! Alert !!");
- }
- if (flame == "1") {
- digitalWrite(buzzer_PIN, HIGH);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Flame Detected !!");
- lcd.setCursor(0, 1);
- lcd.print(" !! Alert !!");
- }
- if (gas_status < 100 && flame == "0") {
- digitalWrite(buzzer_PIN, LOW);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("T:");
- lcd.print(t);
- lcd.setCursor(7, 0);
- lcd.write(byte(0));
- lcd.print("C");
- lcd.setCursor(0, 1);
- lcd.print("H:");
- lcd.print(h);
- lcd.print("%");
- lcd.setCursor(10, 0);
- if (rcv_LED_1_State == "1") lcd.print("L1:ON");
- if (rcv_LED_1_State == "0") lcd.print("L1:OFF");
- lcd.setCursor(10, 1);
- if (rcv_LED_2_State == "1") lcd.print("L2:ON");
- if (rcv_LED_2_State == "0") lcd.print("L2:OFF");
- }
- }
- }
- }
- String getValue(String data, char separator, int index) {
- int found = 0;
- int strIndex[] = { 0, -1 };
- int maxIndex = data.length() - 1;
- for (int i = 0; i <= maxIndex && found <= index; i++) {
- if (data.charAt(i) == separator || i == maxIndex) {
- found++;
- strIndex[0] = strIndex[1] + 1;
- strIndex[1] = (i == maxIndex) ? i + 1 : i;
- }
- }
- return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
- }
- void setup() {
- Serial.begin(9600);
- Serial.println();
- ReyaxLoRa.begin(9600);
- pinMode(buzzer_PIN, OUTPUT);
- pinMode(BTN_1_PIN, INPUT_PULLUP);
- pinMode(BTN_2_PIN, INPUT_PULLUP);
- digitalWrite(buzzer_PIN, LOW);
- Timer1.initialize(100000);
- Timer1.attachInterrupt(timerIsr_BTN_Press);
- lcd.init();
- lcd.backlight();
- lcd.createChar(0, char_Degree);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print(" PLEASE WAIT ");
- delay(500);
- }
- void loop() {
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis >= interval) {
- previousMillis = currentMillis;
- ReyaxLoRa_Send(slave_Address, String(LED_1_State) + "|" + String(LED_2_State));
- }
- ReyaxLoRa_Receive();
- }
- void timerIsr_BTN_Press() {
- if (digitalRead(BTN_1_PIN) == 0 && BTN_1_Trig == true) {
- delay(100);
- BTN_1_Trig = false;
- LED_1_State = !LED_1_State;
- }
- if (digitalRead(BTN_1_PIN) == 1) BTN_1_Trig = true;
- if (digitalRead(BTN_2_PIN) == 0 && BTN_2_Trig == true) {
- delay(100);
- BTN_2_Trig = false;
- LED_2_State = !LED_2_State;
- }
- if (digitalRead(BTN_2_PIN) == 1) BTN_2_Trig = true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement