
Send DHT11 & SHT10 sensors data through LoRa SX1272 board.
Fork of SX1272PingPong by
main.cpp
00001 #include "mbed.h" 00002 #include "main.h" 00003 #include "sx1272-hal.h" 00004 #include "debug.h" 00005 00006 /* Set this flag to '1' to display debug messages on the console */ 00007 #define DEBUG_MESSAGE 1 00008 00009 00010 #define RF_FREQUENCY 868000000 // Hz 00011 #define TX_OUTPUT_POWER 14 // 14 dBm 00012 00013 #define LORA_BANDWIDTH 2 // [0: 125 kHz, 00014 // 1: 250 kHz, 00015 // 2: 500 kHz, 00016 // 3: Reserved] 00017 00018 #define LORA_SPREADING_FACTOR 7 // [SF7..SF12] 00019 #define LORA_CODINGRATE 1 // [1: 4/5, 00020 // 2: 4/6, 00021 // 3: 4/7, 00022 // 4: 4/8] 00023 00024 #define LORA_PREAMBLE_LENGTH 8 00025 #define LORA_SYMBOL_TIMEOUT 5 // Symbols 00026 #define LORA_FIX_LENGTH_PAYLOAD_ON false 00027 #define LORA_FHSS_ENABLED false 00028 #define LORA_NB_SYMB_HOP 4 00029 #define LORA_IQ_INVERSION_ON false 00030 #define LORA_CRC_ENABLED true 00031 00032 #define BUFFER_SIZE 1024 // Define the payload size here 00033 00034 DigitalOut led(LED1); 00035 00036 /*! 00037 * Radio events function pointer 00038 */ 00039 static RadioEvents_t RadioEvents; 00040 00041 /* 00042 * Global variables declarations 00043 */ 00044 SX1272MB2xAS Radio( NULL ); 00045 00046 const uint8_t msg[] = "Temp=184.26 \r\nHumi=423.99 \r\n"; 00047 00048 uint16_t BufferSize = BUFFER_SIZE; 00049 uint8_t Buffer[BUFFER_SIZE]; 00050 00051 int16_t RssiValue = 0.0; 00052 int8_t SnrValue = 0.0; 00053 00054 int msglen = 0; 00055 00056 int main() 00057 { 00058 uint8_t i; 00059 00060 debug( "\n\n\r iGreenhouse Application - Transmitter\n\n\r" ); 00061 00062 // Initialize Radio driver 00063 RadioEvents.TxDone = OnTxDone; 00064 RadioEvents.TxTimeout = OnTxTimeout; 00065 Radio.Init( &RadioEvents ); 00066 00067 // verify the connection with the board 00068 while( Radio.Read( REG_VERSION ) == 0x00 ) { 00069 debug( "Radio could not be detected!\n\r", NULL ); 00070 wait( 1 ); 00071 } 00072 00073 debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ) , "\n\r > Board Type: SX1272MB2xAS < \n\r" ); 00074 00075 Radio.SetChannel( RF_FREQUENCY ); 00076 00077 00078 debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r"); 00079 debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r"); 00080 00081 Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, 00082 LORA_SPREADING_FACTOR, LORA_CODINGRATE, 00083 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, 00084 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, 00085 LORA_IQ_INVERSION_ON, 2000000 ); 00086 00087 debug_if( DEBUG_MESSAGE, "Starting sending loop\r\n" ); 00088 00089 led = 0; 00090 00091 while( 1 ) { 00092 debug("\r\n========\r\nSending a new Packet\r\n========\r\n"); 00093 strcpy( ( char* )Buffer, ( char* ) msg ); 00094 // We fill the buffer with numbers for the payload 00095 msglen = strlen((char *) msg); 00096 for( i = msglen; i < BufferSize; i++ ) { 00097 Buffer[i] = i - msglen; 00098 } 00099 wait_ms( 10 ); 00100 Radio.Send( Buffer, BufferSize ); 00101 00102 led = 1-led; 00103 00104 // Wait 3 seconds before resend a message 00105 wait(3); 00106 } 00107 } 00108 00109 void OnTxDone( void ) 00110 { 00111 Radio.Sleep( ); 00112 debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" ); 00113 debug_if( DEBUG_MESSAGE, "Message: %s \r\nLenght: %d \r\n", (char *) Buffer, msglen); 00114 } 00115 00116 void OnTxTimeout( void ) 00117 { 00118 Radio.Sleep( ); 00119 debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" ); 00120 }
Generated on Thu Jul 14 2022 11:51:16 by
