You need to drink more water
Dependencies: HX711 SeeedGrayOLED XBeeLib mbed
main.cpp
00001 #include "mbed.h" 00002 #include "XBeeLib.h" 00003 #include "HX711.h" 00004 #include "SeeedGrayOLED.h" // Seeed Grove: OLED Display 0.96" 96x96 00005 00006 #define REMOTE_NODE_ADDR64_MSB ((uint32_t)0x0013A200) 00007 #define REMOTE_NODE_ADDR64_LSB ((uint32_t)0x40e8282a) 00008 //#define REMOTE_NODE_ADDR64_LSB ((uint32_t)0x40B8EBCE) 00009 00010 #define REMOTE_NODE_ADDR16 ((uint16_t)0xFFFF) 00011 #define REMOTE_NODE_ADDR64 UINT64(REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB) 00012 00013 //// 00014 #define BOTTLE_SAMPLE_RATE 10 00015 #define MAX_WATER 2000 // 2L 00016 unsigned int waterlog = 0; 00017 00018 //// 00019 enum dataType { 00020 REQTOTAL = 0, 00021 SENDDATA 00022 }; 00023 00024 enum status { 00025 NEED_SET_DEFAULT = 0, 00026 SET_DEFAULT, 00027 CHECK_BOTTLE, 00028 SENSE_WATER_LEVEL, 00029 WAIT_INPUT 00030 }; 00031 00032 00033 using namespace XBeeLib; 00034 00035 Serial pc(DEBUG_TX, DEBUG_RX); 00036 HX711 gram(A1, A0, 64); 00037 DigitalIn default_button(D7); 00038 DigitalIn bottle_weight(D8); 00039 DigitalOut LED(D6); 00040 SeeedGrayOLED SeeedGrayOled(D14, D15); 00041 00042 void Draw_OLED_log(uint8_t val); 00043 void OLED_Display(float val); 00044 00045 00046 static void receiveXbee(const RemoteXBee802& remote, bool broadcast, const uint8_t *const data, uint16_t len) 00047 { 00048 char tmp[50] = {'\0'}; 00049 00050 #ifdef _DBG 00051 00052 if (remote.is_valid_addr16b()) { 00053 printf("\r\n>> Got a %s 16-bit RX packet [%04x], len %d\r\nData: ", broadcast ? "BROADCAST" : "UNICAST", remote.get_addr16(), len); 00054 } else { 00055 printf("\r\n>> Got a %s 64-bit RX packet [%08x:%08x], len %d\r\nData: ", broadcast ? "BROADCAST" : "UNICAST", remote.get_addr64(), len); 00056 } 00057 #endif 00058 00059 for (int i = 1; i < len; i++){ 00060 printf("%c", data[i]); 00061 tmp[i - 1] = data[i]; 00062 } 00063 printf("\r\n"); 00064 00065 if(!strncmp("init",(char*)data,len)){ 00066 waterlog = 0; 00067 00068 } 00069 00070 } 00071 static void sendRemoteXbee(XBee802& xbee, const RemoteXBee802& RemoteDevice, uint8_t type ,long weight) 00072 { 00073 char data[20] = {'\0'}; 00074 sprintf(data, "%1d%i\0",type, weight); 00075 00076 const uint16_t data_len = strlen(data); 00077 00078 const TxStatus txStatus = xbee.send_data(RemoteDevice, (const uint8_t *)data, data_len); 00079 00080 if (txStatus == TxStatusSuccess) 00081 printf(">> Send_data_to_remote_node OK... %s\r\n",data); 00082 else 00083 printf(">> send_data_to_remote_node failed with %d\r\n", (int)txStatus); 00084 } 00085 00086 int checkWeight(){ 00087 long sum = 0; 00088 00089 for(int i = 0; i < BOTTLE_SAMPLE_RATE; i++){ 00090 sum += gram.getGram(); 00091 wait_ms(100); 00092 } 00093 00094 return sum / BOTTLE_SAMPLE_RATE; 00095 } 00096 00097 00098 void ledBlink(int cnt, int delay_ms = 50){ 00099 00100 LED = 0; 00101 00102 for(int i = 0; i < cnt; i++){ 00103 LED = 1; 00104 wait_ms(delay_ms); 00105 LED = 0; 00106 wait_ms(delay_ms); 00107 } 00108 } 00109 ////////////////////////////////////// 00110 ////////////////////////////////////// 00111 ////////////////////////////////////// 00112 ////////////////////////////////////// 00113 ////////////////////////////////////// 00114 ////////////////////////////////////// 00115 ////////////////////////////////////// 00116 00117 int main() 00118 { 00119 pc.baud(9600); 00120 00121 XBee802 xbee = XBee802(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600); 00122 00123 00124 /* Register callback */ 00125 xbee.register_receive_cb(&receiveXbee); 00126 RadioStatus const radioStatus = xbee.init(); 00127 MBED_ASSERT(radioStatus == Success); 00128 const RemoteXBee802 remoteDevice64b = RemoteXBee802(REMOTE_NODE_ADDR64); 00129 00130 SeeedGrayOled.init(); // Initialize SEEED OLED display 00131 SeeedGrayOled.clearDisplay(); // Clear Display 00132 SeeedGrayOled.setNormalDisplay(); // Set Normal Display Mode 00133 00134 // for(int i=0; i<2000; i+=100) 00135 // OLED_Display((float)i); 00136 OLED_Display(0); 00137 00138 long Weight; //current weight 00139 long botWeight = 0; //bottle weight 00140 00141 long WaterLevel = 0; 00142 long pWaterLevel = 0; 00143 long dWaterLevel = 0; 00144 uint8_t Status; 00145 00146 Status = NEED_SET_DEFAULT; 00147 00148 00149 00150 while (true) { 00151 if(!default_button) 00152 Status = SET_DEFAULT; 00153 else if(!bottle_weight) 00154 Status = CHECK_BOTTLE; 00155 00156 switch(Status){ 00157 case NEED_SET_DEFAULT: 00158 // printf(">>> You need to set the default.\n>>>Press the SET KEY\n\n"); 00159 printf("."); 00160 wait(1); 00161 break; 00162 00163 case SET_DEFAULT: 00164 gram.tare(); 00165 ledBlink(1, 100); 00166 00167 printf(">>> Set the base weight\n"); 00168 Weight = 0; 00169 botWeight = 0; 00170 WaterLevel = 0; 00171 pWaterLevel = 0; 00172 00173 Status = WAIT_INPUT; 00174 break; 00175 00176 case CHECK_BOTTLE: 00177 pWaterLevel = 0; 00178 botWeight = checkWeight(); 00179 printf(">>> Your Bottle is %i g\n", botWeight); 00180 ledBlink(3); 00181 Status = SENSE_WATER_LEVEL; 00182 00183 break; 00184 00185 case SENSE_WATER_LEVEL: 00186 00187 Weight = checkWeight(); 00188 WaterLevel = Weight - botWeight; 00189 printf("W: %i \t B: %i\n",WaterLevel,pWaterLevel); 00190 00191 if(WaterLevel <= 10 && WaterLevel > -10) // allowable error 00192 break; 00193 else if(WaterLevel <= -10){ // error 00194 printf("WaterLevel: %i\n",WaterLevel); 00195 //Status = NEED_SET_DEFAULT; 00196 break; 00197 } 00198 00199 if(WaterLevel < (pWaterLevel*0.95)){ 00200 00201 wait(1); 00202 long check_again = checkWeight(); 00203 if(check_again < (WaterLevel*0.98) || check_again > (WaterLevel*1.02)) 00204 break; 00205 00206 dWaterLevel = pWaterLevel - WaterLevel; 00207 ledBlink(5); 00208 waterlog += dWaterLevel; 00209 OLED_Display((float)waterlog); 00210 00211 sendRemoteXbee(xbee, remoteDevice64b, SENDDATA,dWaterLevel); 00212 pWaterLevel = WaterLevel; 00213 } 00214 else if(WaterLevel > pWaterLevel){ 00215 wait(1); 00216 long check_again = checkWeight(); 00217 if(check_again < (WaterLevel*0.98) || check_again > (WaterLevel*1.02)) 00218 break; 00219 pWaterLevel = WaterLevel; 00220 } 00221 break; 00222 case WAIT_INPUT: 00223 default: 00224 break; 00225 } 00226 00227 00228 wait_ms(100); 00229 } 00230 00231 00232 } 00233 00234 00235 void OLED_Display(float val) 00236 { 00237 00238 if(val < MAX_WATER*0.03) 00239 Draw_OLED_log(0); 00240 else if(val < MAX_WATER*0.10) 00241 Draw_OLED_log(10); 00242 else if(val < MAX_WATER*0.30) 00243 Draw_OLED_log(30); 00244 else if(val < MAX_WATER*0.40) 00245 Draw_OLED_log(40); 00246 else if(val < MAX_WATER*0.60) 00247 Draw_OLED_log(60); 00248 else if(val < MAX_WATER*0.70) 00249 Draw_OLED_log(70); 00250 else if(val < MAX_WATER*0.80) 00251 Draw_OLED_log(80); 00252 else if(val < MAX_WATER*0.90) 00253 Draw_OLED_log(90); 00254 else if(val < MAX_WATER) 00255 Draw_OLED_log(100); 00256 else 00257 Draw_OLED_log(0); 00258 00259 } 00260 00261 void Draw_OLED_log(uint8_t val) 00262 { 00263 SeeedGrayOled.setTextXY(0, 1); 00264 SeeedGrayOled.putString("more water"); 00265 00266 SeeedGrayOled.setTextXY(11, 0); 00267 SeeedGrayOled.putString("Reach to 2L"); 00268 00269 switch(val) 00270 { 00271 case 0: 00272 SeeedGrayOled.setTextXY(1, 0); 00273 SeeedGrayOled.putString("==========="); 00274 SeeedGrayOled.setTextXY(2, 0); 00275 SeeedGrayOled.putString("| |"); 00276 SeeedGrayOled.setTextXY(3, 0); 00277 SeeedGrayOled.putString("| |"); 00278 SeeedGrayOled.setTextXY(4, 0); 00279 SeeedGrayOled.putString("| |"); 00280 SeeedGrayOled.setTextXY(5, 0); 00281 SeeedGrayOled.putString("| 0 % |"); 00282 SeeedGrayOled.setTextXY(6, 0); 00283 SeeedGrayOled.putString("| |"); 00284 SeeedGrayOled.setTextXY(7, 0); 00285 SeeedGrayOled.putString("| |"); 00286 SeeedGrayOled.setTextXY(8, 0); 00287 SeeedGrayOled.putString("| |"); 00288 SeeedGrayOled.setTextXY(9, 0); 00289 SeeedGrayOled.putString("| |"); 00290 SeeedGrayOled.setTextXY(10, 0); 00291 SeeedGrayOled.putString("-----------"); 00292 break; 00293 case 10: 00294 SeeedGrayOled.setTextXY(1, 0); 00295 SeeedGrayOled.putString("==========="); 00296 SeeedGrayOled.setTextXY(2, 0); 00297 SeeedGrayOled.putString("| |"); 00298 SeeedGrayOled.setTextXY(3, 0); 00299 SeeedGrayOled.putString("| |"); 00300 SeeedGrayOled.setTextXY(4, 0); 00301 SeeedGrayOled.putString("| |"); 00302 SeeedGrayOled.setTextXY(5, 0); 00303 SeeedGrayOled.putString("| 10 % |"); 00304 SeeedGrayOled.setTextXY(6, 0); 00305 SeeedGrayOled.putString("| |"); 00306 SeeedGrayOled.setTextXY(7, 0); 00307 SeeedGrayOled.putString("| |"); 00308 SeeedGrayOled.setTextXY(8, 0); 00309 SeeedGrayOled.putString("| |"); 00310 SeeedGrayOled.setTextXY(9, 0); 00311 SeeedGrayOled.putString("|~~~~~~~~~|"); 00312 SeeedGrayOled.setTextXY(10, 0); 00313 SeeedGrayOled.putString("-----------"); 00314 break; 00315 case 30: 00316 SeeedGrayOled.setTextXY(1, 0); 00317 SeeedGrayOled.putString("==========="); 00318 SeeedGrayOled.setTextXY(2, 0); 00319 SeeedGrayOled.putString("| |"); 00320 SeeedGrayOled.setTextXY(3, 0); 00321 SeeedGrayOled.putString("| |"); 00322 SeeedGrayOled.setTextXY(4, 0); 00323 SeeedGrayOled.putString("| |"); 00324 SeeedGrayOled.setTextXY(5, 0); 00325 SeeedGrayOled.putString("| 30 % |"); 00326 SeeedGrayOled.setTextXY(6, 0); 00327 SeeedGrayOled.putString("| |"); 00328 SeeedGrayOled.setTextXY(7, 0); 00329 SeeedGrayOled.putString("| |"); 00330 SeeedGrayOled.setTextXY(8, 0); 00331 SeeedGrayOled.putString("|~~~~~~~~~|"); 00332 SeeedGrayOled.setTextXY(9, 0); 00333 SeeedGrayOled.putString("|~~~~~~~~~|"); 00334 SeeedGrayOled.setTextXY(10, 0); 00335 SeeedGrayOled.putString("-----------"); 00336 break; 00337 case 40: 00338 SeeedGrayOled.setTextXY(1, 0); 00339 SeeedGrayOled.putString("==========="); 00340 SeeedGrayOled.setTextXY(2, 0); 00341 SeeedGrayOled.putString("| |"); 00342 SeeedGrayOled.setTextXY(3, 0); 00343 SeeedGrayOled.putString("| |"); 00344 SeeedGrayOled.setTextXY(4, 0); 00345 SeeedGrayOled.putString("| |"); 00346 SeeedGrayOled.setTextXY(5, 0); 00347 SeeedGrayOled.putString("| 40 % |"); 00348 SeeedGrayOled.setTextXY(6, 0); 00349 SeeedGrayOled.putString("| |"); 00350 SeeedGrayOled.setTextXY(7, 0); 00351 SeeedGrayOled.putString("|~~~~~~~~~|"); 00352 SeeedGrayOled.setTextXY(8, 0); 00353 SeeedGrayOled.putString("|~~~~~~~~~|"); 00354 SeeedGrayOled.setTextXY(9, 0); 00355 SeeedGrayOled.putString("|~~~~~~~~~|"); 00356 SeeedGrayOled.setTextXY(10, 0); 00357 SeeedGrayOled.putString("-----------"); 00358 break; 00359 case 60: 00360 SeeedGrayOled.setTextXY(1, 0); 00361 SeeedGrayOled.putString("==========="); 00362 SeeedGrayOled.setTextXY(2, 0); 00363 SeeedGrayOled.putString("| |"); 00364 SeeedGrayOled.setTextXY(3, 0); 00365 SeeedGrayOled.putString("| |"); 00366 SeeedGrayOled.setTextXY(4, 0); 00367 SeeedGrayOled.putString("| |"); 00368 SeeedGrayOled.setTextXY(5, 0); 00369 SeeedGrayOled.putString("| 60 % |"); 00370 SeeedGrayOled.setTextXY(6, 0); 00371 SeeedGrayOled.putString("|~~~~~~~~~|"); 00372 SeeedGrayOled.setTextXY(7, 0); 00373 SeeedGrayOled.putString("|~~~~~~~~~|"); 00374 SeeedGrayOled.setTextXY(8, 0); 00375 SeeedGrayOled.putString("|~~~~~~~~~|"); 00376 SeeedGrayOled.setTextXY(9, 0); 00377 SeeedGrayOled.putString("|~~~~~~~~~|"); 00378 SeeedGrayOled.setTextXY(10, 0); 00379 SeeedGrayOled.putString("-----------"); 00380 break; 00381 case 70: 00382 SeeedGrayOled.setTextXY(1, 0); 00383 SeeedGrayOled.putString("==========="); 00384 SeeedGrayOled.setTextXY(2, 0); 00385 SeeedGrayOled.putString("| |"); 00386 SeeedGrayOled.setTextXY(3, 0); 00387 SeeedGrayOled.putString("| |"); 00388 SeeedGrayOled.setTextXY(4, 0); 00389 SeeedGrayOled.putString("| |"); 00390 SeeedGrayOled.setTextXY(5, 0); 00391 SeeedGrayOled.putString("|~~ 70 %~~|"); 00392 SeeedGrayOled.setTextXY(6, 0); 00393 SeeedGrayOled.putString("|~~~~~~~~~|"); 00394 SeeedGrayOled.setTextXY(7, 0); 00395 SeeedGrayOled.putString("|~~~~~~~~~|"); 00396 SeeedGrayOled.setTextXY(8, 0); 00397 SeeedGrayOled.putString("|~~~~~~~~~|"); 00398 SeeedGrayOled.setTextXY(9, 0); 00399 SeeedGrayOled.putString("|~~~~~~~~~|"); 00400 SeeedGrayOled.setTextXY(10, 0); 00401 SeeedGrayOled.putString("-----------"); 00402 break; 00403 case 80: 00404 SeeedGrayOled.setTextXY(1, 0); 00405 SeeedGrayOled.putString("==========="); 00406 SeeedGrayOled.setTextXY(2, 0); 00407 SeeedGrayOled.putString("| |"); 00408 SeeedGrayOled.setTextXY(3, 0); 00409 SeeedGrayOled.putString("| |"); 00410 SeeedGrayOled.setTextXY(4, 0); 00411 SeeedGrayOled.putString("|~~~~~~~~~|"); 00412 SeeedGrayOled.setTextXY(5, 0); 00413 SeeedGrayOled.putString("|~~ 80 %~~|"); 00414 SeeedGrayOled.setTextXY(6, 0); 00415 SeeedGrayOled.putString("|~~~~~~~~~|"); 00416 SeeedGrayOled.setTextXY(7, 0); 00417 SeeedGrayOled.putString("|~~~~~~~~~|"); 00418 SeeedGrayOled.setTextXY(8, 0); 00419 SeeedGrayOled.putString("|~~~~~~~~~|"); 00420 SeeedGrayOled.setTextXY(9, 0); 00421 SeeedGrayOled.putString("|~~~~~~~~~|"); 00422 SeeedGrayOled.setTextXY(10, 0); 00423 SeeedGrayOled.putString("-----------"); 00424 break; 00425 case 90: 00426 SeeedGrayOled.setTextXY(1, 0); 00427 SeeedGrayOled.putString("==========="); 00428 SeeedGrayOled.setTextXY(2, 0); 00429 SeeedGrayOled.putString("| |"); 00430 SeeedGrayOled.setTextXY(3, 0); 00431 SeeedGrayOled.putString("|~~~~~~~~~|"); 00432 SeeedGrayOled.setTextXY(4, 0); 00433 SeeedGrayOled.putString("|~~~~~~~~~|"); 00434 SeeedGrayOled.setTextXY(5, 0); 00435 SeeedGrayOled.putString("|~~ 90 %~~|"); 00436 SeeedGrayOled.setTextXY(6, 0); 00437 SeeedGrayOled.putString("|~~~~~~~~~|"); 00438 SeeedGrayOled.setTextXY(7, 0); 00439 SeeedGrayOled.putString("|~~~~~~~~~|"); 00440 SeeedGrayOled.setTextXY(8, 0); 00441 SeeedGrayOled.putString("|~~~~~~~~~|"); 00442 SeeedGrayOled.setTextXY(9, 0); 00443 SeeedGrayOled.putString("|~~~~~~~~~|"); 00444 SeeedGrayOled.setTextXY(10, 0); 00445 SeeedGrayOled.putString("-----------"); 00446 break; 00447 case 100: 00448 SeeedGrayOled.setTextXY(1, 0); 00449 SeeedGrayOled.putString("==========="); 00450 SeeedGrayOled.setTextXY(2, 0); 00451 SeeedGrayOled.putString("|~~~~~~~~~|"); 00452 SeeedGrayOled.setTextXY(3, 0); 00453 SeeedGrayOled.putString("|~~~~~~~~~|"); 00454 SeeedGrayOled.setTextXY(4, 0); 00455 SeeedGrayOled.putString("|~~~~~~~~~|"); 00456 SeeedGrayOled.setTextXY(5, 0); 00457 SeeedGrayOled.putString("|~~100 %~~|"); 00458 SeeedGrayOled.setTextXY(6, 0); 00459 SeeedGrayOled.putString("|~~~~~~~~~|"); 00460 SeeedGrayOled.setTextXY(7, 0); 00461 SeeedGrayOled.putString("|~~~~~~~~~|"); 00462 SeeedGrayOled.setTextXY(8, 0); 00463 SeeedGrayOled.putString("|~~~~~~~~~|"); 00464 SeeedGrayOled.setTextXY(9, 0); 00465 SeeedGrayOled.putString("|~~~~~~~~~|"); 00466 SeeedGrayOled.setTextXY(10, 0); 00467 SeeedGrayOled.putString("-----------"); 00468 break; 00469 default: // 20 00470 SeeedGrayOled.setTextXY(2, 0); 00471 SeeedGrayOled.putString("-----------"); 00472 SeeedGrayOled.setTextXY(3, 0); 00473 SeeedGrayOled.putString("| |"); 00474 SeeedGrayOled.setTextXY(4, 0); 00475 SeeedGrayOled.putString("| |"); 00476 SeeedGrayOled.setTextXY(5, 0); 00477 SeeedGrayOled.putString("| 0 % |"); 00478 SeeedGrayOled.setTextXY(6, 0); 00479 SeeedGrayOled.putString("| |"); 00480 SeeedGrayOled.setTextXY(7, 0); 00481 SeeedGrayOled.putString("| ~"); 00482 SeeedGrayOled.setTextXY(8, 0); 00483 SeeedGrayOled.putString("| |"); 00484 SeeedGrayOled.setTextXY(9, 0); 00485 SeeedGrayOled.putString("| |"); 00486 SeeedGrayOled.setTextXY(10, 0); 00487 SeeedGrayOled.putString("-----------"); 00488 break; 00489 } 00490 } 00491 00492 00493
Generated on Fri Jul 22 2022 20:35:21 by 1.7.2