Nakatafu ☆ / Mbed 2 deprecated BLE_Condensation_Monitor

Dependencies:   AQM0802 BME280 HDC1000 VaporCondition mbed BLE_API nRF51822 BLE_Condensation_Monitor

Dependents:   BLE_Condensation_Monitor

Fork of Condensation_Monitor by Nakatafu ☆

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // BLE Condensation Monitor
00002 // https://developer.mbed.org/users/takafuminaka/code/BLE_Condensation_Monitor/
00003 // T.Naka
00004 
00005 #include "mbed.h"
00006 #include "HDC1000.h"
00007 #include "BME280.h"
00008 #include "AQM0802.h"
00009 
00010 #include "VaporCondition.h"
00011 
00012 #define NEED_CONSOLE_OUTPUT 1
00013 #define NEED_BLE_CONSOLE 1
00014 #define NEED_LCD_OUTPUT 1
00015 
00016 #define TYPE_HDC1000 1
00017 #define TYPE_BME280 2
00018 
00019 // #define INSIDE_SENSOR TYPE_HDC1000
00020 #define INSIDE_SENSOR TYPE_BME280
00021 
00022 #if NEED_CONSOLE_OUTPUT
00023 Serial  pc(USBTX, USBRX);
00024 #define PC(...) { pc.printf(__VA_ARGS__); }
00025 #else
00026 #define PC(...) /* nothing */
00027 #endif /* #if NEED_CONSOLE_OUTPUT */
00028 
00029 #if NEED_BLE_CONSOLE
00030 #include "BLEDevice.h"
00031 #include "UARTService.h"
00032 #define BLEC(...) { char __blecstr[32]; sprintf(__blecstr,__VA_ARGS__); if (uart) uart->write(__blecstr, strlen(__blecstr)); }
00033 #else
00034 #define BLEC(...) /* nothing */
00035 #endif /* #if NEED_BLE_CONSOLE */
00036 
00037 #if defined(TARGET_LPC1768)
00038 I2C i2c(p28, p27);
00039 // BME280 sensor(p28, p27, 0x76 << 1);
00040 #else
00041 I2C i2c(I2C_SDA0, I2C_SCL0);
00042 // BME280 sensor(I2C_SDA0, I2C_SCL0, 0x76 << 1);
00043 #endif
00044 
00045 BME280 sensor_out(I2C_SDA0, I2C_SCL0, 0x76 << 1);
00046 
00047 #if INSIDE_SENSOR == TYPE_HDC1000
00048 HDC1000 sensor_in(i2c);
00049 #elif INSIDE_SENSOR == TYPE_BME280
00050 BME280 sensor_in(I2C_SDA0, I2C_SCL0, 0x77 << 1);
00051 #endif
00052 
00053 #if NEED_LCD_OUTPUT
00054 AQM0802 lcd(i2c);
00055 #endif
00056 
00057 DigitalOut led1(LED1);
00058 DigitalOut led2(LED2);
00059 
00060 #if NEED_BLE_CONSOLE
00061 BLEDevice  ble;
00062 UARTService *uart;
00063 
00064 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
00065 {
00066     PC("Disconnected!\n\r");
00067     PC("Restarting the advertising process\n\r");
00068     ble.startAdvertising();
00069 }
00070 
00071 #endif /* #if NEED_BLE_CONSOLE */
00072 
00073 int main()
00074 {
00075     float Tdp_o =0.;
00076     float Tdp_i =0.;
00077     int cautions = 0;
00078     int warnings = 0;
00079     int warn_wid = 20;
00080     char msg1[10],msg2[10];
00081     int mode=0;
00082     int skip=0;
00083     char msg[4][2][10];
00084     char bmsg[4][20];
00085     int skipf[4];
00086 
00087     float Tcur;
00088     float Tdp;
00089     char *sTcur;
00090     char *sWin;
00091     char *ssTcur;
00092     char *ssWin;
00093     char *bsTcur;
00094     char *bsWin;
00095 
00096     int skipped = 0;
00097 
00098     VaporCondition Inside;
00099     VaporCondition Outside;
00100 
00101     // LED Check
00102     led1 = 1;
00103     led2 = 1;
00104     wait(3);
00105 
00106     led1 = 0;
00107     led2 = 0;
00108 
00109     i2c.frequency(100000);
00110 
00111 #if NEED_BLE_CONSOLE
00112     // Setup BLE //
00113     ble.init();
00114     ble.onDisconnection(disconnectionCallback);
00115 
00116     uart = new UARTService(ble);
00117 
00118     /* setup advertising */
00119     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00120     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00121     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
00122                                      (const uint8_t *)"BLE Condensation Monitor", sizeof("BLE Condensation Monitor") - 1);
00123     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
00124                                      (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
00125 
00126     ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
00127     ble.startAdvertising();
00128 #endif /* #if NEED_BLE_CONSOLE */
00129 
00130     while(1) {
00131         if ( skipped == 0 ) {
00132             // Get data
00133 
00134             Outside.t = sensor_out.getTemperature();
00135             Outside.h = sensor_out.getHumidity();
00136 
00137             Outside.p = sensor_out.getPressure();
00138 
00139 #if INSIDE_SENSOR == TYPE_HDC1000
00140             Inside.p = sensor_out.getPressure(); // Usually Pressures are same between inside and outside.
00141             Inside.t = float(sensor_in.temperature())/0x10000*165-40;
00142             Inside.h = float(sensor_in.humidity())/0x10000*100;
00143             PC("In: %2.2f degC, %2.2f %%\r\n", Inside.t, Inside.h);
00144 #elif INSIDE_SENSOR == TYPE_BME280
00145             Inside.t = sensor_in.getTemperature();
00146             Inside.h = sensor_in.getHumidity();
00147             Inside.p = sensor_in.getPressure();
00148             PC("In: %2.2f degC, %2.2f %%, %04.2f hPa\r\n", Inside.t, Inside.h, Inside.p);
00149 #endif
00150 
00151             PC("Out: %2.2f degC, %2.2f %%, %04.2f hPa\r\n", Outside.t, Outside.h, Outside.p);
00152 
00153             PC("Humidity Ratio [g/kg] : In %2.2f Out %2.2f \r\n", Inside.Rh(), Outside.Rh());
00154             Tdp_o = Outside.Tdp();
00155             Tdp_i = Inside.Tdp();
00156             PC("Due Point Temperature [degC] : In %2.2f Out %2.2f \r\n", Tdp_i, Tdp_o);
00157 
00158             // print catuions and warnings //
00159             cautions = 0;
00160             warnings = 0;
00161 
00162             for(int ii=0; ii<4; ii++) {
00163                 if ( (ii % 2) == 1 ) {
00164                     Tcur = Outside.t;   // 1 and 3
00165                     sTcur = "Outside";
00166                     ssTcur = "Out";
00167                     bsTcur = "Out";
00168                 } else {
00169                     Tcur = Inside.t;    // 0 and 2
00170                     sTcur = "Inside";
00171                     ssTcur = "In";
00172                     bsTcur = "In";
00173                 }
00174 
00175                 if ( ii / 2 ) {
00176                     sWin = " Window"; // 2 and 3
00177                     ssWin = "@Win";
00178                     bsWin = "Wind.";
00179                 } else {
00180                     sWin = "";         // 0 and 1
00181                     ssWin = "";
00182                     bsWin = "";
00183                 }
00184 
00185                 if ( ii / 2 == ii %2 ) {
00186                     Tdp = Tdp_i;    // 0 and 3
00187                 } else {
00188                     Tdp = Tdp_o;    // 1 and 1
00189                 }
00190 
00191                 if ( Tdp >= Tcur - warn_wid ) {
00192                     skipf[ii] = 0;
00193                     if ( Tdp >= Tcur ) {
00194                         PC("Condensation at %s %s\r\n",sTcur,sWin);
00195                         sprintf(msg[ii][0],"Condns!!");
00196                         sprintf(msg[ii][1],"%s%s",ssTcur,ssWin);
00197                         sprintf(bmsg[ii],"Cond. at %s%s",bsTcur,bsWin);
00198                         cautions ++;
00199                     } else {
00200                         PC("%4.1f degC to Condensation at %s%s\r\n", Tcur - Tdp, sTcur, sWin);
00201                         sprintf(msg[ii][0],"Cto%4.1fC",Tcur-Tdp);
00202                         sprintf(msg[ii][1],"%s%s\0",ssTcur,ssWin);
00203                         sprintf(bmsg[ii],"%4.1ftoC.at%s%s",Tcur - Tdp, bsTcur, bsWin);
00204                         warnings ++;
00205                     }
00206                 } else {
00207                     skipf[ii] = 1;
00208                 }
00209             }
00210 
00211             PC("\r\n");
00212 
00213             if ( cautions > 0 ) {
00214                 led2 = 1;
00215             } else {
00216                 led2 = 0;
00217             }
00218 
00219             if ( warnings > 0 ) {
00220                 led1 = 1;
00221             } else {
00222                 led1 = 0;
00223             }
00224 
00225         }
00226         // LCD print
00227         switch(mode) {
00228             case (0):
00229                 skip = 1;
00230                 break;
00231 
00232             case (1):
00233                 sprintf(msg1,"Ti %4.1fC",Inside.t);
00234                 sprintf(msg2,"To %4.1fC",Outside.t);
00235                 BLEC("Temp.In%4.1f Out%4.1f\n",Inside.t, Outside.t);
00236                 break;
00237 
00238             case (2):
00239                 sprintf(msg1,"Hi %4.1f%%",Inside.h);
00240                 sprintf(msg2,"Ho %4.1f%%",Outside.h);
00241                 BLEC("Hum.In%2.0f%% Out%2.0f%%\n",Inside.h, Outside.h);
00242                 break;
00243 
00244             case (3):
00245                 sprintf(msg1,"Po%6.1f",Outside.p);
00246                 sprintf(msg2,"   [hPa]");
00247                 BLEC("Prs.Out%6.1fhPa\n",Outside.p);
00248                 break;
00249 
00250             case (4):
00251 #if INSIDE_SENSOR == TYPE_BME280 
00252                 sprintf(msg1,"Pi%6.1f",Inside.p);
00253                 sprintf(msg2,"   [hPa]");
00254                 BLEC("Prs.In %6.1fhPa\n",Inside.p);
00255 #elif INSIDE_SENSOR == TYPE_HDC1000
00256                 skip = 1;
00257 #endif
00258                 break;
00259 
00260             case (5):
00261                 sprintf(msg1,"Dpi%4.1fC",Tdp_i);
00262                 sprintf(msg2,"Dpo%4.1fC",Tdp_o);
00263                 BLEC("DP. In%4.1f Out%4.1f\n",Tdp_i, Tdp_o);
00264                 break;
00265 
00266             case (6):
00267             case (7):
00268             case (8):
00269             case (9):
00270                 int ii = mode - 6;
00271                 skip = skipf[ii];
00272                 sprintf(msg1,"%8s",msg[ii][0]);
00273                 sprintf(msg2,"%8s",msg[ii][1]);
00274                 BLEC("%s\n",bmsg[ii]);
00275                 break;
00276         }
00277         mode++;
00278         if ( mode > 9 ) {
00279             mode = 0;
00280         }
00281 
00282         if ( skip == 0 ) {
00283 #if NEED_LCD_OUTPUT
00284             lcd.locate(0,0);
00285             lcd.print(msg1);
00286             lcd.locate(0,1);
00287             lcd.print(msg2);
00288 
00289 #endif
00290             skipped = 0;
00291             wait(3);
00292         } else {
00293             skip = 0;
00294             skipped = 1;
00295         }
00296     }
00297 
00298 }