Advertisement
safwan092

Untitled

Jan 23rd, 2024
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. // SLAVE Code
  2. #include <SoftwareSerial.h>
  3. #include "DHT.h"
  4.  
  5.  
  6. #define gas_PIN A0
  7. #define flame_PIN 8
  8. #define LED_1_PIN 7
  9. #define LED_2_PIN 6
  10. #define DHTPIN 3
  11.  
  12. #define DHTTYPE DHT11
  13. #define master_Address 1
  14. float t;
  15. int h;
  16. int flame;
  17. int gas;
  18. DHT dht11(DHTPIN, DHTTYPE);
  19. SoftwareSerial ReyaxLoRa(5, 4);
  20.  
  21.  
  22. void setup() {
  23. Serial.begin(9600);
  24. Serial.println();
  25. ReyaxLoRa.begin(9600);
  26. pinMode(gas_PIN, INPUT);
  27. pinMode(flame_PIN, INPUT);
  28. pinMode(LED_1_PIN, OUTPUT);
  29. pinMode(LED_2_PIN, OUTPUT);
  30. dht11.begin();
  31. Serial.println("Waiting for a message from the Master.");
  32. delay(500);
  33. }
  34.  
  35.  
  36. void loop() {
  37. ReyaxLoRa_Receive();
  38. }
  39.  
  40.  
  41.  
  42. void ReyaxLoRa_Send(int addr, String data_send) {
  43. String str_Send;
  44. // AT commands to transmit data.
  45. // For more details see the document "LoRa_AT_Command_RYLR998_RYLR498_EN.pdf" in the "AT+SEND" section.
  46. str_Send = "AT+SEND=" + String(addr) + "," + String(data_send.length()) + "," + data_send + "\r\n";
  47. ReyaxLoRa.print(str_Send);
  48. Serial.println();
  49. Serial.print("Send to Receiver : ");
  50. Serial.print(str_Send);
  51. Serial.flush();
  52. }
  53. void ReyaxLoRa_Receive() {
  54. if (ReyaxLoRa.available() > 0 ) {
  55. String rcv_Data_String = ReyaxLoRa.readString();
  56.  
  57. if (rcv_Data_String.indexOf("OK") > 0 || rcv_Data_String.indexOf("ERR") > 0) {
  58. Serial.println();
  59. Serial.print(F("LoRa Reyax Module Response : "));
  60. Serial.println(rcv_Data_String);
  61. return;
  62. } else {
  63. Serial.println(F("Received from Sender : "));
  64. Serial.println(rcv_Data_String);
  65. Serial.flush();
  66. String _message = getValue(rcv_Data_String, ',', 2);
  67. String rcv_LED_1_State = getValue(_message, '|', 0);
  68. String rcv_LED_2_State = getValue(_message, '|', 1);
  69. if (rcv_LED_1_State == "1") {
  70. digitalWrite(LED_1_PIN, HIGH);
  71. Serial.println("LED_1 : ON");
  72. }
  73. if (rcv_LED_1_State == "0") {
  74. digitalWrite(LED_1_PIN, LOW);
  75. Serial.println("LED_1 : OFF");
  76. }
  77. if (rcv_LED_2_State == "1") {
  78. digitalWrite(LED_2_PIN, HIGH);
  79. Serial.println("LED_2 : ON");
  80. }
  81. if (rcv_LED_2_State == "0") {
  82. digitalWrite(LED_2_PIN, LOW);
  83. Serial.println("LED_2 : OFF");
  84. }
  85. read_Flame_Sensor();
  86. read_GAS_Sensor();
  87. read_DHT11();
  88. bool send_LED_1_State = digitalRead(LED_1_PIN);
  89. bool send_LED_2_State = digitalRead(LED_2_PIN);
  90. delay(100);
  91. ReyaxLoRa_Send(master_Address, String(t) + "|" + String(h) + "|" + String(send_LED_1_State) + "|" + String(send_LED_2_State) + "|" + String(flame) + "|" + String(gas));
  92. }
  93. }
  94. }
  95. String getValue(String data, char separator, int index) {
  96. int found = 0;
  97. int strIndex[] = { 0, -1 };
  98. int maxIndex = data.length() - 1;
  99.  
  100. for (int i = 0; i <= maxIndex && found <= index; i++) {
  101. if (data.charAt(i) == separator || i == maxIndex) {
  102. found++;
  103. strIndex[0] = strIndex[1] + 1;
  104. strIndex[1] = (i == maxIndex) ? i + 1 : i;
  105. }
  106. }
  107. return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
  108. }
  109. void read_DHT11() {
  110. t = dht11.readTemperature();
  111. h = dht11.readHumidity();
  112. if (isnan(t) || isnan(h)) {
  113. Serial.println(F("Failed to read from DHT sensor!"));
  114. t = 0.00;
  115. h = 0;
  116. }
  117. Serial.print(F("Temperature : "));
  118. Serial.print(t);
  119. Serial.println(F("°C "));
  120. Serial.print(F("Humidity : "));
  121. Serial.print(h);
  122. Serial.println(F("%"));
  123. Serial.flush();
  124. }
  125.  
  126. void read_Flame_Sensor() {
  127. flame = digitalRead(flame_PIN);
  128. Serial.print("Flame Status: ");
  129. Serial.println(flame);
  130. }
  131.  
  132.  
  133. void read_GAS_Sensor() {
  134. gas = analogRead(gas_PIN);
  135. Serial.print("Gas Status: ");
  136. Serial.println(gas);
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement