Programme pour envoyer les données avec capteur A

Dependencies:   SX1272Lib WakeUp mbed-src mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "main.h"
00003 #include "sx1272-hal.h"
00004 #include "debug.h"
00005 #include "DHT.h"
00006 #include "WakeUp.h"
00007 
00008 //glenn
00009 AnalogIn in(PC_3);// Pour la photorésistance
00010 AnalogIn in2(PC_2);// Pour la conductivite
00011 
00012 DHT sensor(PC_5,DHT22); // Pour humi/tempe
00013 
00014 
00015 /*****************************************************
00016 Attention /!\ On récupère la valeur de la résistance et pas des lux
00017 Et aussi on a la valeur du sensor direct, pas de la conductivité exactement
00018 Mettre ça dans la raspberry je pense, ou direct sur le site web
00019 ******************************************************/
00020 
00021 
00022 #define DEBUG_MESSAGE   1
00023 
00024 /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */
00025 #define USE_MODEM_LORA  1
00026 #define USE_MODEM_FSK   !USE_MODEM_LORA
00027 
00028 #define RF_FREQUENCY                                    868000000 // Hz
00029 #define TX_OUTPUT_POWER                                 14        // 14 dBm
00030 
00031 #if USE_MODEM_LORA == 1
00032 
00033     #define LORA_BANDWIDTH                              2         // [0: 125 kHz,
00034                                                                   //  1: 250 kHz,
00035                                                                   //  2: 500 kHz,
00036                                                                   //  3: Reserved]
00037     #define LORA_SPREADING_FACTOR                       7         // [SF7..SF12]
00038     #define LORA_CODINGRATE                             1         // [1: 4/5,
00039                                                                   //  2: 4/6,
00040                                                                   //  3: 4/7,
00041                                                                   //  4: 4/8]
00042     #define LORA_PREAMBLE_LENGTH                        8         // Same for Tx and Rx
00043     #define LORA_SYMBOL_TIMEOUT                         5         // Symbols
00044     #define LORA_FIX_LENGTH_PAYLOAD_ON                  false
00045     #define LORA_FHSS_ENABLED                           false  
00046     #define LORA_NB_SYMB_HOP                            4     
00047     #define LORA_IQ_INVERSION_ON                        false
00048     #define LORA_CRC_ENABLED                            true
00049     
00050 #elif USE_MODEM_FSK == 1
00051 
00052     #define FSK_FDEV                                    25000     // Hz
00053     #define FSK_DATARATE                                19200     // bps
00054     #define FSK_BANDWIDTH                               50000     // Hz
00055     #define FSK_AFC_BANDWIDTH                           83333     // Hz
00056     #define FSK_PREAMBLE_LENGTH                         5         // Same for Tx and Rx
00057     #define FSK_FIX_LENGTH_PAYLOAD_ON                   false
00058     #define FSK_CRC_ENABLED                             true
00059     
00060 #else
00061     #error "Please define a modem in the compiler options."
00062 #endif
00063 
00064 #define RX_TIMEOUT_VALUE                                3500000   // in us
00065 #define BUFFER_SIZE                                     32        // Define the payload size here
00066 
00067 #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) )
00068 DigitalOut led(LED2);
00069 #else
00070 DigitalOut led(LED1);
00071 #endif
00072 
00073 /*********************************************
00074 // fonction sleep
00075 **********************************************/
00076 /*Timeout timeout;                            // Create the Timeout object
00077 void wait_LowPow(unsigned long time); 
00078 void timer_interrupt()                      // The Timer interrupt service routine
00079 {
00080                                             // Nothing to do, just for wakeup the system
00081 }
00082 void wait_LowPow(unsigned long time)        // Wait in low power mode function implementation
00083 {
00084     timeout.attach(&timer_interrupt, time); // Set the timer interrupt service rutine (ISR) and the time for the timeout (in seconds)
00085     sleep();                                // Enter Low Power Mode (It cant be used deep_sleep() mode, because that mode disable the timer)
00086 }*/
00087 
00088 //
00089 /*
00090  *  Global variables declarations
00091  */
00092 typedef enum
00093 {
00094     LOWPOWER = 0,
00095     IDLE,
00096     
00097     RX,
00098     RX_TIMEOUT,
00099     RX_ERROR,
00100     
00101     TX,
00102     TX_TIMEOUT,
00103     
00104     CAD,
00105     CAD_DONE
00106 }AppStates_t;
00107 
00108 volatile AppStates_t State = LOWPOWER;
00109 
00110 /*!
00111  * Radio events function pointer
00112  */
00113 static RadioEvents_t RadioEvents;
00114 
00115 /*
00116  *  Global variables declarations
00117  */
00118 SX1272MB2xAS Radio( NULL );
00119 
00120 //const uint8_t PingMsg[] = "PING";
00121 //const uint8_t PongMsg[] = "PONG";
00122 const uint8_t CommandeDebut[] = "1";
00123 
00124 /*const uint8_t PingMsg[] = 42;
00125 const uint8_t PongMsg[] = "Message reçu !";*/
00126 
00127 uint16_t BufferSize = BUFFER_SIZE;
00128 uint8_t Buffer[BUFFER_SIZE];
00129 
00130 int16_t RssiValue = 0.0;
00131 int8_t SnrValue = 0.0;
00132 
00133 
00134 
00135 int main() 
00136 {
00137     int err;//pour dht22
00138     //float val_celcius;//dht22
00139    // float val_humi;//dht22
00140     uint8_t i;
00141     bool isMaster = false;
00142     
00143     debug( "\n\n\r     SX1272 Ping Pong Demo Application \n\n\r" );
00144 
00145     // Initialize Radio driver
00146     RadioEvents.TxDone = OnTxDone;
00147     RadioEvents.RxDone = OnRxDone;
00148     RadioEvents.RxError = OnRxError;
00149     RadioEvents.TxTimeout = OnTxTimeout;
00150     RadioEvents.RxTimeout = OnRxTimeout;
00151     Radio.Init( &RadioEvents );
00152     
00153     
00154    
00155      
00156     //
00157     
00158     // verify the connection with the board
00159     while( Radio.Read( REG_VERSION ) == 0x00  )
00160     {
00161         debug( "Radio could not be detected!\n\r", NULL );
00162         wait( 1 );
00163     }
00164             
00165     debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ) , "\n\r > Board Type: SX1272MB2xAS < \n\r" );
00166   
00167     Radio.SetChannel( RF_FREQUENCY ); 
00168 
00169 #if USE_MODEM_LORA == 1
00170     
00171     debug_if( LORA_FHSS_ENABLED, "\n\n\r             > LORA FHSS Mode < \n\n\r");
00172     debug_if( !LORA_FHSS_ENABLED, "\n\n\r             > LORA Mode < \n\n\r");
00173 
00174     Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
00175                          LORA_SPREADING_FACTOR, LORA_CODINGRATE,
00176                          LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
00177                          LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, 
00178                          LORA_IQ_INVERSION_ON, 2000000 );
00179     
00180     Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
00181                          LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
00182                          LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
00183                          LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, 
00184                          LORA_IQ_INVERSION_ON, true );
00185                          
00186 #elif USE_MODEM_FSK == 1
00187 
00188     debug("\n\n\r              > FSK Mode < \n\n\r");
00189     Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
00190                          FSK_DATARATE, 0,
00191                          FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
00192                          FSK_CRC_ENABLED, 0, 0, 0, 2000000 );
00193     
00194     Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
00195                          0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
00196                          0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED,
00197                          0, 0, false, true );
00198                          
00199 #else
00200 
00201 #error "Please define a modem in the compiler options."
00202 
00203 #endif
00204      
00205     debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" ); 
00206         
00207     led = 0;
00208         
00209     Radio.Rx( RX_TIMEOUT_VALUE );
00210     wait(1);
00211     
00212     union{
00213             float val_celcius;
00214             uint8_t array_celcius[4];
00215         };
00216         union{
00217             float res;
00218             uint8_t array_res[4];
00219         };
00220         union{
00221             float val_humi;
00222             uint8_t array_humi[4];
00223         };
00224         union{
00225             float val_condu;
00226             uint8_t array_condu[4];
00227         };
00228     
00229     /*fill le buffer au debut
00230     */
00231     for( i = 0; i < BufferSize; i++ )
00232                         {
00233                             Buffer[i] = 0;
00234                         }
00235     
00236     WakeUp::calibrate();
00237     
00238     while( 1 )
00239     {
00240         /***************************************************************************************************
00241         Declaraction et récupération des données capteurs
00242         *****************************************************************************************************/
00243         
00244         float in_value = in.read();
00245          res=(10000*in_value)/(3.3 - in_value); // valeur resistance, voir lux après
00246         float in2_value = in2.read();
00247          val_condu = in2_value;//Valeur conductivity, voir pour les coef après
00248        
00249        debug("la valeur de conductivité du capteur vaut %f\n",val_condu);
00250         
00251         err = sensor.readData();  //on lit le dht22
00252         if (err == 0) {//Valeurs pour dht22
00253              val_celcius= sensor.ReadTemperature(CELCIUS);
00254              val_humi= sensor.ReadHumidity();
00255             printf("Temperature is %4.2f C \r\n",sensor.ReadTemperature(CELCIUS));
00256             //printf("val_celcius vaut %f\n",val_celcius);
00257         } else
00258             printf("\r\nErr %i \n",err);
00259         
00260         wait(2);
00261        /***************************************************************************************************
00262         On passe les données en uint8_t pour les mettre dans le buffer
00263         *****************************************************************************************************/
00264         /****************************************************************************************************
00265         Test mise en veille
00266         *****************************************************************************************************/
00267        /* WakeUp::set_ms(5000);
00268              //Enter deepsleep, the program won't go beyond this point until it is woken up
00269                         //sleep(10);
00270                         //wait_LowPow(10);
00271                         deepsleep();
00272                         wait(5);*/
00273         /***************************************************************************************************
00274         Les états de Lora
00275         ***************************************************************************************************/
00276         switch( State )
00277         {
00278         case RX: 
00279                 if( BufferSize > 0 )
00280                 {
00281                     debug("buffer 0 : %d\n",Buffer[0]);
00282                     if(Buffer[0] == 1 )
00283                     {
00284                         led = !led;
00285                         debug( "ordre de commencer\r\n" );
00286 
00287                         //Buffer[0,1,2] c'est pour commander les différents appareils. Ici buffer 0
00288                         Buffer[1]=1;//Ordre pour dire qu'on a bien renvoyer le message
00289                         // We fill the buffer with numbers for the payload 
00290                       Buffer[4]=array_res[0];
00291                         Buffer[5]=array_res[1];
00292                         Buffer[6]=array_res[2];
00293                         Buffer[7]=array_res[3];
00294                         
00295                         Buffer[8]=array_condu[0];
00296                         Buffer[9]=array_condu[1];
00297                         Buffer[10]=array_condu[2];
00298                         Buffer[11]=array_condu[3];
00299                         
00300                         Buffer[12]=array_celcius[0];
00301                         Buffer[13]=array_celcius[1];
00302                         Buffer[14]=array_celcius[2];
00303                         Buffer[15]=array_celcius[3];
00304                         
00305                         Buffer[16]=array_humi[0];
00306                         Buffer[17]=array_humi[1];
00307                         Buffer[18]=array_humi[2];
00308                         Buffer[19]=array_humi[3];
00309                         for( i = 20; i < BufferSize; i++ )
00310                         {
00311                             Buffer[i] = i - 20;
00312                         }
00313                         
00314                         Buffer[0]=0;
00315                         wait_ms( 2000 ); //On attend 2sec c'est pour le dht
00316                         Radio.Send( Buffer, BufferSize );
00317                         //On sleep ici
00318                         //sleep(1000);
00319                         //sleep();
00320                         //wait(10);
00321                        WakeUp::set_ms(3600000);
00322              //Enter deepsleep, the program won't go beyond this point until it is woken up
00323                         //sleep(10);
00324                         //wait_LowPow(10);
00325                         deepsleep();
00326                         wait(3600);
00327                         //debug("fin deepsleep");
00328                         
00329                     }
00330                     else // valid reception but not a PING as expected
00331                     {    // Set device as master and start again
00332                         //isMaster = true;
00333                         Radio.Rx( RX_TIMEOUT_VALUE );
00334                     }    
00335                 }
00336             
00337             State = LOWPOWER;
00338             break;
00339         case TX:    
00340             led = !led; 
00341 
00342             if( isMaster == true )  
00343             {
00344                 //debug( "Ping...\r\n" );
00345 
00346                 //debug("La resistance vaut %f\n",res);
00347             }
00348             else
00349             {
00350                 debug( "Dans tx sans etre le maitre\r\n" );
00351                 debug("La resistance vaut %f\n",res);
00352             }
00353             Radio.Rx( RX_TIMEOUT_VALUE );
00354             State = LOWPOWER;
00355             break;
00356         case RX_TIMEOUT:
00357             if( isMaster == true )
00358             {
00359                 // Send the next PING frame
00360                 //strcpy( ( char* )Buffer, ( char* )PingMsg );
00361                 for( i = 4; i < BufferSize; i++ )
00362                 {
00363                     Buffer[i] = i - 4;
00364                 }
00365                 wait_ms( 2000 ); 
00366                 Radio.Send( Buffer, BufferSize );
00367             }
00368             else
00369             {
00370                 Radio.Rx( RX_TIMEOUT_VALUE );  
00371             }             
00372             State = LOWPOWER;
00373             break;
00374         case RX_ERROR:
00375             // We have received a Packet with a CRC error, send reply as if packet was correct
00376             if( isMaster == true )
00377             {
00378                 // Send the next PING frame
00379                 //strcpy( ( char* )Buffer, ( char* )PingMsg );
00380                 for( i = 4; i < BufferSize; i++ )
00381                 {
00382                     Buffer[i] = i - 4;
00383                 }
00384                 wait_ms( 2000 );  
00385                 Radio.Send( Buffer, BufferSize );
00386             }
00387             else
00388             {
00389                 // Send the next PONG frame
00390                 //strcpy( ( char* )Buffer, ( char* )PongMsg );
00391                 for( i = 4; i < BufferSize; i++ )
00392                 {
00393                     Buffer[i] = i - 4;
00394                 }
00395                 wait_ms( 2000 );  
00396                 Radio.Send( Buffer, BufferSize );
00397             }
00398             State = LOWPOWER;
00399             break;
00400         case TX_TIMEOUT:
00401             Radio.Rx( RX_TIMEOUT_VALUE );
00402             State = LOWPOWER;
00403             break;
00404         case LOWPOWER:
00405             break;
00406         default:
00407             State = LOWPOWER;
00408             break;
00409         } 
00410        //wait(2);
00411     }
00412 }
00413 
00414 void OnTxDone( void )
00415 {
00416     Radio.Sleep( );
00417     State = TX;
00418     debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" );
00419 }
00420 
00421 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
00422 {
00423     Radio.Sleep( );
00424     BufferSize = size;
00425     memcpy( Buffer, payload, BufferSize );
00426     RssiValue = rssi;
00427     SnrValue = snr;
00428     State = RX;
00429     debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" );
00430 }
00431 
00432 void OnTxTimeout( void )
00433 {
00434     Radio.Sleep( );
00435     State = TX_TIMEOUT;
00436     debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" );
00437 }
00438 
00439 void OnRxTimeout( void )
00440 {
00441     Radio.Sleep( );
00442     Buffer[ BufferSize ] = 0;
00443     State = RX_TIMEOUT;
00444     debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" );
00445 }
00446 
00447 void OnRxError( void )
00448 {
00449     Radio.Sleep( );
00450     State = RX_ERROR;
00451     debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" );
00452 }
00453