programme pour recevoir les données des capteurs
Dependencies: SX1272Lib mbed-src mbed
main.cpp@0:fa014443db91, 2017-05-13 (annotated)
- Committer:
- guillouhouede
- Date:
- Sat May 13 16:32:43 2017 +0000
- Revision:
- 0:fa014443db91
Recevoir des donn?es;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
guillouhouede | 0:fa014443db91 | 1 | #include "mbed.h" |
guillouhouede | 0:fa014443db91 | 2 | #include "main.h" |
guillouhouede | 0:fa014443db91 | 3 | #include "sx1272-hal.h" |
guillouhouede | 0:fa014443db91 | 4 | #include "debug.h" |
guillouhouede | 0:fa014443db91 | 5 | //POUR TROUVER MES PODIFICATIONS, RECHERCHE LE MOT "jo" AVEC CTRL+F |
guillouhouede | 0:fa014443db91 | 6 | /* Set this flag to '1' to display debug messages on the console */ |
guillouhouede | 0:fa014443db91 | 7 | #define DEBUG_MESSAGE 1 |
guillouhouede | 0:fa014443db91 | 8 | |
guillouhouede | 0:fa014443db91 | 9 | /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */ |
guillouhouede | 0:fa014443db91 | 10 | #define USE_MODEM_LORA 1 |
guillouhouede | 0:fa014443db91 | 11 | #define USE_MODEM_FSK !USE_MODEM_LORA |
guillouhouede | 0:fa014443db91 | 12 | |
guillouhouede | 0:fa014443db91 | 13 | #define RF_FREQUENCY 868000000 // Hz |
guillouhouede | 0:fa014443db91 | 14 | #define TX_OUTPUT_POWER 14 // 14 dBm |
guillouhouede | 0:fa014443db91 | 15 | |
guillouhouede | 0:fa014443db91 | 16 | #if USE_MODEM_LORA == 1 |
guillouhouede | 0:fa014443db91 | 17 | |
guillouhouede | 0:fa014443db91 | 18 | #define LORA_BANDWIDTH 2 // [0: 125 kHz, |
guillouhouede | 0:fa014443db91 | 19 | // 1: 250 kHz, |
guillouhouede | 0:fa014443db91 | 20 | // 2: 500 kHz, |
guillouhouede | 0:fa014443db91 | 21 | // 3: Reserved] |
guillouhouede | 0:fa014443db91 | 22 | #define LORA_SPREADING_FACTOR 7 // [SF7..SF12] |
guillouhouede | 0:fa014443db91 | 23 | #define LORA_CODINGRATE 1 // [1: 4/5, |
guillouhouede | 0:fa014443db91 | 24 | // 2: 4/6, |
guillouhouede | 0:fa014443db91 | 25 | // 3: 4/7, |
guillouhouede | 0:fa014443db91 | 26 | // 4: 4/8] |
guillouhouede | 0:fa014443db91 | 27 | #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx |
guillouhouede | 0:fa014443db91 | 28 | #define LORA_SYMBOL_TIMEOUT 5 // Symbols |
guillouhouede | 0:fa014443db91 | 29 | #define LORA_FIX_LENGTH_PAYLOAD_ON false |
guillouhouede | 0:fa014443db91 | 30 | #define LORA_FHSS_ENABLED false |
guillouhouede | 0:fa014443db91 | 31 | #define LORA_NB_SYMB_HOP 4 |
guillouhouede | 0:fa014443db91 | 32 | #define LORA_IQ_INVERSION_ON false |
guillouhouede | 0:fa014443db91 | 33 | #define LORA_CRC_ENABLED true |
guillouhouede | 0:fa014443db91 | 34 | |
guillouhouede | 0:fa014443db91 | 35 | #elif USE_MODEM_FSK == 1 |
guillouhouede | 0:fa014443db91 | 36 | |
guillouhouede | 0:fa014443db91 | 37 | #define FSK_FDEV 25000 // Hz |
guillouhouede | 0:fa014443db91 | 38 | #define FSK_DATARATE 19200 // bps |
guillouhouede | 0:fa014443db91 | 39 | #define FSK_BANDWIDTH 50000 // Hz |
guillouhouede | 0:fa014443db91 | 40 | #define FSK_AFC_BANDWIDTH 83333 // Hz |
guillouhouede | 0:fa014443db91 | 41 | #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx |
guillouhouede | 0:fa014443db91 | 42 | #define FSK_FIX_LENGTH_PAYLOAD_ON false |
guillouhouede | 0:fa014443db91 | 43 | #define FSK_CRC_ENABLED true |
guillouhouede | 0:fa014443db91 | 44 | |
guillouhouede | 0:fa014443db91 | 45 | #else |
guillouhouede | 0:fa014443db91 | 46 | #error "Please define a modem in the compiler options." |
guillouhouede | 0:fa014443db91 | 47 | #endif |
guillouhouede | 0:fa014443db91 | 48 | |
guillouhouede | 0:fa014443db91 | 49 | #define RX_TIMEOUT_VALUE 3500000 // in us |
guillouhouede | 0:fa014443db91 | 50 | #define BUFFER_SIZE 32 // Define the payload size here |
guillouhouede | 0:fa014443db91 | 51 | |
guillouhouede | 0:fa014443db91 | 52 | #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) |
guillouhouede | 0:fa014443db91 | 53 | DigitalOut led(LED2); |
guillouhouede | 0:fa014443db91 | 54 | #else |
guillouhouede | 0:fa014443db91 | 55 | DigitalOut led(LED1); |
guillouhouede | 0:fa014443db91 | 56 | #endif |
guillouhouede | 0:fa014443db91 | 57 | |
guillouhouede | 0:fa014443db91 | 58 | //Jonathan |
guillouhouede | 0:fa014443db91 | 59 | |
guillouhouede | 0:fa014443db91 | 60 | |
guillouhouede | 0:fa014443db91 | 61 | // |
guillouhouede | 0:fa014443db91 | 62 | /* |
guillouhouede | 0:fa014443db91 | 63 | * Global variables declarations |
guillouhouede | 0:fa014443db91 | 64 | */ |
guillouhouede | 0:fa014443db91 | 65 | typedef enum |
guillouhouede | 0:fa014443db91 | 66 | { |
guillouhouede | 0:fa014443db91 | 67 | LOWPOWER = 0, |
guillouhouede | 0:fa014443db91 | 68 | IDLE, |
guillouhouede | 0:fa014443db91 | 69 | |
guillouhouede | 0:fa014443db91 | 70 | RX, |
guillouhouede | 0:fa014443db91 | 71 | RX_TIMEOUT, |
guillouhouede | 0:fa014443db91 | 72 | RX_ERROR, |
guillouhouede | 0:fa014443db91 | 73 | |
guillouhouede | 0:fa014443db91 | 74 | TX, |
guillouhouede | 0:fa014443db91 | 75 | TX_TIMEOUT, |
guillouhouede | 0:fa014443db91 | 76 | |
guillouhouede | 0:fa014443db91 | 77 | CAD, |
guillouhouede | 0:fa014443db91 | 78 | CAD_DONE |
guillouhouede | 0:fa014443db91 | 79 | }AppStates_t; |
guillouhouede | 0:fa014443db91 | 80 | |
guillouhouede | 0:fa014443db91 | 81 | volatile AppStates_t State = LOWPOWER; |
guillouhouede | 0:fa014443db91 | 82 | |
guillouhouede | 0:fa014443db91 | 83 | /*! |
guillouhouede | 0:fa014443db91 | 84 | * Radio events function pointer |
guillouhouede | 0:fa014443db91 | 85 | */ |
guillouhouede | 0:fa014443db91 | 86 | static RadioEvents_t RadioEvents; |
guillouhouede | 0:fa014443db91 | 87 | |
guillouhouede | 0:fa014443db91 | 88 | /* |
guillouhouede | 0:fa014443db91 | 89 | * Global variables declarations |
guillouhouede | 0:fa014443db91 | 90 | */ |
guillouhouede | 0:fa014443db91 | 91 | SX1272MB2xAS Radio( NULL ); |
guillouhouede | 0:fa014443db91 | 92 | |
guillouhouede | 0:fa014443db91 | 93 | const uint8_t PingMsg[] = "PING"; |
guillouhouede | 0:fa014443db91 | 94 | const uint8_t PongMsg[] = "PONG"; |
guillouhouede | 0:fa014443db91 | 95 | |
guillouhouede | 0:fa014443db91 | 96 | /*const uint8_t PingMsg[] = 42; |
guillouhouede | 0:fa014443db91 | 97 | const uint8_t PongMsg[] = "Message reçu !";*/ |
guillouhouede | 0:fa014443db91 | 98 | |
guillouhouede | 0:fa014443db91 | 99 | uint16_t BufferSize = BUFFER_SIZE; |
guillouhouede | 0:fa014443db91 | 100 | uint8_t Buffer[BUFFER_SIZE]; |
guillouhouede | 0:fa014443db91 | 101 | |
guillouhouede | 0:fa014443db91 | 102 | int16_t RssiValue = 0.0; |
guillouhouede | 0:fa014443db91 | 103 | int8_t SnrValue = 0.0; |
guillouhouede | 0:fa014443db91 | 104 | |
guillouhouede | 0:fa014443db91 | 105 | |
guillouhouede | 0:fa014443db91 | 106 | |
guillouhouede | 0:fa014443db91 | 107 | |
guillouhouede | 0:fa014443db91 | 108 | int main() |
guillouhouede | 0:fa014443db91 | 109 | { |
guillouhouede | 0:fa014443db91 | 110 | uint8_t i; |
guillouhouede | 0:fa014443db91 | 111 | bool isMaster = true; |
guillouhouede | 0:fa014443db91 | 112 | |
guillouhouede | 0:fa014443db91 | 113 | bool channel; |
guillouhouede | 0:fa014443db91 | 114 | |
guillouhouede | 0:fa014443db91 | 115 | channel = Radio.IsChannelFree(MODEM_LORA,9000000,RssiValue); |
guillouhouede | 0:fa014443db91 | 116 | debug("le channel est %d\n",channel); |
guillouhouede | 0:fa014443db91 | 117 | |
guillouhouede | 0:fa014443db91 | 118 | debug( "\n\n\r SX1272 Ping Pong Demo Application \n\n\r" ); |
guillouhouede | 0:fa014443db91 | 119 | |
guillouhouede | 0:fa014443db91 | 120 | // Initialize Radio driver |
guillouhouede | 0:fa014443db91 | 121 | RadioEvents.TxDone = OnTxDone; |
guillouhouede | 0:fa014443db91 | 122 | RadioEvents.RxDone = OnRxDone; |
guillouhouede | 0:fa014443db91 | 123 | RadioEvents.RxError = OnRxError; |
guillouhouede | 0:fa014443db91 | 124 | RadioEvents.TxTimeout = OnTxTimeout; |
guillouhouede | 0:fa014443db91 | 125 | RadioEvents.RxTimeout = OnRxTimeout; |
guillouhouede | 0:fa014443db91 | 126 | Radio.Init( &RadioEvents ); |
guillouhouede | 0:fa014443db91 | 127 | |
guillouhouede | 0:fa014443db91 | 128 | |
guillouhouede | 0:fa014443db91 | 129 | |
guillouhouede | 0:fa014443db91 | 130 | |
guillouhouede | 0:fa014443db91 | 131 | // |
guillouhouede | 0:fa014443db91 | 132 | |
guillouhouede | 0:fa014443db91 | 133 | // verify the connection with the board |
guillouhouede | 0:fa014443db91 | 134 | while( Radio.Read( REG_VERSION ) == 0x00 ) |
guillouhouede | 0:fa014443db91 | 135 | { |
guillouhouede | 0:fa014443db91 | 136 | debug( "Radio could not be detected!\n\r", NULL ); |
guillouhouede | 0:fa014443db91 | 137 | wait( 1 ); |
guillouhouede | 0:fa014443db91 | 138 | } |
guillouhouede | 0:fa014443db91 | 139 | |
guillouhouede | 0:fa014443db91 | 140 | debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ) , "\n\r > Board Type: SX1272MB2xAS < \n\r" ); |
guillouhouede | 0:fa014443db91 | 141 | |
guillouhouede | 0:fa014443db91 | 142 | Radio.SetChannel( RF_FREQUENCY ); |
guillouhouede | 0:fa014443db91 | 143 | |
guillouhouede | 0:fa014443db91 | 144 | #if USE_MODEM_LORA == 1 |
guillouhouede | 0:fa014443db91 | 145 | |
guillouhouede | 0:fa014443db91 | 146 | debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r"); |
guillouhouede | 0:fa014443db91 | 147 | debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r"); |
guillouhouede | 0:fa014443db91 | 148 | |
guillouhouede | 0:fa014443db91 | 149 | Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, |
guillouhouede | 0:fa014443db91 | 150 | LORA_SPREADING_FACTOR, LORA_CODINGRATE, |
guillouhouede | 0:fa014443db91 | 151 | LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, |
guillouhouede | 0:fa014443db91 | 152 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
guillouhouede | 0:fa014443db91 | 153 | LORA_IQ_INVERSION_ON, 2000000 ); |
guillouhouede | 0:fa014443db91 | 154 | |
guillouhouede | 0:fa014443db91 | 155 | Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, |
guillouhouede | 0:fa014443db91 | 156 | LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, |
guillouhouede | 0:fa014443db91 | 157 | LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, |
guillouhouede | 0:fa014443db91 | 158 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
guillouhouede | 0:fa014443db91 | 159 | LORA_IQ_INVERSION_ON, true ); |
guillouhouede | 0:fa014443db91 | 160 | |
guillouhouede | 0:fa014443db91 | 161 | #elif USE_MODEM_FSK == 1 |
guillouhouede | 0:fa014443db91 | 162 | |
guillouhouede | 0:fa014443db91 | 163 | debug("\n\n\r > FSK Mode < \n\n\r"); |
guillouhouede | 0:fa014443db91 | 164 | Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, |
guillouhouede | 0:fa014443db91 | 165 | FSK_DATARATE, 0, |
guillouhouede | 0:fa014443db91 | 166 | FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, |
guillouhouede | 0:fa014443db91 | 167 | FSK_CRC_ENABLED, 0, 0, 0, 2000000 ); |
guillouhouede | 0:fa014443db91 | 168 | |
guillouhouede | 0:fa014443db91 | 169 | Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, |
guillouhouede | 0:fa014443db91 | 170 | 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, |
guillouhouede | 0:fa014443db91 | 171 | 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED, |
guillouhouede | 0:fa014443db91 | 172 | 0, 0, false, true ); |
guillouhouede | 0:fa014443db91 | 173 | |
guillouhouede | 0:fa014443db91 | 174 | #else |
guillouhouede | 0:fa014443db91 | 175 | |
guillouhouede | 0:fa014443db91 | 176 | #error "Please define a modem in the compiler options." |
guillouhouede | 0:fa014443db91 | 177 | |
guillouhouede | 0:fa014443db91 | 178 | #endif |
guillouhouede | 0:fa014443db91 | 179 | |
guillouhouede | 0:fa014443db91 | 180 | debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" ); |
guillouhouede | 0:fa014443db91 | 181 | |
guillouhouede | 0:fa014443db91 | 182 | led = 0; |
guillouhouede | 0:fa014443db91 | 183 | |
guillouhouede | 0:fa014443db91 | 184 | Radio.Rx( RX_TIMEOUT_VALUE ); |
guillouhouede | 0:fa014443db91 | 185 | uint8_t val_res[4]; |
guillouhouede | 0:fa014443db91 | 186 | uint8_t val_condu[4]; |
guillouhouede | 0:fa014443db91 | 187 | uint8_t val_tempe[4]; |
guillouhouede | 0:fa014443db91 | 188 | uint8_t val_humi[4]; |
guillouhouede | 0:fa014443db91 | 189 | float somme; |
guillouhouede | 0:fa014443db91 | 190 | float somme_condu; |
guillouhouede | 0:fa014443db91 | 191 | float somme_tempe; |
guillouhouede | 0:fa014443db91 | 192 | float somme_humi; |
guillouhouede | 0:fa014443db91 | 193 | wait(1); |
guillouhouede | 0:fa014443db91 | 194 | //on remplit le buffer a 0 |
guillouhouede | 0:fa014443db91 | 195 | for( i = 0; i < BufferSize; i++ ) |
guillouhouede | 0:fa014443db91 | 196 | { |
guillouhouede | 0:fa014443db91 | 197 | Buffer[i] =0; |
guillouhouede | 0:fa014443db91 | 198 | } |
guillouhouede | 0:fa014443db91 | 199 | |
guillouhouede | 0:fa014443db91 | 200 | while( 1 ) |
guillouhouede | 0:fa014443db91 | 201 | { |
guillouhouede | 0:fa014443db91 | 202 | wait(2); |
guillouhouede | 0:fa014443db91 | 203 | switch( State ) |
guillouhouede | 0:fa014443db91 | 204 | { |
guillouhouede | 0:fa014443db91 | 205 | case RX: |
guillouhouede | 0:fa014443db91 | 206 | if( isMaster == true ) |
guillouhouede | 0:fa014443db91 | 207 | { |
guillouhouede | 0:fa014443db91 | 208 | if( BufferSize > 0 ) |
guillouhouede | 0:fa014443db91 | 209 | { |
guillouhouede | 0:fa014443db91 | 210 | if( Buffer[1] == 0 and Buffer[3]==0) |
guillouhouede | 0:fa014443db91 | 211 | { |
guillouhouede | 0:fa014443db91 | 212 | led = !led; |
guillouhouede | 0:fa014443db91 | 213 | debug( "\r\n" ); |
guillouhouede | 0:fa014443db91 | 214 | Buffer[0]=1; |
guillouhouede | 0:fa014443db91 | 215 | |
guillouhouede | 0:fa014443db91 | 216 | wait_ms( 1000 ); |
guillouhouede | 0:fa014443db91 | 217 | Radio.Send( Buffer, BufferSize ); |
guillouhouede | 0:fa014443db91 | 218 | } |
guillouhouede | 0:fa014443db91 | 219 | else if(Buffer[1]==1 and Buffer[3]==0) |
guillouhouede | 0:fa014443db91 | 220 | { // A master already exists then become a slave |
guillouhouede | 0:fa014443db91 | 221 | debug( "On recoit les valeurs de A\r\n" ); |
guillouhouede | 0:fa014443db91 | 222 | |
guillouhouede | 0:fa014443db91 | 223 | led = !led; |
guillouhouede | 0:fa014443db91 | 224 | //isMaster = false; |
guillouhouede | 0:fa014443db91 | 225 | // Send the next PONG frame |
guillouhouede | 0:fa014443db91 | 226 | strcpy( ( char* )Buffer, ( char* )PongMsg ); |
guillouhouede | 0:fa014443db91 | 227 | //debug("buf 0 %d\n",Buffer[0]); |
guillouhouede | 0:fa014443db91 | 228 | val_res[0]=Buffer[4]; |
guillouhouede | 0:fa014443db91 | 229 | val_res[1]=Buffer[5]; |
guillouhouede | 0:fa014443db91 | 230 | val_res[2]=Buffer[6]; |
guillouhouede | 0:fa014443db91 | 231 | val_res[3]=Buffer[7]; |
guillouhouede | 0:fa014443db91 | 232 | somme=*( (float*)(val_res )); |
guillouhouede | 0:fa014443db91 | 233 | //debug("Val res 0 vaut dans RX : %d\n",val_res[0]); |
guillouhouede | 0:fa014443db91 | 234 | //debug("somme vaut : %f\n",somme); |
guillouhouede | 0:fa014443db91 | 235 | val_condu[0]=Buffer[8]; |
guillouhouede | 0:fa014443db91 | 236 | val_condu[1]=Buffer[9]; |
guillouhouede | 0:fa014443db91 | 237 | val_condu[2]=Buffer[10]; |
guillouhouede | 0:fa014443db91 | 238 | val_condu[3]=Buffer[11]; |
guillouhouede | 0:fa014443db91 | 239 | somme_condu=*( (float*)(val_condu )); |
guillouhouede | 0:fa014443db91 | 240 | |
guillouhouede | 0:fa014443db91 | 241 | val_tempe[0]=Buffer[12]; |
guillouhouede | 0:fa014443db91 | 242 | val_tempe[1]=Buffer[13]; |
guillouhouede | 0:fa014443db91 | 243 | val_tempe[2]=Buffer[14]; |
guillouhouede | 0:fa014443db91 | 244 | val_tempe[3]=Buffer[15]; |
guillouhouede | 0:fa014443db91 | 245 | somme_tempe=*( (float*)(val_tempe )); |
guillouhouede | 0:fa014443db91 | 246 | |
guillouhouede | 0:fa014443db91 | 247 | val_humi[0]=Buffer[16]; |
guillouhouede | 0:fa014443db91 | 248 | val_humi[1]=Buffer[17]; |
guillouhouede | 0:fa014443db91 | 249 | val_humi[2]=Buffer[18]; |
guillouhouede | 0:fa014443db91 | 250 | val_humi[3]=Buffer[19]; |
guillouhouede | 0:fa014443db91 | 251 | somme_humi=*( (float*)(val_humi )); |
guillouhouede | 0:fa014443db91 | 252 | // We fill the buffer with numbers for the payload |
guillouhouede | 0:fa014443db91 | 253 | for( i = 20; i < BufferSize; i++ ) |
guillouhouede | 0:fa014443db91 | 254 | { |
guillouhouede | 0:fa014443db91 | 255 | Buffer[i] = i - 20; |
guillouhouede | 0:fa014443db91 | 256 | } |
guillouhouede | 0:fa014443db91 | 257 | Buffer[0]=0; |
guillouhouede | 0:fa014443db91 | 258 | Buffer[2]=1; |
guillouhouede | 0:fa014443db91 | 259 | somme=somme/10000; |
guillouhouede | 0:fa014443db91 | 260 | somme_tempe=somme_tempe/100; |
guillouhouede | 0:fa014443db91 | 261 | somme_humi=somme_humi/1000; |
guillouhouede | 0:fa014443db91 | 262 | debug("A %f R,%f C,%f T,%f H\n",somme,somme_condu,somme_tempe,somme_humi); |
guillouhouede | 0:fa014443db91 | 263 | wait_ms( 1000 ); |
guillouhouede | 0:fa014443db91 | 264 | |
guillouhouede | 0:fa014443db91 | 265 | Radio.Send( Buffer, BufferSize ); |
guillouhouede | 0:fa014443db91 | 266 | //wait(10); |
guillouhouede | 0:fa014443db91 | 267 | } |
guillouhouede | 0:fa014443db91 | 268 | else if(Buffer[3]==1 and Buffer[1]==0) |
guillouhouede | 0:fa014443db91 | 269 | { // A master already exists then become a slave |
guillouhouede | 0:fa014443db91 | 270 | debug( "On recoit les valeurs de A\r\n" ); |
guillouhouede | 0:fa014443db91 | 271 | |
guillouhouede | 0:fa014443db91 | 272 | led = !led; |
guillouhouede | 0:fa014443db91 | 273 | //isMaster = false; |
guillouhouede | 0:fa014443db91 | 274 | // Send the next PONG frame |
guillouhouede | 0:fa014443db91 | 275 | strcpy( ( char* )Buffer, ( char* )PongMsg ); |
guillouhouede | 0:fa014443db91 | 276 | //debug("buf 0 %d\n",Buffer[0]); |
guillouhouede | 0:fa014443db91 | 277 | val_res[0]=Buffer[4]; |
guillouhouede | 0:fa014443db91 | 278 | val_res[1]=Buffer[5]; |
guillouhouede | 0:fa014443db91 | 279 | val_res[2]=Buffer[6]; |
guillouhouede | 0:fa014443db91 | 280 | val_res[3]=Buffer[7]; |
guillouhouede | 0:fa014443db91 | 281 | somme=*( (float*)(val_res )); |
guillouhouede | 0:fa014443db91 | 282 | //debug("Val res 0 vaut dans RX : %d\n",val_res[0]); |
guillouhouede | 0:fa014443db91 | 283 | //debug("somme vaut : %f\n",somme); |
guillouhouede | 0:fa014443db91 | 284 | val_condu[0]=Buffer[8]; |
guillouhouede | 0:fa014443db91 | 285 | val_condu[1]=Buffer[9]; |
guillouhouede | 0:fa014443db91 | 286 | val_condu[2]=Buffer[10]; |
guillouhouede | 0:fa014443db91 | 287 | val_condu[3]=Buffer[11]; |
guillouhouede | 0:fa014443db91 | 288 | somme_condu=*( (float*)(val_condu )); |
guillouhouede | 0:fa014443db91 | 289 | |
guillouhouede | 0:fa014443db91 | 290 | val_tempe[0]=Buffer[12]; |
guillouhouede | 0:fa014443db91 | 291 | val_tempe[1]=Buffer[13]; |
guillouhouede | 0:fa014443db91 | 292 | val_tempe[2]=Buffer[14]; |
guillouhouede | 0:fa014443db91 | 293 | val_tempe[3]=Buffer[15]; |
guillouhouede | 0:fa014443db91 | 294 | somme_tempe=*( (float*)(val_tempe )); |
guillouhouede | 0:fa014443db91 | 295 | |
guillouhouede | 0:fa014443db91 | 296 | val_humi[0]=Buffer[16]; |
guillouhouede | 0:fa014443db91 | 297 | val_humi[1]=Buffer[17]; |
guillouhouede | 0:fa014443db91 | 298 | val_humi[2]=Buffer[18]; |
guillouhouede | 0:fa014443db91 | 299 | val_humi[3]=Buffer[19]; |
guillouhouede | 0:fa014443db91 | 300 | somme_humi=*( (float*)(val_humi )); |
guillouhouede | 0:fa014443db91 | 301 | // We fill the buffer with numbers for the payload |
guillouhouede | 0:fa014443db91 | 302 | for( i = 20; i < BufferSize; i++ ) |
guillouhouede | 0:fa014443db91 | 303 | { |
guillouhouede | 0:fa014443db91 | 304 | Buffer[i] = i - 20; |
guillouhouede | 0:fa014443db91 | 305 | } |
guillouhouede | 0:fa014443db91 | 306 | Buffer[2]=0; |
guillouhouede | 0:fa014443db91 | 307 | Buffer[0]=1; |
guillouhouede | 0:fa014443db91 | 308 | somme=somme/10000; |
guillouhouede | 0:fa014443db91 | 309 | somme_tempe=somme_tempe/100; |
guillouhouede | 0:fa014443db91 | 310 | somme_humi=somme_humi/1000; |
guillouhouede | 0:fa014443db91 | 311 | debug("B %f R,%f C,%f T,%f H\n",somme,somme_condu,somme_tempe,somme_humi); |
guillouhouede | 0:fa014443db91 | 312 | wait_ms( 1000 ); |
guillouhouede | 0:fa014443db91 | 313 | |
guillouhouede | 0:fa014443db91 | 314 | Radio.Send( Buffer, BufferSize ); |
guillouhouede | 0:fa014443db91 | 315 | //wait(10); |
guillouhouede | 0:fa014443db91 | 316 | } |
guillouhouede | 0:fa014443db91 | 317 | else if(Buffer[1]==1 and Buffer[3]==1) |
guillouhouede | 0:fa014443db91 | 318 | { // A master already exists then become a slave |
guillouhouede | 0:fa014443db91 | 319 | debug( "Les deux sont a 1\r\n" ); |
guillouhouede | 0:fa014443db91 | 320 | |
guillouhouede | 0:fa014443db91 | 321 | led = !led; |
guillouhouede | 0:fa014443db91 | 322 | //isMaster = false; |
guillouhouede | 0:fa014443db91 | 323 | // Send the next PONG frame |
guillouhouede | 0:fa014443db91 | 324 | Buffer[1]=0; |
guillouhouede | 0:fa014443db91 | 325 | Buffer[2]=0; |
guillouhouede | 0:fa014443db91 | 326 | Buffer[0]=1; |
guillouhouede | 0:fa014443db91 | 327 | Buffer[3]=0; |
guillouhouede | 0:fa014443db91 | 328 | // We fill the buffer with numbers for the payload |
guillouhouede | 0:fa014443db91 | 329 | for( i = 20; i < BufferSize; i++ ) |
guillouhouede | 0:fa014443db91 | 330 | { |
guillouhouede | 0:fa014443db91 | 331 | Buffer[i] = i - 20; |
guillouhouede | 0:fa014443db91 | 332 | } |
guillouhouede | 0:fa014443db91 | 333 | wait_ms( 1000 ); |
guillouhouede | 0:fa014443db91 | 334 | |
guillouhouede | 0:fa014443db91 | 335 | Radio.Send( Buffer, BufferSize ); |
guillouhouede | 0:fa014443db91 | 336 | //wait(10); |
guillouhouede | 0:fa014443db91 | 337 | } |
guillouhouede | 0:fa014443db91 | 338 | else // valid reception but neither a PING or a PONG message |
guillouhouede | 0:fa014443db91 | 339 | { // Set device as master ans start again |
guillouhouede | 0:fa014443db91 | 340 | isMaster = true; |
guillouhouede | 0:fa014443db91 | 341 | Radio.Rx( RX_TIMEOUT_VALUE ); |
guillouhouede | 0:fa014443db91 | 342 | } |
guillouhouede | 0:fa014443db91 | 343 | } |
guillouhouede | 0:fa014443db91 | 344 | } |
guillouhouede | 0:fa014443db91 | 345 | else |
guillouhouede | 0:fa014443db91 | 346 | { |
guillouhouede | 0:fa014443db91 | 347 | |
guillouhouede | 0:fa014443db91 | 348 | } |
guillouhouede | 0:fa014443db91 | 349 | State = LOWPOWER; |
guillouhouede | 0:fa014443db91 | 350 | break; |
guillouhouede | 0:fa014443db91 | 351 | case TX: |
guillouhouede | 0:fa014443db91 | 352 | led = !led; |
guillouhouede | 0:fa014443db91 | 353 | //uint8_t val_res; |
guillouhouede | 0:fa014443db91 | 354 | int i; |
guillouhouede | 0:fa014443db91 | 355 | if( isMaster == true ) |
guillouhouede | 0:fa014443db91 | 356 | { |
guillouhouede | 0:fa014443db91 | 357 | debug( "Tx envoi\r\n" ); |
guillouhouede | 0:fa014443db91 | 358 | |
guillouhouede | 0:fa014443db91 | 359 | } |
guillouhouede | 0:fa014443db91 | 360 | else |
guillouhouede | 0:fa014443db91 | 361 | { |
guillouhouede | 0:fa014443db91 | 362 | debug( "Pong...\r\n" ); |
guillouhouede | 0:fa014443db91 | 363 | |
guillouhouede | 0:fa014443db91 | 364 | } |
guillouhouede | 0:fa014443db91 | 365 | Radio.Rx( RX_TIMEOUT_VALUE ); |
guillouhouede | 0:fa014443db91 | 366 | State = LOWPOWER; |
guillouhouede | 0:fa014443db91 | 367 | break; |
guillouhouede | 0:fa014443db91 | 368 | case RX_TIMEOUT: |
guillouhouede | 0:fa014443db91 | 369 | if( isMaster == true ) |
guillouhouede | 0:fa014443db91 | 370 | { |
guillouhouede | 0:fa014443db91 | 371 | // Send the next PING frame |
guillouhouede | 0:fa014443db91 | 372 | //strcpy( ( char* )Buffer, ( char* )PingMsg ); |
guillouhouede | 0:fa014443db91 | 373 | if(Buffer[1]==1){ |
guillouhouede | 0:fa014443db91 | 374 | val_res[0]=Buffer[4]; |
guillouhouede | 0:fa014443db91 | 375 | val_res[1]=Buffer[5]; |
guillouhouede | 0:fa014443db91 | 376 | val_res[2]=Buffer[6]; |
guillouhouede | 0:fa014443db91 | 377 | val_res[3]=Buffer[7]; |
guillouhouede | 0:fa014443db91 | 378 | somme=*( (float*)(val_res )); |
guillouhouede | 0:fa014443db91 | 379 | |
guillouhouede | 0:fa014443db91 | 380 | val_condu[0]=Buffer[8]; |
guillouhouede | 0:fa014443db91 | 381 | val_condu[1]=Buffer[9]; |
guillouhouede | 0:fa014443db91 | 382 | val_condu[2]=Buffer[10]; |
guillouhouede | 0:fa014443db91 | 383 | val_condu[3]=Buffer[11]; |
guillouhouede | 0:fa014443db91 | 384 | somme_condu=*( (float*)(val_condu )); |
guillouhouede | 0:fa014443db91 | 385 | |
guillouhouede | 0:fa014443db91 | 386 | val_tempe[0]=Buffer[12]; |
guillouhouede | 0:fa014443db91 | 387 | val_tempe[1]=Buffer[13]; |
guillouhouede | 0:fa014443db91 | 388 | val_tempe[2]=Buffer[14]; |
guillouhouede | 0:fa014443db91 | 389 | val_tempe[3]=Buffer[15]; |
guillouhouede | 0:fa014443db91 | 390 | somme_tempe=*( (float*)(val_tempe )); |
guillouhouede | 0:fa014443db91 | 391 | |
guillouhouede | 0:fa014443db91 | 392 | val_humi[0]=Buffer[16]; |
guillouhouede | 0:fa014443db91 | 393 | val_humi[1]=Buffer[17]; |
guillouhouede | 0:fa014443db91 | 394 | val_humi[2]=Buffer[18]; |
guillouhouede | 0:fa014443db91 | 395 | val_humi[3]=Buffer[19]; |
guillouhouede | 0:fa014443db91 | 396 | somme_humi=*( (float*)(val_humi )); |
guillouhouede | 0:fa014443db91 | 397 | debug("A %f R,%f C,%f T,%f H\n",somme,somme_condu,somme_tempe,somme_humi); |
guillouhouede | 0:fa014443db91 | 398 | } |
guillouhouede | 0:fa014443db91 | 399 | else{ |
guillouhouede | 0:fa014443db91 | 400 | Buffer[0]=1; |
guillouhouede | 0:fa014443db91 | 401 | } |
guillouhouede | 0:fa014443db91 | 402 | for( i = 20; i < BufferSize; i++ ) |
guillouhouede | 0:fa014443db91 | 403 | { |
guillouhouede | 0:fa014443db91 | 404 | Buffer[i] = i - 20; |
guillouhouede | 0:fa014443db91 | 405 | } |
guillouhouede | 0:fa014443db91 | 406 | wait_ms( 1000 ); |
guillouhouede | 0:fa014443db91 | 407 | Radio.Send( Buffer, BufferSize ); |
guillouhouede | 0:fa014443db91 | 408 | } |
guillouhouede | 0:fa014443db91 | 409 | else |
guillouhouede | 0:fa014443db91 | 410 | { |
guillouhouede | 0:fa014443db91 | 411 | Radio.Rx( RX_TIMEOUT_VALUE ); |
guillouhouede | 0:fa014443db91 | 412 | } |
guillouhouede | 0:fa014443db91 | 413 | State = LOWPOWER; |
guillouhouede | 0:fa014443db91 | 414 | break; |
guillouhouede | 0:fa014443db91 | 415 | case RX_ERROR: |
guillouhouede | 0:fa014443db91 | 416 | // We have received a Packet with a CRC error, send reply as if packet was correct |
guillouhouede | 0:fa014443db91 | 417 | if( isMaster == true ) |
guillouhouede | 0:fa014443db91 | 418 | { |
guillouhouede | 0:fa014443db91 | 419 | // Send the next PING frame |
guillouhouede | 0:fa014443db91 | 420 | // strcpy( ( char* )Buffer, ( char* )PingMsg ); |
guillouhouede | 0:fa014443db91 | 421 | for( i = 0; i < BufferSize; i++ ) |
guillouhouede | 0:fa014443db91 | 422 | { |
guillouhouede | 0:fa014443db91 | 423 | Buffer[i] = 0; |
guillouhouede | 0:fa014443db91 | 424 | } |
guillouhouede | 0:fa014443db91 | 425 | wait_ms( 1000 ); |
guillouhouede | 0:fa014443db91 | 426 | Radio.Send( Buffer, BufferSize ); |
guillouhouede | 0:fa014443db91 | 427 | } |
guillouhouede | 0:fa014443db91 | 428 | else |
guillouhouede | 0:fa014443db91 | 429 | { |
guillouhouede | 0:fa014443db91 | 430 | // Send the next PONG frame |
guillouhouede | 0:fa014443db91 | 431 | strcpy( ( char* )Buffer, ( char* )PongMsg ); |
guillouhouede | 0:fa014443db91 | 432 | for( i = 4; i < BufferSize; i++ ) |
guillouhouede | 0:fa014443db91 | 433 | { |
guillouhouede | 0:fa014443db91 | 434 | Buffer[i] = i - 4; |
guillouhouede | 0:fa014443db91 | 435 | } |
guillouhouede | 0:fa014443db91 | 436 | wait_ms( 1000 ); |
guillouhouede | 0:fa014443db91 | 437 | Radio.Send( Buffer, BufferSize ); |
guillouhouede | 0:fa014443db91 | 438 | } |
guillouhouede | 0:fa014443db91 | 439 | State = LOWPOWER; |
guillouhouede | 0:fa014443db91 | 440 | break; |
guillouhouede | 0:fa014443db91 | 441 | case TX_TIMEOUT: |
guillouhouede | 0:fa014443db91 | 442 | Radio.Rx( RX_TIMEOUT_VALUE ); |
guillouhouede | 0:fa014443db91 | 443 | State = LOWPOWER; |
guillouhouede | 0:fa014443db91 | 444 | break; |
guillouhouede | 0:fa014443db91 | 445 | case LOWPOWER: |
guillouhouede | 0:fa014443db91 | 446 | break; |
guillouhouede | 0:fa014443db91 | 447 | default: |
guillouhouede | 0:fa014443db91 | 448 | State = LOWPOWER; |
guillouhouede | 0:fa014443db91 | 449 | break; |
guillouhouede | 0:fa014443db91 | 450 | } |
guillouhouede | 0:fa014443db91 | 451 | //wait(2); |
guillouhouede | 0:fa014443db91 | 452 | } |
guillouhouede | 0:fa014443db91 | 453 | } |
guillouhouede | 0:fa014443db91 | 454 | |
guillouhouede | 0:fa014443db91 | 455 | void OnTxDone( void ) |
guillouhouede | 0:fa014443db91 | 456 | { |
guillouhouede | 0:fa014443db91 | 457 | Radio.Sleep( ); |
guillouhouede | 0:fa014443db91 | 458 | State = TX; |
guillouhouede | 0:fa014443db91 | 459 | debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" ); |
guillouhouede | 0:fa014443db91 | 460 | } |
guillouhouede | 0:fa014443db91 | 461 | |
guillouhouede | 0:fa014443db91 | 462 | void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) |
guillouhouede | 0:fa014443db91 | 463 | { |
guillouhouede | 0:fa014443db91 | 464 | Radio.Sleep( ); |
guillouhouede | 0:fa014443db91 | 465 | BufferSize = size; |
guillouhouede | 0:fa014443db91 | 466 | memcpy( Buffer, payload, BufferSize ); |
guillouhouede | 0:fa014443db91 | 467 | RssiValue = rssi; |
guillouhouede | 0:fa014443db91 | 468 | SnrValue = snr; |
guillouhouede | 0:fa014443db91 | 469 | State = RX; |
guillouhouede | 0:fa014443db91 | 470 | debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" ); |
guillouhouede | 0:fa014443db91 | 471 | } |
guillouhouede | 0:fa014443db91 | 472 | |
guillouhouede | 0:fa014443db91 | 473 | void OnTxTimeout( void ) |
guillouhouede | 0:fa014443db91 | 474 | { |
guillouhouede | 0:fa014443db91 | 475 | Radio.Sleep( ); |
guillouhouede | 0:fa014443db91 | 476 | State = TX_TIMEOUT; |
guillouhouede | 0:fa014443db91 | 477 | debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" ); |
guillouhouede | 0:fa014443db91 | 478 | } |
guillouhouede | 0:fa014443db91 | 479 | |
guillouhouede | 0:fa014443db91 | 480 | void OnRxTimeout( void ) |
guillouhouede | 0:fa014443db91 | 481 | { |
guillouhouede | 0:fa014443db91 | 482 | Radio.Sleep( ); |
guillouhouede | 0:fa014443db91 | 483 | Buffer[ BufferSize ] = 0; |
guillouhouede | 0:fa014443db91 | 484 | State = RX_TIMEOUT; |
guillouhouede | 0:fa014443db91 | 485 | debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" ); |
guillouhouede | 0:fa014443db91 | 486 | } |
guillouhouede | 0:fa014443db91 | 487 | |
guillouhouede | 0:fa014443db91 | 488 | void OnRxError( void ) |
guillouhouede | 0:fa014443db91 | 489 | { |
guillouhouede | 0:fa014443db91 | 490 | Radio.Sleep( ); |
guillouhouede | 0:fa014443db91 | 491 | State = RX_ERROR; |
guillouhouede | 0:fa014443db91 | 492 | debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" ); |
guillouhouede | 0:fa014443db91 | 493 | } |
guillouhouede | 0:fa014443db91 | 494 |