Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 /*===== NODE 2 =====*/ 00002 00003 #include "mbed.h" 00004 #include "main.h" 00005 // SX1272 Lib 00006 #include "sx1272-hal.h" 00007 #include "debug.h " 00008 // DHT11 Lib (air temperature and humidity) 00009 #include "DHT11.h" 00010 // SHT10 (soil temperature and humidity) 00011 #include "SHTx/sht15.hpp" 00012 // Shield capteur 00013 #include "X_NUCLEO_IKS01A2/XNucleoIKS01A2.h" 00014 // Sleep mode library 00015 #include "WakeUp.h" 00016 00017 /* Set this flag to '1' to display debug messages on the console */ 00018 #define DEBUG_MESSAGE 1 00019 00020 /* DELAY between two transmission (in seconds) */ 00021 #define TRANSMISSION_DELAY 1800 00022 00023 #define RF_FREQUENCY 868000000 // Hz 00024 #define TX_OUTPUT_POWER 14 // 14 dBm 00025 00026 #define LORA_BANDWIDTH 2 // [0: 125 kHz, 00027 // 1: 250 kHz, 00028 // 2: 500 kHz, 00029 // 3: Reserved] 00030 00031 #define LORA_SPREADING_FACTOR 7 // [SF7..SF12] 00032 #define LORA_CODINGRATE 1 // [1: 4/5, 00033 // 2: 4/6, 00034 // 3: 4/7, 00035 // 4: 4/8] 00036 00037 #define LORA_PREAMBLE_LENGTH 8 00038 #define LORA_SYMBOL_TIMEOUT 5 // Symbols 00039 #define LORA_FIX_LENGTH_PAYLOAD_ON false 00040 #define LORA_FHSS_ENABLED false 00041 #define LORA_NB_SYMB_HOP 4 00042 #define LORA_IQ_INVERSION_ON false 00043 #define LORA_CRC_ENABLED true 00044 00045 #define BUFFER_SIZE 11 // Define the payload size here 00046 00047 #define KEY 0xA2 00048 00049 00050 00051 /* ===== PRECISION ===== */ 00052 #define PAS_temp 0.5 00053 #define PAS_hum_a 5 00054 00055 /* ===== Période de mesure ===== */ 00056 #define T_mesure 3 /* 600 s */ 00057 00058 00059 DigitalOut led(LED1); 00060 Serial pc(USBTX, USBRX); 00061 00062 /*! 00063 * Radio events function pointer 00064 */ 00065 static RadioEvents_t RadioEvents; 00066 00067 /* 00068 * Global variables declarations 00069 */ 00070 SX1272MB2xAS Radio ( NULL ); 00071 00072 uint8_t msg[BUFFER_SIZE]; 00073 00074 uint16_t BufferSize = BUFFER_SIZE; 00075 uint8_t Buffer[BUFFER_SIZE]; 00076 00077 /* ===================== IKS01A2 ======================= */ 00078 /* Includes */ 00079 #include "XNucleoIKS01A2.h" 00080 00081 /* Instantiate the expansion board */ 00082 static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5); 00083 00084 /* Retrieve the composing elements of the expansion board */ 00085 static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor; 00086 static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor; 00087 00088 00089 /* ===================== Convertion ======================*/ 00090 00091 // Air temperature and humidity sensor 00092 DHT11 airSensor(D6); 00093 int DHT11_state; 00094 00095 // Soil temperature and humidity sensor 00096 SHTx::SHT15 soilSensor(D9, D8); 00097 00098 int nb_mesure = 0; 00099 00100 00101 LowPowerTimeout MyTimeout; 00102 bool expired = false; 00103 00104 void callback(void) { 00105 expired = true; 00106 } 00107 00108 00109 int main() 00110 { 00111 //programAlarmA(20); 00112 WakeUp::calibrate(); 00113 MyTimeout.attach(callback, T_mesure); 00114 00115 while(1) 00116 { 00117 uint8_t id; 00118 float temp, hum_a, temp2, hum_s = 0xAA; 00119 char buffer1[32], buffer2[32]; 00120 00121 pc.printf( "\n\n\r >==== iGreenhouse Application - Transmitter ====< \n\n\r" ); 00122 00123 // IKS01A2 00124 /* Enable all sensors */ 00125 hum_temp->enable(); 00126 press_temp->enable(); 00127 00128 hum_temp->read_id(&id); 00129 //pc.printf("\n\rHTS221 humidity & temperature = 0x%X\n\r", id); 00130 press_temp->read_id(&id); 00131 //pc.printf("\n\rLPS22HB pressure & temperature = 0x%X\n\r", id); 00132 00133 // Initialize Radio driver 00134 RadioEvents.TxDone = OnTxDone; 00135 RadioEvents.TxTimeout = OnTxTimeout; 00136 Radio .Init( &RadioEvents ); 00137 00138 // verify the connection with the board 00139 while( Radio .Read( REG_VERSION ) == 0x00 ) { 00140 debug( "\n\rRadio could not be detected!\n\r", NULL ); 00141 wait( 1 ); 00142 } 00143 00144 debug_if( ( DEBUG_MESSAGE & ( Radio .DetectBoardType( ) == SX1272MB2XAS ) ) , "\n\r > Board Type: SX1272MB2xAS < \n\r" ); 00145 00146 Radio .SetChannel( RF_FREQUENCY ); 00147 00148 00149 debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r"); 00150 debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r"); 00151 00152 Radio .SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, 00153 LORA_SPREADING_FACTOR, LORA_CODINGRATE, 00154 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, 00155 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, 00156 LORA_IQ_INVERSION_ON, 2000000 ); 00157 00158 // Soil sensor configuration 00159 /*soilSensor.setOTPReload(false); 00160 soilSensor.setResolution(true); 00161 soilSensor.setScale(false);*/ 00162 00163 debug_if( DEBUG_MESSAGE, "Starting sending loop\r\n" ); 00164 00165 led = 0; 00166 00167 // Retrieving sensors data 00168 DHT11_state = airSensor.readData();//DHT11 00169 soilSensor.update();//SHT15 00170 00171 /* ============ IKS01A2 ==============*/ 00172 pc.printf("\n\r === OK === \r\n"); 00173 00174 press_temp->get_temperature(&temp); 00175 hum_temp->get_humidity(&hum_a); 00176 hum_temp->get_temperature(&temp2); 00177 00178 pc.printf("\n\r[temp] %7s C\r\n", print_double(buffer1, temp)); 00179 pc.printf("\n\r[temp2] %7s C\r\n", print_double(buffer1, temp2)); 00180 pc.printf("\n\r[hum] %s%%\r\n", print_double(buffer2, hum_a)); 00181 00182 temp*=5; 00183 hum_a*=2; 00184 00185 pc.printf("\n\r[temp] %7s C\r\n", print_double(buffer1, temp)); 00186 pc.printf("\n\r[hum] %s%%\r\n", print_double(buffer2, hum_a)); 00187 00188 00189 msg[0] = 0x00; /* Identificateur Serres connectées */ 00190 msg[1] = KEY; /* Identificateur Serres */ 00191 msg[3*nb_mesure-1] = temp; /* Température */ 00192 msg[3*nb_mesure] = hum_a; /* Humidité air */ 00193 msg[3*nb_mesure+1] = hum_s; /* Humidité sol */ 00194 00195 if(nb_mesure == 3) 00196 { 00197 // Sending a new packet 00198 debug("\r\n========\r\nSending a new Packet\r\n========\r\n"); 00199 for(int i = 0; i < BufferSize; i++) { 00200 pc.printf("%x", msg[i]); 00201 } 00202 debug_if( DEBUG_MESSAGE, "\n" ); 00203 memcpy( Buffer, msg, BufferSize ); 00204 wait_ms(10); 00205 Radio .Send(Buffer, BufferSize); 00206 nb_mesure = 0; 00207 } 00208 00209 temp/=5; 00210 hum_a/=2; 00211 00212 00213 // Switch the led state 00214 led = 1-led; 00215 00216 nb_mesure++; 00217 //BrunoS while(!expired) sleep(); 00218 expired=0; 00219 } 00220 00221 pc.printf("Starting Standby mode...\r\n"); 00222 HAL_PWR_EnterSTANDBYMode(); 00223 } 00224 00225 // temperature: -30 < x < 70 00226 // humidity: 0 < x < 100 00227 uint8_t to_u8(float x, bool isTemp) 00228 { 00229 float a = 0; 00230 float min = 0.0; 00231 float max = 100.0; 00232 if( isTemp) { 00233 min = -30.0; 00234 max = 70.0; 00235 a = 30.0; 00236 } 00237 // On passe le float entre 0 et 1.0 00238 if(x > min && x < max) { 00239 x = (x + a) / 100.0; 00240 } else if(x <= min) { 00241 x = 0.0; 00242 } else { 00243 x = 1.0; 00244 } 00245 return rint(x * 255); 00246 } 00247 00248 void OnTxDone( void ) 00249 { 00250 Radio .Sleep( ); 00251 debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" ); 00252 } 00253 00254 void OnTxTimeout( void ) 00255 { 00256 Radio .Sleep( ); 00257 debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" ); 00258 } 00259 00260 00261 00262 00263 00264 00265 00266 00267 00268 /* ================== IKS01A2 =====================*/ 00269 00270 /* Helper function for printing floats & doubles */ 00271 static char *print_double(char* str, double v) 00272 { 00273 int decimalDigits=2; 00274 int i = 1; 00275 int intPart, fractPart; 00276 int len; 00277 char *ptr; 00278 00279 /* prepare decimal digits multiplicator */ 00280 for (;decimalDigits!=0; i*=10, decimalDigits--); 00281 00282 /* calculate integer & fractinal parts */ 00283 intPart = (int)v; 00284 fractPart = (int)((v-(double)(int)v)*i); 00285 00286 /* fill in integer part */ 00287 sprintf(str, "%i.", intPart); 00288 00289 /* prepare fill in of fractional part */ 00290 len = strlen(str); 00291 ptr = &str[len]; 00292 00293 /* fill in leading fractional zeros */ 00294 for (i/=10;i>1; i/=10, ptr++) { 00295 if (fractPart >= i) { 00296 break; 00297 } 00298 *ptr = '0'; 00299 } 00300 00301 /* fill in (rest of) fractional part */ 00302 sprintf(ptr, "%i", fractPart); 00303 00304 return str; 00305 }
Generated on Tue Jul 12 2022 21:38:09 by
1.7.2