Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MSCUsbHost NetServices RPCInterface TextLCD mbed
main.cpp
00001 #include "mbed.h" 00002 #include "MSCFileSystem.h" 00003 #include "TextLCD.h" 00004 #include "EthernetNetIf.h" 00005 #include "NTPClient.h" 00006 #include "HTTPServer.h" 00007 #include "RPCVariable.h" 00008 #include "RPCFunction.h" 00009 00010 #define NTPServer "ntp.cc.yamaguchi-u.ac.jp" 00011 00012 #if 1 00013 // Use DHCP 00014 EthernetNetIf ethif; 00015 #else 00016 // Use "static IP address" (Parameters:IP, Subnet mask, Gateway, DNS) 00017 EthernetNetIf ethif(IpAddr(xx,xx,xx,xx), IpAddr(xx,xx,xx,xx), IpAddr(xx,xx,xx,xx), IpAddr(xx,xx,xx,xx)); 00018 #endif 00019 00020 TextLCD lcd(p24, p26, p27, p28, p29, p30); 00021 MSCFileSystem msc("usb"); 00022 LocalFileSystem local("local"); 00023 DigitalOut led1(LED1,"led1"); 00024 DigitalOut led2(LED2,"led2"); 00025 DigitalOut led3(LED3,"led3"); 00026 DigitalOut led4(LED4,"led4"); 00027 AnalogIn ain(p15,"ain"); 00028 float temp; 00029 time_t ctTime; 00030 00031 void lastdate(char *input,char *output); 00032 NTPClient ntp; 00033 HTTPServer svr; 00034 00035 RPCVariable<float> rpcv(&temp,"Temp"); 00036 RPCFunction rpcf(&lastdate,"LastDate"); 00037 00038 void lastdate(char *input,char *output) 00039 { 00040 if (input[0]=='0')led4=0; 00041 if (input[0]=='1')led4=1; 00042 char ldate[32]; 00043 strftime(ldate,32,"%x %X",localtime(&ctTime)); 00044 strcpy(output,ldate); 00045 } 00046 00047 void mesure_temp() 00048 { 00049 char buffer[9]; 00050 temp=ain*3.3*100.0; 00051 00052 ctTime = time(NULL); 00053 strftime(buffer,9,"%X",localtime(&ctTime)); 00054 lcd.locate(0,1); 00055 lcd.printf("%s %4.1fDeg",buffer,temp); 00056 00057 char filename[23]; 00058 strftime(filename,23,"/usb/data/%Y%m%d.dat",localtime(&ctTime)); 00059 FILE *fp= fopen(filename, "a"); 00060 if ( fp == NULL ) { 00061 printf("Could not open file for write\n"); 00062 } else { 00063 fprintf(fp, "%s , %4.1f \r\n",buffer,temp); 00064 } 00065 fclose(fp); 00066 } 00067 00068 int main() 00069 { 00070 time_t ctTime; 00071 char buffer[32]; 00072 00073 lcd.cls(); 00074 lcd.locate(0,0); 00075 lcd.printf("Program Start"); 00076 00077 if (ethif.setup()) { 00078 error("Ethernet setup failed."); 00079 return 1; 00080 } 00081 00082 IpAddr ethIp=ethif.getIp(); 00083 lcd.locate(0,0); 00084 lcd.printf("%3d.%3d.%3d.%3d", ethIp[0], ethIp[1], ethIp[2], ethIp[3]); 00085 Host server(IpAddr(), 123, NTPServer); 00086 ntp.setTime(server); 00087 //UTC-->JST +9Hour(32400Sec) 00088 ctTime = time(NULL); 00089 ctTime+=32400; 00090 set_time(ctTime); 00091 ctTime = time(NULL); 00092 strftime(buffer,32,"%x %X",localtime(&ctTime)); 00093 printf("%s \n",buffer); 00094 printf("finish \n"); 00095 00096 Base::add_rpc_class<DigitalOut>(); 00097 Base::add_rpc_class<AnalogIn>(); 00098 FSHandler::mount("/local", "/" ); 00099 svr.addHandler<RPCHandler>("/rpc"); 00100 svr.addHandler<FSHandler>("/"); 00101 svr.addHandler<SimpleHandler>("/hello"); 00102 svr.bind(80); 00103 00104 Timer tm; 00105 Timer tm2; 00106 tm.start(); 00107 tm2.start(); 00108 while (true) { 00109 Net::poll(); 00110 if (tm.read()>.5) { 00111 led1=!led1; //Show that we are alive 00112 tm.start(); 00113 } 00114 if (tm2.read()>5) { 00115 led2=!led2; 00116 tm2.start(); 00117 mesure_temp(); 00118 } 00119 } 00120 }
Generated on Tue Jul 12 2022 20:55:58 by
