Blame view

emetteur/emetteur.cpp 5.8 KB
a12ed64b   rcavalie   recepteur ok
1
  ////////////////////////////////////////////////////////////////////////////////

8c2393cd   rcavalie   travail reception...
2
3
4
5
6
  /*EMETTEUR LoRa

  par : Robin Cavalieri

        IMA5SC

  PFE 2018 */

  ////////////////////////////////////////////////////////////////////////////////

a12ed64b   rcavalie   recepteur ok
7
8
  //Headers

  #include "mbed.h"

7e1ad503   rcavalie   fichiers départ +...
9
10
11
  #include "main.h"

  #include "sx1276-hal.h"

  #include "debug.h"

a12ed64b   rcavalie   recepteur ok
12
  ////////////////////////////////////////////////////////////////////////////////

f766505c   rcavalie   création d'une tr...
13
  #define DEBUG_MESSAGE   0

a12ed64b   rcavalie   recepteur ok
14
15
  ////////////////////////////////////////////////////////////////////////////////

  //Modulation LoRa active

7e1ad503   rcavalie   fichiers départ +...
16
17
  #define USE_MODEM_LORA  1

  #define USE_MODEM_FSK   !USE_MODEM_LORA

a12ed64b   rcavalie   recepteur ok
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  //Paramètres

  #define RF_FREQUENCY 868000000 //Hz : Fréquence de travail pour UE

  #define TX_OUTPUT_POWER 14     //dBm : puissance de sortie du signal

  #define LORA_BANDWIDTH 1         // 0:125 kHz de BP

  #define LORA_SPREADING_FACTOR 10 // [SF7..SF12] Portée du débit. grande portée inutile

  #define LORA_CODINGRATE 1

  #define LORA_PREAMBLE_LENGTH 8   // Same for Tx and Rx

  #define LORA_SYMBOL_TIMEOUT 5    // Symbols

  #define LORA_FIX_LENGTH_PAYLOAD_ON false

  #define LORA_FHSS_ENABLED true

  #define LORA_NB_SYMB_HOP 4

  #define LORA_IQ_INVERSION_ON false

  #define LORA_CRC_ENABLED true

  #define RX_TIMEOUT_VALUE 3500      // in ms

  #define BUFFER_SIZE 32        // Define the payload size here

0ae07db2   rcavalie   emetteur ok
33
  #define TRAME_SIZE 13

a12ed64b   rcavalie   recepteur ok
34
35
36
37
  ////////////////////////////////////////////////////////////////////////////////

  

  ////////////////////////////////////////////////////////////////////////////////

  //Variables globales

7e1ad503   rcavalie   fichiers départ +...
38
39
40
41
  typedef enum

  {

      LOWPOWER = 0,

      IDLE,

7e1ad503   rcavalie   fichiers départ +...
42
43
      TX,

      TX_TIMEOUT,

7e1ad503   rcavalie   fichiers départ +...
44
45
46
47
48
      CAD,

      CAD_DONE

  }AppStates_t;

  

  volatile AppStates_t State = LOWPOWER;

7e1ad503   rcavalie   fichiers départ +...
49
  static RadioEvents_t RadioEvents;

7e1ad503   rcavalie   fichiers départ +...
50
  SX1276MB1xAS Radio( NULL );

7e1ad503   rcavalie   fichiers départ +...
51
52
  uint16_t BufferSize = BUFFER_SIZE;

  uint8_t Buffer[BUFFER_SIZE];

7e1ad503   rcavalie   fichiers départ +...
53
54
  int16_t RssiValue = 0.0;

  int8_t SnrValue = 0.0;

0ae07db2   rcavalie   emetteur ok
55
  uint8_t testMsg[TRAME_SIZE];

f766505c   rcavalie   création d'une tr...
56
57
58
  ////////////////////////////////////////////////////////////////////////////////

  

  ////////////////////////////////////////////////////////////////////////////////

