Send DHT11 & SHT10 sensors data through LoRa SX1272 board.
Dependencies: DHT11 SHTx SX1272Lib mbed
Fork of SX1272-Transmitter by
main.cpp@18:1ee53f77b90d, 2017-03-13 (annotated)
- Committer:
- Antoine38
- Date:
- Mon Mar 13 17:23:03 2017 +0000
- Revision:
- 18:1ee53f77b90d
- Parent:
- 17:cd37e3e63cec
- Child:
- 19:e50e4a513de1
Integration of the code of dalleo/DHT11_Temp_Humid_Air to retrieve real data and send them
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GregCr | 0:1ed39951ab7b | 1 | #include "mbed.h" |
GregCr | 4:5ece30264cd9 | 2 | #include "main.h" |
Antoine38 | 18:1ee53f77b90d | 3 | // SX1272 Lib |
GregCr | 13:edb9b443c1dd | 4 | #include "sx1272-hal.h" |
GregCr | 8:f956dee63a56 | 5 | #include "debug.h" |
Antoine38 | 18:1ee53f77b90d | 6 | // DHT11 Lib (air temperature and humidity) |
Antoine38 | 18:1ee53f77b90d | 7 | #include "DHT11.h" |
GregCr | 0:1ed39951ab7b | 8 | |
GregCr | 0:1ed39951ab7b | 9 | /* Set this flag to '1' to display debug messages on the console */ |
GregCr | 13:edb9b443c1dd | 10 | #define DEBUG_MESSAGE 1 |
GregCr | 0:1ed39951ab7b | 11 | |
GregCr | 0:1ed39951ab7b | 12 | |
Antoine38 | 14:62c3fd006150 | 13 | #define RF_FREQUENCY 868000000 // Hz |
Antoine38 | 14:62c3fd006150 | 14 | #define TX_OUTPUT_POWER 14 // 14 dBm |
GregCr | 0:1ed39951ab7b | 15 | |
Antoine38 | 14:62c3fd006150 | 16 | #define LORA_BANDWIDTH 2 // [0: 125 kHz, |
Antoine38 | 15:24dc42e5d7bf | 17 | // 1: 250 kHz, |
Antoine38 | 15:24dc42e5d7bf | 18 | // 2: 500 kHz, |
Antoine38 | 15:24dc42e5d7bf | 19 | // 3: Reserved] |
Antoine38 | 15:24dc42e5d7bf | 20 | |
Antoine38 | 14:62c3fd006150 | 21 | #define LORA_SPREADING_FACTOR 7 // [SF7..SF12] |
Antoine38 | 14:62c3fd006150 | 22 | #define LORA_CODINGRATE 1 // [1: 4/5, |
Antoine38 | 15:24dc42e5d7bf | 23 | // 2: 4/6, |
Antoine38 | 15:24dc42e5d7bf | 24 | // 3: 4/7, |
Antoine38 | 15:24dc42e5d7bf | 25 | // 4: 4/8] |
Antoine38 | 15:24dc42e5d7bf | 26 | |
Antoine38 | 14:62c3fd006150 | 27 | #define LORA_PREAMBLE_LENGTH 8 |
Antoine38 | 14:62c3fd006150 | 28 | #define LORA_SYMBOL_TIMEOUT 5 // Symbols |
Antoine38 | 14:62c3fd006150 | 29 | #define LORA_FIX_LENGTH_PAYLOAD_ON false |
Antoine38 | 14:62c3fd006150 | 30 | #define LORA_FHSS_ENABLED false |
Antoine38 | 14:62c3fd006150 | 31 | #define LORA_NB_SYMB_HOP 4 |
Antoine38 | 14:62c3fd006150 | 32 | #define LORA_IQ_INVERSION_ON false |
Antoine38 | 14:62c3fd006150 | 33 | #define LORA_CRC_ENABLED true |
GregCr | 0:1ed39951ab7b | 34 | |
Antoine38 | 17:cd37e3e63cec | 35 | #define BUFFER_SIZE 1024 // Define the payload size here |
GregCr | 0:1ed39951ab7b | 36 | |
GregCr | 3:8b9e2a4df4b5 | 37 | DigitalOut led(LED1); |
GregCr | 3:8b9e2a4df4b5 | 38 | |
mluis | 10:7af820d1e1df | 39 | /*! |
mluis | 10:7af820d1e1df | 40 | * Radio events function pointer |
mluis | 10:7af820d1e1df | 41 | */ |
mluis | 10:7af820d1e1df | 42 | static RadioEvents_t RadioEvents; |
mluis | 10:7af820d1e1df | 43 | |
mluis | 10:7af820d1e1df | 44 | /* |
mluis | 10:7af820d1e1df | 45 | * Global variables declarations |
mluis | 10:7af820d1e1df | 46 | */ |
GregCr | 13:edb9b443c1dd | 47 | SX1272MB2xAS Radio( NULL ); |
GregCr | 0:1ed39951ab7b | 48 | |
Antoine38 | 18:1ee53f77b90d | 49 | uint8_t msg[BUFFER_SIZE]; |
GregCr | 0:1ed39951ab7b | 50 | |
GregCr | 0:1ed39951ab7b | 51 | uint16_t BufferSize = BUFFER_SIZE; |
GregCr | 0:1ed39951ab7b | 52 | uint8_t Buffer[BUFFER_SIZE]; |
GregCr | 0:1ed39951ab7b | 53 | |
GregCr | 5:f2431c4fe3bb | 54 | int16_t RssiValue = 0.0; |
GregCr | 5:f2431c4fe3bb | 55 | int8_t SnrValue = 0.0; |
GregCr | 0:1ed39951ab7b | 56 | |
Antoine38 | 17:cd37e3e63cec | 57 | int msglen = 0; |
Antoine38 | 17:cd37e3e63cec | 58 | |
Antoine38 | 18:1ee53f77b90d | 59 | // Air temperature and humidity sensor |
Antoine38 | 18:1ee53f77b90d | 60 | DHT11 d(D6); |
Antoine38 | 18:1ee53f77b90d | 61 | int DHT11_state; |
Antoine38 | 18:1ee53f77b90d | 62 | |
Antoine38 | 14:62c3fd006150 | 63 | int main() |
GregCr | 0:1ed39951ab7b | 64 | { |
GregCr | 0:1ed39951ab7b | 65 | uint8_t i; |
Antoine38 | 14:62c3fd006150 | 66 | |
Antoine38 | 14:62c3fd006150 | 67 | debug( "\n\n\r iGreenhouse Application - Transmitter \n\n\r" ); |
mluis | 10:7af820d1e1df | 68 | |
mluis | 10:7af820d1e1df | 69 | // Initialize Radio driver |
mluis | 10:7af820d1e1df | 70 | RadioEvents.TxDone = OnTxDone; |
mluis | 10:7af820d1e1df | 71 | RadioEvents.TxTimeout = OnTxTimeout; |
mluis | 10:7af820d1e1df | 72 | Radio.Init( &RadioEvents ); |
Antoine38 | 14:62c3fd006150 | 73 | |
GregCr | 7:c1bbd6c56979 | 74 | // verify the connection with the board |
Antoine38 | 14:62c3fd006150 | 75 | while( Radio.Read( REG_VERSION ) == 0x00 ) { |
GregCr | 7:c1bbd6c56979 | 76 | debug( "Radio could not be detected!\n\r", NULL ); |
GregCr | 7:c1bbd6c56979 | 77 | wait( 1 ); |
GregCr | 2:59e108728d71 | 78 | } |
Antoine38 | 14:62c3fd006150 | 79 | |
GregCr | 13:edb9b443c1dd | 80 | debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ) , "\n\r > Board Type: SX1272MB2xAS < \n\r" ); |
GregCr | 0:1ed39951ab7b | 81 | |
Antoine38 | 14:62c3fd006150 | 82 | Radio.SetChannel( RF_FREQUENCY ); |
Antoine38 | 14:62c3fd006150 | 83 | |
Antoine38 | 14:62c3fd006150 | 84 | |
GregCr | 7:c1bbd6c56979 | 85 | debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r"); |
GregCr | 7:c1bbd6c56979 | 86 | debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r"); |
GregCr | 7:c1bbd6c56979 | 87 | |
GregCr | 0:1ed39951ab7b | 88 | Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, |
Antoine38 | 14:62c3fd006150 | 89 | LORA_SPREADING_FACTOR, LORA_CODINGRATE, |
Antoine38 | 14:62c3fd006150 | 90 | LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, |
Antoine38 | 14:62c3fd006150 | 91 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
Antoine38 | 14:62c3fd006150 | 92 | LORA_IQ_INVERSION_ON, 2000000 ); |
GregCr | 0:1ed39951ab7b | 93 | |
Antoine38 | 14:62c3fd006150 | 94 | debug_if( DEBUG_MESSAGE, "Starting sending loop\r\n" ); |
GregCr | 0:1ed39951ab7b | 95 | |
GregCr | 3:8b9e2a4df4b5 | 96 | led = 0; |
Antoine38 | 14:62c3fd006150 | 97 | |
Antoine38 | 18:1ee53f77b90d | 98 | while(1) { |
Antoine38 | 18:1ee53f77b90d | 99 | // Retrieving sensors data |
Antoine38 | 18:1ee53f77b90d | 100 | DHT11_state = d.readData(); |
Antoine38 | 18:1ee53f77b90d | 101 | if (DHT11_state == DHT11::OK) { |
Antoine38 | 18:1ee53f77b90d | 102 | sprintf((char*) msg, "Temp=%d \r\nHumi=%d \r\n", d.readTemperature(), d.readHumidity()); |
Antoine38 | 18:1ee53f77b90d | 103 | } |
Antoine38 | 18:1ee53f77b90d | 104 | |
Antoine38 | 18:1ee53f77b90d | 105 | // Sending a new packet |
Antoine38 | 14:62c3fd006150 | 106 | debug("\r\n========\r\nSending a new Packet\r\n========\r\n"); |
Antoine38 | 17:cd37e3e63cec | 107 | strcpy( ( char* )Buffer, ( char* ) msg ); |
Antoine38 | 14:62c3fd006150 | 108 | // We fill the buffer with numbers for the payload |
Antoine38 | 17:cd37e3e63cec | 109 | msglen = strlen((char *) msg); |
Antoine38 | 17:cd37e3e63cec | 110 | for( i = msglen; i < BufferSize; i++ ) { |
Antoine38 | 17:cd37e3e63cec | 111 | Buffer[i] = i - msglen; |
Antoine38 | 14:62c3fd006150 | 112 | } |
Antoine38 | 18:1ee53f77b90d | 113 | wait_ms(10); |
Antoine38 | 18:1ee53f77b90d | 114 | Radio.Send(Buffer, BufferSize); |
Antoine38 | 18:1ee53f77b90d | 115 | |
Antoine38 | 18:1ee53f77b90d | 116 | // Reversing the led state |
Antoine38 | 16:80f027a8b2bc | 117 | led = 1-led; |
Antoine38 | 18:1ee53f77b90d | 118 | |
Antoine38 | 18:1ee53f77b90d | 119 | // wait 3 seconds before resend data |
Antoine38 | 15:24dc42e5d7bf | 120 | wait(3); |
GregCr | 0:1ed39951ab7b | 121 | } |
GregCr | 0:1ed39951ab7b | 122 | } |
GregCr | 0:1ed39951ab7b | 123 | |
GregCr | 0:1ed39951ab7b | 124 | void OnTxDone( void ) |
GregCr | 0:1ed39951ab7b | 125 | { |
GregCr | 5:f2431c4fe3bb | 126 | Radio.Sleep( ); |
GregCr | 7:c1bbd6c56979 | 127 | debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" ); |
GregCr | 0:1ed39951ab7b | 128 | } |
GregCr | 0:1ed39951ab7b | 129 | |
GregCr | 0:1ed39951ab7b | 130 | void OnTxTimeout( void ) |
GregCr | 0:1ed39951ab7b | 131 | { |
GregCr | 0:1ed39951ab7b | 132 | Radio.Sleep( ); |
GregCr | 7:c1bbd6c56979 | 133 | debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" ); |
GregCr | 0:1ed39951ab7b | 134 | } |
GregCr | 0:1ed39951ab7b | 135 |