Send DHT11 & SHT10 sensors data through LoRa SX1272 board.

Dependencies:   DHT11 SHTx SX1272Lib mbed

Fork of SX1272-Transmitter by Antoine Boisadam

Revision:
21:3ea7ab726dd4
Parent:
20:c4f5299fa99d
Child:
22:e6a5e6cf38cb
--- a/main.cpp	Tue Mar 28 14:04:29 2017 +0000
+++ b/main.cpp	Sat Apr 01 21:47:25 2017 +0000
@@ -12,11 +12,10 @@
 #define DEBUG_MESSAGE   1
 
 /* DELAY between two transmission (in seconds) */
-#define DELAY 1800
+#define TRANSMISSION_DELAY                          1800
 
 #define RF_FREQUENCY                                868000000 // Hz
 #define TX_OUTPUT_POWER                             14        // 14 dBm
-#define TX_TIMEOUT                                  5000000   // in us
 
 #define LORA_BANDWIDTH                              2         // [0: 125 kHz,
 //  1: 250 kHz,
@@ -37,7 +36,7 @@
 #define LORA_IQ_INVERSION_ON                        false
 #define LORA_CRC_ENABLED                            true
 
-#define BUFFER_SIZE                                 1024      // Define the payload size here
+#define BUFFER_SIZE                                 6         // Define the payload size here
 
 DigitalOut led(LED1);
 
@@ -56,22 +55,15 @@
 uint16_t BufferSize = BUFFER_SIZE;
 uint8_t Buffer[BUFFER_SIZE];
 
-int16_t RssiValue = 0.0;
-int8_t SnrValue = 0.0;
-
-int msglen = 0;
-
 // Air temperature and humidity sensor
 DHT11 airSensor(D6);
 int DHT11_state;
 
 // Soil temperature and humidity sensor
-SHTx::SHT15 soilSensor(D9, D8); 
+SHTx::SHT15 soilSensor(D9, D8);
 
 int main()
 {
-    uint8_t i;
-
     debug( "\n\n\r     iGreenhouse Application - Transmitter \n\n\r" );
 
     // Initialize Radio driver
@@ -97,7 +89,7 @@
                        LORA_SPREADING_FACTOR, LORA_CODINGRATE,
                        LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
                        LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
-                       LORA_IQ_INVERSION_ON, TX_TIMEOUT );
+                       LORA_IQ_INVERSION_ON, 2000000 );
                        
     // Soil sensor configuration
     soilSensor.setOTPReload(false);
@@ -111,31 +103,67 @@
     while(1) {
         // Retrieving sensors data
         DHT11_state = airSensor.readData();
+        soilSensor.update();
+        
         if (DHT11_state == DHT11::OK) {
-            sprintf((char*) msg, "TempA=%d \r\nHumiA=%d \r\nTempS=%3.2f \r\nHumiS=%3.2f \r\n", airSensor.readTemperature(), airSensor.readHumidity(), soilSensor.getTemperature(), soilSensor.getHumidity());
+            msg[0] = airSensor.readTemperature(); // Temperature - Air
+            msg[1] = airSensor.readHumidity(); // Humidity - Air
         } else {
-            sprintf((char*) msg, "TempS=%3.2f \r\nHumiS=%3.2f \r\n", soilSensor.getTemperature(), soilSensor.getHumidity());
+            msg[0] = 0x00; // Temperature - Air
+            msg[1] = 0x00; // Humidity - Air
         }
+        soilSensor.setScale(false);
+        msg[2] = to_u8(soilSensor.getTemperature(), true); // Temperature - Soil
+        soilSensor.setScale(true);
+        msg[3] = to_u8(soilSensor.getHumidity(), false); // Humidity - Soil
+        // Measurements types - Should be 0111 0010 -> 0x72 
+        msg[4] = 0x72;
+        // Greenhouse num 1 and sensors in the middle - Should be 0001 0001
+        msg[5] = 0x11; // id serre
         
         // Sending a new packet
         debug("\r\n========\r\nSending a new Packet\r\n========\r\n");
-        strcpy( ( char* )Buffer, ( char* ) msg );
-        // We fill the buffer with numbers for the payload
-        msglen = strlen((char *) msg);
-        for( i = msglen; i < BufferSize; i++ ) {
-            Buffer[i] = i - msglen;
+        for(int i = 0; i < BufferSize; i++) {
+            debug("%x", msg[i]);    
         }
+        debug_if( DEBUG_MESSAGE, "\n" );
+        memcpy( Buffer, msg, BufferSize );
         wait_ms(10);
         Radio.Send(Buffer, BufferSize);
         
-        // Reversing the led state
+        // Switch the led state
         led = 1-led;
         
-        // wait DELAY seconds before resend data
-        wait(DELAY);
+        // wait X seconds before resend data
+        //wait(TRANSMISSION_DELAY);
+        wait(5);
     }
 }
 
+// temperature: -30 < x < 70
+// humidity: 0 < x < 100
+uint8_t to_u8(float x, bool isTemp)
+{
+  printf( "%3.2f isTemp ? %d \r\n", x, isTemp );
+  float a = 30;
+  float min = 0.0;
+  float max = 100.0;
+  if( isTemp) {
+    min = -30.0;
+    max = 70.0;  
+    a = 30.0;  
+  }
+  // On passe le float entre 0 et 1.0
+  if(x > min && x < max) {
+    x = (x + a) / 100.0;
+  } else if(x <= -30) {
+    x = 0.0;
+  } else {
+    x = 1.0;
+  }
+  return rint(x * 255);
+}
+
 void OnTxDone( void )
 {
     Radio.Sleep( );