0ae07db2   rcavalie   emetteur ok
59
60
61
62
63
64
  void TrameCreation()

  {

      //Création d'une trame

      /*

      [1...] : ID de l'emetteur

      [...] : ID du recepteur cible

8c2393cd   rcavalie   travail reception...
65
      [...] : Température communiquée ici 20 pour l'exemple

0ae07db2   rcavalie   emetteur ok
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
      */

      //Converti en ASCII

      testMsg[0] =  0x46; //emetteur F

      testMsg[1] =  0x33; //emetteur 3

      testMsg[2] =  0x30; //emetteur 0

      testMsg[3] =  0x33; //emetteur 3

      testMsg[4] =  0x52; //emetteur R

      testMsg[5] =  0x45; //emetteur E

      //

      testMsg[6] =  0x46; //recepteur F

      testMsg[7] =  0x30; //recepteur 0

      testMsg[8] =  0x39; //recepteur 9

      testMsg[9] =  0x31; //recepteur 1

      testMsg[10] = 0x43; //recepteur C

      //

      testMsg[11] = 0x32; //temperature 2 dizaines

8c2393cd   rcavalie   travail reception...
82
      testMsg[12] = 0x31; //temperature 0 unités

a12ed64b   rcavalie   recepteur ok
83
84
85
86
87
88
89
90
  }

  ////////////////////////////////////////////////////////////////////////////////

  

  ////////////////////////////////////////////////////////////////////////////////

  //Main emission

  int main( void )

  {

      uint8_t i;

f766505c   rcavalie   création d'une tr...
91
      debug("\nEmetteur SX1276 \n\n\r");

7e1ad503   rcavalie   fichiers départ +...
92
93
94
  

      // Initialize Radio driver

      RadioEvents.TxDone = OnTxDone;

7e1ad503   rcavalie   fichiers départ +...
95
      RadioEvents.TxTimeout = OnTxTimeout;

a12ed64b   rcavalie   recepteur ok
96
97
      RadioEvents.FhssChangeChannel = OnFhssChangeChannel;

      Radio.Init(&RadioEvents);

7e1ad503   rcavalie   fichiers départ +...
98
  

a12ed64b   rcavalie   recepteur ok
99
100
      //Véification de la connexion avec la carte

      while(Radio.Read(REG_VERSION) == 0x00)

7e1ad503   rcavalie   fichiers départ +...
101
      {

a12ed64b   rcavalie   recepteur ok
102
103
          debug("Aucune radio detectee\n\r", NULL);

          wait(1);

7e1ad503   rcavalie   fichiers départ +...
104
105
      }

  

a12ed64b   rcavalie   recepteur ok
106
      //Checking du matériel connecté

f766505c   rcavalie   création d'une tr...
107
108
      debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1LAS ) ), "\n\r Materiel detecte : SX1276MB1LAS \n\r" );

      debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1MAS ) ), "\n\r Materiel detecte : SX1276MB1MAS \n\r" );

7e1ad503   rcavalie   fichiers départ +...
109
  

a12ed64b   rcavalie   recepteur ok
110
111
      //Paramétrage du canal

      Radio.SetChannel( HoppingFrequencies[0] );

7e1ad503   rcavalie   fichiers départ +...
112
  

a12ed64b   rcavalie   recepteur ok
113
      #if USE_MODEM_LORA == 1

7e1ad503   rcavalie   fichiers départ +...
114
  

f766505c   rcavalie   création d'une tr...
115
116
          debug_if( LORA_FHSS_ENABLED, "\n\n\r LORA FHSS Mode \n\n\r" );

          debug_if( !LORA_FHSS_ENABLED, "\n\n\r LORA Mode \n\n\r" );

7e1ad503   rcavalie   fichiers départ +...
117
  

a12ed64b   rcavalie   recepteur ok
118
119
120
121
122
123
124
125
126
          //Configuration du modem transmission

          Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,

                               LORA_SPREADING_FACTOR, LORA_CODINGRATE,

                               LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,

                               LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,

                               LORA_IQ_INVERSION_ON, 4000 );

      #else

          #error "Aucun modem defini"

      #endif

7e1ad503   rcavalie   fichiers départ +...
127
  

a12ed64b   rcavalie   recepteur ok
128
      debug_if( DEBUG_MESSAGE, "Procedure d'emission lancee\r\n" );

a12ed64b   rcavalie   recepteur ok
129
130
      //Boucle dmission infinie

      while(1)

7e1ad503   rcavalie   fichiers départ +...
131
      {

0ae07db2   rcavalie   emetteur ok
132
133
134
135
136
         //On génère la trame

         TrameCreation();

         debug("\nTentative d'envoi... \r\n");

         strcpy((char*)Buffer, (char*)testMsg);

         for(i=TRAME_SIZE; i<BufferSize; i++)

a12ed64b   rcavalie   recepteur ok
137
         {

0ae07db2   rcavalie   emetteur ok
138
              Buffer[i] = i - TRAME_SIZE;

a12ed64b   rcavalie   recepteur ok
139
         }

0ae07db2   rcavalie   emetteur ok
140
         debug("Trame : %s\r\n",(char*)Buffer);

a12ed64b   rcavalie   recepteur ok
141
         Radio.Send(Buffer, BufferSize);

0ae07db2   rcavalie   emetteur ok
142
143
         debug("Message envoye \r\n");

         wait_ms(10000);

7e1ad503   rcavalie   fichiers départ +...
144
145
      }

  }

f766505c   rcavalie   création d'une tr...
146
  ////////////////////////////////////////////////////////////////////////////////

8c2393cd   rcavalie   travail reception...
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  

  ////////////////////////////////////////////////////////////////////////////////

  //Fonctions utiles

  

  //Debug pour savoir si le message a été transmis

  void OnTxDone( void )

  {

      Radio.SetChannel( HoppingFrequencies[0] );

      Radio.Sleep( );

      State = TX;

      debug_if( DEBUG_MESSAGE, "\nEnvoye \n\r" );

  }

  

  //Time out de transmission. Échec de l'envoi

  void OnTxTimeout( void )

  {

      Radio.SetChannel(HoppingFrequencies[0]);

      Radio.Sleep( );

      State = TX_TIMEOUT;

      debug_if( DEBUG_MESSAGE, "Echec de l'envoi \n\r");

  }

  

  //Modification du canal d'emission

  void OnFhssChangeChannel( uint8_t channelIndex )

  {

      Radio.SetChannel(HoppingFrequencies[channelIndex]);

      debug_if(DEBUG_MESSAGE, "F%d-", channelIndex);

  }

  ////////////////////////////////////////////////////////////////////////////////