ban4jp - / Mbed 2 deprecated SNIC-ntpclient-example

Dependencies:   NTPClient SNICInterface_PullReq mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SNIC_WifiInterface.h"
00003 #include "NTPClient.h"
00004 
00005 #define WIFI_SSID           "mbed_ap"
00006 #define WIFI_SECUTIRY_KEY   "password1234"
00007 #define WIFI_SECURITY_TYPE  e_SEC_WPA2_AES
00008 
00009 #if defined(TARGET_LPC1768)
00010 C_SNIC_WifiInterface wifi( p9, p10, NC, NC, p30 );
00011 Serial pc(USBTX, USBRX);    // This is required when defined "_DEBUG"
00012 #else
00013 #error no defined pin.
00014 #endif
00015 
00016 NTPClient ntp;
00017 char str[32];
00018 
00019 int main()
00020 {
00021     pc.baud( 115200 );
00022     pc.printf("----------------\n");
00023     pc.printf("Murata TypeYD - NTPClient example\n");
00024 
00025     int ret = wifi.init();
00026     if (!ret) {
00027         printf("Initialized\n");
00028         //printf("Initialized, MAC: %s\n", wifi.getMACAddress());
00029     } else {
00030         printf("Error wifi.init() - ret = %d\n", ret);
00031         return -1;
00032     }
00033 
00034     wait(0.5);
00035 
00036     ret = wifi.disconnect();
00037 
00038     wait(0.5);
00039 
00040     ret = wifi.getFWVersion((unsigned char *)str);
00041     if (!ret) {
00042         printf("Firmware version: %s\n", str);
00043     } else {
00044         printf("Error wifi.getFWVersion() - ret = %d\n", ret);
00045         return -1;
00046     }
00047 
00048     wait(0.5);
00049 
00050     ret = wifi.connect( WIFI_SSID, strlen(WIFI_SSID)
00051                         , WIFI_SECURITY_TYPE
00052                         , WIFI_SECUTIRY_KEY, strlen(WIFI_SECUTIRY_KEY) );
00053     if (!ret) {
00054         printf("Connected\n");
00055         /*
00056         printf("Connected, IP: %s, MASK: %s, GW: %s\n",
00057                wifi.getIPAddress(), wifi.getNetworkMask(), wifi.getGateway());
00058         */
00059     } else {
00060         printf("Error wifi.connect() - ret = %d\n", ret);
00061         return -1;
00062     }
00063 
00064     // Use DHCP
00065     wifi.setIPConfig( true );
00066     // Use Static IP
00067     //wifi.setIPConfig( false, "192.168.0.48", "255.255.255.0", "192.168.0.1" );
00068 
00069     wait(0.5);
00070 
00071     printf("\nTrying to update time...\n");
00072     //ret = ntp.setTime("pool.ntp.org");    // Do not work (SNICInterface bug)
00073     //ret = ntp.setTime("ntp.nict.jp");     // Do not work (SNICInterface bug)
00074     ret = ntp.setTime("133.243.238.164");   // = ntp.nict.jp
00075     if (!ret) {
00076         printf("Set time successfully\n");
00077         time_t ctTime;
00078         ctTime = time(NULL);
00079         printf("Time is set to (UTC): %s\n", ctime(&ctTime));
00080     } else {
00081         printf("Error\n");
00082     }
00083 
00084     ret = wifi.disconnect();
00085     if (!ret) {
00086         printf("Disconnected\n");
00087     } else {
00088         printf("Error wifi.disconnect() - ret = %d\n", ret);
00089     }
00090 
00091     while(1) {
00092     }
00093 }