Advertisement
safwan092

Untitled

Jan 23rd, 2024
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. // MASTER Code
  2. #include <SoftwareSerial.h>
  3. #include <LiquidCrystal_I2C.h>
  4. #include <TimerOne.h>
  5.  
  6. #define buzzer_PIN 6
  7. #define BTN_1_PIN 3
  8. #define BTN_2_PIN 2
  9.  
  10. #define slave_Address 2
  11. bool LED_1_State = false;
  12. bool LED_2_State = false;
  13. unsigned long previousMillis = 0;
  14. const long interval = 5000;
  15. byte char_Degree[8] = {B00110, B01001, B01001, B00110, B00000, B00000, B00000, B00000};
  16. bool BTN_1_Trig = true;
  17. bool BTN_2_Trig = true;
  18.  
  19. SoftwareSerial ReyaxLoRa(5, 4); //--> RX, TX
  20. LiquidCrystal_I2C lcd(0x27, 16, 2);
  21.  
  22. void ReyaxLoRa_Send(int addr, String data_send) {
  23. String str_Send;
  24. str_Send = "AT+SEND=" + String(addr) + "," + String(data_send.length()) + "," + data_send + "\r\n";
  25. ReyaxLoRa.print(str_Send);
  26. Serial.println();
  27. Serial.print("Send to Slave : ");
  28. Serial.print(str_Send);
  29. Serial.flush();
  30. }
  31. void ReyaxLoRa_Receive() {
  32. if (ReyaxLoRa.available() > 0 ) {
  33. String rcv_Data_String = ReyaxLoRa.readString();
  34.  
  35. if (rcv_Data_String.indexOf("OK") > 0 || rcv_Data_String.indexOf("ERR") > 0) {
  36. Serial.println();
  37. Serial.print(F("LoRa Reyax Module Response : "));
  38. Serial.println(rcv_Data_String);
  39. Serial.flush();
  40. return;
  41. } else {
  42. // Print received data or messages.
  43. Serial.println(F("Received from Sender : "));
  44. Serial.println(rcv_Data_String);
  45. Serial.flush();
  46. String _message = getValue(rcv_Data_String, ',', 2);
  47. String t = getValue(_message, '|', 0);
  48. String h = getValue(_message, '|', 1);
  49. String rcv_LED_1_State = getValue(_message, '|', 2);
  50. String rcv_LED_2_State = getValue(_message, '|', 3);
  51. String flame = getValue(_message, '|', 4);
  52. String gas = getValue(_message, '|', 5);
  53.  
  54. int gas_status = gas.toInt();
  55. Serial.println("*******************************");
  56. Serial.println(gas_status);
  57. Serial.println("*******************************");
  58. lcd.clear();
  59. if (gas_status > 100) {
  60. digitalWrite(buzzer_PIN, HIGH);
  61. lcd.clear();
  62. lcd.setCursor(0, 0);
  63. lcd.print("Gas Detected !!");
  64. lcd.setCursor(0, 1);
  65. lcd.print(" !! Alert !!");
  66. }
  67. if (flame == "1") {
  68. digitalWrite(buzzer_PIN, HIGH);
  69. lcd.clear();
  70. lcd.setCursor(0, 0);
  71. lcd.print("Flame Detected !!");
  72. lcd.setCursor(0, 1);
  73. lcd.print(" !! Alert !!");
  74. }
  75. if (gas_status < 100 && flame == "0") {
  76. digitalWrite(buzzer_PIN, LOW);
  77. lcd.clear();
  78. lcd.setCursor(0, 0);
  79. lcd.print("T:");
  80. lcd.print(t);
  81. lcd.setCursor(7, 0);
  82. lcd.write(byte(0));
  83. lcd.print("C");
  84. lcd.setCursor(0, 1);
  85. lcd.print("H:");
  86. lcd.print(h);
  87. lcd.print("%");
  88. lcd.setCursor(10, 0);
  89. if (rcv_LED_1_State == "1") lcd.print("L1:ON");
  90. if (rcv_LED_1_State == "0") lcd.print("L1:OFF");
  91. lcd.setCursor(10, 1);
  92. if (rcv_LED_2_State == "1") lcd.print("L2:ON");
  93. if (rcv_LED_2_State == "0") lcd.print("L2:OFF");
  94. }
  95. }
  96. }
  97. }
  98. String getValue(String data, char separator, int index) {
  99. int found = 0;
  100. int strIndex[] = { 0, -1 };
  101. int maxIndex = data.length() - 1;
  102. for (int i = 0; i <= maxIndex && found <= index; i++) {
  103. if (data.charAt(i) == separator || i == maxIndex) {
  104. found++;
  105. strIndex[0] = strIndex[1] + 1;
  106. strIndex[1] = (i == maxIndex) ? i + 1 : i;
  107. }
  108. }
  109. return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
  110. }
  111. void setup() {
  112. Serial.begin(9600);
  113. Serial.println();
  114. ReyaxLoRa.begin(9600);
  115. pinMode(buzzer_PIN, OUTPUT);
  116. pinMode(BTN_1_PIN, INPUT_PULLUP);
  117. pinMode(BTN_2_PIN, INPUT_PULLUP);
  118. digitalWrite(buzzer_PIN, LOW);
  119. Timer1.initialize(100000);
  120. Timer1.attachInterrupt(timerIsr_BTN_Press);
  121. lcd.init();
  122. lcd.backlight();
  123. lcd.createChar(0, char_Degree);
  124. lcd.clear();
  125. lcd.setCursor(0, 0);
  126. lcd.print(" PLEASE WAIT ");
  127. delay(500);
  128. }
  129. void loop() {
  130. unsigned long currentMillis = millis();
  131. if (currentMillis - previousMillis >= interval) {
  132. previousMillis = currentMillis;
  133. ReyaxLoRa_Send(slave_Address, String(LED_1_State) + "|" + String(LED_2_State));
  134. }
  135. ReyaxLoRa_Receive();
  136. }
  137. void timerIsr_BTN_Press() {
  138. if (digitalRead(BTN_1_PIN) == 0 && BTN_1_Trig == true) {
  139. delay(100);
  140. BTN_1_Trig = false;
  141. LED_1_State = !LED_1_State;
  142. }
  143. if (digitalRead(BTN_1_PIN) == 1) BTN_1_Trig = true;
  144.  
  145. if (digitalRead(BTN_2_PIN) == 0 && BTN_2_Trig == true) {
  146. delay(100);
  147. BTN_2_Trig = false;
  148. LED_2_State = !LED_2_State;
  149. }
  150. if (digitalRead(BTN_2_PIN) == 1) BTN_2_Trig = true;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement