Send DHT11 & SHT10 sensors data through LoRa SX1272 board.

Dependencies:   DHT11 SHTx SX1272Lib mbed

Fork of SX1272-Transmitter by Antoine Boisadam

Committer:
Antoine38
Date:
Mon Mar 13 10:58:50 2017 +0000
Revision:
14:62c3fd006150
Parent:
13:edb9b443c1dd
Child:
15:24dc42e5d7bf
PingPong is now only a transmitter

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 14:62c3fd006150 14 // 1: 250 kHz,
Antoine38 14:62c3fd006150 15 // 2: 500 kHz,
Antoine38 14:62c3fd006150 16 // 3: Reserved]
Antoine38 14:62c3fd006150 17
Antoine38 14:62c3fd006150 18 #define LORA_SPREADING_FACTOR 7 // [SF7..SF12]
Antoine38 14:62c3fd006150 19 #define LORA_CODINGRATE 1 // [1: 4/5,
Antoine38 14:62c3fd006150 20 // 2: 4/6,
Antoine38 14:62c3fd006150 21 // 3: 4/7,
Antoine38 14:62c3fd006150 22 // 4: 4/8]
Antoine38 14:62c3fd006150 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 14:62c3fd006150 32 #define BUFFER_SIZE 32 // Define the payload size here
GregCr 0:1ed39951ab7b 33
GregCr 3:8b9e2a4df4b5 34 DigitalOut led(LED1);
GregCr 3:8b9e2a4df4b5 35
GregCr 0:1ed39951ab7b 36 /*
GregCr 0:1ed39951ab7b 37 * Global variables declarations
GregCr 0:1ed39951ab7b 38 */
Antoine38 14:62c3fd006150 39 typedef enum {
mluis 10:7af820d1e1df 40 LOWPOWER = 0,
mluis 10:7af820d1e1df 41 IDLE,
Antoine38 14:62c3fd006150 42
mluis 10:7af820d1e1df 43 TX,
mluis 10:7af820d1e1df 44 TX_TIMEOUT,
Antoine38 14:62c3fd006150 45
mluis 10:7af820d1e1df 46 CAD,
mluis 10:7af820d1e1df 47 CAD_DONE
Antoine38 14:62c3fd006150 48 } AppStates_t;
GregCr 0:1ed39951ab7b 49
mluis 10:7af820d1e1df 50 volatile AppStates_t State = LOWPOWER;
mluis 10:7af820d1e1df 51
mluis 10:7af820d1e1df 52 /*!
mluis 10:7af820d1e1df 53 * Radio events function pointer
mluis 10:7af820d1e1df 54 */
mluis 10:7af820d1e1df 55 static RadioEvents_t RadioEvents;
mluis 10:7af820d1e1df 56
mluis 10:7af820d1e1df 57 /*
mluis 10:7af820d1e1df 58 * Global variables declarations
mluis 10:7af820d1e1df 59 */
GregCr 13:edb9b443c1dd 60 SX1272MB2xAS Radio( NULL );
GregCr 0:1ed39951ab7b 61
GregCr 0:1ed39951ab7b 62 const uint8_t PingMsg[] = "PING";
GregCr 0:1ed39951ab7b 63
GregCr 0:1ed39951ab7b 64 uint16_t BufferSize = BUFFER_SIZE;
GregCr 0:1ed39951ab7b 65 uint8_t Buffer[BUFFER_SIZE];
GregCr 0:1ed39951ab7b 66
GregCr 5:f2431c4fe3bb 67 int16_t RssiValue = 0.0;
GregCr 5:f2431c4fe3bb 68 int8_t SnrValue = 0.0;
GregCr 0:1ed39951ab7b 69
Antoine38 14:62c3fd006150 70 int main()
GregCr 0:1ed39951ab7b 71 {
GregCr 0:1ed39951ab7b 72 uint8_t i;
Antoine38 14:62c3fd006150 73
Antoine38 14:62c3fd006150 74 debug( "\n\n\r iGreenhouse Application - Transmitter \n\n\r" );
mluis 10:7af820d1e1df 75
mluis 10:7af820d1e1df 76 // Initialize Radio driver
mluis 10:7af820d1e1df 77 RadioEvents.TxDone = OnTxDone;
mluis 10:7af820d1e1df 78 RadioEvents.TxTimeout = OnTxTimeout;
mluis 10:7af820d1e1df 79 Radio.Init( &RadioEvents );
Antoine38 14:62c3fd006150 80
GregCr 7:c1bbd6c56979 81 // verify the connection with the board
Antoine38 14:62c3fd006150 82 while( Radio.Read( REG_VERSION ) == 0x00 ) {
GregCr 7:c1bbd6c56979 83 debug( "Radio could not be detected!\n\r", NULL );
GregCr 7:c1bbd6c56979 84 wait( 1 );
GregCr 2:59e108728d71 85 }
Antoine38 14:62c3fd006150 86
GregCr 13:edb9b443c1dd 87 debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ) , "\n\r > Board Type: SX1272MB2xAS < \n\r" );
GregCr 0:1ed39951ab7b 88
Antoine38 14:62c3fd006150 89 Radio.SetChannel( RF_FREQUENCY );
Antoine38 14:62c3fd006150 90
Antoine38 14:62c3fd006150 91
GregCr 7:c1bbd6c56979 92 debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r");
GregCr 7:c1bbd6c56979 93 debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r");
GregCr 7:c1bbd6c56979 94
GregCr 0:1ed39951ab7b 95 Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
Antoine38 14:62c3fd006150 96 LORA_SPREADING_FACTOR, LORA_CODINGRATE,
Antoine38 14:62c3fd006150 97 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
Antoine38 14:62c3fd006150 98 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
Antoine38 14:62c3fd006150 99 LORA_IQ_INVERSION_ON, 2000000 );
GregCr 0:1ed39951ab7b 100
Antoine38 14:62c3fd006150 101 debug_if( DEBUG_MESSAGE, "Starting sending loop\r\n" );
GregCr 0:1ed39951ab7b 102
GregCr 3:8b9e2a4df4b5 103 led = 0;
Antoine38 14:62c3fd006150 104
Antoine38 14:62c3fd006150 105 while( 1 ) {
Antoine38 14:62c3fd006150 106 debug("\r\n========\r\nSending a new Packet\r\n========\r\n");
Antoine38 14:62c3fd006150 107 strcpy( ( char* )Buffer, ( char* )PingMsg );
Antoine38 14:62c3fd006150 108 // We fill the buffer with numbers for the payload
Antoine38 14:62c3fd006150 109 for( i = 4; i < BufferSize; i++ ) {
Antoine38 14:62c3fd006150 110 Buffer[i] = i - 4;
Antoine38 14:62c3fd006150 111 }
Antoine38 14:62c3fd006150 112 wait_ms( 10 );
Antoine38 14:62c3fd006150 113 Radio.Send( Buffer, BufferSize );
Antoine38 14:62c3fd006150 114 wait(3);
GregCr 0:1ed39951ab7b 115 }
GregCr 0:1ed39951ab7b 116 }
GregCr 0:1ed39951ab7b 117
GregCr 0:1ed39951ab7b 118 void OnTxDone( void )
GregCr 0:1ed39951ab7b 119 {
GregCr 5:f2431c4fe3bb 120 Radio.Sleep( );
GregCr 0:1ed39951ab7b 121 State = TX;
GregCr 7:c1bbd6c56979 122 debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" );
GregCr 0:1ed39951ab7b 123 }
GregCr 0:1ed39951ab7b 124
GregCr 0:1ed39951ab7b 125 void OnTxTimeout( void )
GregCr 0:1ed39951ab7b 126 {
GregCr 0:1ed39951ab7b 127 Radio.Sleep( );
GregCr 0:1ed39951ab7b 128 State = TX_TIMEOUT;
GregCr 7:c1bbd6c56979 129 debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" );
GregCr 0:1ed39951ab7b 130 }
GregCr 0:1ed39951ab7b 131