Commit 17f34671e0ffd89edd847051c9ebfa1dc1524857
0 parents
code module lora embarque
Showing
1 changed file
with
136 additions
and
0 deletions
Show diff stats
1 | +++ a/LORA_nacelle.ino | ||
@@ -0,0 +1,136 @@ | @@ -0,0 +1,136 @@ | ||
1 | +// Code contenu dans le module Lora Feather EMBARQUE dans la nacelle | ||
2 | + | ||
3 | + | ||
4 | +#include <SPI.h> | ||
5 | +#include <RH_RF95.h> | ||
6 | + | ||
7 | +/* for feather m0 */ | ||
8 | +#define RFM95_CS 8 | ||
9 | +#define RFM95_RST 4 | ||
10 | +#define RFM95_INT 3 | ||
11 | + | ||
12 | +#define TAILLE_RADIOPACKET 20 | ||
13 | +#define LED 13 | ||
14 | + | ||
15 | +// Change to 434.0 or other frequency, must match RX's freq! | ||
16 | +#define RF95_FREQ 434.0 | ||
17 | +// Singleton instance of the radio driver | ||
18 | +RH_RF95 rf95(RFM95_CS, RFM95_INT); | ||
19 | + | ||
20 | +//Fonction vidage de buffer série | ||
21 | +void serialFlush(){ | ||
22 | + pinMode(LED, OUTPUT); | ||
23 | + while(Serial.available() > 0) { | ||
24 | + char t = Serial.read(); | ||
25 | + } | ||
26 | +} | ||
27 | + | ||
28 | +void setup() | ||
29 | +{ | ||
30 | + pinMode(RFM95_RST, OUTPUT); | ||
31 | + digitalWrite(RFM95_RST, HIGH); | ||
32 | + while (!Serial); // peut être à enlever avec rasp.. | ||
33 | + Serial.begin(9600); | ||
34 | + delay(100); | ||
35 | + //Serial.println("Feather LoRa TX Test!"); | ||
36 | + // manual reset | ||
37 | + digitalWrite(RFM95_RST, LOW); | ||
38 | + delay(10); | ||
39 | + digitalWrite(RFM95_RST, HIGH); | ||
40 | + delay(10); | ||
41 | + while (!rf95.init()) { | ||
42 | + //Serial.println("LoRa radio init failed"); | ||
43 | + while (1); | ||
44 | + } | ||
45 | + //Serial.println("LoRa radio init completed"); | ||
46 | + // Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM | ||
47 | + if (!rf95.setFrequency(RF95_FREQ)) { | ||
48 | + //Serial.println("setFrequency failed"); | ||
49 | + while (1); | ||
50 | + | ||
51 | + serialFlush(); | ||
52 | + } | ||
53 | + | ||
54 | + //Serial.print("Set Freq to: "); Serial.println(RF95_FREQ); | ||
55 | + // Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on | ||
56 | + // The default transmitter power is 13dBm, using PA_BOOST. | ||
57 | + // If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then | ||
58 | + // you can set transmitter powers from 5 to 23 dBm: | ||
59 | + rf95.setTxPower(23, false); | ||
60 | +} | ||
61 | + | ||
62 | +int nb_envoi; | ||
63 | +uint8_t radiopacket[TAILLE_RADIOPACKET]="null"; //Chaîne contenant la requête à envoyer à la nacelle | ||
64 | +uint8_t radiopacket_tmp[TAILLE_RADIOPACKET]="null"; | ||
65 | + | ||
66 | +void loop() | ||
67 | +{ | ||
68 | + //DECLARATION VARIABLES et INITIALISATION | ||
69 | + char caractere_lu = 0; //caractere à lire | ||
70 | + int caractere_dispo =0; // nombre de caracteres dispo dans le buffer | ||
71 | + int i_chaine = 0; //indique l'endroit où on doit écrire dans la chaîne | ||
72 | + | ||
73 | + memcpy(radiopacket_tmp,radiopacket,TAILLE_RADIOPACKET); | ||
74 | + | ||
75 | + //LECTURE DES DONNEES A ENVOYER AU SOL | ||
76 | + caractere_dispo = Serial.available(); | ||
77 | + | ||
78 | + while(caractere_dispo > 0) //tant qu'il y a des caractères à lire | ||
79 | + { | ||
80 | + caractere_lu = Serial.read(); //on lit le caractère | ||
81 | + radiopacket[i_chaine] = caractere_lu; | ||
82 | + //Serial.print(caractere_lu); //puis on le renvoi à l’expéditeur tel quel | ||
83 | + caractere_dispo = Serial.available(); //on relit le nombre de caractères dispo | ||
84 | + i_chaine++; | ||
85 | + } | ||
86 | + radiopacket[TAILLE_RADIOPACKET-1] = 0; // marquage fin du packet | ||
87 | + | ||
88 | + //si la donnée reçu est différente de la précédente | ||
89 | + if(memcmp(radiopacket,radiopacket_tmp,TAILLE_RADIOPACKET)!=0){ | ||
90 | + nb_envoi = 0; | ||
91 | + } | ||
92 | + | ||
93 | + | ||
94 | + //Serial.println("Sending..."); delay(10); | ||
95 | + | ||
96 | + //ENVOI DES DONNEES AU SOL | ||
97 | + if (nb_envoi==0){ | ||
98 | + rf95.send((uint8_t *)radiopacket, TAILLE_RADIOPACKET); | ||
99 | + rf95.waitPacketSent(); | ||
100 | + //Serial.println("TX done"); delay(10); | ||
101 | + digitalWrite(LED, HIGH); | ||
102 | + nb_envoi++; | ||
103 | + } | ||
104 | + | ||
105 | + | ||
106 | + //RECEPTION DES REQUETES PROVENANT DU SOL | ||
107 | + uint8_t buf[RH_RF95_MAX_MESSAGE_LEN]; | ||
108 | + uint8_t len = sizeof(buf); | ||
109 | + //Serial.println("Waiting for reply..."); delay(10); | ||
110 | + if (rf95.waitAvailableTimeout(1000)) | ||
111 | + { | ||
112 | + // Should be a reply message for us now | ||
113 | + if (rf95.recv(buf, &len)) | ||
114 | + { | ||
115 | + //ENVOI DE LA REQUETE VERS LA RASPBERRY | ||
116 | + Serial.println((char*)buf); | ||
117 | + | ||
118 | + //Serial.print("RSSI: "); | ||
119 | + //Serial.println(rf95.lastRssi(), DEC); | ||
120 | + | ||
121 | + } | ||
122 | + else | ||
123 | + { | ||
124 | + //Serial.println("Receive failed"); | ||
125 | + } | ||
126 | + } | ||
127 | + else | ||
128 | + { | ||
129 | + //Serial.println("No reply"); | ||
130 | + } | ||
131 | + digitalWrite(LED, LOW); | ||
132 | + | ||
133 | + delay(200); | ||
134 | +} | ||
135 | + | ||
136 | + |