Blame view

LORA_terrestre.ino 4.33 KB
1c8cfea4   root   code module lora ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  // Feather9x_RX
  // -*- mode: C++ -*-
  // Example sketch showing how to create a simple messaging client (receiver)
  // with the RH_RF95 class. RH_RF95 class does not provide for addressing or
  // reliability, so you should only use RH_RF95 if you do not need the higher
  // level messaging abilities.
  // It is designed to work with the other example Feather9x_TX
  #include <SPI.h>
  #include <RH_RF95.h>
  
   //for feather m0  
  #define RFM95_CS 8
  #define RFM95_RST 4
  #define RFM95_INT 3
  
  //taille des paquets Lora envoyés à chaque émission
  #define TAILLE_RADIOPACKET 20
  
  // Change to 434.0 or other frequency, must match RX's freq!
  #define RF95_FREQ 434.0
  // Singleton instance of the radio driver
  RH_RF95 rf95(RFM95_CS, RFM95_INT);
  // Blinky on receipt
  #define LED 13
  
  
  
  //Fonction vidage de buffer série
  void serialFlush(){
    pinMode(LED, OUTPUT);
    while(Serial.available() > 0) {
      char t = Serial.read();
    }
  }
  
  void RAZradiopacket(uint8_t radiopacket[TAILLE_RADIOPACKET]){
    int j;
    for(j=0;j<TAILLE_RADIOPACKET;j++){
    radiopacket[j] = '0';
    }
  }
  
  
  
  void setup()
  {
  pinMode(LED, OUTPUT);
  pinMode(RFM95_RST, OUTPUT);
  digitalWrite(RFM95_RST, HIGH);
  while (!Serial);
  Serial.begin(9600);
  delay(100);
  //Serial.println("Feather LoRa RX Test!");
  // manual reset
  digitalWrite(RFM95_RST, LOW);
  delay(10);
  digitalWrite(RFM95_RST, HIGH);
  delay(10);
  while (!rf95.init()) {
  Serial.println("LoRa radio init failed");
  while (1);
  }
  Serial.println("Projet IMA Ballon atmospherique ");
  // Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM
  if (!rf95.setFrequency(RF95_FREQ)) {
  Serial.println("setFrequency failed");
  while (1);
  serialFlush();
  
  }
  Serial.print("Set Freq to: "); Serial.println(RF95_FREQ);
  // Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on
  // The default transmitter power is 13dBm, using PA_BOOST.
  // If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then
  // you can set transmitter powers from 5 to 23 dBm:
  rf95.setTxPower(23, false);
  //RAZradiopacket(radiopacket_tmp);
  //RAZradiopacket(radiopacket);
  }
  
  
  int nb_envoi;
  uint8_t radiopacket[TAILLE_RADIOPACKET] ="null"; //Chaîne contenant la requête à envoyer à la nacelle 
  uint8_t radiopacket_tmp[TAILLE_RADIOPACKET]="null";
  
  void loop()
    {
      //DECLARATION VARIABLES ET INITIALISATION
    char caractere_lu = 0; //caractere à lire
    int caractere_dispo = 0; // nombre de caracteres dispo dans le buffer
    int i_chaine = 0; //indique l'endroit où on doit écrire dans la chaîne
   
    memcpy(radiopacket_tmp,radiopacket,TAILLE_RADIOPACKET);
  
    //LECTURE REQUETE SUR PORT SERIE "SOL"
    caractere_dispo = Serial.available(); 
  
    while(caractere_dispo > 0) //tant qu'il y a des caractères à lire
      {
          caractere_lu = Serial.read(); //on lit le caractère
          radiopacket[i_chaine] = caractere_lu;
          Serial.print(caractere_lu); //puis on le renvoi à l’expéditeur tel quel
          caractere_dispo = Serial.available(); //on relit le nombre de caractères dispo
          i_chaine++;
      }
      radiopacket[i_chaine] = 0;  //  marquage fin du packet 
  
      //si la donnée reçu est différente de la précédente
      if(memcmp(radiopacket,radiopacket_tmp,TAILLE_RADIOPACKET)!=0){
        Serial.print("\nRequete differente\n");
        nb_envoi = 0;
      }
  
      
     //ENVOI REQUETE 
     if (nb_envoi==0){    
      rf95.send((uint8_t *)radiopacket, sizeof(radiopacket));
      Serial.print("Envoi commande\n");
      nb_envoi++;   
     Serial.print("Nb_envoi:");
     Serial.println(nb_envoi);
     //radiopacket[0] = '0';
     //RAZradiopacket(radiopacket);
     }
     
     
    
      //LECTURE DONNEES VENANT DE LA NACELLE 
      if (rf95.available())
        {
         // Should be a message for us now
         uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
         uint8_t len = sizeof(buf);
           if (rf95.recv(buf, &len))
            {
              digitalWrite(LED, HIGH);
             // RH_RF95::printBuffer("Received: ", buf, len);
              Serial.print("Reception: ");
              Serial.println((char*)buf);
              serialFlush();
              
              
              
              // Send a reply
              //rf95.send(data, sizeof(data));
              //rf95.waitPacketSent();
              //Serial.println("Sent a reply");
              //digitalWrite(LED, LOW);
            }
            else
              {
                Serial.println("Receive failed");
                }
          }
       delay(200);
  }