Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SLAVE Code
- #include <SoftwareSerial.h>
- #include "DHT.h"
- #define gas_PIN A0
- #define flame_PIN 8
- #define LED_1_PIN 7
- #define LED_2_PIN 6
- #define DHTPIN 3
- #define DHTTYPE DHT11
- #define master_Address 1
- float t;
- int h;
- int flame;
- int gas;
- DHT dht11(DHTPIN, DHTTYPE);
- SoftwareSerial ReyaxLoRa(5, 4);
- void setup() {
- Serial.begin(9600);
- Serial.println();
- ReyaxLoRa.begin(9600);
- pinMode(gas_PIN, INPUT);
- pinMode(flame_PIN, INPUT);
- pinMode(LED_1_PIN, OUTPUT);
- pinMode(LED_2_PIN, OUTPUT);
- dht11.begin();
- Serial.println("Waiting for a message from the Master.");
- delay(500);
- }
- void loop() {
- ReyaxLoRa_Receive();
- }
- void ReyaxLoRa_Send(int addr, String data_send) {
- String str_Send;
- // AT commands to transmit data.
- // For more details see the document "LoRa_AT_Command_RYLR998_RYLR498_EN.pdf" in the "AT+SEND" section.
- str_Send = "AT+SEND=" + String(addr) + "," + String(data_send.length()) + "," + data_send + "\r\n";
- ReyaxLoRa.print(str_Send);
- Serial.println();
- Serial.print("Send to Receiver : ");
- 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);
- return;
- } else {
- Serial.println(F("Received from Sender : "));
- Serial.println(rcv_Data_String);
- Serial.flush();
- String _message = getValue(rcv_Data_String, ',', 2);
- String rcv_LED_1_State = getValue(_message, '|', 0);
- String rcv_LED_2_State = getValue(_message, '|', 1);
- if (rcv_LED_1_State == "1") {
- digitalWrite(LED_1_PIN, HIGH);
- Serial.println("LED_1 : ON");
- }
- if (rcv_LED_1_State == "0") {
- digitalWrite(LED_1_PIN, LOW);
- Serial.println("LED_1 : OFF");
- }
- if (rcv_LED_2_State == "1") {
- digitalWrite(LED_2_PIN, HIGH);
- Serial.println("LED_2 : ON");
- }
- if (rcv_LED_2_State == "0") {
- digitalWrite(LED_2_PIN, LOW);
- Serial.println("LED_2 : OFF");
- }
- read_Flame_Sensor();
- read_GAS_Sensor();
- read_DHT11();
- bool send_LED_1_State = digitalRead(LED_1_PIN);
- bool send_LED_2_State = digitalRead(LED_2_PIN);
- delay(100);
- ReyaxLoRa_Send(master_Address, String(t) + "|" + String(h) + "|" + String(send_LED_1_State) + "|" + String(send_LED_2_State) + "|" + String(flame) + "|" + String(gas));
- }
- }
- }
- 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 read_DHT11() {
- t = dht11.readTemperature();
- h = dht11.readHumidity();
- if (isnan(t) || isnan(h)) {
- Serial.println(F("Failed to read from DHT sensor!"));
- t = 0.00;
- h = 0;
- }
- Serial.print(F("Temperature : "));
- Serial.print(t);
- Serial.println(F("°C "));
- Serial.print(F("Humidity : "));
- Serial.print(h);
- Serial.println(F("%"));
- Serial.flush();
- }
- void read_Flame_Sensor() {
- flame = digitalRead(flame_PIN);
- Serial.print("Flame Status: ");
- Serial.println(flame);
- }
- void read_GAS_Sensor() {
- gas = analogRead(gas_PIN);
- Serial.print("Gas Status: ");
- Serial.println(gas);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement