![](/media/cache/profiles/100d5d9191f185eeb98d6e291756954a.50x50_q85.png)
Measure system
Dependencies: EthernetNetIf mbed RF12B
MeasureSystem.cpp@2:afe5826411e3, 2011-03-27 (annotated)
- Committer:
- benecsj
- Date:
- Sun Mar 27 07:56:59 2011 +0000
- Revision:
- 2:afe5826411e3
- Parent:
- 1:b26ab2467b1a
- Child:
- 3:799d8c61fb03
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
benecsj | 0:8d62137f7ff4 | 1 | #define MEASURE_C |
benecsj | 0:8d62137f7ff4 | 2 | |
benecsj | 0:8d62137f7ff4 | 3 | #include "EthernetNetIf.h" |
benecsj | 0:8d62137f7ff4 | 4 | #include "HTTPServer.h" |
benecsj | 0:8d62137f7ff4 | 5 | #include "SDFileSystem.h" |
benecsj | 0:8d62137f7ff4 | 6 | #include "TextLCD.h" |
benecsj | 0:8d62137f7ff4 | 7 | #include "DS1820.h" |
benecsj | 0:8d62137f7ff4 | 8 | #include "MeasureSystem.h" |
benecsj | 0:8d62137f7ff4 | 9 | #include "NTPClient.h" |
benecsj | 0:8d62137f7ff4 | 10 | #include "WatchDog.h" |
benecsj | 0:8d62137f7ff4 | 11 | #include "HTTPClient.h" |
benecsj | 1:b26ab2467b1a | 12 | #include "AvailableMemory.h" |
benecsj | 0:8d62137f7ff4 | 13 | |
benecsj | 2:afe5826411e3 | 14 | //-------------------Defines-------------------------------- |
benecsj | 2:afe5826411e3 | 15 | |
benecsj | 0:8d62137f7ff4 | 16 | |
benecsj | 2:afe5826411e3 | 17 | //-----------------Objects---------------------- |
benecsj | 2:afe5826411e3 | 18 | EthernetNetIf *eth; //Network IP stack |
benecsj | 2:afe5826411e3 | 19 | HTTPServer svr; //HTTP server |
benecsj | 2:afe5826411e3 | 20 | Serial pc(USBTX, USBRX); // Serial debug usb port |
benecsj | 2:afe5826411e3 | 21 | DigitalIn dhcpOn(p30); // Button |
benecsj | 2:afe5826411e3 | 22 | DigitalOut led1(LED1, "led1"); //Led 1 |
benecsj | 2:afe5826411e3 | 23 | DigitalOut led2(LED2, "led2"); //Led 2 |
benecsj | 2:afe5826411e3 | 24 | DigitalOut led3(LED3, "led3"); //Led 3 |
benecsj | 2:afe5826411e3 | 25 | DigitalOut led4(LED4, "led4"); //Led 4 |
benecsj | 2:afe5826411e3 | 26 | LocalFileSystem local("local");//Internal flash |
benecsj | 2:afe5826411e3 | 27 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // // External sd card |
benecsj | 2:afe5826411e3 | 28 | TextLCD lcd(p21, p22, p23, p24, p25, p26, TextLCD::LCD20x4 ); //Char lcd |
benecsj | 2:afe5826411e3 | 29 | watchdog wdog(15); // Watch dog timer 15 seconds |
benecsj | 2:afe5826411e3 | 30 | DS1820* probe[MAX_PROBES]; //Temp sensors |
benecsj | 0:8d62137f7ff4 | 31 | |
benecsj | 0:8d62137f7ff4 | 32 | //------status flags----------------- |
benecsj | 0:8d62137f7ff4 | 33 | int devices_found=0; |
benecsj | 2:afe5826411e3 | 34 | char wanIP[18]; |
benecsj | 2:afe5826411e3 | 35 | char localIP[18]; |
benecsj | 1:b26ab2467b1a | 36 | int fileindex =0; |
benecsj | 0:8d62137f7ff4 | 37 | bool getIPfromDHCP; |
benecsj | 1:b26ab2467b1a | 38 | Timer timerMeasure; |
benecsj | 0:8d62137f7ff4 | 39 | int ipConfig[4]; |
benecsj | 0:8d62137f7ff4 | 40 | int maskConfig[4]; |
benecsj | 0:8d62137f7ff4 | 41 | int gatewayConfig[4]; |
benecsj | 0:8d62137f7ff4 | 42 | int dnsConfig[4]; |
benecsj | 1:b26ab2467b1a | 43 | int handlerCounter =0; |
benecsj | 2:afe5826411e3 | 44 | string myUrl; |
benecsj | 2:afe5826411e3 | 45 | int measureCommand; |
benecsj | 0:8d62137f7ff4 | 46 | //---------------------MAIN----------------------- |
benecsj | 0:8d62137f7ff4 | 47 | int main() { |
benecsj | 2:afe5826411e3 | 48 | reset = false; |
benecsj | 2:afe5826411e3 | 49 | //Init serial com |
benecsj | 2:afe5826411e3 | 50 | pc.baud(BAUDRATE); |
benecsj | 2:afe5826411e3 | 51 | logfile = new char[50]; |
benecsj | 2:afe5826411e3 | 52 | //Init flags |
benecsj | 1:b26ab2467b1a | 53 | measureCommand = 0; |
benecsj | 2:afe5826411e3 | 54 | HandlerActive = false; |
benecsj | 2:afe5826411e3 | 55 | postOK = 0; |
benecsj | 2:afe5826411e3 | 56 | |
benecsj | 2:afe5826411e3 | 57 | //Init local variables for init sequence |
benecsj | 1:b26ab2467b1a | 58 | char buf[40]; |
benecsj | 1:b26ab2467b1a | 59 | string tempstring = (""); |
benecsj | 0:8d62137f7ff4 | 60 | //-----------------LCD test------------------- |
benecsj | 0:8d62137f7ff4 | 61 | printf("\r\n\r\n<----------------------------------------------->"); |
benecsj | 0:8d62137f7ff4 | 62 | printf("\r\n<System Startup>\r\n"); |
benecsj | 2:afe5826411e3 | 63 | PrintRAM(); |
benecsj | 0:8d62137f7ff4 | 64 | lcd.cls(); |
benecsj | 0:8d62137f7ff4 | 65 | lcd.printf("LCD Init.......Done\n"); |
benecsj | 0:8d62137f7ff4 | 66 | wait(0.2); |
benecsj | 0:8d62137f7ff4 | 67 | |
benecsj | 0:8d62137f7ff4 | 68 | //----------------SD.card test---------------- |
benecsj | 0:8d62137f7ff4 | 69 | printf("<SD card init>\r\n"); |
benecsj | 0:8d62137f7ff4 | 70 | lcd.printf("SDcard Init...."); |
benecsj | 1:b26ab2467b1a | 71 | mkdir(DATA_FOLDER, 0777); |
benecsj | 0:8d62137f7ff4 | 72 | FILE *fp = fopen(LOGGER_FILE, "a+"); |
benecsj | 0:8d62137f7ff4 | 73 | if (fp == NULL) { |
benecsj | 0:8d62137f7ff4 | 74 | error("Could not open file for write\r\n"); |
benecsj | 1:b26ab2467b1a | 75 | } else { |
benecsj | 1:b26ab2467b1a | 76 | fprintf(fp, "<-------------------->\r\n"); |
benecsj | 1:b26ab2467b1a | 77 | fclose(fp); |
benecsj | 1:b26ab2467b1a | 78 | tempstring ="SYSTEM STARTUP"; |
benecsj | 1:b26ab2467b1a | 79 | LogWrite(tempstring); |
benecsj | 0:8d62137f7ff4 | 80 | } |
benecsj | 0:8d62137f7ff4 | 81 | lcd.printf("Done\n"); |
benecsj | 1:b26ab2467b1a | 82 | |
benecsj | 1:b26ab2467b1a | 83 | |
benecsj | 0:8d62137f7ff4 | 84 | wdog.feed(); |
benecsj | 0:8d62137f7ff4 | 85 | //----------------DS1820 init----------------- |
benecsj | 0:8d62137f7ff4 | 86 | printf("<DS1820 init>\r\n"); |
benecsj | 0:8d62137f7ff4 | 87 | lcd.printf("Init sensors."); |
benecsj | 0:8d62137f7ff4 | 88 | InitDS1820(); |
benecsj | 0:8d62137f7ff4 | 89 | lcd.printf(".Done\n"); |
benecsj | 0:8d62137f7ff4 | 90 | |
benecsj | 0:8d62137f7ff4 | 91 | wdog.feed(); |
benecsj | 0:8d62137f7ff4 | 92 | //--------------Read config.txt--------------- |
benecsj | 0:8d62137f7ff4 | 93 | LoadConfig(); |
benecsj | 0:8d62137f7ff4 | 94 | //---------------Network init----------------- |
benecsj | 0:8d62137f7ff4 | 95 | lcd.printf("Network Init..."); |
benecsj | 0:8d62137f7ff4 | 96 | printf("<Network Init>\r\n"); |
benecsj | 0:8d62137f7ff4 | 97 | if (getIPfromDHCP) { |
benecsj | 0:8d62137f7ff4 | 98 | eth = new EthernetNetIf(); |
benecsj | 0:8d62137f7ff4 | 99 | } else { |
benecsj | 0:8d62137f7ff4 | 100 | eth = new EthernetNetIf( |
benecsj | 0:8d62137f7ff4 | 101 | IpAddr(ipConfig[0],ipConfig[1],ipConfig[2],ipConfig[3]), //IP Address |
benecsj | 0:8d62137f7ff4 | 102 | IpAddr(maskConfig[0],maskConfig[1],maskConfig[2],maskConfig[3]), //Network Mask |
benecsj | 0:8d62137f7ff4 | 103 | IpAddr(gatewayConfig[0],gatewayConfig[1],gatewayConfig[2],gatewayConfig[3]), //Gateway |
benecsj | 0:8d62137f7ff4 | 104 | IpAddr(dnsConfig[0],dnsConfig[1],dnsConfig[2],dnsConfig[3]) //DNS |
benecsj | 0:8d62137f7ff4 | 105 | ); |
benecsj | 0:8d62137f7ff4 | 106 | } |
benecsj | 0:8d62137f7ff4 | 107 | EthernetErr ethErr = eth->setup(10000); |
benecsj | 0:8d62137f7ff4 | 108 | if (ethErr) { |
benecsj | 0:8d62137f7ff4 | 109 | printf("Error %d in setup.\r\n", ethErr); |
benecsj | 0:8d62137f7ff4 | 110 | lcd.printf("Error"); |
benecsj | 0:8d62137f7ff4 | 111 | } |
benecsj | 2:afe5826411e3 | 112 | |
benecsj | 0:8d62137f7ff4 | 113 | sprintf(buf,"%d.%d.%d.%d", |
benecsj | 0:8d62137f7ff4 | 114 | eth->getIp()[0], eth->getIp()[1], eth->getIp()[2], eth->getIp()[3]); |
benecsj | 1:b26ab2467b1a | 115 | sprintf(localIP,"%s",buf); |
benecsj | 0:8d62137f7ff4 | 116 | printf("Setup OK\r\n"); |
benecsj | 0:8d62137f7ff4 | 117 | lcd.printf("Done\n"); |
benecsj | 0:8d62137f7ff4 | 118 | wdog.feed(); |
benecsj | 0:8d62137f7ff4 | 119 | |
benecsj | 0:8d62137f7ff4 | 120 | //-----------------WAN check--------------------------- |
benecsj | 0:8d62137f7ff4 | 121 | |
benecsj | 0:8d62137f7ff4 | 122 | wait(1); |
benecsj | 0:8d62137f7ff4 | 123 | printf("<WAN Connection Check>\r\n"); |
benecsj | 0:8d62137f7ff4 | 124 | lcd.cls(); |
benecsj | 0:8d62137f7ff4 | 125 | lcd.printf("Internet conn.."); |
benecsj | 0:8d62137f7ff4 | 126 | GetMyIP(); |
benecsj | 2:afe5826411e3 | 127 | if (wanIP[0]!=0) { |
benecsj | 0:8d62137f7ff4 | 128 | lcd.printf("Done\n"); |
benecsj | 1:b26ab2467b1a | 129 | printf("OK: %s\r\n",wanIP); |
benecsj | 0:8d62137f7ff4 | 130 | |
benecsj | 0:8d62137f7ff4 | 131 | } else { |
benecsj | 0:8d62137f7ff4 | 132 | lcd.printf("Fail\n"); |
benecsj | 0:8d62137f7ff4 | 133 | printf("No connection\r\n"); |
benecsj | 0:8d62137f7ff4 | 134 | } |
benecsj | 0:8d62137f7ff4 | 135 | wdog.feed(); |
benecsj | 0:8d62137f7ff4 | 136 | //-----------------RTC INIT------------------------ |
benecsj | 0:8d62137f7ff4 | 137 | |
benecsj | 0:8d62137f7ff4 | 138 | lcd.printf("RTClock Init..."); |
benecsj | 0:8d62137f7ff4 | 139 | printf("<RTC Init>\r\n"); |
benecsj | 0:8d62137f7ff4 | 140 | |
benecsj | 2:afe5826411e3 | 141 | if (wanIP[0]!=0) { |
benecsj | 0:8d62137f7ff4 | 142 | UpdateTime(); |
benecsj | 0:8d62137f7ff4 | 143 | strftime(buf,sizeof(buf), "%A %m/%d/%Y %H:%M:%S\n", localtime(&ctTime)); |
benecsj | 0:8d62137f7ff4 | 144 | printf("Time is now : %s JST\r\n", buf); |
benecsj | 0:8d62137f7ff4 | 145 | lcd.printf("Done\n"); |
benecsj | 0:8d62137f7ff4 | 146 | } else { |
benecsj | 0:8d62137f7ff4 | 147 | printf("Cannot connect NTP Server!!\r\n"); |
benecsj | 0:8d62137f7ff4 | 148 | lcd.printf("Fail\n"); |
benecsj | 0:8d62137f7ff4 | 149 | } |
benecsj | 0:8d62137f7ff4 | 150 | |
benecsj | 0:8d62137f7ff4 | 151 | wdog.feed(); |
benecsj | 0:8d62137f7ff4 | 152 | |
benecsj | 0:8d62137f7ff4 | 153 | //-------Init index.htm in local filesystem--------- |
benecsj | 0:8d62137f7ff4 | 154 | lcd.printf("Loading config."); |
benecsj | 0:8d62137f7ff4 | 155 | LoadStatus(); |
benecsj | 0:8d62137f7ff4 | 156 | IndexInit(); |
benecsj | 2:afe5826411e3 | 157 | if (logging == 1) { |
benecsj | 2:afe5826411e3 | 158 | timerMeasure.start(); |
benecsj | 1:b26ab2467b1a | 159 | } |
benecsj | 0:8d62137f7ff4 | 160 | lcd.printf("Done\n"); |
benecsj | 0:8d62137f7ff4 | 161 | wdog.feed(); |
benecsj | 0:8d62137f7ff4 | 162 | //-----------------Welcome message------------------ |
benecsj | 0:8d62137f7ff4 | 163 | wait(2); |
benecsj | 0:8d62137f7ff4 | 164 | lcd.cls(); |
benecsj | 0:8d62137f7ff4 | 165 | lcd.printf("Welcome to mbed. \nMeasure system.\n"); |
benecsj | 1:b26ab2467b1a | 166 | lcd.printf("IP: %s\n",localIP); |
benecsj | 0:8d62137f7ff4 | 167 | //--------------HTTP SERVER------------------------- |
benecsj | 0:8d62137f7ff4 | 168 | |
benecsj | 0:8d62137f7ff4 | 169 | FSHandler::mount("/local", "/"); //Mount /wwww path on web root path |
benecsj | 0:8d62137f7ff4 | 170 | FSHandler::mount("/sd", "/extern"); //Mount /wwww path on web root path |
benecsj | 0:8d62137f7ff4 | 171 | svr.addHandler<FSHandler>("/files");// this does not see the subdirectory |
benecsj | 0:8d62137f7ff4 | 172 | svr.addHandler<FSHandler>("/"); //Default handler |
benecsj | 0:8d62137f7ff4 | 173 | svr.addHandler<SimpleHandler>("/com"); //Com hander |
benecsj | 0:8d62137f7ff4 | 174 | svr.bind(80); |
benecsj | 0:8d62137f7ff4 | 175 | |
benecsj | 0:8d62137f7ff4 | 176 | printf("<INIT DONE... Server running>\r\n"); |
benecsj | 1:b26ab2467b1a | 177 | ctTime = time(NULL); |
benecsj | 1:b26ab2467b1a | 178 | ctTime += (clockoffset*3600); |
benecsj | 1:b26ab2467b1a | 179 | strftime(buf,sizeof(buf), "%Y/%m/%d %H:%M:%S", localtime(&ctTime)); |
benecsj | 0:8d62137f7ff4 | 180 | tempstring =""; |
benecsj | 1:b26ab2467b1a | 181 | tempstring += buf; |
benecsj | 1:b26ab2467b1a | 182 | LogWrite(tempstring); |
benecsj | 1:b26ab2467b1a | 183 | tempstring ="SYSTEM READY"; |
benecsj | 1:b26ab2467b1a | 184 | LogWrite(tempstring); |
benecsj | 0:8d62137f7ff4 | 185 | Timer tm; |
benecsj | 0:8d62137f7ff4 | 186 | tm.start(); |
benecsj | 0:8d62137f7ff4 | 187 | //Listen indefinitely |
benecsj | 0:8d62137f7ff4 | 188 | int counter =0; |
benecsj | 0:8d62137f7ff4 | 189 | |
benecsj | 0:8d62137f7ff4 | 190 | //-------------------MAIN LOOP-------------------- |
benecsj | 0:8d62137f7ff4 | 191 | while (true) { |
benecsj | 0:8d62137f7ff4 | 192 | Net::poll(); // Network listen |
benecsj | 0:8d62137f7ff4 | 193 | |
benecsj | 0:8d62137f7ff4 | 194 | if (tm.read()>1) { |
benecsj | 2:afe5826411e3 | 195 | //RESET REQUIED |
benecsj | 2:afe5826411e3 | 196 | if (reset) |
benecsj | 2:afe5826411e3 | 197 | { |
benecsj | 2:afe5826411e3 | 198 | mbed_reset(); |
benecsj | 2:afe5826411e3 | 199 | } |
benecsj | 2:afe5826411e3 | 200 | |
benecsj | 2:afe5826411e3 | 201 | counter = (counter++)%36000; //Timer counter loop |
benecsj | 0:8d62137f7ff4 | 202 | |
benecsj | 0:8d62137f7ff4 | 203 | led1=!led1; //Show that we are alive 1 sec timed |
benecsj | 2:afe5826411e3 | 204 | |
benecsj | 2:afe5826411e3 | 205 | //Show if logging |
benecsj | 1:b26ab2467b1a | 206 | if (logging==1) { |
benecsj | 1:b26ab2467b1a | 207 | led4 =!led1; |
benecsj | 1:b26ab2467b1a | 208 | } else { |
benecsj | 1:b26ab2467b1a | 209 | led4 = 0; |
benecsj | 1:b26ab2467b1a | 210 | } |
benecsj | 2:afe5826411e3 | 211 | |
benecsj | 2:afe5826411e3 | 212 | //Refress LCD |
benecsj | 2:afe5826411e3 | 213 | UpdateLCD(counter); |
benecsj | 2:afe5826411e3 | 214 | |
benecsj | 2:afe5826411e3 | 215 | //If no network detected aat startup then try to connect every 30 sec |
benecsj | 2:afe5826411e3 | 216 | if ( (wanIP[0]==0 & (counter%30)==0) | (wanIP[0]!=0 & (counter)==0)) { |
benecsj | 0:8d62137f7ff4 | 217 | GetMyIP(); |
benecsj | 2:afe5826411e3 | 218 | if (wanIP[0]!=0) { |
benecsj | 0:8d62137f7ff4 | 219 | IndexInit() ; |
benecsj | 0:8d62137f7ff4 | 220 | UpdateTime(); |
benecsj | 0:8d62137f7ff4 | 221 | } |
benecsj | 0:8d62137f7ff4 | 222 | } |
benecsj | 0:8d62137f7ff4 | 223 | |
benecsj | 1:b26ab2467b1a | 224 | //DEADLOCK PREVENTER |
benecsj | 1:b26ab2467b1a | 225 | if (HandlerActive) { |
benecsj | 1:b26ab2467b1a | 226 | handlerCounter++; |
benecsj | 1:b26ab2467b1a | 227 | } else { |
benecsj | 1:b26ab2467b1a | 228 | handlerCounter = 0; |
benecsj | 1:b26ab2467b1a | 229 | } |
benecsj | 1:b26ab2467b1a | 230 | if (handlerCounter>60) { |
benecsj | 1:b26ab2467b1a | 231 | handlerCounter = 0; |
benecsj | 1:b26ab2467b1a | 232 | HandlerActive = false; |
benecsj | 1:b26ab2467b1a | 233 | } |
benecsj | 1:b26ab2467b1a | 234 | |
benecsj | 2:afe5826411e3 | 235 | //Logger |
benecsj | 2:afe5826411e3 | 236 | if (logging == 1 ) { |
benecsj | 1:b26ab2467b1a | 237 | MeasureLogger(); |
benecsj | 1:b26ab2467b1a | 238 | } |
benecsj | 1:b26ab2467b1a | 239 | |
benecsj | 1:b26ab2467b1a | 240 | |
benecsj | 0:8d62137f7ff4 | 241 | wdog.feed(); |
benecsj | 0:8d62137f7ff4 | 242 | tm.start(); |
benecsj | 2:afe5826411e3 | 243 | |
benecsj | 2:afe5826411e3 | 244 | PrintRAM(); |
benecsj | 0:8d62137f7ff4 | 245 | } |
benecsj | 0:8d62137f7ff4 | 246 | |
benecsj | 0:8d62137f7ff4 | 247 | } |
benecsj | 0:8d62137f7ff4 | 248 | |
benecsj | 0:8d62137f7ff4 | 249 | |
benecsj | 0:8d62137f7ff4 | 250 | } |
benecsj | 0:8d62137f7ff4 | 251 | |
benecsj | 0:8d62137f7ff4 | 252 | |
benecsj | 0:8d62137f7ff4 | 253 | //---------------------------Functions-------------------------- |
benecsj | 0:8d62137f7ff4 | 254 | |
benecsj | 0:8d62137f7ff4 | 255 | void InitDS1820() { |
benecsj | 0:8d62137f7ff4 | 256 | |
benecsj | 0:8d62137f7ff4 | 257 | int i; |
benecsj | 0:8d62137f7ff4 | 258 | // Initialize the probe array to DS1820 objects |
benecsj | 0:8d62137f7ff4 | 259 | for (i = 0; i < MAX_PROBES; i++) |
benecsj | 2:afe5826411e3 | 260 | probe[i] = new DS1820(p27); |
benecsj | 0:8d62137f7ff4 | 261 | // Initialize global state variables |
benecsj | 0:8d62137f7ff4 | 262 | probe[0]->search_ROM_setup(); |
benecsj | 0:8d62137f7ff4 | 263 | // Loop to find all devices on the data line |
benecsj | 0:8d62137f7ff4 | 264 | while (probe[devices_found]->search_ROM() and devices_found<MAX_PROBES-1) |
benecsj | 0:8d62137f7ff4 | 265 | devices_found++; |
benecsj | 0:8d62137f7ff4 | 266 | // If maximum number of probes are found, |
benecsj | 0:8d62137f7ff4 | 267 | // bump the counter to include the last array entry |
benecsj | 0:8d62137f7ff4 | 268 | if (probe[devices_found]->ROM[0] != 0xFF) |
benecsj | 0:8d62137f7ff4 | 269 | devices_found++; |
benecsj | 0:8d62137f7ff4 | 270 | |
benecsj | 0:8d62137f7ff4 | 271 | if (devices_found==0) |
benecsj | 0:8d62137f7ff4 | 272 | printf("No devices found"); |
benecsj | 0:8d62137f7ff4 | 273 | else { |
benecsj | 0:8d62137f7ff4 | 274 | |
benecsj | 0:8d62137f7ff4 | 275 | probe[0]->convert_temperature(DS1820::all_devices); |
benecsj | 0:8d62137f7ff4 | 276 | for (i=0; i<devices_found; i++) { |
benecsj | 0:8d62137f7ff4 | 277 | printf("%3.1f \r\n",probe[i]->temperature('c')); |
benecsj | 0:8d62137f7ff4 | 278 | } |
benecsj | 0:8d62137f7ff4 | 279 | } |
benecsj | 0:8d62137f7ff4 | 280 | |
benecsj | 0:8d62137f7ff4 | 281 | lcd.printf("%d",devices_found); |
benecsj | 0:8d62137f7ff4 | 282 | } |
benecsj | 0:8d62137f7ff4 | 283 | |
benecsj | 0:8d62137f7ff4 | 284 | //-------------------Get WAN IP adress------------------------- |
benecsj | 0:8d62137f7ff4 | 285 | void GetMyIP() { |
benecsj | 0:8d62137f7ff4 | 286 | HTTPClient http; |
benecsj | 0:8d62137f7ff4 | 287 | HTTPText txt; |
benecsj | 0:8d62137f7ff4 | 288 | HTTPResult r = http.get("http://www.whatismyip.com/automation/n09230945NL.asp", &txt); |
benecsj | 0:8d62137f7ff4 | 289 | if (r==HTTP_OK) { |
benecsj | 1:b26ab2467b1a | 290 | sprintf(wanIP,"%s", txt.gets()); |
benecsj | 0:8d62137f7ff4 | 291 | } else { |
benecsj | 2:afe5826411e3 | 292 | sprintf(wanIP,NOINTERNET); |
benecsj | 0:8d62137f7ff4 | 293 | } |
benecsj | 0:8d62137f7ff4 | 294 | } |
benecsj | 0:8d62137f7ff4 | 295 | |
benecsj | 0:8d62137f7ff4 | 296 | //-----------------Create Index HTM in local------------------- |
benecsj | 0:8d62137f7ff4 | 297 | void IndexInit() { |
benecsj | 0:8d62137f7ff4 | 298 | FILE *fs_src; |
benecsj | 0:8d62137f7ff4 | 299 | FILE *fs_tgt; |
benecsj | 0:8d62137f7ff4 | 300 | char buffer[ COPY_BLOCK_SIZE ]; |
benecsj | 0:8d62137f7ff4 | 301 | |
benecsj | 0:8d62137f7ff4 | 302 | printf( "file copier started.\r\n" ); |
benecsj | 0:8d62137f7ff4 | 303 | printf( " source:%s >>> target:%s\r\n", SOURCE_FILE, TARGET_FILE ); |
benecsj | 0:8d62137f7ff4 | 304 | |
benecsj | 0:8d62137f7ff4 | 305 | if ( NULL == (fs_src = fopen( SOURCE_FILE, "rb" )) ) { |
benecsj | 0:8d62137f7ff4 | 306 | error( "couldn't open source file" ); |
benecsj | 0:8d62137f7ff4 | 307 | } |
benecsj | 0:8d62137f7ff4 | 308 | if ( NULL == (fs_tgt = fopen( TARGET_FILE, "wb" )) ) { |
benecsj | 0:8d62137f7ff4 | 309 | error( "couldn't open target file" ); |
benecsj | 0:8d62137f7ff4 | 310 | } |
benecsj | 0:8d62137f7ff4 | 311 | |
benecsj | 0:8d62137f7ff4 | 312 | /* |
benecsj | 0:8d62137f7ff4 | 313 | while ( size = fread( s, sizeof( char ), COPY_BLOCK_SIZE, fs_src ) ) |
benecsj | 0:8d62137f7ff4 | 314 | { |
benecsj | 0:8d62137f7ff4 | 315 | fwrite( s, sizeof( char ), size, fs_tgt ); |
benecsj | 0:8d62137f7ff4 | 316 | total += size; |
benecsj | 0:8d62137f7ff4 | 317 | |
benecsj | 0:8d62137f7ff4 | 318 | led1 = (0x1 << progress_ind++ % 4); |
benecsj | 0:8d62137f7ff4 | 319 | |
benecsj | 0:8d62137f7ff4 | 320 | printf( " %d bytes copied\r", total ); |
benecsj | 0:8d62137f7ff4 | 321 | } |
benecsj | 0:8d62137f7ff4 | 322 | */ |
benecsj | 0:8d62137f7ff4 | 323 | string temp; |
benecsj | 0:8d62137f7ff4 | 324 | char token[] = "ServerAddress"; |
benecsj | 0:8d62137f7ff4 | 325 | while (fgets(buffer, COPY_BLOCK_SIZE, fs_src)) { |
benecsj | 0:8d62137f7ff4 | 326 | |
benecsj | 0:8d62137f7ff4 | 327 | temp = string(buffer); |
benecsj | 0:8d62137f7ff4 | 328 | int j=0; |
benecsj | 0:8d62137f7ff4 | 329 | for (int i=0;i<COPY_BLOCK_SIZE;i++) { |
benecsj | 0:8d62137f7ff4 | 330 | if (buffer[i]==token[j]) { |
benecsj | 0:8d62137f7ff4 | 331 | j++; |
benecsj | 0:8d62137f7ff4 | 332 | } else { |
benecsj | 0:8d62137f7ff4 | 333 | j=0; |
benecsj | 0:8d62137f7ff4 | 334 | } |
benecsj | 0:8d62137f7ff4 | 335 | |
benecsj | 0:8d62137f7ff4 | 336 | if (j==12) break; |
benecsj | 0:8d62137f7ff4 | 337 | |
benecsj | 0:8d62137f7ff4 | 338 | if (buffer[i]==0) { |
benecsj | 0:8d62137f7ff4 | 339 | break; |
benecsj | 0:8d62137f7ff4 | 340 | } |
benecsj | 0:8d62137f7ff4 | 341 | } |
benecsj | 0:8d62137f7ff4 | 342 | |
benecsj | 0:8d62137f7ff4 | 343 | if (j==12) { //Change to find string element search by char. |
benecsj | 2:afe5826411e3 | 344 | if (wanIP[0]!=0) { |
benecsj | 1:b26ab2467b1a | 345 | fprintf(fs_tgt," <param name=\"ServerAddress\" value=\"%s\">",myUrl.c_str()); |
benecsj | 2:afe5826411e3 | 346 | } |
benecsj | 2:afe5826411e3 | 347 | else |
benecsj | 2:afe5826411e3 | 348 | { |
benecsj | 2:afe5826411e3 | 349 | fprintf(fs_tgt," <param name=\"ServerAddress\" value=\"%s\">",localIP); |
benecsj | 2:afe5826411e3 | 350 | } |
benecsj | 0:8d62137f7ff4 | 351 | } else { |
benecsj | 0:8d62137f7ff4 | 352 | fprintf(fs_tgt,"%s",temp.c_str()); |
benecsj | 0:8d62137f7ff4 | 353 | } |
benecsj | 0:8d62137f7ff4 | 354 | } |
benecsj | 0:8d62137f7ff4 | 355 | |
benecsj | 0:8d62137f7ff4 | 356 | led1 = 0xF; |
benecsj | 0:8d62137f7ff4 | 357 | |
benecsj | 0:8d62137f7ff4 | 358 | fclose( fs_src ); |
benecsj | 0:8d62137f7ff4 | 359 | fclose( fs_tgt ); |
benecsj | 0:8d62137f7ff4 | 360 | |
benecsj | 0:8d62137f7ff4 | 361 | printf( "done\r\n" ); |
benecsj | 0:8d62137f7ff4 | 362 | } |
benecsj | 0:8d62137f7ff4 | 363 | |
benecsj | 0:8d62137f7ff4 | 364 | //------------Update lcd------------------------------ |
benecsj | 0:8d62137f7ff4 | 365 | bool IPshowflag = false; |
benecsj | 0:8d62137f7ff4 | 366 | void UpdateLCD(int counter) { |
benecsj | 2:afe5826411e3 | 367 | static int sensorcounter=0; |
benecsj | 1:b26ab2467b1a | 368 | char buf[40]; |
benecsj | 1:b26ab2467b1a | 369 | |
benecsj | 1:b26ab2467b1a | 370 | if (counter%1==0) { //Every sec |
benecsj | 1:b26ab2467b1a | 371 | lcd.locate(0,1); |
benecsj | 1:b26ab2467b1a | 372 | if (logging) { |
benecsj | 1:b26ab2467b1a | 373 | lcd.printf("Logging with: %d ",interval); |
benecsj | 1:b26ab2467b1a | 374 | } else { |
benecsj | 1:b26ab2467b1a | 375 | lcd.printf("Not logging... "); |
benecsj | 1:b26ab2467b1a | 376 | } |
benecsj | 1:b26ab2467b1a | 377 | } |
benecsj | 2:afe5826411e3 | 378 | |
benecsj | 2:afe5826411e3 | 379 | if (counter%2==0) { |
benecsj | 2:afe5826411e3 | 380 | lcd.locate(0,0); |
benecsj | 2:afe5826411e3 | 381 | if (devices_found>0) |
benecsj | 2:afe5826411e3 | 382 | { |
benecsj | 2:afe5826411e3 | 383 | lcd.printf("%d) sensor: %3.1fC ",(sensorcounter+1),probe[sensorcounter]->temperature('c')); |
benecsj | 2:afe5826411e3 | 384 | sensorcounter = (++sensorcounter)%devices_found; |
benecsj | 2:afe5826411e3 | 385 | } |
benecsj | 2:afe5826411e3 | 386 | } |
benecsj | 2:afe5826411e3 | 387 | |
benecsj | 0:8d62137f7ff4 | 388 | if (counter%4==0) { |
benecsj | 0:8d62137f7ff4 | 389 | lcd.locate(0,2); |
benecsj | 2:afe5826411e3 | 390 | if (wanIP[0]!=0) { |
benecsj | 0:8d62137f7ff4 | 391 | if (IPshowflag) { |
benecsj | 1:b26ab2467b1a | 392 | lcd.printf("IP: %s ", localIP); |
benecsj | 0:8d62137f7ff4 | 393 | } else { |
benecsj | 1:b26ab2467b1a | 394 | lcd.printf("IP: %s ", wanIP); |
benecsj | 0:8d62137f7ff4 | 395 | } |
benecsj | 0:8d62137f7ff4 | 396 | IPshowflag = !IPshowflag; |
benecsj | 0:8d62137f7ff4 | 397 | } else { |
benecsj | 0:8d62137f7ff4 | 398 | //DO nothing only one ip address is shown |
benecsj | 0:8d62137f7ff4 | 399 | } |
benecsj | 0:8d62137f7ff4 | 400 | } |
benecsj | 0:8d62137f7ff4 | 401 | |
benecsj | 0:8d62137f7ff4 | 402 | if (counter%1==0) { //Every sec |
benecsj | 0:8d62137f7ff4 | 403 | lcd.locate(0,3); |
benecsj | 0:8d62137f7ff4 | 404 | ctTime = time(NULL); |
benecsj | 0:8d62137f7ff4 | 405 | ctTime += (clockoffset*3600); |
benecsj | 0:8d62137f7ff4 | 406 | strftime(buf,sizeof(buf), "%Y/%m/%d %H:%M:%S", localtime(&ctTime)); |
benecsj | 0:8d62137f7ff4 | 407 | lcd.printf("%s", buf); |
benecsj | 0:8d62137f7ff4 | 408 | } |
benecsj | 0:8d62137f7ff4 | 409 | |
benecsj | 0:8d62137f7ff4 | 410 | |
benecsj | 0:8d62137f7ff4 | 411 | } |
benecsj | 0:8d62137f7ff4 | 412 | |
benecsj | 0:8d62137f7ff4 | 413 | //----------------Update time--------------------- |
benecsj | 0:8d62137f7ff4 | 414 | void UpdateTime() { |
benecsj | 2:afe5826411e3 | 415 | NTPClient * ntp = new NTPClient(); //NTP client |
benecsj | 0:8d62137f7ff4 | 416 | time_t ctTime; |
benecsj | 0:8d62137f7ff4 | 417 | ctTime = time(NULL); |
benecsj | 0:8d62137f7ff4 | 418 | Host server(IpAddr(), 123, "0.hu.pool.ntp.org"); |
benecsj | 2:afe5826411e3 | 419 | ntp->setTime(server); |
benecsj | 0:8d62137f7ff4 | 420 | ctTime = time(NULL); |
benecsj | 0:8d62137f7ff4 | 421 | ctTime += (clockoffset*3600); //set jst time |
benecsj | 0:8d62137f7ff4 | 422 | } |
benecsj | 0:8d62137f7ff4 | 423 | |
benecsj | 2:afe5826411e3 | 424 | //-------------Get file name--------------------- |
benecsj | 1:b26ab2467b1a | 425 | void GetFile(int index, char *st) { |
benecsj | 1:b26ab2467b1a | 426 | string stringt = (""); |
benecsj | 1:b26ab2467b1a | 427 | char buff[50]; |
benecsj | 1:b26ab2467b1a | 428 | ctTime = time(NULL); |
benecsj | 1:b26ab2467b1a | 429 | ctTime += (clockoffset*3600); |
benecsj | 1:b26ab2467b1a | 430 | strftime(buff,sizeof(buff), "%Y/%m/%d %H:%M:%S", localtime(&ctTime)); |
benecsj | 1:b26ab2467b1a | 431 | stringt += buff[2]; |
benecsj | 1:b26ab2467b1a | 432 | stringt += buff[3]; |
benecsj | 1:b26ab2467b1a | 433 | stringt += buff[5]; |
benecsj | 1:b26ab2467b1a | 434 | stringt += buff[6]; |
benecsj | 1:b26ab2467b1a | 435 | stringt += buff[8]; |
benecsj | 1:b26ab2467b1a | 436 | stringt += buff[9]; |
benecsj | 1:b26ab2467b1a | 437 | sprintf(buff,"%02d",index); |
benecsj | 1:b26ab2467b1a | 438 | stringt += buff; |
benecsj | 1:b26ab2467b1a | 439 | stringt += ".txt"; |
benecsj | 1:b26ab2467b1a | 440 | strcpy(st, stringt.c_str()); |
benecsj | 1:b26ab2467b1a | 441 | } |
benecsj | 1:b26ab2467b1a | 442 | |
benecsj | 0:8d62137f7ff4 | 443 | //---------------Load config---------------------- |
benecsj | 0:8d62137f7ff4 | 444 | void LoadConfig() { |
benecsj | 0:8d62137f7ff4 | 445 | FILE *fs_src; |
benecsj | 0:8d62137f7ff4 | 446 | char buffer[ 100 ]; |
benecsj | 0:8d62137f7ff4 | 447 | int linecounter = 0; |
benecsj | 0:8d62137f7ff4 | 448 | fs_src = fopen( CONFIG_FILE, "rb" ); |
benecsj | 0:8d62137f7ff4 | 449 | while (fgets(buffer, 100, fs_src)) { |
benecsj | 0:8d62137f7ff4 | 450 | linecounter++; |
benecsj | 0:8d62137f7ff4 | 451 | |
benecsj | 0:8d62137f7ff4 | 452 | switch (linecounter) { |
benecsj | 0:8d62137f7ff4 | 453 | case 2 : |
benecsj | 0:8d62137f7ff4 | 454 | sscanf(buffer,"%d.%d.%d.%d",&ipConfig[0],&ipConfig[1],&ipConfig[2],&ipConfig[3]); |
benecsj | 0:8d62137f7ff4 | 455 | break; |
benecsj | 0:8d62137f7ff4 | 456 | case 4 : |
benecsj | 0:8d62137f7ff4 | 457 | sscanf(buffer,"%d.%d.%d.%d",&maskConfig[0],&maskConfig[1],&maskConfig[2],&maskConfig[3]); |
benecsj | 0:8d62137f7ff4 | 458 | break; |
benecsj | 0:8d62137f7ff4 | 459 | case 6 : |
benecsj | 0:8d62137f7ff4 | 460 | sscanf(buffer,"%d.%d.%d.%d",&gatewayConfig[0],&gatewayConfig[1],&gatewayConfig[2],&gatewayConfig[3]); |
benecsj | 0:8d62137f7ff4 | 461 | break; |
benecsj | 0:8d62137f7ff4 | 462 | case 8 : |
benecsj | 0:8d62137f7ff4 | 463 | sscanf(buffer,"%d.%d.%d.%d",&dnsConfig[0],&dnsConfig[1],&dnsConfig[2],&dnsConfig[3]); |
benecsj | 0:8d62137f7ff4 | 464 | break; |
benecsj | 0:8d62137f7ff4 | 465 | case 10: |
benecsj | 0:8d62137f7ff4 | 466 | if (buffer[0]=='t' | buffer[0]=='T') getIPfromDHCP= true; |
benecsj | 0:8d62137f7ff4 | 467 | else getIPfromDHCP = false; |
benecsj | 0:8d62137f7ff4 | 468 | break; |
benecsj | 0:8d62137f7ff4 | 469 | case 13: |
benecsj | 0:8d62137f7ff4 | 470 | sscanf(buffer,"%d",&interval); |
benecsj | 0:8d62137f7ff4 | 471 | break; |
benecsj | 0:8d62137f7ff4 | 472 | case 15: |
benecsj | 0:8d62137f7ff4 | 473 | sscanf(buffer,"%d",&clockoffset); |
benecsj | 0:8d62137f7ff4 | 474 | break; |
benecsj | 1:b26ab2467b1a | 475 | case 18: |
benecsj | 1:b26ab2467b1a | 476 | myUrl = buffer; |
benecsj | 1:b26ab2467b1a | 477 | break; |
benecsj | 0:8d62137f7ff4 | 478 | } |
benecsj | 0:8d62137f7ff4 | 479 | |
benecsj | 0:8d62137f7ff4 | 480 | } |
benecsj | 0:8d62137f7ff4 | 481 | |
benecsj | 0:8d62137f7ff4 | 482 | fclose( fs_src ); |
benecsj | 0:8d62137f7ff4 | 483 | } |
benecsj | 2:afe5826411e3 | 484 | //---------------Save config-------------------------- |
benecsj | 2:afe5826411e3 | 485 | void SaveConfig() |
benecsj | 2:afe5826411e3 | 486 | { |
benecsj | 2:afe5826411e3 | 487 | printf("SAVING CONFIG\r\n"); |
benecsj | 2:afe5826411e3 | 488 | FILE *fs_src; |
benecsj | 2:afe5826411e3 | 489 | fs_src = fopen( CONFIG_FILE, "w" ); |
benecsj | 2:afe5826411e3 | 490 | printf("SAVING CONFIG\r\n"); |
benecsj | 2:afe5826411e3 | 491 | fprintf(fs_src,"%s\r\n","IP adress"); |
benecsj | 2:afe5826411e3 | 492 | fprintf(fs_src,"%d.%d.%d.%d\r\n",ipConfig[0],ipConfig[1],ipConfig[2],ipConfig[3]); |
benecsj | 2:afe5826411e3 | 493 | fprintf(fs_src,"%s\r\n","Network Mask"); |
benecsj | 2:afe5826411e3 | 494 | fprintf(fs_src,"%d.%d.%d.%d\r\n",maskConfig[0],maskConfig[1],maskConfig[2],maskConfig[3]); |
benecsj | 2:afe5826411e3 | 495 | fprintf(fs_src,"%s\r\n","Gateway"); |
benecsj | 2:afe5826411e3 | 496 | fprintf(fs_src,"%d.%d.%d.%d\r\n",gatewayConfig[0],gatewayConfig[1],gatewayConfig[2],gatewayConfig[3]); |
benecsj | 2:afe5826411e3 | 497 | fprintf(fs_src,"%s\r\n","DNS"); |
benecsj | 2:afe5826411e3 | 498 | fprintf(fs_src,"%d.%d.%d.%d\r\n",dnsConfig[0],dnsConfig[1],dnsConfig[2],dnsConfig[3]); |
benecsj | 2:afe5826411e3 | 499 | fprintf(fs_src,"%s\r\n","Get IP from DHCP (true/false)"); |
benecsj | 2:afe5826411e3 | 500 | if (getIPfromDHCP) |
benecsj | 2:afe5826411e3 | 501 | { |
benecsj | 2:afe5826411e3 | 502 | fprintf(fs_src,"True\r\n"); |
benecsj | 2:afe5826411e3 | 503 | } |
benecsj | 2:afe5826411e3 | 504 | else |
benecsj | 2:afe5826411e3 | 505 | { |
benecsj | 2:afe5826411e3 | 506 | fprintf(fs_src,"False\r\n"); |
benecsj | 2:afe5826411e3 | 507 | } |
benecsj | 2:afe5826411e3 | 508 | fprintf(fs_src,"%s\r\n","---------------"); |
benecsj | 2:afe5826411e3 | 509 | fprintf(fs_src,"%s\r\n","Interval (sec)"); |
benecsj | 2:afe5826411e3 | 510 | fprintf(fs_src,"%d\r\n",interval); |
benecsj | 2:afe5826411e3 | 511 | fprintf(fs_src,"%s\r\n","CET Clock offset (hour)"); |
benecsj | 2:afe5826411e3 | 512 | fprintf(fs_src,"%d\r\n",clockoffset); |
benecsj | 2:afe5826411e3 | 513 | fprintf(fs_src,"%s\r\n","---------------"); |
benecsj | 2:afe5826411e3 | 514 | fprintf(fs_src,"%s\r\n","My URL"); |
benecsj | 2:afe5826411e3 | 515 | fprintf(fs_src,"%s\r\n",myUrl.c_str()); |
benecsj | 0:8d62137f7ff4 | 516 | |
benecsj | 2:afe5826411e3 | 517 | fclose( fs_src ); |
benecsj | 2:afe5826411e3 | 518 | } |
benecsj | 0:8d62137f7ff4 | 519 | //---------------Load status-------------------------- |
benecsj | 1:b26ab2467b1a | 520 | void LoadStatus() { |
benecsj | 0:8d62137f7ff4 | 521 | FILE *fs_src; |
benecsj | 0:8d62137f7ff4 | 522 | char buffer[ 100 ]; |
benecsj | 0:8d62137f7ff4 | 523 | int linecounter = 0; |
benecsj | 0:8d62137f7ff4 | 524 | fs_src = fopen( STATUS_FILE, "rb" ); |
benecsj | 0:8d62137f7ff4 | 525 | while (fgets(buffer, 100, fs_src)) { |
benecsj | 0:8d62137f7ff4 | 526 | linecounter++; |
benecsj | 0:8d62137f7ff4 | 527 | |
benecsj | 0:8d62137f7ff4 | 528 | switch (linecounter) { |
benecsj | 0:8d62137f7ff4 | 529 | case 1: |
benecsj | 0:8d62137f7ff4 | 530 | sscanf(buffer,"%d",&logging); |
benecsj | 0:8d62137f7ff4 | 531 | break; |
benecsj | 0:8d62137f7ff4 | 532 | case 2: |
benecsj | 1:b26ab2467b1a | 533 | sscanf(buffer,"%s",logfile); |
benecsj | 0:8d62137f7ff4 | 534 | break; |
benecsj | 0:8d62137f7ff4 | 535 | } |
benecsj | 0:8d62137f7ff4 | 536 | |
benecsj | 0:8d62137f7ff4 | 537 | } |
benecsj | 0:8d62137f7ff4 | 538 | |
benecsj | 0:8d62137f7ff4 | 539 | fclose( fs_src ); |
benecsj | 0:8d62137f7ff4 | 540 | } |
benecsj | 0:8d62137f7ff4 | 541 | |
benecsj | 1:b26ab2467b1a | 542 | //-----------------Save status------------------------- |
benecsj | 1:b26ab2467b1a | 543 | void SaveStatus() { |
benecsj | 1:b26ab2467b1a | 544 | printf("Saving status\r\n"); |
benecsj | 1:b26ab2467b1a | 545 | FILE *fs_dest; |
benecsj | 1:b26ab2467b1a | 546 | fs_dest = fopen( STATUS_FILE, "w" ); |
benecsj | 1:b26ab2467b1a | 547 | fprintf(fs_dest,"%d\r\n%s\r\n",logging,logfile); |
benecsj | 1:b26ab2467b1a | 548 | fclose( fs_dest ); |
benecsj | 1:b26ab2467b1a | 549 | printf("Status saved\r\n"); |
benecsj | 1:b26ab2467b1a | 550 | } |
benecsj | 1:b26ab2467b1a | 551 | |
benecsj | 1:b26ab2467b1a | 552 | //---------------Log writer----------------------------- |
benecsj | 1:b26ab2467b1a | 553 | void LogWrite(string stringin) { |
benecsj | 2:afe5826411e3 | 554 | FILE *fp = fopen(LOGGER_FILE, "a+"); |
benecsj | 0:8d62137f7ff4 | 555 | if (fp == NULL) { |
benecsj | 0:8d62137f7ff4 | 556 | error("Could not open file for write\r\n"); |
benecsj | 0:8d62137f7ff4 | 557 | } |
benecsj | 1:b26ab2467b1a | 558 | fprintf(fp,"%s\r\n",stringin.c_str()); |
benecsj | 0:8d62137f7ff4 | 559 | fclose(fp); |
benecsj | 0:8d62137f7ff4 | 560 | } |
benecsj | 1:b26ab2467b1a | 561 | |
benecsj | 2:afe5826411e3 | 562 | //---------------Measurement start -------------- |
benecsj | 1:b26ab2467b1a | 563 | |
benecsj | 1:b26ab2467b1a | 564 | void MeasureStart() { |
benecsj | 2:afe5826411e3 | 565 | |
benecsj | 1:b26ab2467b1a | 566 | char file[20]; |
benecsj | 2:afe5826411e3 | 567 | char st[50]; |
benecsj | 2:afe5826411e3 | 568 | |
benecsj | 1:b26ab2467b1a | 569 | if (logging ==0) { |
benecsj | 1:b26ab2467b1a | 570 | |
benecsj | 1:b26ab2467b1a | 571 | |
benecsj | 1:b26ab2467b1a | 572 | logging = 1; |
benecsj | 1:b26ab2467b1a | 573 | timerMeasure.start(); |
benecsj | 1:b26ab2467b1a | 574 | do { |
benecsj | 2:afe5826411e3 | 575 | GetFile(fileindex++,(char*)&st); |
benecsj | 2:afe5826411e3 | 576 | strcpy(file,(char*)&st); |
benecsj | 1:b26ab2467b1a | 577 | |
benecsj | 1:b26ab2467b1a | 578 | sprintf(logfile,"%s%s%s",DATA_FOLDER,"/",file); |
benecsj | 2:afe5826411e3 | 579 | PrintRAM(); |
benecsj | 1:b26ab2467b1a | 580 | } while (exists(DATA_FOLDER,file)); // while (sdcardIsFileExist (filepath.c_str())=='t'); |
benecsj | 2:afe5826411e3 | 581 | |
benecsj | 1:b26ab2467b1a | 582 | printf ("Saving log file name : %s\r\n",logfile); |
benecsj | 1:b26ab2467b1a | 583 | |
benecsj | 1:b26ab2467b1a | 584 | |
benecsj | 1:b26ab2467b1a | 585 | SaveStatus(); |
benecsj | 1:b26ab2467b1a | 586 | |
benecsj | 1:b26ab2467b1a | 587 | printf ("Printing header into log file: %s\r\n",logfile); |
benecsj | 2:afe5826411e3 | 588 | |
benecsj | 1:b26ab2467b1a | 589 | sdcardWriteFile (logfile,WRITEFILE,"Measure Start\r\n"); |
benecsj | 2:afe5826411e3 | 590 | |
benecsj | 1:b26ab2467b1a | 591 | } |
benecsj | 2:afe5826411e3 | 592 | |
benecsj | 2:afe5826411e3 | 593 | |
benecsj | 1:b26ab2467b1a | 594 | } |
benecsj | 1:b26ab2467b1a | 595 | |
benecsj | 2:afe5826411e3 | 596 | //-------------------Measure stop method----------------------------------- |
benecsj | 1:b26ab2467b1a | 597 | void MeasureStop() { |
benecsj | 1:b26ab2467b1a | 598 | if (logging == 1) { |
benecsj | 1:b26ab2467b1a | 599 | printf ("Stopping measure."); |
benecsj | 1:b26ab2467b1a | 600 | logging = 0; |
benecsj | 1:b26ab2467b1a | 601 | SaveStatus(); |
benecsj | 1:b26ab2467b1a | 602 | timerMeasure.stop(); |
benecsj | 1:b26ab2467b1a | 603 | printf ("Measure stopped."); |
benecsj | 1:b26ab2467b1a | 604 | } |
benecsj | 1:b26ab2467b1a | 605 | |
benecsj | 1:b26ab2467b1a | 606 | } |
benecsj | 1:b26ab2467b1a | 607 | |
benecsj | 1:b26ab2467b1a | 608 | /** ---------------------------------------------------------------------- |
benecsj | 1:b26ab2467b1a | 609 | * sdcardWriteFile |
benecsj | 1:b26ab2467b1a | 610 | * Ecrit une trame dans le fichier specifie |
benecsj | 1:b26ab2467b1a | 611 | * In : nom du fichier, mode d'ecriture, donnees a ecrire |
benecsj | 1:b26ab2467b1a | 612 | * Out : true/false |
benecsj | 1:b26ab2467b1a | 613 | * ---------------------------------------------------------------------*/ |
benecsj | 1:b26ab2467b1a | 614 | char sdcardWriteFile(const char *fileName, char mode, char *data) { |
benecsj | 1:b26ab2467b1a | 615 | |
benecsj | 1:b26ab2467b1a | 616 | FILE *fp2; |
benecsj | 1:b26ab2467b1a | 617 | switch (mode) { |
benecsj | 1:b26ab2467b1a | 618 | case WRITEFILE : |
benecsj | 1:b26ab2467b1a | 619 | fp2 = fopen(fileName, "w+"); |
benecsj | 1:b26ab2467b1a | 620 | break; |
benecsj | 1:b26ab2467b1a | 621 | case APPENDFILE : |
benecsj | 1:b26ab2467b1a | 622 | fp2 = fopen(fileName, "a+"); |
benecsj | 1:b26ab2467b1a | 623 | break; |
benecsj | 1:b26ab2467b1a | 624 | default: |
benecsj | 1:b26ab2467b1a | 625 | fp2 = fopen(fileName, "r"); |
benecsj | 1:b26ab2467b1a | 626 | } |
benecsj | 1:b26ab2467b1a | 627 | |
benecsj | 1:b26ab2467b1a | 628 | if (!fp2) { |
benecsj | 1:b26ab2467b1a | 629 | return (false); |
benecsj | 1:b26ab2467b1a | 630 | } else { |
benecsj | 1:b26ab2467b1a | 631 | fprintf(fp2,data); |
benecsj | 1:b26ab2467b1a | 632 | fclose(fp2); |
benecsj | 1:b26ab2467b1a | 633 | } |
benecsj | 1:b26ab2467b1a | 634 | |
benecsj | 1:b26ab2467b1a | 635 | return (true); |
benecsj | 1:b26ab2467b1a | 636 | } |
benecsj | 1:b26ab2467b1a | 637 | |
benecsj | 1:b26ab2467b1a | 638 | |
benecsj | 1:b26ab2467b1a | 639 | //---------- Logger method------------------------------------------------ |
benecsj | 1:b26ab2467b1a | 640 | void MeasureLogger() { |
benecsj | 1:b26ab2467b1a | 641 | |
benecsj | 2:afe5826411e3 | 642 | if (timerMeasure.read() >= (interval-1)) { |
benecsj | 2:afe5826411e3 | 643 | led2 = 1; |
benecsj | 1:b26ab2467b1a | 644 | char buf[50]; |
benecsj | 1:b26ab2467b1a | 645 | ctTime = time(NULL); |
benecsj | 1:b26ab2467b1a | 646 | ctTime += (clockoffset*3600); |
benecsj | 1:b26ab2467b1a | 647 | strftime(buf,sizeof(buf), "%Y.%m.%d / %H:%M:%S", localtime(&ctTime)); |
benecsj | 1:b26ab2467b1a | 648 | |
benecsj | 1:b26ab2467b1a | 649 | char resp[100] = ""; |
benecsj | 1:b26ab2467b1a | 650 | strcat(resp,buf); |
benecsj | 1:b26ab2467b1a | 651 | char temp[32] ; |
benecsj | 1:b26ab2467b1a | 652 | probe[0]->convert_temperature(DS1820::all_devices); |
benecsj | 1:b26ab2467b1a | 653 | for (int i=0; i<devices_found; i++) { |
benecsj | 1:b26ab2467b1a | 654 | sprintf(temp," / %3.1f",(probe[i]->temperature('c'))); |
benecsj | 1:b26ab2467b1a | 655 | strcat(resp,temp); |
benecsj | 1:b26ab2467b1a | 656 | } |
benecsj | 1:b26ab2467b1a | 657 | sprintf( temp , "\r\n"); |
benecsj | 1:b26ab2467b1a | 658 | strcat(resp,temp); |
benecsj | 1:b26ab2467b1a | 659 | sdcardWriteFile (logfile,APPENDFILE,resp); |
benecsj | 1:b26ab2467b1a | 660 | |
benecsj | 2:afe5826411e3 | 661 | led2 = 0; |
benecsj | 1:b26ab2467b1a | 662 | timerMeasure.reset(); |
benecsj | 1:b26ab2467b1a | 663 | |
benecsj | 1:b26ab2467b1a | 664 | } |
benecsj | 1:b26ab2467b1a | 665 | |
benecsj | 1:b26ab2467b1a | 666 | } |
benecsj | 1:b26ab2467b1a | 667 | |
benecsj | 2:afe5826411e3 | 668 | //------------Checks if file exist-------------------------------- |
benecsj | 1:b26ab2467b1a | 669 | bool exists(char * root, char *filename) { |
benecsj | 1:b26ab2467b1a | 670 | DIR *d = opendir(root); |
benecsj | 1:b26ab2467b1a | 671 | struct dirent *p; |
benecsj | 1:b26ab2467b1a | 672 | //printf("\nList of files in the directory %s:\r\n", root); |
benecsj | 1:b26ab2467b1a | 673 | bool found = false; |
benecsj | 1:b26ab2467b1a | 674 | if ( d != NULL ) { |
benecsj | 1:b26ab2467b1a | 675 | while ( !found && (p = readdir(d)) != NULL ) { |
benecsj | 1:b26ab2467b1a | 676 | //printf(" - %s\r\n", p->d_name); |
benecsj | 1:b26ab2467b1a | 677 | if ( strcmp(p->d_name, filename) == 0 ) { |
benecsj | 1:b26ab2467b1a | 678 | found = true; |
benecsj | 1:b26ab2467b1a | 679 | printf("File exist\r\n"); |
benecsj | 1:b26ab2467b1a | 680 | } |
benecsj | 1:b26ab2467b1a | 681 | } |
benecsj | 1:b26ab2467b1a | 682 | } |
benecsj | 1:b26ab2467b1a | 683 | if (!found) { |
benecsj | 1:b26ab2467b1a | 684 | printf("File not found\r\n"); |
benecsj | 1:b26ab2467b1a | 685 | } |
benecsj | 1:b26ab2467b1a | 686 | closedir(d); |
benecsj | 1:b26ab2467b1a | 687 | return found; |
benecsj | 2:afe5826411e3 | 688 | } |
benecsj | 2:afe5826411e3 | 689 | |
benecsj | 2:afe5826411e3 | 690 | //------------------Prints out free ram space---------------------- |
benecsj | 2:afe5826411e3 | 691 | void PrintRAM() |
benecsj | 2:afe5826411e3 | 692 | { |
benecsj | 2:afe5826411e3 | 693 | printf("Available memory (exact bytes) : %d\r\n", AvailableMemory(1)); |
benecsj | 2:afe5826411e3 | 694 | } |
benecsj | 2:afe5826411e3 | 695 | //-----------------Update List------------------------------------- |
benecsj | 2:afe5826411e3 | 696 | void UpdateList() |
benecsj | 2:afe5826411e3 | 697 | { |
benecsj | 2:afe5826411e3 | 698 | DIR *d = opendir(DATA_FOLDER); |
benecsj | 2:afe5826411e3 | 699 | struct dirent *p; |
benecsj | 2:afe5826411e3 | 700 | char buff[40]; |
benecsj | 2:afe5826411e3 | 701 | printf("\nList of files in the directory %s:\r\n", DATA_FOLDER); |
benecsj | 2:afe5826411e3 | 702 | |
benecsj | 2:afe5826411e3 | 703 | sdcardWriteFile (FILELIST_FILE,WRITEFILE,""); |
benecsj | 2:afe5826411e3 | 704 | |
benecsj | 2:afe5826411e3 | 705 | FILE *fp2; |
benecsj | 2:afe5826411e3 | 706 | fp2 = fopen(FILELIST_FILE, "a+"); |
benecsj | 2:afe5826411e3 | 707 | |
benecsj | 2:afe5826411e3 | 708 | while ( (p = readdir(d)) != NULL ) { |
benecsj | 2:afe5826411e3 | 709 | printf(" - %s\r\n", p->d_name); |
benecsj | 2:afe5826411e3 | 710 | sprintf(buff,"%s\r\n", p->d_name); |
benecsj | 2:afe5826411e3 | 711 | fprintf(fp2,buff); |
benecsj | 2:afe5826411e3 | 712 | } |
benecsj | 2:afe5826411e3 | 713 | fprintf(fp2,"\r\n"); |
benecsj | 2:afe5826411e3 | 714 | fclose(fp2); |
benecsj | 2:afe5826411e3 | 715 | closedir(d); |
benecsj | 1:b26ab2467b1a | 716 | } |