Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
3.1 kB
0
Indexable
Never
#include <LoRa.h>
#include <SPI.h>
#include <DHT.h>
#define DHTPIN 4           
#define DHTTYPE DHT22    
#define Relay1 27         
#define Relay2 26        
#define Relay3 25        
#define Relay4 33        
#define ObjectID 13
#define ss 5
#define rst 14
#define dio0 2
#define MAX_DESCRIPTION_LENGTH 50 

DHT dht(DHTPIN, DHTTYPE);
bool Relay_1 = false , Relay_2 = false, Relay_3 = false ,Relay_4 = false ;
int relayPins[] = {Relay1, Relay2, Relay3, Relay4};
int numRelays = sizeof(relayPins) / sizeof(relayPins[0]);

typedef struct  __attribute__((__packed__)) {
uint8_t id;
float temp;
float speed;
char description[MAX_DESCRIPTION_LENGTH];
} Donnee;

void setup() {

  Serial.begin(115200);

 for (int i = 0; i < numRelays; i++) {
    pinMode(relayPins[i], OUTPUT);
    digitalWrite(relayPins[i], LOW);
  }

  dht.begin();
  while (!Serial);
  Serial.println("LoRa Receiver ") ;
  LoRa.setPins(ss, rst, dio0);
    while (!LoRa.begin(433E6)) {
    Serial.println(".");
    delay(500);
  }
  LoRa.setSyncWord(0xF3);
  Serial.println("LoRa Initializing OK!");
}
void loop() {
  // try to parse packett
int packetSize = LoRa.parsePacket ();
Donnee donnee; 
if (packetSize) {

uint8_t* x_tab = new uint8_t [sizeof (Donnee)];
int i = 0;
while (LoRa.available()) {
uint8_t     incoming = LoRa.read();
x_tab[i]  = incoming;
i++;
}
Donnee* x = (Donnee *) x_tab;
      Serial.print("L'identicatif de l'émetteur: ");
      Serial.println(x->id);
      Serial.println(x->description);
      Serial.print("Wind speed : ");
      Serial.print(x->speed);      
      Serial.println("m/s"); 
  Serial.println("-----------------------------------");

    float temp1  = dht.readTemperature();
    float hum    = dht.readHumidity();
    donnee.id    = ObjectID;
    delay(1000);
 
  Serial.print("L'identifiant est: ");
  Serial.println( donnee.id);
  Serial.print("Temperature1: ");
  Serial.print( temp1);
  Serial.print( "°C");
  Serial.print(" | Humidité1: ");
  Serial.print( hum);
  Serial.println("%");
  Serial.println("-----------------------------------");
 
   if (strstr(x->description, "rain") != NULL) {
  digitalWrite(Relay4, LOW);
  }
    if (x->speed > 30) {
      digitalWrite(Relay1, HIGH);
      digitalWrite(Relay2, HIGH);
      digitalWrite(Relay3, HIGH);
      digitalWrite(Relay4, HIGH);
  }
if ( hum < 40 || temp1 >30)
    digitalWrite(Relay1,HIGH);
    delay(30000);
    Relay_1 = true;
  }
  else { 
    digitalWrite(Relay1,LOW);
    Relay_1 = false;
  }
  if ( hum < 40 || temp1 >30){
    digitalWrite(Relay2,HIGH);
    delay(30000);
    Relay_2 = true;
  }
  else { 
    digitalWrite(Relay2,LOW);
    Relay_2 = false;
  }  
  if ( hum < 40 || temp1 >30){
    digitalWrite(Relay3,HIGH);
    delay(30000);
    Relay_3 = true;
  }
  else { 
    digitalWrite(Relay3,LOW);
    Relay_3 = false;
  }
  if (Relay_1   || Relay_2   || Relay_3 ){
    digitalWrite(Relay4,HIGH);
  }
  else { 
    digitalWrite(Relay4,LOW);
  }
 
  delay(500);
delete x_tab;
}
}