Data logger with STM32 F7 spi,sdcard,wifi working. FTP not yet
Dependencies: BSP_DISCO_F746NG LCD_DISCO_F746NG NSAPITests NetworkSocketAPI RTC-DS1307 SDFileSystem SD_DISCO_F746NG TS_DISCO_F746NG X_NUCLEO_IDW01M1v2 mbed-dev mbed-rtos
main.cpp@12:4b39c6fb8f21, 2017-01-26 (annotated)
- Committer:
- MikGa87
- Date:
- Thu Jan 26 10:22:01 2017 +0000
- Revision:
- 12:4b39c6fb8f21
- Parent:
- 11:0556197a0641
V.Laurea
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikGa87 | 0:1a41a881bf24 | 1 | #include "mbed.h" |
MikGa87 | 0:1a41a881bf24 | 2 | #include "Serial.h" |
MikGa87 | 0:1a41a881bf24 | 3 | #include "LCD_DISCO_F746NG.h" |
MikGa87 | 0:1a41a881bf24 | 4 | #include "TS_DISCO_F746NG.h" |
MikGa87 | 0:1a41a881bf24 | 5 | #include "SD_DISCO_F746NG.h" |
MikGa87 | 0:1a41a881bf24 | 6 | #include "SpwfInterface.h" |
MikGa87 | 0:1a41a881bf24 | 7 | #include "SDFileSystem.h" |
MikGa87 | 0:1a41a881bf24 | 8 | #include "TCPSocket.h" |
MikGa87 | 1:5eb93d7d6eca | 9 | #include "Rtc_Ds1307.h" |
MikGa87 | 1:5eb93d7d6eca | 10 | |
MikGa87 | 1:5eb93d7d6eca | 11 | |
MikGa87 | 1:5eb93d7d6eca | 12 | |
MikGa87 | 0:1a41a881bf24 | 13 | |
MikGa87 | 0:1a41a881bf24 | 14 | |
MikGa87 | 0:1a41a881bf24 | 15 | //NUCLEO: D1->UART6_TX , D0->UART6_RX |
MikGa87 | 1:5eb93d7d6eca | 16 | SpwfSAInterface wifi(D1, D0, false); |
MikGa87 | 5:bb9095a49526 | 17 | TCPSocket socket,socket2; |
MikGa87 | 0:1a41a881bf24 | 18 | |
MikGa87 | 0:1a41a881bf24 | 19 | |
MikGa87 | 0:1a41a881bf24 | 20 | LCD_DISCO_F746NG lcd; |
MikGa87 | 0:1a41a881bf24 | 21 | TS_DISCO_F746NG ts; |
MikGa87 | 0:1a41a881bf24 | 22 | SDFileSystem sdc(D7, D8, D13, D2, "sd"); // MOSI, MISO, SCK, CS |
MikGa87 | 0:1a41a881bf24 | 23 | SPI spi(A2, A3, A4); // mosi, miso, sclk |
MikGa87 | 0:1a41a881bf24 | 24 | DigitalOut chipSelect(D4); |
MikGa87 | 0:1a41a881bf24 | 25 | Serial pc(USBTX, USBRX); // tx, rx |
MikGa87 | 0:1a41a881bf24 | 26 | DigitalIn button(USER_BUTTON); |
MikGa87 | 1:5eb93d7d6eca | 27 | //timer |
MikGa87 | 1:5eb93d7d6eca | 28 | Rtc_Ds1307 rtc(PB_9, PB_8); |
MikGa87 | 0:1a41a881bf24 | 29 | |
MikGa87 | 1:5eb93d7d6eca | 30 | |
MikGa87 | 1:5eb93d7d6eca | 31 | |
MikGa87 | 1:5eb93d7d6eca | 32 | //uint8_t tabellachip[3]; |
MikGa87 | 1:5eb93d7d6eca | 33 | uint8_t stop=0; |
MikGa87 | 0:1a41a881bf24 | 34 | uint16_t Xpos=0; |
MikGa87 | 0:1a41a881bf24 | 35 | uint16_t Ypos=0; |
MikGa87 | 0:1a41a881bf24 | 36 | char c; |
MikGa87 | 0:1a41a881bf24 | 37 | char temp='#'; |
MikGa87 | 3:07ca6890f7b8 | 38 | bool sdck=0; |
MikGa87 | 0:1a41a881bf24 | 39 | uint8_t setpage=0; |
MikGa87 | 1:5eb93d7d6eca | 40 | uint8_t channel=0; |
MikGa87 | 0:1a41a881bf24 | 41 | uint8_t logrun=0; |
MikGa87 | 1:5eb93d7d6eca | 42 | char serverip[12]; |
MikGa87 | 4:7ee8caacbf20 | 43 | char USER[15]; |
MikGa87 | 4:7ee8caacbf20 | 44 | char PASSWORD[12]; |
MikGa87 | 7:951e8154af62 | 45 | int controllo=0; |
MikGa87 | 4:7ee8caacbf20 | 46 | |
MikGa87 | 3:07ca6890f7b8 | 47 | |
MikGa87 | 3:07ca6890f7b8 | 48 | /////////Variabili Gestione File/////////// |
MikGa87 | 3:07ca6890f7b8 | 49 | char filename1[100]; |
MikGa87 | 3:07ca6890f7b8 | 50 | char filename2[100]; |
MikGa87 | 3:07ca6890f7b8 | 51 | char filename3[100]; |
MikGa87 | 3:07ca6890f7b8 | 52 | |
MikGa87 | 3:07ca6890f7b8 | 53 | ///////Variabili WIFI//////// |
MikGa87 | 3:07ca6890f7b8 | 54 | int32_t connected; |
MikGa87 | 3:07ca6890f7b8 | 55 | char * ssid = "dlink"; |
MikGa87 | 3:07ca6890f7b8 | 56 | char * seckey = "STMdemoPWD"; |
MikGa87 | 3:07ca6890f7b8 | 57 | const char *mac; |
MikGa87 | 3:07ca6890f7b8 | 58 | const char *ip; |
MikGa87 | 4:7ee8caacbf20 | 59 | char str[4096]; |
MikGa87 | 4:7ee8caacbf20 | 60 | |
MikGa87 | 3:07ca6890f7b8 | 61 | |
MikGa87 | 3:07ca6890f7b8 | 62 | ///////Variabili Touch screen//////// |
MikGa87 | 3:07ca6890f7b8 | 63 | uint16_t x, y; |
MikGa87 | 3:07ca6890f7b8 | 64 | uint8_t text[30]; |
MikGa87 | 3:07ca6890f7b8 | 65 | uint8_t status_ts; |
MikGa87 | 3:07ca6890f7b8 | 66 | uint8_t idx=0; |
MikGa87 | 3:07ca6890f7b8 | 67 | uint8_t cleared = 0; |
MikGa87 | 3:07ca6890f7b8 | 68 | uint8_t prev_nb_touches = 0; |
MikGa87 | 3:07ca6890f7b8 | 69 | |
MikGa87 | 3:07ca6890f7b8 | 70 | |
MikGa87 | 3:07ca6890f7b8 | 71 | ////////Variabili Timer/////// |
MikGa87 | 3:07ca6890f7b8 | 72 | int year; |
MikGa87 | 3:07ca6890f7b8 | 73 | int mon; |
MikGa87 | 3:07ca6890f7b8 | 74 | int date; |
MikGa87 | 3:07ca6890f7b8 | 75 | int hour; |
MikGa87 | 3:07ca6890f7b8 | 76 | int min; |
MikGa87 | 3:07ca6890f7b8 | 77 | int sec; |
MikGa87 | 3:07ca6890f7b8 | 78 | |
MikGa87 | 3:07ca6890f7b8 | 79 | |
MikGa87 | 1:5eb93d7d6eca | 80 | //FILE *fp = fopen("/sd/sdtest.txt", "w"); |
MikGa87 | 1:5eb93d7d6eca | 81 | FILE *fp1; |
MikGa87 | 1:5eb93d7d6eca | 82 | FILE *fp2; |
MikGa87 | 1:5eb93d7d6eca | 83 | FILE *fp3; |
MikGa87 | 11:0556197a0641 | 84 | //FILE *foto; |
MikGa87 | 0:1a41a881bf24 | 85 | |
MikGa87 | 3:07ca6890f7b8 | 86 | |
MikGa87 | 3:07ca6890f7b8 | 87 | ////////////////INTERFACCIA GRAFICA/////////////// |
MikGa87 | 3:07ca6890f7b8 | 88 | ////////////////////////////////////////////////// |
MikGa87 | 3:07ca6890f7b8 | 89 | /*Elenco Pagine |
MikGa87 | 3:07ca6890f7b8 | 90 | menu_page setpage=0 |
MikGa87 | 3:07ca6890f7b8 | 91 | logging_page setpage=1 |
MikGa87 | 3:07ca6890f7b8 | 92 | */ |
MikGa87 | 3:07ca6890f7b8 | 93 | |
MikGa87 | 4:7ee8caacbf20 | 94 | void set_gps_page(){ |
MikGa87 | 4:7ee8caacbf20 | 95 | |
MikGa87 | 4:7ee8caacbf20 | 96 | lcd.Clear(LCD_COLOR_BLUE); |
MikGa87 | 4:7ee8caacbf20 | 97 | lcd.SetTextColor(LCD_COLOR_YELLOW); |
MikGa87 | 4:7ee8caacbf20 | 98 | lcd.FillRect(100, 200, 100, 50); ///Disegna bottone |
MikGa87 | 4:7ee8caacbf20 | 99 | lcd.FillRect(125, 175, 50, 100); ///Disegna bottone |
MikGa87 | 4:7ee8caacbf20 | 100 | |
MikGa87 | 4:7ee8caacbf20 | 101 | } |
MikGa87 | 3:07ca6890f7b8 | 102 | |
MikGa87 | 3:07ca6890f7b8 | 103 | void menu_page(){ |
MikGa87 | 3:07ca6890f7b8 | 104 | |
MikGa87 | 3:07ca6890f7b8 | 105 | lcd.Clear(LCD_COLOR_BLUE); |
MikGa87 | 3:07ca6890f7b8 | 106 | lcd.SetTextColor(LCD_COLOR_GREEN); |
MikGa87 | 4:7ee8caacbf20 | 107 | lcd.FillRect(10, 200, 50, 50); ///Disegna bottone Verde |
MikGa87 | 3:07ca6890f7b8 | 108 | lcd.SetTextColor(LCD_COLOR_RED); |
MikGa87 | 4:7ee8caacbf20 | 109 | lcd.FillRect(100, 200, 50, 50); ///Disegna bottone Rosso |
MikGa87 | 3:07ca6890f7b8 | 110 | lcd.SetTextColor(LCD_COLOR_WHITE); |
MikGa87 | 3:07ca6890f7b8 | 111 | lcd.SetBackColor(LCD_COLOR_BLUE); |
MikGa87 | 3:07ca6890f7b8 | 112 | lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"Menu Principale", CENTER_MODE); |
MikGa87 | 4:7ee8caacbf20 | 113 | lcd.DisplayStringAt(0, LINE(7), (uint8_t *)"Press Green Button to start logging", LEFT_MODE); |
MikGa87 | 4:7ee8caacbf20 | 114 | lcd.DisplayStringAt(0, LINE(8), (uint8_t *)"Press Red Button to stop logging", LEFT_MODE); |
MikGa87 | 3:07ca6890f7b8 | 115 | |
MikGa87 | 3:07ca6890f7b8 | 116 | |
MikGa87 | 3:07ca6890f7b8 | 117 | |
MikGa87 | 3:07ca6890f7b8 | 118 | } |
MikGa87 | 3:07ca6890f7b8 | 119 | |
MikGa87 | 3:07ca6890f7b8 | 120 | void logging_page(){ |
MikGa87 | 3:07ca6890f7b8 | 121 | |
MikGa87 | 3:07ca6890f7b8 | 122 | |
MikGa87 | 3:07ca6890f7b8 | 123 | lcd.Clear(LCD_COLOR_BLUE); |
MikGa87 | 3:07ca6890f7b8 | 124 | lcd.SetTextColor(LCD_COLOR_RED); |
MikGa87 | 3:07ca6890f7b8 | 125 | lcd.FillRect(100, 200, 50, 50); ///Disegna bottone |
MikGa87 | 3:07ca6890f7b8 | 126 | lcd.SetTextColor(LCD_COLOR_WHITE); |
MikGa87 | 3:07ca6890f7b8 | 127 | lcd.SetBackColor(LCD_COLOR_BLUE); |
MikGa87 | 3:07ca6890f7b8 | 128 | lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"Logging Process", CENTER_MODE); |
MikGa87 | 3:07ca6890f7b8 | 129 | lcd.DisplayStringAt(0, LINE(8), (uint8_t *)"Press Red Button to stop logging", LEFT_MODE); |
MikGa87 | 3:07ca6890f7b8 | 130 | wait(1); |
MikGa87 | 3:07ca6890f7b8 | 131 | lcd.SetTextColor(LCD_COLOR_WHITE); |
MikGa87 | 3:07ca6890f7b8 | 132 | lcd.DisplayStringAt(0, LINE(10), (uint8_t *)"Logging...Don't remove SD Card", CENTER_MODE); |
MikGa87 | 3:07ca6890f7b8 | 133 | |
MikGa87 | 3:07ca6890f7b8 | 134 | |
MikGa87 | 3:07ca6890f7b8 | 135 | |
MikGa87 | 3:07ca6890f7b8 | 136 | } |
MikGa87 | 3:07ca6890f7b8 | 137 | |
MikGa87 | 6:e0caf7838dbb | 138 | void server_connession_page(){ |
MikGa87 | 6:e0caf7838dbb | 139 | |
MikGa87 | 6:e0caf7838dbb | 140 | |
MikGa87 | 6:e0caf7838dbb | 141 | lcd.Clear(LCD_COLOR_BLUE); |
MikGa87 | 6:e0caf7838dbb | 142 | //lcd.SetTextColor(LCD_COLOR_RED); |
MikGa87 | 6:e0caf7838dbb | 143 | //lcd.FillRect(100, 200, 50, 50); ///Disegna bottone |
MikGa87 | 6:e0caf7838dbb | 144 | lcd.SetTextColor(LCD_COLOR_WHITE); |
MikGa87 | 6:e0caf7838dbb | 145 | lcd.SetBackColor(LCD_COLOR_BLUE); |
MikGa87 | 6:e0caf7838dbb | 146 | lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"Connessione al Server", CENTER_MODE); |
MikGa87 | 6:e0caf7838dbb | 147 | lcd.DisplayStringAt(0, LINE(8), (uint8_t *)"attendere trasferimento file", LEFT_MODE); |
MikGa87 | 6:e0caf7838dbb | 148 | wait(1); |
MikGa87 | 6:e0caf7838dbb | 149 | lcd.SetTextColor(LCD_COLOR_WHITE); |
MikGa87 | 6:e0caf7838dbb | 150 | lcd.DisplayStringAt(0, LINE(10), (uint8_t *)"Uploading...Don't remove SD Card", CENTER_MODE); |
MikGa87 | 6:e0caf7838dbb | 151 | |
MikGa87 | 6:e0caf7838dbb | 152 | |
MikGa87 | 6:e0caf7838dbb | 153 | |
MikGa87 | 6:e0caf7838dbb | 154 | } |
MikGa87 | 3:07ca6890f7b8 | 155 | |
MikGa87 | 3:07ca6890f7b8 | 156 | |
MikGa87 | 3:07ca6890f7b8 | 157 | |
MikGa87 | 3:07ca6890f7b8 | 158 | |
MikGa87 | 1:5eb93d7d6eca | 159 | |
MikGa87 | 0:1a41a881bf24 | 160 | void write_sd(char c){ |
MikGa87 | 0:1a41a881bf24 | 161 | |
MikGa87 | 0:1a41a881bf24 | 162 | |
MikGa87 | 0:1a41a881bf24 | 163 | if(logrun){ |
MikGa87 | 3:07ca6890f7b8 | 164 | if (fp1 != NULL && fp2 != NULL && fp3 != NULL && sdck) { |
MikGa87 | 10:1808c51051e5 | 165 | //it checks the communication channel and writes the character in the right file |
MikGa87 | 1:5eb93d7d6eca | 166 | switch(channel){ |
MikGa87 | 1:5eb93d7d6eca | 167 | |
MikGa87 | 1:5eb93d7d6eca | 168 | case(1): |
MikGa87 | 1:5eb93d7d6eca | 169 | fprintf(fp1,"%c",c); |
MikGa87 | 1:5eb93d7d6eca | 170 | break; |
MikGa87 | 1:5eb93d7d6eca | 171 | |
MikGa87 | 1:5eb93d7d6eca | 172 | case(2): |
MikGa87 | 1:5eb93d7d6eca | 173 | fprintf(fp2,"%c",c); |
MikGa87 | 1:5eb93d7d6eca | 174 | break; |
MikGa87 | 1:5eb93d7d6eca | 175 | |
MikGa87 | 1:5eb93d7d6eca | 176 | case(3): |
MikGa87 | 1:5eb93d7d6eca | 177 | fprintf(fp3,"%c",c); |
MikGa87 | 1:5eb93d7d6eca | 178 | break; |
MikGa87 | 1:5eb93d7d6eca | 179 | |
MikGa87 | 1:5eb93d7d6eca | 180 | default: |
MikGa87 | 1:5eb93d7d6eca | 181 | fprintf(fp1,"%c",c); |
MikGa87 | 1:5eb93d7d6eca | 182 | break; |
MikGa87 | 1:5eb93d7d6eca | 183 | |
MikGa87 | 1:5eb93d7d6eca | 184 | } |
MikGa87 | 1:5eb93d7d6eca | 185 | |
MikGa87 | 0:1a41a881bf24 | 186 | |
MikGa87 | 1:5eb93d7d6eca | 187 | |
MikGa87 | 1:5eb93d7d6eca | 188 | }else{ |
MikGa87 | 7:951e8154af62 | 189 | controllo=1; |
MikGa87 | 1:5eb93d7d6eca | 190 | logrun=0; |
MikGa87 | 1:5eb93d7d6eca | 191 | lcd.Clear(LCD_COLOR_RED); |
MikGa87 | 1:5eb93d7d6eca | 192 | lcd.SetTextColor(LCD_COLOR_WHITE); |
MikGa87 | 1:5eb93d7d6eca | 193 | lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"Error SD! File not created", CENTER_MODE); |
MikGa87 | 0:1a41a881bf24 | 194 | } |
MikGa87 | 0:1a41a881bf24 | 195 | |
MikGa87 | 1:5eb93d7d6eca | 196 | if(button || stop){ |
MikGa87 | 1:5eb93d7d6eca | 197 | //fprintf(fp,"Canale = %d",channel); |
MikGa87 | 1:5eb93d7d6eca | 198 | fclose(fp1); |
MikGa87 | 1:5eb93d7d6eca | 199 | fclose(fp2); |
MikGa87 | 1:5eb93d7d6eca | 200 | fclose(fp3); |
MikGa87 | 0:1a41a881bf24 | 201 | logrun=0; |
MikGa87 | 1:5eb93d7d6eca | 202 | sdc.unmount(); |
MikGa87 | 1:5eb93d7d6eca | 203 | lcd.Clear(LCD_COLOR_BLUE); |
MikGa87 | 1:5eb93d7d6eca | 204 | lcd.SetTextColor(LCD_COLOR_WHITE); |
MikGa87 | 1:5eb93d7d6eca | 205 | lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"Logging Stopped", CENTER_MODE); |
MikGa87 | 1:5eb93d7d6eca | 206 | //lcd.DisplayStringAt(0, LINE(6), (uint8_t *)"Remove SD", CENTER_MODE); |
MikGa87 | 1:5eb93d7d6eca | 207 | |
MikGa87 | 0:1a41a881bf24 | 208 | |
MikGa87 | 0:1a41a881bf24 | 209 | } |
MikGa87 | 1:5eb93d7d6eca | 210 | |
MikGa87 | 0:1a41a881bf24 | 211 | } |
MikGa87 | 0:1a41a881bf24 | 212 | } |
MikGa87 | 1:5eb93d7d6eca | 213 | |
MikGa87 | 1:5eb93d7d6eca | 214 | //****************FIND IP FUNCTION********************** |
MikGa87 | 1:5eb93d7d6eca | 215 | //****************************************************** |
MikGa87 | 1:5eb93d7d6eca | 216 | void read_IP(){ |
MikGa87 | 1:5eb93d7d6eca | 217 | FILE *fd = fopen("/sd/indirizzo_ip.txt","r");//open file where is saved the ip of server |
MikGa87 | 1:5eb93d7d6eca | 218 | if(fd != NULL){ |
MikGa87 | 1:5eb93d7d6eca | 219 | fgets(serverip,12,fd); |
MikGa87 | 3:07ca6890f7b8 | 220 | sdck=1;//pc.printf("\r\nip:%s",indip); |
MikGa87 | 1:5eb93d7d6eca | 221 | } |
MikGa87 | 0:1a41a881bf24 | 222 | |
MikGa87 | 1:5eb93d7d6eca | 223 | else{ |
MikGa87 | 1:5eb93d7d6eca | 224 | pc.printf("\r\nerror reading server ip"); |
MikGa87 | 3:07ca6890f7b8 | 225 | sdck=0; |
MikGa87 | 1:5eb93d7d6eca | 226 | } |
MikGa87 | 5:bb9095a49526 | 227 | fclose(fd); |
MikGa87 | 1:5eb93d7d6eca | 228 | } |
MikGa87 | 4:7ee8caacbf20 | 229 | |
MikGa87 | 5:bb9095a49526 | 230 | int read_ftp_code(char *response){ |
MikGa87 | 10:1808c51051e5 | 231 | //The FTP FileZilla Server answers with determinated codes |
MikGa87 | 10:1808c51051e5 | 232 | //so this function finds the first three number that identify |
MikGa87 | 10:1808c51051e5 | 233 | //the answer and return it |
MikGa87 | 5:bb9095a49526 | 234 | char s_code[4]; |
MikGa87 | 5:bb9095a49526 | 235 | strncpy(s_code,response,3); |
MikGa87 | 5:bb9095a49526 | 236 | return atoi(s_code); |
MikGa87 | 5:bb9095a49526 | 237 | |
MikGa87 | 6:e0caf7838dbb | 238 | } |
MikGa87 | 6:e0caf7838dbb | 239 | |
MikGa87 | 6:e0caf7838dbb | 240 | int get_data_port(char *response){ |
MikGa87 | 10:1808c51051e5 | 241 | //It Finds the port for exchange data with server |
MikGa87 | 10:1808c51051e5 | 242 | //The server send this port with cripted code |
MikGa87 | 10:1808c51051e5 | 243 | //192,168,nnn,nnn,xxx,yyy and the solution is port=((xxx*256)+yyy) |
MikGa87 | 10:1808c51051e5 | 244 | //the problem is find this code in the answer |
MikGa87 | 10:1808c51051e5 | 245 | //so analyzing the string it search the 4th comma ',' and read xxx and yyy |
MikGa87 | 6:e0caf7838dbb | 246 | char data[4]; |
MikGa87 | 6:e0caf7838dbb | 247 | char *ptrstart; |
MikGa87 | 6:e0caf7838dbb | 248 | char *ptrend; |
MikGa87 | 6:e0caf7838dbb | 249 | int i=0; |
MikGa87 | 6:e0caf7838dbb | 250 | int leng; |
MikGa87 | 6:e0caf7838dbb | 251 | int port=-1; |
MikGa87 | 10:1808c51051e5 | 252 | ptrstart=response; //pointer to the pointer of the string |
MikGa87 | 6:e0caf7838dbb | 253 | pc.printf("cerco la virgola"); |
MikGa87 | 10:1808c51051e5 | 254 | for(i=0;i<4;i++) ptrstart=strchr(ptrstart+1,','); //it finds 4th ',' |
MikGa87 | 6:e0caf7838dbb | 255 | if(ptrstart==NULL) return -1; |
MikGa87 | 6:e0caf7838dbb | 256 | pc.printf("virgola trovata"); |
MikGa87 | 6:e0caf7838dbb | 257 | ptrstart++; |
MikGa87 | 10:1808c51051e5 | 258 | ptrend=strchr(ptrstart,','); //it finds 5th ',' to calculate number of character |
MikGa87 | 6:e0caf7838dbb | 259 | if(ptrend==NULL) return -1; |
MikGa87 | 6:e0caf7838dbb | 260 | pc.printf("calcolo caratteri"); |
MikGa87 | 10:1808c51051e5 | 261 | leng=(ptrend-ptrstart); //number of character |
MikGa87 | 6:e0caf7838dbb | 262 | strncpy(data,ptrstart,leng); |
MikGa87 | 10:1808c51051e5 | 263 | port=atoi(data)*256; //xxx*256 |
MikGa87 | 6:e0caf7838dbb | 264 | ptrstart=ptrend+1; |
MikGa87 | 6:e0caf7838dbb | 265 | ptrend=strchr(ptrstart,'\r'); |
MikGa87 | 6:e0caf7838dbb | 266 | if(ptrend==NULL) return -1; |
MikGa87 | 6:e0caf7838dbb | 267 | leng=(ptrend-ptrstart); |
MikGa87 | 6:e0caf7838dbb | 268 | strncpy(data,ptrstart,leng); |
MikGa87 | 10:1808c51051e5 | 269 | port=port+atoi(data); //(xxx*256)+yyy |
MikGa87 | 6:e0caf7838dbb | 270 | |
MikGa87 | 6:e0caf7838dbb | 271 | return port; |
MikGa87 | 6:e0caf7838dbb | 272 | |
MikGa87 | 5:bb9095a49526 | 273 | } |
MikGa87 | 1:5eb93d7d6eca | 274 | |
MikGa87 | 8:92f815712fd8 | 275 | int send_data_to_server(char *path){ |
MikGa87 | 12:4b39c6fb8f21 | 276 | |
MikGa87 | 12:4b39c6fb8f21 | 277 | int rx; |
MikGa87 | 12:4b39c6fb8f21 | 278 | int port; |
MikGa87 | 8:92f815712fd8 | 279 | int size=0; |
MikGa87 | 8:92f815712fd8 | 280 | int res; |
MikGa87 | 8:92f815712fd8 | 281 | int len=0; |
MikGa87 | 12:4b39c6fb8f21 | 282 | static char rxbuffer[256]; |
MikGa87 | 12:4b39c6fb8f21 | 283 | char type[20]; |
MikGa87 | 12:4b39c6fb8f21 | 284 | char mode[20]; |
MikGa87 | 12:4b39c6fb8f21 | 285 | char stru[20]; |
MikGa87 | 12:4b39c6fb8f21 | 286 | char pasv[20]; |
MikGa87 | 12:4b39c6fb8f21 | 287 | char store[256]; |
MikGa87 | 12:4b39c6fb8f21 | 288 | strcpy(type,"TYPE A\r\n"); |
MikGa87 | 12:4b39c6fb8f21 | 289 | strcpy(stru,"STRU F\r\n"); |
MikGa87 | 12:4b39c6fb8f21 | 290 | strcpy(mode,"MODE S\r\n"); |
MikGa87 | 12:4b39c6fb8f21 | 291 | strcpy(pasv,"PASV\r\n"); |
MikGa87 | 12:4b39c6fb8f21 | 292 | strcpy(store,"STOR "); |
MikGa87 | 12:4b39c6fb8f21 | 293 | strncat(store,path,100); |
MikGa87 | 12:4b39c6fb8f21 | 294 | strncat(store,"\r\n",5); |
MikGa87 | 12:4b39c6fb8f21 | 295 | pc.printf("comando di store del file = %s",store); |
MikGa87 | 12:4b39c6fb8f21 | 296 | |
MikGa87 | 12:4b39c6fb8f21 | 297 | socket.send(type,strlen(type));//exchange data in ascii type |
MikGa87 | 12:4b39c6fb8f21 | 298 | socket.recv(rxbuffer,sizeof(rxbuffer)); |
MikGa87 | 12:4b39c6fb8f21 | 299 | pc.printf("rxbuffer=%s\r\n",rxbuffer); |
MikGa87 | 12:4b39c6fb8f21 | 300 | socket.send(stru,strlen(stru)); |
MikGa87 | 12:4b39c6fb8f21 | 301 | socket.recv(rxbuffer,sizeof(rxbuffer)); |
MikGa87 | 12:4b39c6fb8f21 | 302 | pc.printf("rxbuffer=%s\r\n",rxbuffer); |
MikGa87 | 12:4b39c6fb8f21 | 303 | socket.send(mode,strlen(mode)); |
MikGa87 | 12:4b39c6fb8f21 | 304 | socket.recv(rxbuffer,sizeof(rxbuffer)); |
MikGa87 | 12:4b39c6fb8f21 | 305 | pc.printf("rxbuffer=%s\r\n",rxbuffer); |
MikGa87 | 12:4b39c6fb8f21 | 306 | socket.send(pasv,strlen(pasv)); |
MikGa87 | 12:4b39c6fb8f21 | 307 | socket.recv(rxbuffer,sizeof(rxbuffer)); |
MikGa87 | 12:4b39c6fb8f21 | 308 | rx=read_ftp_code(rxbuffer); |
MikGa87 | 12:4b39c6fb8f21 | 309 | port=get_data_port(rxbuffer); |
MikGa87 | 12:4b39c6fb8f21 | 310 | pc.printf("Porta dati =%d\r\n",port); |
MikGa87 | 12:4b39c6fb8f21 | 311 | //if(port<0)port=65535; |
MikGa87 | 12:4b39c6fb8f21 | 312 | |
MikGa87 | 12:4b39c6fb8f21 | 313 | if(rx==227){ |
MikGa87 | 12:4b39c6fb8f21 | 314 | |
MikGa87 | 12:4b39c6fb8f21 | 315 | socket2.open(&wifi); //open new socket for data transmission |
MikGa87 | 12:4b39c6fb8f21 | 316 | socket.recv(rxbuffer,sizeof(rxbuffer)); |
MikGa87 | 12:4b39c6fb8f21 | 317 | pc.printf("rxbuffer=%s\r\n",rxbuffer); |
MikGa87 | 12:4b39c6fb8f21 | 318 | socket2.bind(ip,port); //bind the data port that is available on the server calculated by get_data_port function |
MikGa87 | 12:4b39c6fb8f21 | 319 | socket.recv(rxbuffer,sizeof(rxbuffer)); |
MikGa87 | 12:4b39c6fb8f21 | 320 | pc.printf("rxbuffer=%s\r\n",rxbuffer); |
MikGa87 | 12:4b39c6fb8f21 | 321 | socket2.connect(serverip,port); |
MikGa87 | 12:4b39c6fb8f21 | 322 | socket.recv(rxbuffer,sizeof(rxbuffer)); |
MikGa87 | 12:4b39c6fb8f21 | 323 | pc.printf("rxbuffer=%s\r\n",rxbuffer); |
MikGa87 | 12:4b39c6fb8f21 | 324 | socket.send(store,strlen(store));//STOR reserve a memory area for a file |
MikGa87 | 12:4b39c6fb8f21 | 325 | socket.recv(rxbuffer,sizeof(rxbuffer)); |
MikGa87 | 12:4b39c6fb8f21 | 326 | pc.printf("rxbuffer=%s\r\n",rxbuffer); |
MikGa87 | 12:4b39c6fb8f21 | 327 | |
MikGa87 | 12:4b39c6fb8f21 | 328 | FILE *fp = fopen(path,"r");//open file |
MikGa87 | 12:4b39c6fb8f21 | 329 | pc.printf("file aperto\r\n"); |
MikGa87 | 12:4b39c6fb8f21 | 330 | // find dimension of file |
MikGa87 | 12:4b39c6fb8f21 | 331 | fseek(fp, 0, SEEK_END); |
MikGa87 | 12:4b39c6fb8f21 | 332 | size = ftell(fp); |
MikGa87 | 12:4b39c6fb8f21 | 333 | fseek(fp, 0, SEEK_SET); |
MikGa87 | 12:4b39c6fb8f21 | 334 | pc.printf("file size= %d\r\n",size); |
MikGa87 | 12:4b39c6fb8f21 | 335 | int n=0; |
MikGa87 | 12:4b39c6fb8f21 | 336 | while(1){ |
MikGa87 | 12:4b39c6fb8f21 | 337 | n++; |
MikGa87 | 12:4b39c6fb8f21 | 338 | res=fread(&str,1,4094,fp); |
MikGa87 | 12:4b39c6fb8f21 | 339 | if(n*4094<=size){ |
MikGa87 | 12:4b39c6fb8f21 | 340 | pc.printf("Invio paccheto da 4096 byte\r\n"); |
MikGa87 | 12:4b39c6fb8f21 | 341 | socket2.send(str,strlen(str)); //send multiple of 4094 byte at time |
MikGa87 | 12:4b39c6fb8f21 | 342 | } |
MikGa87 | 12:4b39c6fb8f21 | 343 | else{ |
MikGa87 | 12:4b39c6fb8f21 | 344 | len = strlen(str)-(n*4094-size); //calculate the rest of byte to send |
MikGa87 | 12:4b39c6fb8f21 | 345 | char log[len]; |
MikGa87 | 12:4b39c6fb8f21 | 346 | for(int i=0;i<len;i++){ |
MikGa87 | 12:4b39c6fb8f21 | 347 | log[i]=str[i]; |
MikGa87 | 12:4b39c6fb8f21 | 348 | } |
MikGa87 | 12:4b39c6fb8f21 | 349 | |
MikGa87 | 12:4b39c6fb8f21 | 350 | log[len]='\0'; //terminate the string with '\0' |
MikGa87 | 12:4b39c6fb8f21 | 351 | socket2.send(log,len); |
MikGa87 | 12:4b39c6fb8f21 | 352 | pc.printf("Invio paccheto da %d byte\r\n",len); |
MikGa87 | 12:4b39c6fb8f21 | 353 | break; |
MikGa87 | 12:4b39c6fb8f21 | 354 | } |
MikGa87 | 12:4b39c6fb8f21 | 355 | } |
MikGa87 | 12:4b39c6fb8f21 | 356 | fclose(fp); |
MikGa87 | 12:4b39c6fb8f21 | 357 | } |
MikGa87 | 7:951e8154af62 | 358 | |
MikGa87 | 12:4b39c6fb8f21 | 359 | socket2.close(); |
MikGa87 | 8:92f815712fd8 | 360 | return 1; |
MikGa87 | 8:92f815712fd8 | 361 | } |
MikGa87 | 8:92f815712fd8 | 362 | |
MikGa87 | 8:92f815712fd8 | 363 | |
MikGa87 | 7:951e8154af62 | 364 | |
MikGa87 | 7:951e8154af62 | 365 | |
MikGa87 | 1:5eb93d7d6eca | 366 | void connect_to_server(){ |
MikGa87 | 4:7ee8caacbf20 | 367 | |
MikGa87 | 8:92f815712fd8 | 368 | |
MikGa87 | 4:7ee8caacbf20 | 369 | int failed; |
MikGa87 | 5:bb9095a49526 | 370 | static char rxbuffer[256]; |
MikGa87 | 5:bb9095a49526 | 371 | int rx; |
MikGa87 | 12:4b39c6fb8f21 | 372 | int sended; |
MikGa87 | 4:7ee8caacbf20 | 373 | char user[1024]; |
MikGa87 | 12:4b39c6fb8f21 | 374 | char pwd[1024]; |
MikGa87 | 12:4b39c6fb8f21 | 375 | //char store2[50]; |
MikGa87 | 12:4b39c6fb8f21 | 376 | //char mode[20]; |
MikGa87 | 12:4b39c6fb8f21 | 377 | //char stru[20]; |
MikGa87 | 12:4b39c6fb8f21 | 378 | //char pasv[20]; |
MikGa87 | 12:4b39c6fb8f21 | 379 | |
MikGa87 | 7:951e8154af62 | 380 | |
MikGa87 | 10:1808c51051e5 | 381 | //creation of string command to send to server to start the communication |
MikGa87 | 10:1808c51051e5 | 382 | strcpy(user,"USER datalogger\r\n"); |
MikGa87 | 12:4b39c6fb8f21 | 383 | strcpy(pwd,"PASS project\r\n"); |
MikGa87 | 5:bb9095a49526 | 384 | socket.set_timeout(3000); |
MikGa87 | 5:bb9095a49526 | 385 | socket2.set_timeout(3000); |
MikGa87 | 4:7ee8caacbf20 | 386 | pc.printf("prova di connessione a server ftp"); |
MikGa87 | 4:7ee8caacbf20 | 387 | int erroropen=socket.open(&wifi); |
MikGa87 | 5:bb9095a49526 | 388 | socket.bind(ip,22); |
MikGa87 | 8:92f815712fd8 | 389 | |
MikGa87 | 5:bb9095a49526 | 390 | if(!erroropen){ |
MikGa87 | 8:92f815712fd8 | 391 | pc.printf("connessione in corso...\r\n"); |
MikGa87 | 8:92f815712fd8 | 392 | failed=socket.connect(serverip,21); |
MikGa87 | 8:92f815712fd8 | 393 | controllo=failed; |
MikGa87 | 8:92f815712fd8 | 394 | socket.recv(rxbuffer,sizeof(rxbuffer)); |
MikGa87 | 8:92f815712fd8 | 395 | rx=read_ftp_code(rxbuffer); |
MikGa87 | 8:92f815712fd8 | 396 | |
MikGa87 | 9:5efca5bb6025 | 397 | if(rx==220){ |
MikGa87 | 9:5efca5bb6025 | 398 | pc.printf("%d connected on port 21\r\n",rx); |
MikGa87 | 9:5efca5bb6025 | 399 | pc.printf("rxbuffer=%s\r\n",rxbuffer); |
MikGa87 | 9:5efca5bb6025 | 400 | socket.send(user,strlen(user)); |
MikGa87 | 8:92f815712fd8 | 401 | socket.recv(rxbuffer,sizeof(rxbuffer)); |
MikGa87 | 8:92f815712fd8 | 402 | pc.printf("rxbuffer=%s\r\n",rxbuffer); |
MikGa87 | 9:5efca5bb6025 | 403 | socket.send(pwd,strlen(pwd)); |
MikGa87 | 9:5efca5bb6025 | 404 | socket.recv(rxbuffer,sizeof(rxbuffer)); |
MikGa87 | 9:5efca5bb6025 | 405 | pc.printf("rxbuffer=%s\r\n",rxbuffer); |
MikGa87 | 12:4b39c6fb8f21 | 406 | sended=send_data_to_server(filename1); |
MikGa87 | 12:4b39c6fb8f21 | 407 | if (sended) send_data_to_server(filename2); |
MikGa87 | 9:5efca5bb6025 | 408 | |
MikGa87 | 12:4b39c6fb8f21 | 409 | } |
MikGa87 | 12:4b39c6fb8f21 | 410 | else{ |
MikGa87 | 12:4b39c6fb8f21 | 411 | controllo=-1; |
MikGa87 | 12:4b39c6fb8f21 | 412 | pc.printf("Errore connessione con il server\r\n"); |
MikGa87 | 12:4b39c6fb8f21 | 413 | } |
MikGa87 | 12:4b39c6fb8f21 | 414 | |
MikGa87 | 6:e0caf7838dbb | 415 | } |
MikGa87 | 12:4b39c6fb8f21 | 416 | |
MikGa87 | 12:4b39c6fb8f21 | 417 | |
MikGa87 | 5:bb9095a49526 | 418 | socket.close(); |
MikGa87 | 4:7ee8caacbf20 | 419 | } |
MikGa87 | 1:5eb93d7d6eca | 420 | |
MikGa87 | 4:7ee8caacbf20 | 421 | |
MikGa87 | 0:1a41a881bf24 | 422 | |
MikGa87 | 3:07ca6890f7b8 | 423 | |
MikGa87 | 3:07ca6890f7b8 | 424 | |
MikGa87 | 3:07ca6890f7b8 | 425 | void init_wifi(){ |
MikGa87 | 3:07ca6890f7b8 | 426 | |
MikGa87 | 3:07ca6890f7b8 | 427 | |
MikGa87 | 3:07ca6890f7b8 | 428 | mac = wifi.get_mac_address(); |
MikGa87 | 8:92f815712fd8 | 429 | //connected=wifi.connect(ssid, seckey, NSAPI_SECURITY_WPA2); |
MikGa87 | 8:92f815712fd8 | 430 | //ip = wifi.get_ip_address(); |
MikGa87 | 0:1a41a881bf24 | 431 | |
MikGa87 | 0:1a41a881bf24 | 432 | |
MikGa87 | 3:07ca6890f7b8 | 433 | } |
MikGa87 | 3:07ca6890f7b8 | 434 | |
MikGa87 | 3:07ca6890f7b8 | 435 | |
MikGa87 | 3:07ca6890f7b8 | 436 | void init_ts(){ |
MikGa87 | 0:1a41a881bf24 | 437 | |
MikGa87 | 3:07ca6890f7b8 | 438 | ////////Touch Screen//////// |
MikGa87 | 0:1a41a881bf24 | 439 | |
MikGa87 | 3:07ca6890f7b8 | 440 | //TS_StateTypeDef TS_State; |
MikGa87 | 3:07ca6890f7b8 | 441 | status_ts = ts.Init(lcd.GetXSize(), lcd.GetYSize()); |
MikGa87 | 3:07ca6890f7b8 | 442 | |
MikGa87 | 3:07ca6890f7b8 | 443 | } |
MikGa87 | 3:07ca6890f7b8 | 444 | |
MikGa87 | 3:07ca6890f7b8 | 445 | |
MikGa87 | 3:07ca6890f7b8 | 446 | void init_time(){ |
MikGa87 | 3:07ca6890f7b8 | 447 | |
MikGa87 | 1:5eb93d7d6eca | 448 | //INIT TIME |
MikGa87 | 1:5eb93d7d6eca | 449 | |
MikGa87 | 1:5eb93d7d6eca | 450 | Rtc_Ds1307::Time_rtc tm = {}; |
MikGa87 | 1:5eb93d7d6eca | 451 | |
MikGa87 | 12:4b39c6fb8f21 | 452 | year=tm.year=2017; |
MikGa87 | 12:4b39c6fb8f21 | 453 | mon=tm.mon=1; |
MikGa87 | 12:4b39c6fb8f21 | 454 | date=tm.date=23; |
MikGa87 | 3:07ca6890f7b8 | 455 | hour=tm.hour=9; |
MikGa87 | 3:07ca6890f7b8 | 456 | min=tm.min=3; |
MikGa87 | 3:07ca6890f7b8 | 457 | sec=tm.sec=00; |
MikGa87 | 1:5eb93d7d6eca | 458 | rtc.setTime(tm,true,false);//rtc initialization |
MikGa87 | 1:5eb93d7d6eca | 459 | |
MikGa87 | 1:5eb93d7d6eca | 460 | rtc.startClock(); |
MikGa87 | 1:5eb93d7d6eca | 461 | |
MikGa87 | 1:5eb93d7d6eca | 462 | rtc.getTime(tm); |
MikGa87 | 1:5eb93d7d6eca | 463 | pc.printf("%02d-%02d-%04d_%02d.%02d.%02d",tm.date, tm.mon, tm.year, tm.hour, tm.min, tm.sec); |
MikGa87 | 1:5eb93d7d6eca | 464 | |
MikGa87 | 3:07ca6890f7b8 | 465 | } |
MikGa87 | 1:5eb93d7d6eca | 466 | |
MikGa87 | 3:07ca6890f7b8 | 467 | |
MikGa87 | 3:07ca6890f7b8 | 468 | |
MikGa87 | 3:07ca6890f7b8 | 469 | |
MikGa87 | 3:07ca6890f7b8 | 470 | void boot(){ |
MikGa87 | 1:5eb93d7d6eca | 471 | |
MikGa87 | 0:1a41a881bf24 | 472 | |
MikGa87 | 3:07ca6890f7b8 | 473 | init_time(); |
MikGa87 | 3:07ca6890f7b8 | 474 | |
MikGa87 | 3:07ca6890f7b8 | 475 | |
MikGa87 | 1:5eb93d7d6eca | 476 | lcd.Clear(LCD_COLOR_WHITE); |
MikGa87 | 0:1a41a881bf24 | 477 | lcd.SetBackColor(LCD_COLOR_WHITE); |
MikGa87 | 1:5eb93d7d6eca | 478 | lcd.SetTextColor(LCD_COLOR_BLUE); |
MikGa87 | 1:5eb93d7d6eca | 479 | lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"STMicroelectronics", CENTER_MODE); |
MikGa87 | 0:1a41a881bf24 | 480 | |
MikGa87 | 1:5eb93d7d6eca | 481 | lcd.SetFont(&Font12); |
MikGa87 | 1:5eb93d7d6eca | 482 | lcd.DisplayStringAt(0, LINE(0), (uint8_t *)"Inizializzazione...", LEFT_MODE); |
MikGa87 | 1:5eb93d7d6eca | 483 | wait(0.5); |
MikGa87 | 1:5eb93d7d6eca | 484 | lcd.DisplayStringAt(0, LINE(1), (uint8_t *)"WI FI init...", LEFT_MODE); |
MikGa87 | 3:07ca6890f7b8 | 485 | |
MikGa87 | 3:07ca6890f7b8 | 486 | init_wifi(); //inizializzazione wi-fi |
MikGa87 | 3:07ca6890f7b8 | 487 | |
MikGa87 | 1:5eb93d7d6eca | 488 | lcd.DisplayStringAt(0, LINE(2), (uint8_t *)"MAC ADDRESS", LEFT_MODE); |
MikGa87 | 1:5eb93d7d6eca | 489 | lcd.DisplayStringAt(30, LINE(2), (uint8_t *)mac, LEFT_MODE); |
MikGa87 | 0:1a41a881bf24 | 490 | |
MikGa87 | 8:92f815712fd8 | 491 | /*if(connected) { |
MikGa87 | 1:5eb93d7d6eca | 492 | lcd.DisplayStringAt(0, LINE(2), (uint8_t *)"WI FI connected......", LEFT_MODE); |
MikGa87 | 0:1a41a881bf24 | 493 | } else { |
MikGa87 | 1:5eb93d7d6eca | 494 | lcd.DisplayStringAt(0, LINE(2), (uint8_t *)"WI FI NOT connected......", LEFT_MODE); |
MikGa87 | 0:1a41a881bf24 | 495 | |
MikGa87 | 1:5eb93d7d6eca | 496 | } |
MikGa87 | 3:07ca6890f7b8 | 497 | |
MikGa87 | 1:5eb93d7d6eca | 498 | lcd.DisplayStringAt(0, LINE(1), (uint8_t *)ip, LEFT_MODE); |
MikGa87 | 8:92f815712fd8 | 499 | */ |
MikGa87 | 3:07ca6890f7b8 | 500 | init_ts(); //inizializzazione touch_screen |
MikGa87 | 3:07ca6890f7b8 | 501 | |
MikGa87 | 3:07ca6890f7b8 | 502 | if (status_ts != TS_OK) { |
MikGa87 | 3:07ca6890f7b8 | 503 | lcd.SetTextColor(LCD_COLOR_RED); |
MikGa87 | 3:07ca6890f7b8 | 504 | lcd.DisplayStringAt(0, LINE(4), (uint8_t *)"TOUCHSCREEN INIT FAIL", LEFT_MODE); |
MikGa87 | 3:07ca6890f7b8 | 505 | } else { |
MikGa87 | 3:07ca6890f7b8 | 506 | lcd.DisplayStringAt(0, LINE(4), (uint8_t *)"Touchscreen init success", LEFT_MODE); |
MikGa87 | 3:07ca6890f7b8 | 507 | } |
MikGa87 | 3:07ca6890f7b8 | 508 | |
MikGa87 | 0:1a41a881bf24 | 509 | |
MikGa87 | 0:1a41a881bf24 | 510 | //////// SD ////////////// |
MikGa87 | 1:5eb93d7d6eca | 511 | |
MikGa87 | 3:07ca6890f7b8 | 512 | //sdck=sdc.card_present(); |
MikGa87 | 3:07ca6890f7b8 | 513 | read_IP(); //legge l'indirizzo ip dal file di configurazione per connessione a server ftp |
MikGa87 | 1:5eb93d7d6eca | 514 | |
MikGa87 | 1:5eb93d7d6eca | 515 | if(sdck){ |
MikGa87 | 1:5eb93d7d6eca | 516 | |
MikGa87 | 1:5eb93d7d6eca | 517 | lcd.DisplayStringAt(0, LINE(3), (uint8_t *)"SD PRESENTE", LEFT_MODE); |
MikGa87 | 0:1a41a881bf24 | 518 | wait(1); |
MikGa87 | 0:1a41a881bf24 | 519 | } |
MikGa87 | 3:07ca6890f7b8 | 520 | if(!sdck){ |
MikGa87 | 1:5eb93d7d6eca | 521 | |
MikGa87 | 1:5eb93d7d6eca | 522 | lcd.SetTextColor(LCD_COLOR_RED); |
MikGa87 | 1:5eb93d7d6eca | 523 | lcd.DisplayStringAt(0, LINE(3), (uint8_t *)"SD NON PRESENTE", LEFT_MODE); |
MikGa87 | 1:5eb93d7d6eca | 524 | lcd.DisplayStringAt(0, LINE(4), (uint8_t *)"SI PREGA INSERIRE SD E RIAVVIARE", LEFT_MODE); |
MikGa87 | 1:5eb93d7d6eca | 525 | wait(2); |
MikGa87 | 1:5eb93d7d6eca | 526 | } |
MikGa87 | 0:1a41a881bf24 | 527 | |
MikGa87 | 6:e0caf7838dbb | 528 | //connect_to_server(); |
MikGa87 | 11:0556197a0641 | 529 | //foto = fopen("/sd/bitmap.bmp", "w"); |
MikGa87 | 11:0556197a0641 | 530 | //lcd.DrawBitmap(0,0,*foto); |
MikGa87 | 0:1a41a881bf24 | 531 | |
MikGa87 | 3:07ca6890f7b8 | 532 | wait(1); |
MikGa87 | 0:1a41a881bf24 | 533 | |
MikGa87 | 3:07ca6890f7b8 | 534 | } |
MikGa87 | 3:07ca6890f7b8 | 535 | |
MikGa87 | 3:07ca6890f7b8 | 536 | |
MikGa87 | 3:07ca6890f7b8 | 537 | |
MikGa87 | 3:07ca6890f7b8 | 538 | |
MikGa87 | 3:07ca6890f7b8 | 539 | int main() { |
MikGa87 | 0:1a41a881bf24 | 540 | |
MikGa87 | 7:951e8154af62 | 541 | int stato=0; |
MikGa87 | 0:1a41a881bf24 | 542 | int valueToSendToSlave = 99; // Starting value only |
MikGa87 | 3:07ca6890f7b8 | 543 | pc.baud(115200); |
MikGa87 | 0:1a41a881bf24 | 544 | spi.format(16,0); // Setup: bit data |
MikGa87 | 0:1a41a881bf24 | 545 | spi.frequency(24000000); //1MHz |
MikGa87 | 0:1a41a881bf24 | 546 | |
MikGa87 | 3:07ca6890f7b8 | 547 | boot(); |
MikGa87 | 3:07ca6890f7b8 | 548 | |
MikGa87 | 3:07ca6890f7b8 | 549 | |
MikGa87 | 3:07ca6890f7b8 | 550 | mkdir("/sd/log", 0777); |
MikGa87 | 3:07ca6890f7b8 | 551 | sprintf(filename1,"/sd/log/file_serial2_%02d-%02d-%04d_%02d.%02d.%02d_gps1.txt",date, mon, year, hour, min, sec); |
MikGa87 | 3:07ca6890f7b8 | 552 | sprintf(filename2,"/sd/log/file_serial2_%02d-%02d-%04d_%02d.%02d.%02d_gps2.txt",date, mon, year, hour, min, sec); |
MikGa87 | 3:07ca6890f7b8 | 553 | sprintf(filename3,"/sd/log/file_serial2_%02d-%02d-%04d_%02d.%02d.%02d_gps3.txt",date, mon, year, hour, min, sec); |
MikGa87 | 3:07ca6890f7b8 | 554 | |
MikGa87 | 3:07ca6890f7b8 | 555 | |
MikGa87 | 3:07ca6890f7b8 | 556 | fp1 = fopen(filename1, "w"); |
MikGa87 | 7:951e8154af62 | 557 | pc.printf("file 1 aperto\r\n"); |
MikGa87 | 3:07ca6890f7b8 | 558 | fp2 = fopen(filename2, "w"); |
MikGa87 | 7:951e8154af62 | 559 | pc.printf("file 2 aperto\r\n"); |
MikGa87 | 3:07ca6890f7b8 | 560 | fp3 = fopen(filename3, "w"); |
MikGa87 | 7:951e8154af62 | 561 | pc.printf("file 3 aperto\r\n"); |
MikGa87 | 4:7ee8caacbf20 | 562 | //connect_to_server(); |
MikGa87 | 0:1a41a881bf24 | 563 | |
MikGa87 | 0:1a41a881bf24 | 564 | |
MikGa87 | 3:07ca6890f7b8 | 565 | lcd.SetBackColor(LCD_COLOR_BLUE); |
MikGa87 | 3:07ca6890f7b8 | 566 | lcd.SetTextColor(LCD_COLOR_WHITE); |
MikGa87 | 1:5eb93d7d6eca | 567 | lcd.Clear(LCD_COLOR_WHITE); |
MikGa87 | 1:5eb93d7d6eca | 568 | lcd.SetBackColor(LCD_COLOR_WHITE); |
MikGa87 | 1:5eb93d7d6eca | 569 | lcd.SetTextColor(LCD_COLOR_BLUE); |
MikGa87 | 0:1a41a881bf24 | 570 | wait(0.3); |
MikGa87 | 0:1a41a881bf24 | 571 | lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"Welcome to", CENTER_MODE); |
MikGa87 | 0:1a41a881bf24 | 572 | lcd.DisplayStringAt(0, LINE(6), (uint8_t *)"GNNS LOGGER V3.0", CENTER_MODE); |
MikGa87 | 0:1a41a881bf24 | 573 | lcd.DisplayStringAt(0, LINE(10), (uint8_t *)"Touch The Screen to Start", CENTER_MODE); |
MikGa87 | 0:1a41a881bf24 | 574 | |
MikGa87 | 0:1a41a881bf24 | 575 | |
MikGa87 | 0:1a41a881bf24 | 576 | |
MikGa87 | 0:1a41a881bf24 | 577 | |
MikGa87 | 0:1a41a881bf24 | 578 | |
MikGa87 | 0:1a41a881bf24 | 579 | while (1) { |
MikGa87 | 0:1a41a881bf24 | 580 | |
MikGa87 | 0:1a41a881bf24 | 581 | |
MikGa87 | 1:5eb93d7d6eca | 582 | |
MikGa87 | 0:1a41a881bf24 | 583 | ///////////////Touch Screen/////////////// |
MikGa87 | 3:07ca6890f7b8 | 584 | TS_StateTypeDef TS_State; |
MikGa87 | 0:1a41a881bf24 | 585 | ts.GetState(&TS_State); |
MikGa87 | 0:1a41a881bf24 | 586 | |
MikGa87 | 7:951e8154af62 | 587 | switch(stato){ |
MikGa87 | 7:951e8154af62 | 588 | |
MikGa87 | 7:951e8154af62 | 589 | case(0): |
MikGa87 | 11:0556197a0641 | 590 | //initial state |
MikGa87 | 11:0556197a0641 | 591 | //it draws the menu page anche check the touchscreen |
MikGa87 | 7:951e8154af62 | 592 | |
MikGa87 | 7:951e8154af62 | 593 | |
MikGa87 | 7:951e8154af62 | 594 | if (TS_State.touchDetected && setpage==0) { |
MikGa87 | 7:951e8154af62 | 595 | |
MikGa87 | 7:951e8154af62 | 596 | |
MikGa87 | 7:951e8154af62 | 597 | menu_page(); |
MikGa87 | 7:951e8154af62 | 598 | |
MikGa87 | 7:951e8154af62 | 599 | for (idx = 0; idx < TS_State.touchDetected; idx++) { |
MikGa87 | 7:951e8154af62 | 600 | x = TS_State.touchX[idx]; |
MikGa87 | 7:951e8154af62 | 601 | y = TS_State.touchY[idx]; |
MikGa87 | 7:951e8154af62 | 602 | } |
MikGa87 | 7:951e8154af62 | 603 | |
MikGa87 | 7:951e8154af62 | 604 | if((x>=10) && (x<=60) && (y>=200) && (y<=250)){ |
MikGa87 | 11:0556197a0641 | 605 | //if is pressed the green button, the logging starts |
MikGa87 | 7:951e8154af62 | 606 | logrun=1; |
MikGa87 | 7:951e8154af62 | 607 | setpage=1; |
MikGa87 | 7:951e8154af62 | 608 | stato=1; |
MikGa87 | 7:951e8154af62 | 609 | logging_page(); |
MikGa87 | 7:951e8154af62 | 610 | |
MikGa87 | 7:951e8154af62 | 611 | |
MikGa87 | 7:951e8154af62 | 612 | } |
MikGa87 | 7:951e8154af62 | 613 | |
MikGa87 | 7:951e8154af62 | 614 | |
MikGa87 | 7:951e8154af62 | 615 | } |
MikGa87 | 7:951e8154af62 | 616 | break; |
MikGa87 | 7:951e8154af62 | 617 | |
MikGa87 | 7:951e8154af62 | 618 | case(1): |
MikGa87 | 11:0556197a0641 | 619 | //in this state starts the communication by SPI with slave |
MikGa87 | 11:0556197a0641 | 620 | //and starts to save data on sdcard |
MikGa87 | 7:951e8154af62 | 621 | |
MikGa87 | 7:951e8154af62 | 622 | if(logrun){ |
MikGa87 | 7:951e8154af62 | 623 | |
MikGa87 | 7:951e8154af62 | 624 | valueToSendToSlave = 99; |
MikGa87 | 7:951e8154af62 | 625 | chipSelect = 1; // Deselect device |
MikGa87 | 7:951e8154af62 | 626 | chipSelect = 0; // Select device |
MikGa87 | 7:951e8154af62 | 627 | uint16_t dataFromSlave = spi.write(valueToSendToSlave); |
MikGa87 | 7:951e8154af62 | 628 | |
MikGa87 | 7:951e8154af62 | 629 | chipSelect = 1; // Deselect device |
MikGa87 | 7:951e8154af62 | 630 | |
MikGa87 | 7:951e8154af62 | 631 | c=dataFromSlave; |
MikGa87 | 7:951e8154af62 | 632 | channel=dataFromSlave>>8; |
MikGa87 | 7:951e8154af62 | 633 | if(c!=0) write_sd(c); |
MikGa87 | 7:951e8154af62 | 634 | |
MikGa87 | 7:951e8154af62 | 635 | |
MikGa87 | 7:951e8154af62 | 636 | |
MikGa87 | 7:951e8154af62 | 637 | |
MikGa87 | 7:951e8154af62 | 638 | for (idx = 0; idx < TS_State.touchDetected; idx++) { |
MikGa87 | 7:951e8154af62 | 639 | x = TS_State.touchX[idx]; |
MikGa87 | 7:951e8154af62 | 640 | y = TS_State.touchY[idx]; |
MikGa87 | 7:951e8154af62 | 641 | } |
MikGa87 | 7:951e8154af62 | 642 | |
MikGa87 | 7:951e8154af62 | 643 | if((x>=100) && (x<=160) && (y>=200) && (y<=250)){ |
MikGa87 | 10:1808c51051e5 | 644 | lcd.Clear(LCD_COLOR_BLUE); |
MikGa87 | 10:1808c51051e5 | 645 | lcd.SetTextColor(LCD_COLOR_WHITE); |
MikGa87 | 10:1808c51051e5 | 646 | lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"Connecting to WiFi Network", CENTER_MODE); |
MikGa87 | 7:951e8154af62 | 647 | stop=1; |
MikGa87 | 7:951e8154af62 | 648 | logrun=0; |
MikGa87 | 7:951e8154af62 | 649 | setpage=0; |
MikGa87 | 7:951e8154af62 | 650 | stato=2; |
MikGa87 | 7:951e8154af62 | 651 | } |
MikGa87 | 7:951e8154af62 | 652 | if(controllo){ |
MikGa87 | 7:951e8154af62 | 653 | pc.printf("errore file\r\n"); |
MikGa87 | 7:951e8154af62 | 654 | stato=0; |
MikGa87 | 7:951e8154af62 | 655 | } |
MikGa87 | 7:951e8154af62 | 656 | break; |
MikGa87 | 7:951e8154af62 | 657 | |
MikGa87 | 7:951e8154af62 | 658 | |
MikGa87 | 7:951e8154af62 | 659 | case(2): |
MikGa87 | 11:0556197a0641 | 660 | //Here start the connection to server by Wi-Fi module to send data to FileZilla Server |
MikGa87 | 7:951e8154af62 | 661 | if(!controllo){ |
MikGa87 | 8:92f815712fd8 | 662 | connected=wifi.connect(ssid, seckey, NSAPI_SECURITY_WPA2); |
MikGa87 | 8:92f815712fd8 | 663 | ip = wifi.get_ip_address(); |
MikGa87 | 8:92f815712fd8 | 664 | if(connected){ |
MikGa87 | 10:1808c51051e5 | 665 | lcd.Clear(LCD_COLOR_BLUE); |
MikGa87 | 10:1808c51051e5 | 666 | lcd.SetTextColor(LCD_COLOR_WHITE); |
MikGa87 | 10:1808c51051e5 | 667 | lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"WI FI connected......", LEFT_MODE); |
MikGa87 | 8:92f815712fd8 | 668 | |
MikGa87 | 8:92f815712fd8 | 669 | connect_to_server(); |
MikGa87 | 8:92f815712fd8 | 670 | server_connession_page(); |
MikGa87 | 8:92f815712fd8 | 671 | |
MikGa87 | 8:92f815712fd8 | 672 | stato=0; |
MikGa87 | 8:92f815712fd8 | 673 | stop=0; |
MikGa87 | 8:92f815712fd8 | 674 | } |
MikGa87 | 8:92f815712fd8 | 675 | else { |
MikGa87 | 8:92f815712fd8 | 676 | lcd.DisplayStringAt(0, LINE(2), (uint8_t *)"WI FI NOT connected......", LEFT_MODE); |
MikGa87 | 8:92f815712fd8 | 677 | |
MikGa87 | 8:92f815712fd8 | 678 | } |
MikGa87 | 7:951e8154af62 | 679 | } |
MikGa87 | 7:951e8154af62 | 680 | if(controllo){ |
MikGa87 | 7:951e8154af62 | 681 | pc.printf("errore connessione server\r\n"); |
MikGa87 | 7:951e8154af62 | 682 | stato=0; |
MikGa87 | 7:951e8154af62 | 683 | stop=0; |
MikGa87 | 7:951e8154af62 | 684 | } |
MikGa87 | 7:951e8154af62 | 685 | break; |
MikGa87 | 0:1a41a881bf24 | 686 | |
MikGa87 | 3:07ca6890f7b8 | 687 | } |
MikGa87 | 0:1a41a881bf24 | 688 | |
MikGa87 | 0:1a41a881bf24 | 689 | |
MikGa87 | 1:5eb93d7d6eca | 690 | } |
MikGa87 | 0:1a41a881bf24 | 691 | |
MikGa87 | 0:1a41a881bf24 | 692 | } |
MikGa87 | 3:07ca6890f7b8 | 693 | |
MikGa87 | 3:07ca6890f7b8 | 694 | |
MikGa87 | 0:1a41a881bf24 | 695 | } |