Commit 1c8cfea43b1465f4e08631ad5cf966369f0b0936

Authored by root
1 parent 17f34671

code module lora terrestre

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