![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
programme pour envoyer les données du capteur B
Dependencies: DHT SX1272Lib WakeUp mbed-src mbed
main.cpp@0:5a4281204073, 2017-05-13 (annotated)
- Committer:
- guillouhouede
- Date:
- Sat May 13 16:30:04 2017 +0000
- Revision:
- 0:5a4281204073
programme pour envoyer avec capteur B;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
guillouhouede | 0:5a4281204073 | 1 | #include "mbed.h" |
guillouhouede | 0:5a4281204073 | 2 | #include "main.h" |
guillouhouede | 0:5a4281204073 | 3 | #include "sx1272-hal.h" |
guillouhouede | 0:5a4281204073 | 4 | #include "debug.h" |
guillouhouede | 0:5a4281204073 | 5 | #include "DHT.h" |
guillouhouede | 0:5a4281204073 | 6 | #include "WakeUp.h" |
guillouhouede | 0:5a4281204073 | 7 | |
guillouhouede | 0:5a4281204073 | 8 | //glenn |
guillouhouede | 0:5a4281204073 | 9 | AnalogIn in(PC_3);// Pour la photorésistance |
guillouhouede | 0:5a4281204073 | 10 | AnalogIn in2(PC_2);// Pour la conductivite |
guillouhouede | 0:5a4281204073 | 11 | |
guillouhouede | 0:5a4281204073 | 12 | DHT sensor(PC_5,DHT22); // Pour humi/tempe |
guillouhouede | 0:5a4281204073 | 13 | |
guillouhouede | 0:5a4281204073 | 14 | |
guillouhouede | 0:5a4281204073 | 15 | /***************************************************** |
guillouhouede | 0:5a4281204073 | 16 | Attention /!\ On récupère la valeur de la résistance et pas des lux |
guillouhouede | 0:5a4281204073 | 17 | Et aussi on a la valeur du sensor direct, pas de la conductivité exactement |
guillouhouede | 0:5a4281204073 | 18 | Mettre ça dans la raspberry je pense, ou direct sur le site web |
guillouhouede | 0:5a4281204073 | 19 | ******************************************************/ |
guillouhouede | 0:5a4281204073 | 20 | |
guillouhouede | 0:5a4281204073 | 21 | |
guillouhouede | 0:5a4281204073 | 22 | #define DEBUG_MESSAGE 1 |
guillouhouede | 0:5a4281204073 | 23 | |
guillouhouede | 0:5a4281204073 | 24 | /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */ |
guillouhouede | 0:5a4281204073 | 25 | #define USE_MODEM_LORA 1 |
guillouhouede | 0:5a4281204073 | 26 | #define USE_MODEM_FSK !USE_MODEM_LORA |
guillouhouede | 0:5a4281204073 | 27 | |
guillouhouede | 0:5a4281204073 | 28 | #define RF_FREQUENCY 868000000 // Hz |
guillouhouede | 0:5a4281204073 | 29 | #define TX_OUTPUT_POWER 14 // 14 dBm |
guillouhouede | 0:5a4281204073 | 30 | |
guillouhouede | 0:5a4281204073 | 31 | #if USE_MODEM_LORA == 1 |
guillouhouede | 0:5a4281204073 | 32 | |
guillouhouede | 0:5a4281204073 | 33 | #define LORA_BANDWIDTH 2 // [0: 125 kHz, |
guillouhouede | 0:5a4281204073 | 34 | // 1: 250 kHz, |
guillouhouede | 0:5a4281204073 | 35 | // 2: 500 kHz, |
guillouhouede | 0:5a4281204073 | 36 | // 3: Reserved] |
guillouhouede | 0:5a4281204073 | 37 | #define LORA_SPREADING_FACTOR 7 // [SF7..SF12] |
guillouhouede | 0:5a4281204073 | 38 | #define LORA_CODINGRATE 1 // [1: 4/5, |
guillouhouede | 0:5a4281204073 | 39 | // 2: 4/6, |
guillouhouede | 0:5a4281204073 | 40 | // 3: 4/7, |
guillouhouede | 0:5a4281204073 | 41 | // 4: 4/8] |
guillouhouede | 0:5a4281204073 | 42 | #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx |
guillouhouede | 0:5a4281204073 | 43 | #define LORA_SYMBOL_TIMEOUT 5 // Symbols |
guillouhouede | 0:5a4281204073 | 44 | #define LORA_FIX_LENGTH_PAYLOAD_ON false |
guillouhouede | 0:5a4281204073 | 45 | #define LORA_FHSS_ENABLED false |
guillouhouede | 0:5a4281204073 | 46 | #define LORA_NB_SYMB_HOP 4 |
guillouhouede | 0:5a4281204073 | 47 | #define LORA_IQ_INVERSION_ON false |
guillouhouede | 0:5a4281204073 | 48 | #define LORA_CRC_ENABLED true |
guillouhouede | 0:5a4281204073 | 49 | |
guillouhouede | 0:5a4281204073 | 50 | #elif USE_MODEM_FSK == 1 |
guillouhouede | 0:5a4281204073 | 51 | |
guillouhouede | 0:5a4281204073 | 52 | #define FSK_FDEV 25000 // Hz |
guillouhouede | 0:5a4281204073 | 53 | #define FSK_DATARATE 19200 // bps |
guillouhouede | 0:5a4281204073 | 54 | #define FSK_BANDWIDTH 50000 // Hz |
guillouhouede | 0:5a4281204073 | 55 | #define FSK_AFC_BANDWIDTH 83333 // Hz |
guillouhouede | 0:5a4281204073 | 56 | #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx |
guillouhouede | 0:5a4281204073 | 57 | #define FSK_FIX_LENGTH_PAYLOAD_ON false |
guillouhouede | 0:5a4281204073 | 58 | #define FSK_CRC_ENABLED true |
guillouhouede | 0:5a4281204073 | 59 | |
guillouhouede | 0:5a4281204073 | 60 | #else |
guillouhouede | 0:5a4281204073 | 61 | #error "Please define a modem in the compiler options." |
guillouhouede | 0:5a4281204073 | 62 | #endif |
guillouhouede | 0:5a4281204073 | 63 | |
guillouhouede | 0:5a4281204073 | 64 | #define RX_TIMEOUT_VALUE 3500000 // in us |
guillouhouede | 0:5a4281204073 | 65 | #define BUFFER_SIZE 32 // Define the payload size here |
guillouhouede | 0:5a4281204073 | 66 | |
guillouhouede | 0:5a4281204073 | 67 | #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) |
guillouhouede | 0:5a4281204073 | 68 | DigitalOut led(LED2); |
guillouhouede | 0:5a4281204073 | 69 | #else |
guillouhouede | 0:5a4281204073 | 70 | DigitalOut led(LED1); |
guillouhouede | 0:5a4281204073 | 71 | #endif |
guillouhouede | 0:5a4281204073 | 72 | |
guillouhouede | 0:5a4281204073 | 73 | /********************************************* |
guillouhouede | 0:5a4281204073 | 74 | // fonction sleep |
guillouhouede | 0:5a4281204073 | 75 | **********************************************/ |
guillouhouede | 0:5a4281204073 | 76 | /*Timeout timeout; // Create the Timeout object |
guillouhouede | 0:5a4281204073 | 77 | void wait_LowPow(unsigned long time); |
guillouhouede | 0:5a4281204073 | 78 | void timer_interrupt() // The Timer interrupt service routine |
guillouhouede | 0:5a4281204073 | 79 | { |
guillouhouede | 0:5a4281204073 | 80 | // Nothing to do, just for wakeup the system |
guillouhouede | 0:5a4281204073 | 81 | } |
guillouhouede | 0:5a4281204073 | 82 | void wait_LowPow(unsigned long time) // Wait in low power mode function implementation |
guillouhouede | 0:5a4281204073 | 83 | { |
guillouhouede | 0:5a4281204073 | 84 | timeout.attach(&timer_interrupt, time); // Set the timer interrupt service rutine (ISR) and the time for the timeout (in seconds) |
guillouhouede | 0:5a4281204073 | 85 | sleep(); // Enter Low Power Mode (It cant be used deep_sleep() mode, because that mode disable the timer) |
guillouhouede | 0:5a4281204073 | 86 | }*/ |
guillouhouede | 0:5a4281204073 | 87 | |
guillouhouede | 0:5a4281204073 | 88 | // |
guillouhouede | 0:5a4281204073 | 89 | /* |
guillouhouede | 0:5a4281204073 | 90 | * Global variables declarations |
guillouhouede | 0:5a4281204073 | 91 | */ |
guillouhouede | 0:5a4281204073 | 92 | typedef enum |
guillouhouede | 0:5a4281204073 | 93 | { |
guillouhouede | 0:5a4281204073 | 94 | LOWPOWER = 0, |
guillouhouede | 0:5a4281204073 | 95 | IDLE, |
guillouhouede | 0:5a4281204073 | 96 | |
guillouhouede | 0:5a4281204073 | 97 | RX, |
guillouhouede | 0:5a4281204073 | 98 | RX_TIMEOUT, |
guillouhouede | 0:5a4281204073 | 99 | RX_ERROR, |
guillouhouede | 0:5a4281204073 | 100 | |
guillouhouede | 0:5a4281204073 | 101 | TX, |
guillouhouede | 0:5a4281204073 | 102 | TX_TIMEOUT, |
guillouhouede | 0:5a4281204073 | 103 | |
guillouhouede | 0:5a4281204073 | 104 | CAD, |
guillouhouede | 0:5a4281204073 | 105 | CAD_DONE |
guillouhouede | 0:5a4281204073 | 106 | }AppStates_t; |
guillouhouede | 0:5a4281204073 | 107 | |
guillouhouede | 0:5a4281204073 | 108 | volatile AppStates_t State = LOWPOWER; |
guillouhouede | 0:5a4281204073 | 109 | |
guillouhouede | 0:5a4281204073 | 110 | /*! |
guillouhouede | 0:5a4281204073 | 111 | * Radio events function pointer |
guillouhouede | 0:5a4281204073 | 112 | */ |
guillouhouede | 0:5a4281204073 | 113 | static RadioEvents_t RadioEvents; |
guillouhouede | 0:5a4281204073 | 114 | |
guillouhouede | 0:5a4281204073 | 115 | /* |
guillouhouede | 0:5a4281204073 | 116 | * Global variables declarations |
guillouhouede | 0:5a4281204073 | 117 | */ |
guillouhouede | 0:5a4281204073 | 118 | SX1272MB2xAS Radio( NULL ); |
guillouhouede | 0:5a4281204073 | 119 | |
guillouhouede | 0:5a4281204073 | 120 | //const uint8_t PingMsg[] = "PING"; |
guillouhouede | 0:5a4281204073 | 121 | //const uint8_t PongMsg[] = "PONG"; |
guillouhouede | 0:5a4281204073 | 122 | const uint8_t CommandeDebut[] = "1"; |
guillouhouede | 0:5a4281204073 | 123 | |
guillouhouede | 0:5a4281204073 | 124 | /*const uint8_t PingMsg[] = 42; |
guillouhouede | 0:5a4281204073 | 125 | const uint8_t PongMsg[] = "Message reçu !";*/ |
guillouhouede | 0:5a4281204073 | 126 | |
guillouhouede | 0:5a4281204073 | 127 | uint16_t BufferSize = BUFFER_SIZE; |
guillouhouede | 0:5a4281204073 | 128 | uint8_t Buffer[BUFFER_SIZE]; |
guillouhouede | 0:5a4281204073 | 129 | |
guillouhouede | 0:5a4281204073 | 130 | int16_t RssiValue = 0.0; |
guillouhouede | 0:5a4281204073 | 131 | int8_t SnrValue = 0.0; |
guillouhouede | 0:5a4281204073 | 132 | |
guillouhouede | 0:5a4281204073 | 133 | |
guillouhouede | 0:5a4281204073 | 134 | |
guillouhouede | 0:5a4281204073 | 135 | int main() |
guillouhouede | 0:5a4281204073 | 136 | { |
guillouhouede | 0:5a4281204073 | 137 | int err;//pour dht22 |
guillouhouede | 0:5a4281204073 | 138 | //float val_celcius;//dht22 |
guillouhouede | 0:5a4281204073 | 139 | // float val_humi;//dht22 |
guillouhouede | 0:5a4281204073 | 140 | uint8_t i; |
guillouhouede | 0:5a4281204073 | 141 | bool isMaster = false; |
guillouhouede | 0:5a4281204073 | 142 | |
guillouhouede | 0:5a4281204073 | 143 | debug( "\n\n\r SX1272 Ping Pong Demo Application \n\n\r" ); |
guillouhouede | 0:5a4281204073 | 144 | |
guillouhouede | 0:5a4281204073 | 145 | // Initialize Radio driver |
guillouhouede | 0:5a4281204073 | 146 | RadioEvents.TxDone = OnTxDone; |
guillouhouede | 0:5a4281204073 | 147 | RadioEvents.RxDone = OnRxDone; |
guillouhouede | 0:5a4281204073 | 148 | RadioEvents.RxError = OnRxError; |
guillouhouede | 0:5a4281204073 | 149 | RadioEvents.TxTimeout = OnTxTimeout; |
guillouhouede | 0:5a4281204073 | 150 | RadioEvents.RxTimeout = OnRxTimeout; |
guillouhouede | 0:5a4281204073 | 151 | Radio.Init( &RadioEvents ); |
guillouhouede | 0:5a4281204073 | 152 | |
guillouhouede | 0:5a4281204073 | 153 | |
guillouhouede | 0:5a4281204073 | 154 | |
guillouhouede | 0:5a4281204073 | 155 | |
guillouhouede | 0:5a4281204073 | 156 | // |
guillouhouede | 0:5a4281204073 | 157 | |
guillouhouede | 0:5a4281204073 | 158 | // verify the connection with the board |
guillouhouede | 0:5a4281204073 | 159 | while( Radio.Read( REG_VERSION ) == 0x00 ) |
guillouhouede | 0:5a4281204073 | 160 | { |
guillouhouede | 0:5a4281204073 | 161 | debug( "Radio could not be detected!\n\r", NULL ); |
guillouhouede | 0:5a4281204073 | 162 | wait( 1 ); |
guillouhouede | 0:5a4281204073 | 163 | } |
guillouhouede | 0:5a4281204073 | 164 | |
guillouhouede | 0:5a4281204073 | 165 | debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ) , "\n\r > Board Type: SX1272MB2xAS < \n\r" ); |
guillouhouede | 0:5a4281204073 | 166 | |
guillouhouede | 0:5a4281204073 | 167 | Radio.SetChannel( RF_FREQUENCY ); |
guillouhouede | 0:5a4281204073 | 168 | |
guillouhouede | 0:5a4281204073 | 169 | #if USE_MODEM_LORA == 1 |
guillouhouede | 0:5a4281204073 | 170 | |
guillouhouede | 0:5a4281204073 | 171 | debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r"); |
guillouhouede | 0:5a4281204073 | 172 | debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r"); |
guillouhouede | 0:5a4281204073 | 173 | |
guillouhouede | 0:5a4281204073 | 174 | Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, |
guillouhouede | 0:5a4281204073 | 175 | LORA_SPREADING_FACTOR, LORA_CODINGRATE, |
guillouhouede | 0:5a4281204073 | 176 | LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, |
guillouhouede | 0:5a4281204073 | 177 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
guillouhouede | 0:5a4281204073 | 178 | LORA_IQ_INVERSION_ON, 2000000 ); |
guillouhouede | 0:5a4281204073 | 179 | |
guillouhouede | 0:5a4281204073 | 180 | Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, |
guillouhouede | 0:5a4281204073 | 181 | LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, |
guillouhouede | 0:5a4281204073 | 182 | LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, |
guillouhouede | 0:5a4281204073 | 183 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
guillouhouede | 0:5a4281204073 | 184 | LORA_IQ_INVERSION_ON, true ); |
guillouhouede | 0:5a4281204073 | 185 | |
guillouhouede | 0:5a4281204073 | 186 | #elif USE_MODEM_FSK == 1 |
guillouhouede | 0:5a4281204073 | 187 | |
guillouhouede | 0:5a4281204073 | 188 | debug("\n\n\r > FSK Mode < \n\n\r"); |
guillouhouede | 0:5a4281204073 | 189 | Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, |
guillouhouede | 0:5a4281204073 | 190 | FSK_DATARATE, 0, |
guillouhouede | 0:5a4281204073 | 191 | FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, |
guillouhouede | 0:5a4281204073 | 192 | FSK_CRC_ENABLED, 0, 0, 0, 2000000 ); |
guillouhouede | 0:5a4281204073 | 193 | |
guillouhouede | 0:5a4281204073 | 194 | Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, |
guillouhouede | 0:5a4281204073 | 195 | 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, |
guillouhouede | 0:5a4281204073 | 196 | 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED, |
guillouhouede | 0:5a4281204073 | 197 | 0, 0, false, true ); |
guillouhouede | 0:5a4281204073 | 198 | |
guillouhouede | 0:5a4281204073 | 199 | #else |
guillouhouede | 0:5a4281204073 | 200 | |
guillouhouede | 0:5a4281204073 | 201 | #error "Please define a modem in the compiler options." |
guillouhouede | 0:5a4281204073 | 202 | |
guillouhouede | 0:5a4281204073 | 203 | #endif |
guillouhouede | 0:5a4281204073 | 204 | |
guillouhouede | 0:5a4281204073 | 205 | debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" ); |
guillouhouede | 0:5a4281204073 | 206 | |
guillouhouede | 0:5a4281204073 | 207 | led = 0; |
guillouhouede | 0:5a4281204073 | 208 | |
guillouhouede | 0:5a4281204073 | 209 | Radio.Rx( RX_TIMEOUT_VALUE ); |
guillouhouede | 0:5a4281204073 | 210 | wait(1); |
guillouhouede | 0:5a4281204073 | 211 | |
guillouhouede | 0:5a4281204073 | 212 | union{ |
guillouhouede | 0:5a4281204073 | 213 | float val_celcius; |
guillouhouede | 0:5a4281204073 | 214 | uint8_t array_celcius[4]; |
guillouhouede | 0:5a4281204073 | 215 | }; |
guillouhouede | 0:5a4281204073 | 216 | union{ |
guillouhouede | 0:5a4281204073 | 217 | float res; |
guillouhouede | 0:5a4281204073 | 218 | uint8_t array_res[4]; |
guillouhouede | 0:5a4281204073 | 219 | }; |
guillouhouede | 0:5a4281204073 | 220 | union{ |
guillouhouede | 0:5a4281204073 | 221 | float val_humi; |
guillouhouede | 0:5a4281204073 | 222 | uint8_t array_humi[4]; |
guillouhouede | 0:5a4281204073 | 223 | }; |
guillouhouede | 0:5a4281204073 | 224 | union{ |
guillouhouede | 0:5a4281204073 | 225 | float val_condu; |
guillouhouede | 0:5a4281204073 | 226 | uint8_t array_condu[4]; |
guillouhouede | 0:5a4281204073 | 227 | }; |
guillouhouede | 0:5a4281204073 | 228 | |
guillouhouede | 0:5a4281204073 | 229 | /*fill le buffer au debut |
guillouhouede | 0:5a4281204073 | 230 | */ |
guillouhouede | 0:5a4281204073 | 231 | for( i = 0; i < BufferSize; i++ ) |
guillouhouede | 0:5a4281204073 | 232 | { |
guillouhouede | 0:5a4281204073 | 233 | Buffer[i] = 0; |
guillouhouede | 0:5a4281204073 | 234 | } |
guillouhouede | 0:5a4281204073 | 235 | |
guillouhouede | 0:5a4281204073 | 236 | WakeUp::calibrate(); |
guillouhouede | 0:5a4281204073 | 237 | |
guillouhouede | 0:5a4281204073 | 238 | while( 1 ) |
guillouhouede | 0:5a4281204073 | 239 | { |
guillouhouede | 0:5a4281204073 | 240 | /*************************************************************************************************** |
guillouhouede | 0:5a4281204073 | 241 | Declaraction et récupération des données capteurs |
guillouhouede | 0:5a4281204073 | 242 | *****************************************************************************************************/ |
guillouhouede | 0:5a4281204073 | 243 | |
guillouhouede | 0:5a4281204073 | 244 | float in_value = in.read(); |
guillouhouede | 0:5a4281204073 | 245 | res=(10000*in_value)/(3.3 - in_value); // valeur resistance, voir lux après |
guillouhouede | 0:5a4281204073 | 246 | float in2_value = in2.read(); |
guillouhouede | 0:5a4281204073 | 247 | val_condu = in2_value;//Valeur conductivity, voir pour les coef après |
guillouhouede | 0:5a4281204073 | 248 | |
guillouhouede | 0:5a4281204073 | 249 | debug("la valeur de conductivité du capteur vaut %f\n",val_condu); |
guillouhouede | 0:5a4281204073 | 250 | |
guillouhouede | 0:5a4281204073 | 251 | err = sensor.readData(); //on lit le dht22 |
guillouhouede | 0:5a4281204073 | 252 | if (err == 0) {//Valeurs pour dht22 |
guillouhouede | 0:5a4281204073 | 253 | val_celcius= sensor.ReadTemperature(CELCIUS); |
guillouhouede | 0:5a4281204073 | 254 | val_humi= sensor.ReadHumidity(); |
guillouhouede | 0:5a4281204073 | 255 | printf("Temperature is %4.2f C \r\n",sensor.ReadTemperature(CELCIUS)); |
guillouhouede | 0:5a4281204073 | 256 | //printf("val_celcius vaut %f\n",val_celcius); |
guillouhouede | 0:5a4281204073 | 257 | } else |
guillouhouede | 0:5a4281204073 | 258 | printf("\r\nErr %i \n",err); |
guillouhouede | 0:5a4281204073 | 259 | |
guillouhouede | 0:5a4281204073 | 260 | wait(2); |
guillouhouede | 0:5a4281204073 | 261 | /*************************************************************************************************** |
guillouhouede | 0:5a4281204073 | 262 | On passe les données en uint8_t pour les mettre dans le buffer |
guillouhouede | 0:5a4281204073 | 263 | *****************************************************************************************************/ |
guillouhouede | 0:5a4281204073 | 264 | /**************************************************************************************************** |
guillouhouede | 0:5a4281204073 | 265 | Test mise en veille |
guillouhouede | 0:5a4281204073 | 266 | *****************************************************************************************************/ |
guillouhouede | 0:5a4281204073 | 267 | /* WakeUp::set_ms(5000); |
guillouhouede | 0:5a4281204073 | 268 | //Enter deepsleep, the program won't go beyond this point until it is woken up |
guillouhouede | 0:5a4281204073 | 269 | //sleep(10); |
guillouhouede | 0:5a4281204073 | 270 | //wait_LowPow(10); |
guillouhouede | 0:5a4281204073 | 271 | deepsleep(); |
guillouhouede | 0:5a4281204073 | 272 | wait(5);*/ |
guillouhouede | 0:5a4281204073 | 273 | /*************************************************************************************************** |
guillouhouede | 0:5a4281204073 | 274 | Les états de Lora |
guillouhouede | 0:5a4281204073 | 275 | ***************************************************************************************************/ |
guillouhouede | 0:5a4281204073 | 276 | switch( State ) |
guillouhouede | 0:5a4281204073 | 277 | { |
guillouhouede | 0:5a4281204073 | 278 | case RX: |
guillouhouede | 0:5a4281204073 | 279 | if( BufferSize > 0 ) |
guillouhouede | 0:5a4281204073 | 280 | { |
guillouhouede | 0:5a4281204073 | 281 | debug("buffer 2 : %d\n",Buffer[2]); |
guillouhouede | 0:5a4281204073 | 282 | if(Buffer[2] == 1 ) |
guillouhouede | 0:5a4281204073 | 283 | { |
guillouhouede | 0:5a4281204073 | 284 | led = !led; |
guillouhouede | 0:5a4281204073 | 285 | debug( "ordre de commencer\r\n" ); |
guillouhouede | 0:5a4281204073 | 286 | |
guillouhouede | 0:5a4281204073 | 287 | //Buffer[0,1,2] c'est pour commander les différents appareils. Ici buffer 0 |
guillouhouede | 0:5a4281204073 | 288 | Buffer[3]=1;//Ordre pour dire qu'on a bien renvoyer le message |
guillouhouede | 0:5a4281204073 | 289 | // We fill the buffer with numbers for the payload |
guillouhouede | 0:5a4281204073 | 290 | Buffer[4]=array_res[0]; |
guillouhouede | 0:5a4281204073 | 291 | Buffer[5]=array_res[1]; |
guillouhouede | 0:5a4281204073 | 292 | Buffer[6]=array_res[2]; |
guillouhouede | 0:5a4281204073 | 293 | Buffer[7]=array_res[3]; |
guillouhouede | 0:5a4281204073 | 294 | |
guillouhouede | 0:5a4281204073 | 295 | Buffer[8]=array_condu[0]; |
guillouhouede | 0:5a4281204073 | 296 | Buffer[9]=array_condu[1]; |
guillouhouede | 0:5a4281204073 | 297 | Buffer[10]=array_condu[2]; |
guillouhouede | 0:5a4281204073 | 298 | Buffer[11]=array_condu[3]; |
guillouhouede | 0:5a4281204073 | 299 | |
guillouhouede | 0:5a4281204073 | 300 | Buffer[12]=array_celcius[0]; |
guillouhouede | 0:5a4281204073 | 301 | Buffer[13]=array_celcius[1]; |
guillouhouede | 0:5a4281204073 | 302 | Buffer[14]=array_celcius[2]; |
guillouhouede | 0:5a4281204073 | 303 | Buffer[15]=array_celcius[3]; |
guillouhouede | 0:5a4281204073 | 304 | |
guillouhouede | 0:5a4281204073 | 305 | Buffer[16]=array_humi[0]; |
guillouhouede | 0:5a4281204073 | 306 | Buffer[17]=array_humi[1]; |
guillouhouede | 0:5a4281204073 | 307 | Buffer[18]=array_humi[2]; |
guillouhouede | 0:5a4281204073 | 308 | Buffer[19]=array_humi[3]; |
guillouhouede | 0:5a4281204073 | 309 | for( i = 20; i < BufferSize; i++ ) |
guillouhouede | 0:5a4281204073 | 310 | { |
guillouhouede | 0:5a4281204073 | 311 | Buffer[i] = i - 20; |
guillouhouede | 0:5a4281204073 | 312 | } |
guillouhouede | 0:5a4281204073 | 313 | |
guillouhouede | 0:5a4281204073 | 314 | Buffer[2]=0; |
guillouhouede | 0:5a4281204073 | 315 | wait_ms( 2000 ); //On attend 2sec c'est pour le dht |
guillouhouede | 0:5a4281204073 | 316 | Radio.Send( Buffer, BufferSize ); |
guillouhouede | 0:5a4281204073 | 317 | //On sleep ici |
guillouhouede | 0:5a4281204073 | 318 | //sleep(1000); |
guillouhouede | 0:5a4281204073 | 319 | //sleep(); |
guillouhouede | 0:5a4281204073 | 320 | //wait(10); |
guillouhouede | 0:5a4281204073 | 321 | WakeUp::set_ms(3600000); |
guillouhouede | 0:5a4281204073 | 322 | //Enter deepsleep, the program won't go beyond this point until it is woken up |
guillouhouede | 0:5a4281204073 | 323 | //sleep(10); |
guillouhouede | 0:5a4281204073 | 324 | //wait_LowPow(10); |
guillouhouede | 0:5a4281204073 | 325 | deepsleep(); |
guillouhouede | 0:5a4281204073 | 326 | wait(3600); |
guillouhouede | 0:5a4281204073 | 327 | //debug("fin deepsleep"); |
guillouhouede | 0:5a4281204073 | 328 | |
guillouhouede | 0:5a4281204073 | 329 | } |
guillouhouede | 0:5a4281204073 | 330 | else // valid reception but not a PING as expected |
guillouhouede | 0:5a4281204073 | 331 | { // Set device as master and start again |
guillouhouede | 0:5a4281204073 | 332 | //isMaster = true; |
guillouhouede | 0:5a4281204073 | 333 | Radio.Rx( RX_TIMEOUT_VALUE ); |
guillouhouede | 0:5a4281204073 | 334 | } |
guillouhouede | 0:5a4281204073 | 335 | } |
guillouhouede | 0:5a4281204073 | 336 | |
guillouhouede | 0:5a4281204073 | 337 | State = LOWPOWER; |
guillouhouede | 0:5a4281204073 | 338 | break; |
guillouhouede | 0:5a4281204073 | 339 | case TX: |
guillouhouede | 0:5a4281204073 | 340 | led = !led; |
guillouhouede | 0:5a4281204073 | 341 | |
guillouhouede | 0:5a4281204073 | 342 | if( isMaster == true ) |
guillouhouede | 0:5a4281204073 | 343 | { |
guillouhouede | 0:5a4281204073 | 344 | //debug( "Ping...\r\n" ); |
guillouhouede | 0:5a4281204073 | 345 | |
guillouhouede | 0:5a4281204073 | 346 | //debug("La resistance vaut %f\n",res); |
guillouhouede | 0:5a4281204073 | 347 | } |
guillouhouede | 0:5a4281204073 | 348 | else |
guillouhouede | 0:5a4281204073 | 349 | { |
guillouhouede | 0:5a4281204073 | 350 | debug( "Dans tx sans etre le maitre\r\n" ); |
guillouhouede | 0:5a4281204073 | 351 | debug("La resistance vaut %f\n",res); |
guillouhouede | 0:5a4281204073 | 352 | } |
guillouhouede | 0:5a4281204073 | 353 | Radio.Rx( RX_TIMEOUT_VALUE ); |
guillouhouede | 0:5a4281204073 | 354 | State = LOWPOWER; |
guillouhouede | 0:5a4281204073 | 355 | break; |
guillouhouede | 0:5a4281204073 | 356 | case RX_TIMEOUT: |
guillouhouede | 0:5a4281204073 | 357 | if( isMaster == true ) |
guillouhouede | 0:5a4281204073 | 358 | { |
guillouhouede | 0:5a4281204073 | 359 | // Send the next PING frame |
guillouhouede | 0:5a4281204073 | 360 | //strcpy( ( char* )Buffer, ( char* )PingMsg ); |
guillouhouede | 0:5a4281204073 | 361 | for( i = 4; i < BufferSize; i++ ) |
guillouhouede | 0:5a4281204073 | 362 | { |
guillouhouede | 0:5a4281204073 | 363 | Buffer[i] = i - 4; |
guillouhouede | 0:5a4281204073 | 364 | } |
guillouhouede | 0:5a4281204073 | 365 | wait_ms( 2000 ); |
guillouhouede | 0:5a4281204073 | 366 | Radio.Send( Buffer, BufferSize ); |
guillouhouede | 0:5a4281204073 | 367 | } |
guillouhouede | 0:5a4281204073 | 368 | else |
guillouhouede | 0:5a4281204073 | 369 | { |
guillouhouede | 0:5a4281204073 | 370 | Radio.Rx( RX_TIMEOUT_VALUE ); |
guillouhouede | 0:5a4281204073 | 371 | } |
guillouhouede | 0:5a4281204073 | 372 | State = LOWPOWER; |
guillouhouede | 0:5a4281204073 | 373 | break; |
guillouhouede | 0:5a4281204073 | 374 | case RX_ERROR: |
guillouhouede | 0:5a4281204073 | 375 | // We have received a Packet with a CRC error, send reply as if packet was correct |
guillouhouede | 0:5a4281204073 | 376 | if( isMaster == true ) |
guillouhouede | 0:5a4281204073 | 377 | { |
guillouhouede | 0:5a4281204073 | 378 | // Send the next PING frame |
guillouhouede | 0:5a4281204073 | 379 | //strcpy( ( char* )Buffer, ( char* )PingMsg ); |
guillouhouede | 0:5a4281204073 | 380 | for( i = 4; i < BufferSize; i++ ) |
guillouhouede | 0:5a4281204073 | 381 | { |
guillouhouede | 0:5a4281204073 | 382 | Buffer[i] = i - 4; |
guillouhouede | 0:5a4281204073 | 383 | } |
guillouhouede | 0:5a4281204073 | 384 | wait_ms( 2000 ); |
guillouhouede | 0:5a4281204073 | 385 | Radio.Send( Buffer, BufferSize ); |
guillouhouede | 0:5a4281204073 | 386 | } |
guillouhouede | 0:5a4281204073 | 387 | else |
guillouhouede | 0:5a4281204073 | 388 | { |
guillouhouede | 0:5a4281204073 | 389 | // Send the next PONG frame |
guillouhouede | 0:5a4281204073 | 390 | //strcpy( ( char* )Buffer, ( char* )PongMsg ); |
guillouhouede | 0:5a4281204073 | 391 | for( i = 4; i < BufferSize; i++ ) |
guillouhouede | 0:5a4281204073 | 392 | { |
guillouhouede | 0:5a4281204073 | 393 | Buffer[i] = i - 4; |
guillouhouede | 0:5a4281204073 | 394 | } |
guillouhouede | 0:5a4281204073 | 395 | wait_ms( 2000 ); |
guillouhouede | 0:5a4281204073 | 396 | Radio.Send( Buffer, BufferSize ); |
guillouhouede | 0:5a4281204073 | 397 | } |
guillouhouede | 0:5a4281204073 | 398 | State = LOWPOWER; |
guillouhouede | 0:5a4281204073 | 399 | break; |
guillouhouede | 0:5a4281204073 | 400 | case TX_TIMEOUT: |
guillouhouede | 0:5a4281204073 | 401 | Radio.Rx( RX_TIMEOUT_VALUE ); |
guillouhouede | 0:5a4281204073 | 402 | State = LOWPOWER; |
guillouhouede | 0:5a4281204073 | 403 | break; |
guillouhouede | 0:5a4281204073 | 404 | case LOWPOWER: |
guillouhouede | 0:5a4281204073 | 405 | break; |
guillouhouede | 0:5a4281204073 | 406 | default: |
guillouhouede | 0:5a4281204073 | 407 | State = LOWPOWER; |
guillouhouede | 0:5a4281204073 | 408 | break; |
guillouhouede | 0:5a4281204073 | 409 | } |
guillouhouede | 0:5a4281204073 | 410 | //wait(2); |
guillouhouede | 0:5a4281204073 | 411 | } |
guillouhouede | 0:5a4281204073 | 412 | } |
guillouhouede | 0:5a4281204073 | 413 | |
guillouhouede | 0:5a4281204073 | 414 | void OnTxDone( void ) |
guillouhouede | 0:5a4281204073 | 415 | { |
guillouhouede | 0:5a4281204073 | 416 | Radio.Sleep( ); |
guillouhouede | 0:5a4281204073 | 417 | State = TX; |
guillouhouede | 0:5a4281204073 | 418 | debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" ); |
guillouhouede | 0:5a4281204073 | 419 | } |
guillouhouede | 0:5a4281204073 | 420 | |
guillouhouede | 0:5a4281204073 | 421 | void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) |
guillouhouede | 0:5a4281204073 | 422 | { |
guillouhouede | 0:5a4281204073 | 423 | Radio.Sleep( ); |
guillouhouede | 0:5a4281204073 | 424 | BufferSize = size; |
guillouhouede | 0:5a4281204073 | 425 | memcpy( Buffer, payload, BufferSize ); |
guillouhouede | 0:5a4281204073 | 426 | RssiValue = rssi; |
guillouhouede | 0:5a4281204073 | 427 | SnrValue = snr; |
guillouhouede | 0:5a4281204073 | 428 | State = RX; |
guillouhouede | 0:5a4281204073 | 429 | debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" ); |
guillouhouede | 0:5a4281204073 | 430 | } |
guillouhouede | 0:5a4281204073 | 431 | |
guillouhouede | 0:5a4281204073 | 432 | void OnTxTimeout( void ) |
guillouhouede | 0:5a4281204073 | 433 | { |
guillouhouede | 0:5a4281204073 | 434 | Radio.Sleep( ); |
guillouhouede | 0:5a4281204073 | 435 | State = TX_TIMEOUT; |
guillouhouede | 0:5a4281204073 | 436 | debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" ); |
guillouhouede | 0:5a4281204073 | 437 | } |
guillouhouede | 0:5a4281204073 | 438 | |
guillouhouede | 0:5a4281204073 | 439 | void OnRxTimeout( void ) |
guillouhouede | 0:5a4281204073 | 440 | { |
guillouhouede | 0:5a4281204073 | 441 | Radio.Sleep( ); |
guillouhouede | 0:5a4281204073 | 442 | Buffer[ BufferSize ] = 0; |
guillouhouede | 0:5a4281204073 | 443 | State = RX_TIMEOUT; |
guillouhouede | 0:5a4281204073 | 444 | debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" ); |
guillouhouede | 0:5a4281204073 | 445 | } |
guillouhouede | 0:5a4281204073 | 446 | |
guillouhouede | 0:5a4281204073 | 447 | void OnRxError( void ) |
guillouhouede | 0:5a4281204073 | 448 | { |
guillouhouede | 0:5a4281204073 | 449 | Radio.Sleep( ); |
guillouhouede | 0:5a4281204073 | 450 | State = RX_ERROR; |
guillouhouede | 0:5a4281204073 | 451 | debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" ); |
guillouhouede | 0:5a4281204073 | 452 | } |
guillouhouede | 0:5a4281204073 | 453 |