Commit 4ce52ccf authored by Yongjia Niu's avatar Yongjia Niu
Browse files

Enable Wi-Fi network

parent 8011fcfc
compile_commands.json
\ No newline at end of file
#include "mbed.h"
#include "mbed_trace.h"
#include "NTPClient.h"
#define TRACE_GROUP "Main"
NetworkInterface* network = NULL;
NetworkInterface* connect_network() {
NetworkInterface* net = NULL;
tr_info("Opening network interface...");
net = NetworkInterface::get_default_instance();
if (!net) {
tr_error("Unable to open network interface.");
return NULL;
}
nsapi_error_t net_status = NSAPI_ERROR_NO_CONNECTION;
while ((net_status = net->connect()) != NSAPI_ERROR_OK) {
tr_error("Unable to connect to network (%d). Retrying...", net_status);
}
SocketAddress address;
net->get_ip_address(&address);
tr_info("Connected to the network successfully. IP address: %s\n", address.get_ip_address());
return net;
}
int sync_rtc_time(NetworkInterface* net, const char* ntp_server = NULL) {
const char* ntp_srv = NULL;
if (net == NULL) {
return -1;
}
if (ntp_server) {
ntp_srv = ntp_server;
} else {
ntp_srv = "ntp.aliyun.com";
}
NTPClient ntp(net);
ntp.set_server(ntp_srv, 123);
tr_info("Try to get timestamp from %s", ntp_srv);
time_t now = ntp.get_timestamp();
if (now <= 0) {
tr_error("Failed to retrieve the time from %s:123", ntp_srv);
return -1;
}
set_time(now);
tr_info("Time is now %s", ctime(&now));
return 0;
}
int main() {
int ret = 0;
mbed_trace_init();
#ifdef MBED_MAJOR_VERSION
printf("Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
#endif
// Connect to network
network = connect_network();
if (network == NULL) {
return -1;
}
// sync the real time clock (RTC)
ret = sync_rtc_time(network);
if (ret != 0) {
return ret;
}
return 0;
}
\ No newline at end of file
{
"macros": [
],
"target_overrides": {
"K64F": {
"platform.stdio-baud-rate": 9600
"*": {
"nsapi.default-wifi-security": "WPA_WPA2",
"nsapi.default-wifi-ssid": "\"NETGEAR86\"",
"nsapi.default-wifi-password": "\"raspberry\"",
"mbed-trace.enable": 1,
"mbed-trace.max-level": "TRACE_LEVEL_DEBUG",
"platform.stdio-baud-rate": 115200,
"platform.stdio-convert-newlines": true
},
"B_L4S5I_IOT01A": {
"target.network-default-interface-type": "WIFI",
"nsapi.default-wifi-security": "WPA_WPA2",
"ism43362.provide-default": true,
"ism43362.wifi-country-code": "\"CN\""
},
"DISCO_L475VG_IOT01A": {
"target.network-default-interface-type": "WIFI",
"nsapi.default-wifi-security": "WPA_WPA2",
"ism43362.provide-default": true,
"ism43362.wifi-country-code": "\"CN\""
}
}
}
\ No newline at end of file
https://github.com/ARMmbed/ntp-client/#a4ccf62992b9adc5086a9afea08fa4deb2e2fbd7
https://github.com/ARMmbed/wifi-ism43362/#8b5bda2b2724ca08ce1a44961a82b3d6f7c59706
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment