SUNGTAE KIM
/
WIZwiki-W7500_LoRa_Dust_Roadsign
Dust Display Road Sign using Lora Connectivity
main.cpp@0:ee87c3246381, 2018-10-30 (annotated)
- Committer:
- stkim92
- Date:
- Tue Oct 30 06:26:12 2018 +0000
- Revision:
- 0:ee87c3246381
Dust Display Road Sign using LoRa Connectivity
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stkim92 | 0:ee87c3246381 | 1 | #include "mbed.h" |
stkim92 | 0:ee87c3246381 | 2 | #include "kor_char_code.h" |
stkim92 | 0:ee87c3246381 | 3 | #include <stdint.h> |
stkim92 | 0:ee87c3246381 | 4 | |
stkim92 | 0:ee87c3246381 | 5 | #define COV_RATIO 0.2 //ug/mmm / mv |
stkim92 | 0:ee87c3246381 | 6 | #define NO_DUST_VOLTAGE 400 //mv |
stkim92 | 0:ee87c3246381 | 7 | #define SYS_VOLTAGE 5000 |
stkim92 | 0:ee87c3246381 | 8 | |
stkim92 | 0:ee87c3246381 | 9 | Serial pc(USBTX,USBRX); // tx, rx |
stkim92 | 0:ee87c3246381 | 10 | Serial TFT(PC_02,PC_03); // tx, rx |
stkim92 | 0:ee87c3246381 | 11 | Serial lora(PA_13,PA_14); // tx, rx |
stkim92 | 0:ee87c3246381 | 12 | Timer timer; |
stkim92 | 0:ee87c3246381 | 13 | AnalogIn dustVal(A0); |
stkim92 | 0:ee87c3246381 | 14 | DigitalOut myled(D6); |
stkim92 | 0:ee87c3246381 | 15 | int flag_first = 0; |
stkim92 | 0:ee87c3246381 | 16 | |
stkim92 | 0:ee87c3246381 | 17 | int calibration(int m) |
stkim92 | 0:ee87c3246381 | 18 | { |
stkim92 | 0:ee87c3246381 | 19 | static int _buff[10], sum; |
stkim92 | 0:ee87c3246381 | 20 | const int _buff_max = 10; |
stkim92 | 0:ee87c3246381 | 21 | int i; |
stkim92 | 0:ee87c3246381 | 22 | |
stkim92 | 0:ee87c3246381 | 23 | if(flag_first == 0) |
stkim92 | 0:ee87c3246381 | 24 | { |
stkim92 | 0:ee87c3246381 | 25 | flag_first = 1; |
stkim92 | 0:ee87c3246381 | 26 | |
stkim92 | 0:ee87c3246381 | 27 | for(i = 0, sum = 0; i < _buff_max; i++) |
stkim92 | 0:ee87c3246381 | 28 | { |
stkim92 | 0:ee87c3246381 | 29 | _buff[i] = m; |
stkim92 | 0:ee87c3246381 | 30 | sum += _buff[i]; |
stkim92 | 0:ee87c3246381 | 31 | } |
stkim92 | 0:ee87c3246381 | 32 | return m; |
stkim92 | 0:ee87c3246381 | 33 | } |
stkim92 | 0:ee87c3246381 | 34 | else |
stkim92 | 0:ee87c3246381 | 35 | { |
stkim92 | 0:ee87c3246381 | 36 | sum -= _buff[0]; |
stkim92 | 0:ee87c3246381 | 37 | for(i = 0; i < (_buff_max - 1); i++) |
stkim92 | 0:ee87c3246381 | 38 | { |
stkim92 | 0:ee87c3246381 | 39 | _buff[i] = _buff[i + 1]; |
stkim92 | 0:ee87c3246381 | 40 | } |
stkim92 | 0:ee87c3246381 | 41 | _buff[9] = m; |
stkim92 | 0:ee87c3246381 | 42 | sum += _buff[9]; |
stkim92 | 0:ee87c3246381 | 43 | |
stkim92 | 0:ee87c3246381 | 44 | i = sum / 10.0; |
stkim92 | 0:ee87c3246381 | 45 | return i; |
stkim92 | 0:ee87c3246381 | 46 | } |
stkim92 | 0:ee87c3246381 | 47 | } |
stkim92 | 0:ee87c3246381 | 48 | |
stkim92 | 0:ee87c3246381 | 49 | |
stkim92 | 0:ee87c3246381 | 50 | int main() |
stkim92 | 0:ee87c3246381 | 51 | { |
stkim92 | 0:ee87c3246381 | 52 | char buffer[200]; |
stkim92 | 0:ee87c3246381 | 53 | TFT.baud(115200); |
stkim92 | 0:ee87c3246381 | 54 | lora.baud(38400); |
stkim92 | 0:ee87c3246381 | 55 | unsigned int curr_Time1; |
stkim92 | 0:ee87c3246381 | 56 | unsigned int curr_Time2; |
stkim92 | 0:ee87c3246381 | 57 | float analogVal =0; |
stkim92 | 0:ee87c3246381 | 58 | int analogVal_u16 =0; |
stkim92 | 0:ee87c3246381 | 59 | float digitalVal =0; |
stkim92 | 0:ee87c3246381 | 60 | float dustDensity =0; |
stkim92 | 0:ee87c3246381 | 61 | int samplingTime = 280; |
stkim92 | 0:ee87c3246381 | 62 | int deltaTime = 40; |
stkim92 | 0:ee87c3246381 | 63 | int sleepTime = 9680; |
stkim92 | 0:ee87c3246381 | 64 | int x=1; |
stkim92 | 0:ee87c3246381 | 65 | int y=1; |
stkim92 | 0:ee87c3246381 | 66 | int w = 500; |
stkim92 | 0:ee87c3246381 | 67 | int h = 220; |
stkim92 | 0:ee87c3246381 | 68 | int red = 255; |
stkim92 | 0:ee87c3246381 | 69 | int green = 255; |
stkim92 | 0:ee87c3246381 | 70 | int blue = 255; |
stkim92 | 0:ee87c3246381 | 71 | int fill=1; |
stkim92 | 0:ee87c3246381 | 72 | |
stkim92 | 0:ee87c3246381 | 73 | timer.start(); |
stkim92 | 0:ee87c3246381 | 74 | curr_Time1 = timer.read(); |
stkim92 | 0:ee87c3246381 | 75 | TFT.printf("$l,0,%d,%d,%d#",red,green,blue); // 사각형 색(흰색) 지정 |
stkim92 | 0:ee87c3246381 | 76 | lora.printf("AT+RST\r\n"); |
stkim92 | 0:ee87c3246381 | 77 | |
stkim92 | 0:ee87c3246381 | 78 | while(1) |
stkim92 | 0:ee87c3246381 | 79 | { |
stkim92 | 0:ee87c3246381 | 80 | /* |
stkim92 | 0:ee87c3246381 | 81 | if(lora.readable()){ |
stkim92 | 0:ee87c3246381 | 82 | lora.gets(buffer,40); |
stkim92 | 0:ee87c3246381 | 83 | |
stkim92 | 0:ee87c3246381 | 84 | if( strstr(buffer,"JOINED\r\n") == NULL ){ |
stkim92 | 0:ee87c3246381 | 85 | TFT.printf("$f,4,1,#"); // 글자 크기 조절 |
stkim92 | 0:ee87c3246381 | 86 | TFT.printf("$f,0,Not joined ,1,120,#\r\n"); |
stkim92 | 0:ee87c3246381 | 87 | continue; |
stkim92 | 0:ee87c3246381 | 88 | } |
stkim92 | 0:ee87c3246381 | 89 | |
stkim92 | 0:ee87c3246381 | 90 | if( strstr(buffer,"JOINED\r\n") != NULL ){ |
stkim92 | 0:ee87c3246381 | 91 | TFT.printf("$f,0,joined !!! ,1,140,#"); |
stkim92 | 0:ee87c3246381 | 92 | continue; |
stkim92 | 0:ee87c3246381 | 93 | } |
stkim92 | 0:ee87c3246381 | 94 | |
stkim92 | 0:ee87c3246381 | 95 | }*/ |
stkim92 | 0:ee87c3246381 | 96 | TFT.printf("$f,4,2,#"); // 글자 크기 조절 |
stkim92 | 0:ee87c3246381 | 97 | curr_Time2 = timer.read(); |
stkim92 | 0:ee87c3246381 | 98 | |
stkim92 | 0:ee87c3246381 | 99 | |
stkim92 | 0:ee87c3246381 | 100 | if( curr_Time2-curr_Time1 == 1) { |
stkim92 | 0:ee87c3246381 | 101 | |
stkim92 | 0:ee87c3246381 | 102 | myled = 0; |
stkim92 | 0:ee87c3246381 | 103 | wait_us(samplingTime); |
stkim92 | 0:ee87c3246381 | 104 | |
stkim92 | 0:ee87c3246381 | 105 | analogVal = dustVal.read(); |
stkim92 | 0:ee87c3246381 | 106 | analogVal_u16 = dustVal.read_u16(); |
stkim92 | 0:ee87c3246381 | 107 | wait_us(deltaTime); |
stkim92 | 0:ee87c3246381 | 108 | myled = 1; |
stkim92 | 0:ee87c3246381 | 109 | wait_us(sleepTime); |
stkim92 | 0:ee87c3246381 | 110 | |
stkim92 | 0:ee87c3246381 | 111 | analogVal_u16 = calibration(analogVal_u16); // 보정 |
stkim92 | 0:ee87c3246381 | 112 | |
stkim92 | 0:ee87c3246381 | 113 | //digitalVal = analogVal_u16 *(5.0/4096); |
stkim92 | 0:ee87c3246381 | 114 | digitalVal = (SYS_VOLTAGE / 4096.0) * analogVal_u16; //adc변 (mv) |
stkim92 | 0:ee87c3246381 | 115 | // dustDensity = (0.17*digitalVal) -0.1; |
stkim92 | 0:ee87c3246381 | 116 | |
stkim92 | 0:ee87c3246381 | 117 | if(digitalVal >= NO_DUST_VOLTAGE) |
stkim92 | 0:ee87c3246381 | 118 | { |
stkim92 | 0:ee87c3246381 | 119 | digitalVal -= NO_DUST_VOLTAGE; |
stkim92 | 0:ee87c3246381 | 120 | dustDensity = digitalVal * COV_RATIO; // Voltage(mv)값을 dust density로 변(ug/m3) |
stkim92 | 0:ee87c3246381 | 121 | dustDensity -= 50; |
stkim92 | 0:ee87c3246381 | 122 | } |
stkim92 | 0:ee87c3246381 | 123 | else |
stkim92 | 0:ee87c3246381 | 124 | { |
stkim92 | 0:ee87c3246381 | 125 | dustDensity = 0; |
stkim92 | 0:ee87c3246381 | 126 | } |
stkim92 | 0:ee87c3246381 | 127 | |
stkim92 | 0:ee87c3246381 | 128 | |
stkim92 | 0:ee87c3246381 | 129 | TFT.printf("$l,2,%d,%d,%d,%d,%d#",x,y,w,h,fill==0?0:1); //흰 사각형 출력(지우기) |
stkim92 | 0:ee87c3246381 | 130 | TFT.printf("$f,0,Fine Dust(PM 2.5),1,1,#\r\n"); |
stkim92 | 0:ee87c3246381 | 131 | TFT.printf("$f,0,Value : %f (ug/m3),1,30,#\r\n",dustDensity); |
stkim92 | 0:ee87c3246381 | 132 | TFT.printf("$f,0,current Time : %f (s),1,60,#\r\n",timer.read()); |
stkim92 | 0:ee87c3246381 | 133 | |
stkim92 | 0:ee87c3246381 | 134 | if( (dustDensity>=0) && (dustDensity<=15) ) |
stkim92 | 0:ee87c3246381 | 135 | { |
stkim92 | 0:ee87c3246381 | 136 | TFT.printf("$f,0,STATUS :,1,90,#\r\n"); |
stkim92 | 0:ee87c3246381 | 137 | TFT.printf("$f,1,0,174,240#\r\n"); |
stkim92 | 0:ee87c3246381 | 138 | TFT.printf("$f,0,GOOD :> ,140,90,#\r\n"); |
stkim92 | 0:ee87c3246381 | 139 | TFT.printf("$f,1,0,0,0#\r\n"); |
stkim92 | 0:ee87c3246381 | 140 | |
stkim92 | 0:ee87c3246381 | 141 | } |
stkim92 | 0:ee87c3246381 | 142 | |
stkim92 | 0:ee87c3246381 | 143 | else if( (dustDensity>=16) && (dustDensity<=50) ) |
stkim92 | 0:ee87c3246381 | 144 | { |
stkim92 | 0:ee87c3246381 | 145 | TFT.printf("$f,0,STATUS :,1,90,#\r\n"); |
stkim92 | 0:ee87c3246381 | 146 | TFT.printf("$f,1,120,254,98#\r\n"); |
stkim92 | 0:ee87c3246381 | 147 | TFT.printf("$f,0,NORMAL :) ,140,90,#\r\n"); |
stkim92 | 0:ee87c3246381 | 148 | TFT.printf("$f,1,0,0,0#\r\n"); |
stkim92 | 0:ee87c3246381 | 149 | } |
stkim92 | 0:ee87c3246381 | 150 | |
stkim92 | 0:ee87c3246381 | 151 | else if( (dustDensity>=51) && (dustDensity<=100) ) |
stkim92 | 0:ee87c3246381 | 152 | { |
stkim92 | 0:ee87c3246381 | 153 | TFT.printf("$f,0,STATUS :,1,90,#\r\n"); |
stkim92 | 0:ee87c3246381 | 154 | TFT.printf("$f,1,255,255,75#\r\n"); |
stkim92 | 0:ee87c3246381 | 155 | TFT.printf("$f,0,BAD :( ,140,90,#\r\n"); |
stkim92 | 0:ee87c3246381 | 156 | TFT.printf("$f,1,0,0,0#\r\n"); |
stkim92 | 0:ee87c3246381 | 157 | } |
stkim92 | 0:ee87c3246381 | 158 | |
stkim92 | 0:ee87c3246381 | 159 | else if(dustDensity>=101) |
stkim92 | 0:ee87c3246381 | 160 | { |
stkim92 | 0:ee87c3246381 | 161 | TFT.printf("$f,0,STATUS :,1,90,#\r\n"); |
stkim92 | 0:ee87c3246381 | 162 | TFT.printf("$f,1,255,51,51#\r\n"); |
stkim92 | 0:ee87c3246381 | 163 | TFT.printf("$f,0,DANGER :<,140,90,#\r\n"); |
stkim92 | 0:ee87c3246381 | 164 | TFT.printf("$f,1,0,0,0#\r\n"); |
stkim92 | 0:ee87c3246381 | 165 | } |
stkim92 | 0:ee87c3246381 | 166 | else |
stkim92 | 0:ee87c3246381 | 167 | { |
stkim92 | 0:ee87c3246381 | 168 | TFT.printf("$f,0,STATUS :,1,90,#\r\n"); |
stkim92 | 0:ee87c3246381 | 169 | TFT.printf("$f,1,0,0,0#\r\n"); |
stkim92 | 0:ee87c3246381 | 170 | TFT.printf("$f,0,WAIT !,140,90,#\r\n"); |
stkim92 | 0:ee87c3246381 | 171 | TFT.printf("$f,1,0,0,0#\r\n"); |
stkim92 | 0:ee87c3246381 | 172 | } |
stkim92 | 0:ee87c3246381 | 173 | |
stkim92 | 0:ee87c3246381 | 174 | |
stkim92 | 0:ee87c3246381 | 175 | //lora.printf("AT+SEND 01015445//\r\n"); |
stkim92 | 0:ee87c3246381 | 176 | |
stkim92 | 0:ee87c3246381 | 177 | curr_Time1 = curr_Time2; |
stkim92 | 0:ee87c3246381 | 178 | } |
stkim92 | 0:ee87c3246381 | 179 | } |
stkim92 | 0:ee87c3246381 | 180 | } |
stkim92 | 0:ee87c3246381 | 181 | |
stkim92 | 0:ee87c3246381 | 182 |