Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
5 years, 7 months ago.
How to use NTPClient on Mbed-os
Can anyone help me get NTPClient working on Mbed-os please?
Code below works fine on os2.
I get this message:
Error: Expression must have class type in "main.cpp", Line: 47, Col: 6
#include "mbed.h" #include "EthernetInterface.h" #include "NTPClient.h" using namespace std; #define IP "192.168.1.120" #define GATEWAY "192.168.1.1" #define NETMASK "255.255.255.0" #define PORT 80 EthernetInterface* net; TCPServer server; TCPSocket client; SocketAddress clientAddress; NTPClient* ntp; char receiveBuf[1024] = { }; int main(void) { net = new EthernetInterface(); if (!net) { printf("Error! No network inteface found.\n"); return 0; } net->set_network (IP, NETMASK, GATEWAY); // include this for using static IP address nsapi_size_or_error_t r = net->connect(); if (r != 0) { printf("Error! net->connect() returned: %d\n", r); return r; } // Show the network address const char *ip = net->get_ip_address(); const char *netmask = net->get_netmask(); const char *gateway = net->get_gateway(); printf("Server IP: %s:\n", ip ? ip : "None"); printf("Netmask: %s\n", netmask ? netmask : "None"); printf("Gateway: %s\n\n", gateway ? gateway : "None"); ntp.setTime("0.pool.ntp.org",123,2000);
2 Answers
5 years, 6 months ago.
Hi Paul,
thank you for your information from my previous comment. Now my suggestion…
I thing the problem in your code is you never call constructor of NTPClient so you never refer the object EthernetInterface for/to the NTPClient.
The code below is without poiters and it is working on the NucleoF767ZI.
Working...
#include "mbed.h" #include "EthernetInterface.h" #include "NTPClient.h" EthernetInterface net; NTPClient ntp(net); int main(void) { printf("Starting NTPclient...\n"); time_t seconds = time(NULL); printf("Starting time: %s", ctime(&seconds)); printf("EthernetInterface connecting...\n"); net.connect(); const char *ip = net.get_ip_address(); if(ip){ printf("IP address is: %s\n", ip); ntp.setTime("0.pool.ntp.org",123,3000); }else{ printf("No IP\n"); } while(1){ time_t seconds = time(NULL); printf("System time set by NTP: %s", ctime(&seconds)); wait(1); } }
Edit: Sam was faster :)
Ps: In the NTPClient.cpp file on the line 21 you can change 0 to 1 for better debugging.
Best regards
J.
5 years, 6 months ago.
I didn't try to reproduce but from reading the code, ntp is declared as a pointer which might not be the correct usage.
Using;
ntp->setTime("0.pool.ntp.org",123,2000);
This will compile, however I now get this hard fault error bellow. If I comment this out it runs, just doesn't like the NTP part. Strange because NTP works fine on os2 (Mbed-Rtos).
Also,
TCPServer server; server.accept(&client, &clientAddress);
Gives me depreciated warnings, but I can't find any suggestions how to resolve this, what do I use instead to stop the warnings? These warnings are on the Mbed-os-examples too.
Initialise! Server IP: 192.168.1.120: Netmask: 255.255.255.0 Gateway: 192.168.1.1 ++ MbedOS Fault Handler ++ FaultType: HardFault Context: R0 : B9E6D6EC R1 : 2000DC31 R2 : 2000DC78 R3 : 00000000 R4 : 00000000 R5 : 00000FA0 R6 : 0800F6C8 R7 : 2000EA84 R8 : 00000000 R9 : 00000000 R10 : 00000000 R11 : 00000000 R12 : 40000000 SP : 2000DC48 LR : 080075E9 PC : 0800947C xPSR : 41000000 PSP : 2000DBE0 MSP : 2007FFD8 CPUID: 411FC270 HFSR : 40000000 MMFSR: 00000000 BFSR : 00000082 UFSR : 00000000 DFSR : 0000000B AFSR : 00000000 BFAR : B9E6D6EC Mode : Thread Priv : Privileged Stack: PSP -- MbedOS Fault Handler -- ++ MbedOS Error Info ++ Error Status: 0x80FF013D Code: 317 Module: 255 Error Message: Fault exception Location: 0x800F939 Error Value: 0x800947C Current Thread: Id: 0x2000DCB8 Entry: 0x800FBF5 StackSize: 0x1000 StackMem: 0x2000CCB8 SP: 0x2007FF90 For more info, visit: https://armmbed.github.io/mbedos-error/?error=0x80FF013D -- MbedOS Error Info --
NTPClient* ntp; doesnt look to be initialized anywhere so that hard-fault most likely occurs when dereferencing ntp;
All these pointers to things are really nasty but looking at the ntp client library it seems the expected usage is:
// on the main stack net = new EthernetInterface(); NTPClient ntp(net); // or maybe (&net)
Still not sure about that NTP library, it doesn't look to initialize the socket it uses and there seems to be confusion about pointers and references but this might get you closer to something that works.
posted by 27 Apr 2019And regarding the TCPServer, looking at the docs for 5.12 it looks like client and server capabilities were merged into TCPSocket https://os.mbed.com/docs/mbed-os/v5.12/apis/tcpsocket.html
posted by 27 Apr 2019
Hi, maybe an info about what NTP library and board you use will be good. J.
posted by Jan Kamidra 26 Apr 2019Hi Jan,
It's this library:
https://os.mbed.com/users/vpcola/code/NTPClient/
I'm using Mbed-os Rev.5.10 17th Nov 2018
posted by Paul Staron 27 Apr 2019