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: EthernetNetIf NTPClient_NetServices TextLCD mbed
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetNetIf.h" 00003 #include "UDPSocket.h" 00004 #include "NTPClient.h" 00005 #include "TextLCD.h" 00006 00007 #define PORT 50001 00008 #define LOCATION 1000 00009 00010 TextLCD lcd(p24, p26, p27, p28, p29, p30); 00011 AnalogIn temp_in(p20); 00012 AnalogIn humid_in(p19); 00013 00014 Host server; 00015 IpAddr ip; 00016 UDPSocket udp; 00017 NTPClient ntp; 00018 00019 Ticker in; 00020 00021 void Update(){ 00022 char msg[64] ; 00023 float r_temp, r_humid; 00024 float temp,humid; 00025 char strTimeMsg[16]; 00026 time_t ctTime; 00027 00028 temp = temp_in; 00029 humid = humid_in; 00030 00031 r_humid = humid * 3.3 * 100 ; 00032 r_temp = temp * 55.0 ; 00033 00034 ctTime = time(NULL)+32400; 00035 strftime(strTimeMsg,16,"%y/%m/%d %H:%M",localtime(&ctTime)); 00036 00037 lcd.cls(); 00038 lcd.locate(0,0); 00039 lcd.printf("%s",strTimeMsg); 00040 00041 lcd.locate(0,1); 00042 lcd.printf("H%5.1f%% T%5.1f",r_humid,r_temp); 00043 00044 lcd.locate(14,1); 00045 lcd.putc(0xDf); 00046 lcd.putc(0x43); 00047 00048 ctTime = time(NULL)+32400; 00049 strftime(strTimeMsg,16,"%y/%m/%d %H:%M",localtime(&ctTime)); 00050 sprintf(msg, "%4d\t,%6.2f\t,%6.2f\t,",LOCATION,r_temp,r_humid); 00051 strcat(msg,strTimeMsg); 00052 udp.sendto( msg, strlen(msg), &server ); 00053 printf("%s\r\n",msg); 00054 } 00055 00056 void setRTC_NTP() 00057 { 00058 char strNtpErrMsg[32] ; 00059 00060 Host ntpsrv(IpAddr(), 123, "ntp.nict.jp") ; 00061 NTPResult ntpResult = ntp.setTime(ntpsrv) ; 00062 00063 if( ntpResult == NTP_OK ){ 00064 sprintf(strNtpErrMsg,"NTP Connect OK!"); 00065 }else if ( ntpResult == NTP_PRTCL ){ 00066 sprintf(strNtpErrMsg,"NTP Protocol error.") ; 00067 }else if ( ntpResult == NTP_TIMEOUT ){ 00068 sprintf(strNtpErrMsg,"Connection timeout."); 00069 }else if ( ntpResult == NTP_DNS ){ 00070 sprintf(strNtpErrMsg,"Could not resolve DNS hostname.") ; 00071 }else if ( ntpResult == NTP_PROCESSING ){ 00072 sprintf(strNtpErrMsg,"Processing."); 00073 }else{ 00074 sprintf(strNtpErrMsg,"NTP Error."); 00075 } 00076 printf("[%s]\r\n",strNtpErrMsg); 00077 } 00078 00079 int main() { 00080 00081 char ipaddr[16]; 00082 00083 EthernetNetIf eth; // (1) -- DHCP 00084 // EthernetNetIf eth( // (2) -- static IP address 00085 // IpAddr(192,168,0,10), // IP Address 00086 // IpAddr(255,255,255,0), // Subnet Mask 00087 // IpAddr(192,168,0,1), // Default Gateway 00088 // IpAddr(192,168,0,1) // DNS Server 00089 // ) ; 00090 00091 lcd.cls(); 00092 lcd.locate(0,0); 00093 lcd.printf("NW Setup ... "); 00094 00095 EthernetErr ethErr = eth.setup(); 00096 00097 if( ethErr != ETH_OK ) 00098 { 00099 lcd.locate(0,0); 00100 lcd.printf("NW Setup Error! ", ethErr); 00101 return -1; 00102 } 00103 00104 lcd.locate(0,0); 00105 lcd.printf("NW Setup ...[OK]"); 00106 00107 // ip = IpAddr(192,168,0,4); 00108 ip = IpAddr(192,244,80,66); 00109 server = Host(ip, PORT, NULL); 00110 00111 lcd.locate(0,1); 00112 sprintf(ipaddr,"%d.%d.%d.%d ",ip[0],ip[1],ip[2],ip[3]); 00113 lcd.printf("%s",ipaddr); 00114 wait(1.0); 00115 00116 setRTC_NTP(); 00117 00118 in.attach(&Update,10); 00119 00120 while(true) 00121 { 00122 Net::poll(); 00123 } 00124 }
Generated on Tue Jul 12 2022 21:45:03 by
1.7.2