![](/media/cache/profiles/100d5d9191f185eeb98d6e291756954a.50x50_q85.png)
Measure system
Dependencies: EthernetNetIf mbed RF12B
Diff: MeasureSystem.cpp
- Revision:
- 2:afe5826411e3
- Parent:
- 1:b26ab2467b1a
- Child:
- 3:799d8c61fb03
diff -r b26ab2467b1a -r afe5826411e3 MeasureSystem.cpp --- a/MeasureSystem.cpp Thu Mar 10 19:56:45 2011 +0000 +++ b/MeasureSystem.cpp Sun Mar 27 07:56:59 2011 +0000 @@ -1,10 +1,5 @@ #define MEASURE_C - -#define WRITEFILE 1 -#define APPENDFILE 2 -#define READFILE 0 - #include "EthernetNetIf.h" #include "HTTPServer.h" #include "SDFileSystem.h" @@ -16,57 +11,56 @@ #include "HTTPClient.h" #include "AvailableMemory.h" -//--------------Objects and vars------------------ -EthernetNetIf *eth; -HTTPServer svr; -NTPClient ntp; +//-------------------Defines-------------------------------- + -Serial pc(USBTX, USBRX); // Serial debug usb port -DigitalIn dhcpOn(p30); -DigitalOut led1(LED1, "led1"); -DigitalOut led2(LED2, "led2"); -DigitalOut led3(LED3, "led3"); -DigitalOut led4(LED4, "led4"); -LocalFileSystem local("local"); //Internal flash -SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board -TextLCD lcd(p21, p23, p24, p25, p26, p27, TextLCD::LCD20x4 ); //Char lcd -watchdog wdog(15); // WDT 15 seconds - -const int MAX_PROBES = 2; -#define LOGS_FOLDER "/sd/log" -#define DATA_FOLDER "/sd/data" -#define LOGGER_FILE "/sd/data/log.txt" -#define STATUS_FILE "/local/status.dat" -#define CONFIG_FILE "/local/config.txt" -#define SOURCE_FILE "/sd/index.htm" -#define TARGET_FILE "/local/index.htm" -#define COPY_BLOCK_SIZE 2048 -DS1820* probe[MAX_PROBES]; //Temp sensors +//-----------------Objects---------------------- +EthernetNetIf *eth; //Network IP stack +HTTPServer svr; //HTTP server +Serial pc(USBTX, USBRX); // Serial debug usb port +DigitalIn dhcpOn(p30); // Button +DigitalOut led1(LED1, "led1"); //Led 1 +DigitalOut led2(LED2, "led2"); //Led 2 +DigitalOut led3(LED3, "led3"); //Led 3 +DigitalOut led4(LED4, "led4"); //Led 4 +LocalFileSystem local("local");//Internal flash +SDFileSystem sd(p5, p6, p7, p8, "sd"); // // External sd card +TextLCD lcd(p21, p22, p23, p24, p25, p26, TextLCD::LCD20x4 ); //Char lcd +watchdog wdog(15); // Watch dog timer 15 seconds +DS1820* probe[MAX_PROBES]; //Temp sensors //------status flags----------------- int devices_found=0; -static char wanIP[18]; -static char localIP[18]; +char wanIP[18]; +char localIP[18]; int fileindex =0; -char logfile[50] ; bool getIPfromDHCP; -int interval; Timer timerMeasure; int ipConfig[4]; int maskConfig[4]; int gatewayConfig[4]; int dnsConfig[4]; int handlerCounter =0; +string myUrl; +int measureCommand; //---------------------MAIN----------------------- int main() { + reset = false; + //Init serial com + pc.baud(BAUDRATE); + logfile = new char[50]; + //Init flags measureCommand = 0; + HandlerActive = false; + postOK = 0; + + //Init local variables for init sequence char buf[40]; string tempstring = (""); - pc.baud(921600); - HandlerActive = false; //-----------------LCD test------------------- printf("\r\n\r\n<----------------------------------------------->"); printf("\r\n<System Startup>\r\n"); + PrintRAM(); lcd.cls(); lcd.printf("LCD Init.......Done\n"); wait(0.2); @@ -74,7 +68,6 @@ //----------------SD.card test---------------- printf("<SD card init>\r\n"); lcd.printf("SDcard Init...."); - mkdir(LOGS_FOLDER, 0777); mkdir(DATA_FOLDER, 0777); FILE *fp = fopen(LOGGER_FILE, "a+"); if (fp == NULL) { @@ -101,14 +94,6 @@ //---------------Network init----------------- lcd.printf("Network Init..."); printf("<Network Init>\r\n"); - /* - char mac[6]; - mbed_mac_address(mac); - for (int i=0; i<6;i++) { - printf("%02X ", mac[i]); - } - printf("\n"); - */ if (getIPfromDHCP) { eth = new EthernetNetIf(); } else { @@ -124,7 +109,7 @@ printf("Error %d in setup.\r\n", ethErr); lcd.printf("Error"); } - + sprintf(buf,"%d.%d.%d.%d", eth->getIp()[0], eth->getIp()[1], eth->getIp()[2], eth->getIp()[3]); sprintf(localIP,"%s",buf); @@ -139,7 +124,7 @@ lcd.cls(); lcd.printf("Internet conn.."); GetMyIP(); - if (wanIP!="none") { + if (wanIP[0]!=0) { lcd.printf("Done\n"); printf("OK: %s\r\n",wanIP); @@ -153,7 +138,7 @@ lcd.printf("RTClock Init..."); printf("<RTC Init>\r\n"); - if (wanIP!="none") { + if (wanIP[0]!=0) { UpdateTime(); strftime(buf,sizeof(buf), "%A %m/%d/%Y %H:%M:%S\n", localtime(&ctTime)); printf("Time is now : %s JST\r\n", buf); @@ -169,9 +154,8 @@ lcd.printf("Loading config."); LoadStatus(); IndexInit(); - if (logging == 1) - { - timerMeasure.start(); + if (logging == 1) { + timerMeasure.start(); } lcd.printf("Done\n"); wdog.feed(); @@ -208,19 +192,30 @@ Net::poll(); // Network listen if (tm.read()>1) { - counter = (counter++)%3600; //Timer counter loop + //RESET REQUIED + if (reset) + { + mbed_reset(); + } + + counter = (counter++)%36000; //Timer counter loop led1=!led1; //Show that we are alive 1 sec timed + + //Show if logging if (logging==1) { led4 =!led1; } else { led4 = 0; } - UpdateLCD(counter); //Refress LCD - - if (wanIP=="none" & (counter%30)==0) { //If no network detected aat startup then try to connect every 30 sec + + //Refress LCD + UpdateLCD(counter); + + //If no network detected aat startup then try to connect every 30 sec + if ( (wanIP[0]==0 & (counter%30)==0) | (wanIP[0]!=0 & (counter)==0)) { GetMyIP(); - if (wanIP!="none") { + if (wanIP[0]!=0) { IndexInit() ; UpdateTime(); } @@ -237,15 +232,16 @@ HandlerActive = false; } - if (logging == 1 & !HandlerActive) { + //Logger + if (logging == 1 ) { MeasureLogger(); } wdog.feed(); tm.start(); - - printf("Available memory (exact bytes) : %d\r\n", AvailableMemory(1)); + +PrintRAM(); } } @@ -261,7 +257,7 @@ int i; // Initialize the probe array to DS1820 objects for (i = 0; i < MAX_PROBES; i++) - probe[i] = new DS1820(p29); + probe[i] = new DS1820(p27); // Initialize global state variables probe[0]->search_ROM_setup(); // Loop to find all devices on the data line @@ -293,7 +289,7 @@ if (r==HTTP_OK) { sprintf(wanIP,"%s", txt.gets()); } else { - sprintf(wanIP,"none"); + sprintf(wanIP,NOINTERNET); } } @@ -345,7 +341,13 @@ } if (j==12) { //Change to find string element search by char. + if (wanIP[0]!=0) { fprintf(fs_tgt," <param name=\"ServerAddress\" value=\"%s\">",myUrl.c_str()); + } + else + { + fprintf(fs_tgt," <param name=\"ServerAddress\" value=\"%s\">",localIP); + } } else { fprintf(fs_tgt,"%s",temp.c_str()); } @@ -362,6 +364,7 @@ //------------Update lcd------------------------------ bool IPshowflag = false; void UpdateLCD(int counter) { +static int sensorcounter=0; char buf[40]; if (counter%1==0) { //Every sec @@ -372,10 +375,19 @@ lcd.printf("Not logging... "); } } - + + if (counter%2==0) { + lcd.locate(0,0); + if (devices_found>0) + { + lcd.printf("%d) sensor: %3.1fC ",(sensorcounter+1),probe[sensorcounter]->temperature('c')); + sensorcounter = (++sensorcounter)%devices_found; + } + } + if (counter%4==0) { lcd.locate(0,2); - if (wanIP!="none") { + if (wanIP[0]!=0) { if (IPshowflag) { lcd.printf("IP: %s ", localIP); } else { @@ -400,15 +412,16 @@ //----------------Update time--------------------- void UpdateTime() { + NTPClient * ntp = new NTPClient(); //NTP client time_t ctTime; ctTime = time(NULL); Host server(IpAddr(), 123, "0.hu.pool.ntp.org"); - ntp.setTime(server); + ntp->setTime(server); ctTime = time(NULL); ctTime += (clockoffset*3600); //set jst time } -//-------------Get Date short--------------------- +//-------------Get file name--------------------- void GetFile(int index, char *st) { string stringt = (""); char buff[50]; @@ -468,7 +481,41 @@ fclose( fs_src ); } +//---------------Save config-------------------------- +void SaveConfig() +{ +printf("SAVING CONFIG\r\n"); +FILE *fs_src; +fs_src = fopen( CONFIG_FILE, "w" ); +printf("SAVING CONFIG\r\n"); +fprintf(fs_src,"%s\r\n","IP adress"); +fprintf(fs_src,"%d.%d.%d.%d\r\n",ipConfig[0],ipConfig[1],ipConfig[2],ipConfig[3]); +fprintf(fs_src,"%s\r\n","Network Mask"); +fprintf(fs_src,"%d.%d.%d.%d\r\n",maskConfig[0],maskConfig[1],maskConfig[2],maskConfig[3]); +fprintf(fs_src,"%s\r\n","Gateway"); +fprintf(fs_src,"%d.%d.%d.%d\r\n",gatewayConfig[0],gatewayConfig[1],gatewayConfig[2],gatewayConfig[3]); +fprintf(fs_src,"%s\r\n","DNS"); +fprintf(fs_src,"%d.%d.%d.%d\r\n",dnsConfig[0],dnsConfig[1],dnsConfig[2],dnsConfig[3]); +fprintf(fs_src,"%s\r\n","Get IP from DHCP (true/false)"); +if (getIPfromDHCP) +{ +fprintf(fs_src,"True\r\n"); +} +else +{ +fprintf(fs_src,"False\r\n"); +} +fprintf(fs_src,"%s\r\n","---------------"); +fprintf(fs_src,"%s\r\n","Interval (sec)"); +fprintf(fs_src,"%d\r\n",interval); +fprintf(fs_src,"%s\r\n","CET Clock offset (hour)"); +fprintf(fs_src,"%d\r\n",clockoffset); +fprintf(fs_src,"%s\r\n","---------------"); +fprintf(fs_src,"%s\r\n","My URL"); +fprintf(fs_src,"%s\r\n",myUrl.c_str()); +fclose( fs_src ); +} //---------------Load status-------------------------- void LoadStatus() { FILE *fs_src; @@ -504,7 +551,7 @@ //---------------Log writer----------------------------- void LogWrite(string stringin) { - FILE *fp = fopen(LOGGER_FILE, "a"); + FILE *fp = fopen(LOGGER_FILE, "a+"); if (fp == NULL) { error("Could not open file for write\r\n"); } @@ -512,38 +559,41 @@ fclose(fp); } -//---------------Measurement start/ stop -------------- +//---------------Measurement start -------------- void MeasureStart() { - + char file[20]; - char *st; - st = new char[50]; - + char st[50]; + if (logging ==0) { logging = 1; timerMeasure.start(); do { - GetFile(fileindex++,st); - strcpy(file,st); + GetFile(fileindex++,(char*)&st); + strcpy(file,(char*)&st); sprintf(logfile,"%s%s%s",DATA_FOLDER,"/",file); - printf("Available memory (exact bytes) : %d\r\n", AvailableMemory(1)); + PrintRAM(); } while (exists(DATA_FOLDER,file)); // while (sdcardIsFileExist (filepath.c_str())=='t'); - + printf ("Saving log file name : %s\r\n",logfile); SaveStatus(); printf ("Printing header into log file: %s\r\n",logfile); - + sdcardWriteFile (logfile,WRITEFILE,"Measure Start\r\n"); + } + + } +//-------------------Measure stop method----------------------------------- void MeasureStop() { if (logging == 1) { printf ("Stopping measure."); @@ -587,11 +637,10 @@ //---------- Logger method------------------------------------------------ - void MeasureLogger() { - if (timerMeasure.read() > interval) { - led2 = 1; + if (timerMeasure.read() >= (interval-1)) { + led2 = 1; char buf[50]; ctTime = time(NULL); ctTime += (clockoffset*3600); @@ -609,13 +658,14 @@ strcat(resp,temp); sdcardWriteFile (logfile,APPENDFILE,resp); -led2 = 0; + led2 = 0; timerMeasure.reset(); } } +//------------Checks if file exist-------------------------------- bool exists(char * root, char *filename) { DIR *d = opendir(root); struct dirent *p; @@ -635,4 +685,32 @@ } closedir(d); return found; +} + +//------------------Prints out free ram space---------------------- +void PrintRAM() +{ +printf("Available memory (exact bytes) : %d\r\n", AvailableMemory(1)); +} +//-----------------Update List------------------------------------- +void UpdateList() +{ + DIR *d = opendir(DATA_FOLDER); + struct dirent *p; + char buff[40]; + printf("\nList of files in the directory %s:\r\n", DATA_FOLDER); + + sdcardWriteFile (FILELIST_FILE,WRITEFILE,""); + + FILE *fp2; + fp2 = fopen(FILELIST_FILE, "a+"); + + while ( (p = readdir(d)) != NULL ) { + printf(" - %s\r\n", p->d_name); + sprintf(buff,"%s\r\n", p->d_name); + fprintf(fp2,buff); + } +fprintf(fp2,"\r\n"); + fclose(fp2); + closedir(d); } \ No newline at end of file