Send DHT11 & SHT10 sensors data through LoRa SX1272 board.

Dependencies:   SX1272Lib mbed

Fork of SX1272PingPong by Semtech

Committer:
Antoine38
Date:
Wed Mar 29 12:59:56 2017 +0000
Revision:
23:5e58cd413573
Parent:
22:6d4464ff7b32
Fix some display

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GregCr 0:1ed39951ab7b 1 #include "mbed.h"
GregCr 4:5ece30264cd9 2 #include "main.h"
GregCr 13:edb9b443c1dd 3 #include "sx1272-hal.h"
GregCr 8:f956dee63a56 4 #include "debug.h"
GregCr 0:1ed39951ab7b 5
GregCr 0:1ed39951ab7b 6 /* Set this flag to '1' to display debug messages on the console */
GregCr 13:edb9b443c1dd 7 #define DEBUG_MESSAGE 1
GregCr 0:1ed39951ab7b 8
GregCr 0:1ed39951ab7b 9
Antoine38 14:62c3fd006150 10 #define RF_FREQUENCY 868000000 // Hz
Antoine38 14:62c3fd006150 11 #define TX_OUTPUT_POWER 14 // 14 dBm
GregCr 0:1ed39951ab7b 12
Antoine38 14:62c3fd006150 13 #define LORA_BANDWIDTH 2 // [0: 125 kHz,
Antoine38 15:24dc42e5d7bf 14 // 1: 250 kHz,
Antoine38 15:24dc42e5d7bf 15 // 2: 500 kHz,
Antoine38 15:24dc42e5d7bf 16 // 3: Reserved]
Antoine38 15:24dc42e5d7bf 17
Antoine38 14:62c3fd006150 18 #define LORA_SPREADING_FACTOR 7 // [SF7..SF12]
Antoine38 14:62c3fd006150 19 #define LORA_CODINGRATE 1 // [1: 4/5,
Antoine38 15:24dc42e5d7bf 20 // 2: 4/6,
Antoine38 15:24dc42e5d7bf 21 // 3: 4/7,
Antoine38 15:24dc42e5d7bf 22 // 4: 4/8]
Antoine38 15:24dc42e5d7bf 23
Antoine38 14:62c3fd006150 24 #define LORA_PREAMBLE_LENGTH 8
Antoine38 14:62c3fd006150 25 #define LORA_SYMBOL_TIMEOUT 5 // Symbols
Antoine38 14:62c3fd006150 26 #define LORA_FIX_LENGTH_PAYLOAD_ON false
Antoine38 14:62c3fd006150 27 #define LORA_FHSS_ENABLED false
Antoine38 14:62c3fd006150 28 #define LORA_NB_SYMB_HOP 4
Antoine38 14:62c3fd006150 29 #define LORA_IQ_INVERSION_ON false
Antoine38 14:62c3fd006150 30 #define LORA_CRC_ENABLED true
GregCr 0:1ed39951ab7b 31
Antoine38 17:cd37e3e63cec 32 #define BUFFER_SIZE 1024 // Define the payload size here
GregCr 0:1ed39951ab7b 33
GregCr 3:8b9e2a4df4b5 34 DigitalOut led(LED1);
GregCr 3:8b9e2a4df4b5 35
mluis 10:7af820d1e1df 36 /*!
mluis 10:7af820d1e1df 37 * Radio events function pointer
mluis 10:7af820d1e1df 38 */
mluis 10:7af820d1e1df 39 static RadioEvents_t RadioEvents;
mluis 10:7af820d1e1df 40
mluis 10:7af820d1e1df 41 /*
mluis 10:7af820d1e1df 42 * Global variables declarations
mluis 10:7af820d1e1df 43 */
GregCr 13:edb9b443c1dd 44 SX1272MB2xAS Radio( NULL );
GregCr 0:1ed39951ab7b 45
Antoine38 15:24dc42e5d7bf 46 const uint8_t msg[] = "Temp=184.26 \r\nHumi=423.99 \r\n";
GregCr 0:1ed39951ab7b 47
GregCr 0:1ed39951ab7b 48 uint16_t BufferSize = BUFFER_SIZE;
GregCr 0:1ed39951ab7b 49 uint8_t Buffer[BUFFER_SIZE];
GregCr 0:1ed39951ab7b 50
GregCr 5:f2431c4fe3bb 51 int16_t RssiValue = 0.0;
GregCr 5:f2431c4fe3bb 52 int8_t SnrValue = 0.0;
GregCr 0:1ed39951ab7b 53
Antoine38 17:cd37e3e63cec 54 int msglen = 0;
Antoine38 17:cd37e3e63cec 55
Antoine38 14:62c3fd006150 56 int main()
GregCr 0:1ed39951ab7b 57 {
GregCr 0:1ed39951ab7b 58 uint8_t i;
Antoine38 14:62c3fd006150 59
Antoine38 20:740593ff1121 60 debug( "\n\n\r iGreenhouse Application - Transmitter\n\n\r" );
mluis 10:7af820d1e1df 61
mluis 10:7af820d1e1df 62 // Initialize Radio driver
mluis 10:7af820d1e1df 63 RadioEvents.TxDone = OnTxDone;
mluis 10:7af820d1e1df 64 RadioEvents.TxTimeout = OnTxTimeout;
mluis 10:7af820d1e1df 65 Radio.Init( &RadioEvents );
Antoine38 14:62c3fd006150 66
GregCr 7:c1bbd6c56979 67 // verify the connection with the board
Antoine38 14:62c3fd006150 68 while( Radio.Read( REG_VERSION ) == 0x00 ) {
GregCr 7:c1bbd6c56979 69 debug( "Radio could not be detected!\n\r", NULL );
GregCr 7:c1bbd6c56979 70 wait( 1 );
GregCr 2:59e108728d71 71 }
Antoine38 14:62c3fd006150 72
GregCr 13:edb9b443c1dd 73 debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ) , "\n\r > Board Type: SX1272MB2xAS < \n\r" );
GregCr 0:1ed39951ab7b 74
Antoine38 14:62c3fd006150 75 Radio.SetChannel( RF_FREQUENCY );
Antoine38 14:62c3fd006150 76
Antoine38 14:62c3fd006150 77
GregCr 7:c1bbd6c56979 78 debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r");
GregCr 7:c1bbd6c56979 79 debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r");
GregCr 7:c1bbd6c56979 80
GregCr 0:1ed39951ab7b 81 Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
Antoine38 14:62c3fd006150 82 LORA_SPREADING_FACTOR, LORA_CODINGRATE,
Antoine38 14:62c3fd006150 83 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
Antoine38 14:62c3fd006150 84 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
Antoine38 14:62c3fd006150 85 LORA_IQ_INVERSION_ON, 2000000 );
GregCr 0:1ed39951ab7b 86
Antoine38 14:62c3fd006150 87 debug_if( DEBUG_MESSAGE, "Starting sending loop\r\n" );
GregCr 0:1ed39951ab7b 88
GregCr 3:8b9e2a4df4b5 89 led = 0;
Antoine38 14:62c3fd006150 90
Antoine38 14:62c3fd006150 91 while( 1 ) {
Antoine38 14:62c3fd006150 92 debug("\r\n========\r\nSending a new Packet\r\n========\r\n");
Antoine38 17:cd37e3e63cec 93 strcpy( ( char* )Buffer, ( char* ) msg );
Antoine38 14:62c3fd006150 94 // We fill the buffer with numbers for the payload
Antoine38 17:cd37e3e63cec 95 msglen = strlen((char *) msg);
Antoine38 17:cd37e3e63cec 96 for( i = msglen; i < BufferSize; i++ ) {
Antoine38 17:cd37e3e63cec 97 Buffer[i] = i - msglen;
Antoine38 14:62c3fd006150 98 }
Antoine38 14:62c3fd006150 99 wait_ms( 10 );
Antoine38 14:62c3fd006150 100 Radio.Send( Buffer, BufferSize );
Antoine38 23:5e58cd413573 101
Antoine38 16:80f027a8b2bc 102 led = 1-led;
Antoine38 23:5e58cd413573 103
Antoine38 23:5e58cd413573 104 // Wait 3 seconds before resend a message
Antoine38 15:24dc42e5d7bf 105 wait(3);
GregCr 0:1ed39951ab7b 106 }
GregCr 0:1ed39951ab7b 107 }
GregCr 0:1ed39951ab7b 108
GregCr 0:1ed39951ab7b 109 void OnTxDone( void )
GregCr 0:1ed39951ab7b 110 {
GregCr 5:f2431c4fe3bb 111 Radio.Sleep( );
GregCr 7:c1bbd6c56979 112 debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" );
Antoine38 23:5e58cd413573 113 debug_if( DEBUG_MESSAGE, "Message: %s \r\nLenght: %d \r\n", (char *) Buffer, msglen);
GregCr 0:1ed39951ab7b 114 }
GregCr 0:1ed39951ab7b 115
GregCr 0:1ed39951ab7b 116 void OnTxTimeout( void )
GregCr 0:1ed39951ab7b 117 {
GregCr 0:1ed39951ab7b 118 Radio.Sleep( );
GregCr 7:c1bbd6c56979 119 debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" );
GregCr 0:1ed39951ab7b 120 }