Send DHT11 & SHT10 sensors data through LoRa SX1272 board.

Dependencies:   DHT11 SHTx SX1272Lib mbed

Fork of SX1272-Transmitter by Antoine Boisadam

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "main.h"
00003 // SX1272 Lib
00004 #include "sx1272-hal.h"
00005 #include "debug.h"
00006 // DHT11 Lib (air temperature and humidity)
00007 #include "DHT11.h"
00008 // SHT10 (soil temperature and humidity)
00009 #include "SHTx/sht15.hpp"
00010 
00011 /* Set this flag to '1' to display debug messages on the console */
00012 #define DEBUG_MESSAGE   1
00013 
00014 /* DELAY between two transmission (in seconds) */
00015 #define TRANSMISSION_DELAY                          1800
00016 
00017 #define RF_FREQUENCY                                868000000 // Hz
00018 #define TX_OUTPUT_POWER                             14        // 14 dBm
00019 
00020 #define LORA_BANDWIDTH                              2         // [0: 125 kHz,
00021 //  1: 250 kHz,
00022 //  2: 500 kHz,
00023 //  3: Reserved]
00024 
00025 #define LORA_SPREADING_FACTOR                       7         // [SF7..SF12]
00026 #define LORA_CODINGRATE                             1         // [1: 4/5,
00027 //  2: 4/6,
00028 //  3: 4/7,
00029 //  4: 4/8]
00030 
00031 #define LORA_PREAMBLE_LENGTH                        8
00032 #define LORA_SYMBOL_TIMEOUT                         5         // Symbols
00033 #define LORA_FIX_LENGTH_PAYLOAD_ON                  false
00034 #define LORA_FHSS_ENABLED                           false
00035 #define LORA_NB_SYMB_HOP                            4
00036 #define LORA_IQ_INVERSION_ON                        false
00037 #define LORA_CRC_ENABLED                            true
00038 
00039 #define BUFFER_SIZE                                 6         // Define the payload size here
00040 
00041 DigitalOut led(LED1);
00042 
00043 /*!
00044  * Radio events function pointer
00045  */
00046 static RadioEvents_t RadioEvents;
00047 
00048 /*
00049  *  Global variables declarations
00050  */
00051 SX1272MB2xAS Radio( NULL );
00052 
00053 uint8_t msg[BUFFER_SIZE];
00054 
00055 uint16_t BufferSize = BUFFER_SIZE;
00056 uint8_t Buffer[BUFFER_SIZE];
00057 
00058 // Air temperature and humidity sensor
00059 DHT11 airSensor(D6);
00060 int DHT11_state;
00061 
00062 // Soil temperature and humidity sensor
00063 SHTx::SHT15 soilSensor(D9, D8);
00064 
00065 int main()
00066 {
00067     debug( "\n\n\r     iGreenhouse Application - Transmitter \n\n\r" );
00068 
00069     // Initialize Radio driver
00070     RadioEvents.TxDone = OnTxDone;
00071     RadioEvents.TxTimeout = OnTxTimeout;
00072     Radio.Init( &RadioEvents );
00073 
00074     // verify the connection with the board
00075     while( Radio.Read( REG_VERSION ) == 0x00  ) {
00076         debug( "Radio could not be detected!\n\r", NULL );
00077         wait( 1 );
00078     }
00079 
00080     debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ) , "\n\r > Board Type: SX1272MB2xAS < \n\r" );
00081 
00082     Radio.SetChannel( RF_FREQUENCY );
00083 
00084 
00085     debug_if( LORA_FHSS_ENABLED, "\n\n\r             > LORA FHSS Mode < \n\n\r");
00086     debug_if( !LORA_FHSS_ENABLED, "\n\n\r             > LORA Mode < \n\n\r");
00087 
00088     Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
00089                        LORA_SPREADING_FACTOR, LORA_CODINGRATE,
00090                        LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
00091                        LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
00092                        LORA_IQ_INVERSION_ON, 2000000 );
00093                        
00094     // Soil sensor configuration
00095     soilSensor.setOTPReload(false);
00096     soilSensor.setResolution(true);
00097     soilSensor.setScale(false);
00098 
00099     debug_if( DEBUG_MESSAGE, "Starting sending loop\r\n" );
00100 
00101     led = 0;
00102 
00103     while(1) {
00104         // Retrieving sensors data
00105         DHT11_state = airSensor.readData();
00106         soilSensor.update();
00107         
00108         if (DHT11_state == DHT11::OK) {
00109             msg[0] = airSensor.readTemperature(); // Temperature - Air
00110             msg[1] = airSensor.readHumidity(); // Humidity - Air
00111         } else {
00112             msg[0] = 0x00; // Temperature - Air
00113             msg[1] = 0x00; // Humidity - Air
00114         }
00115         
00116         msg[2] = to_u8(soilSensor.getTemperature(), true); // Temperature - Soil
00117         msg[3] = to_u8(soilSensor.getHumidity(), false); // Humidity - Soil
00118         // Measurements types - Should be 0111 0010 -> 0x72 
00119         msg[4] = 0x72;
00120         // Greenhouse num 1 and sensors in the middle - Should be 0001 0001
00121         msg[5] = 0x11; // id serre
00122         
00123         // Sending a new packet
00124         debug("\r\n========\r\nSending a new Packet\r\n========\r\n");
00125         for(int i = 0; i < BufferSize; i++) {
00126             debug("%x", msg[i]);    
00127         }
00128         debug_if( DEBUG_MESSAGE, "\n" );
00129         memcpy( Buffer, msg, BufferSize );
00130         wait_ms(10);
00131         Radio.Send(Buffer, BufferSize);
00132         
00133         // Switch the led state
00134         led = 1-led;
00135         
00136         // wait X seconds before resend data
00137         //wait(TRANSMISSION_DELAY);
00138         wait(5);
00139     }
00140 }
00141 
00142 // temperature: -30 < x < 70
00143 // humidity: 0 < x < 100
00144 uint8_t to_u8(float x, bool isTemp)
00145 {
00146   float a = 0;
00147   float min = 0.0;
00148   float max = 100.0;
00149   if( isTemp) {
00150     min = -30.0;
00151     max = 70.0;  
00152     a = 30.0;  
00153   }
00154   // On passe le float entre 0 et 1.0
00155   if(x > min && x < max) {
00156     x = (x + a) / 100.0;
00157   } else if(x <= min) {
00158     x = 0.0;
00159   } else {
00160     x = 1.0;
00161   }
00162   return rint(x * 255);
00163 }
00164 
00165 void OnTxDone( void )
00166 {
00167     Radio.Sleep( );
00168     debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" );
00169 }
00170 
00171 void OnTxTimeout( void )
00172 {
00173     Radio.Sleep( );
00174     debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" );
00175 }
00176