ESP8266 code not compiling after adding NTP time synchronization.

I’m building a simple ESP8266 “rebooter”, which every day at a specific hour would toggle off a relay for an interval then back on again, rebooting the connected device. The whole thing would be controlled via MQTT (for example setting the time of day) My problem is the following: after adding time synchronization code from the Time library, the code doesn’t compile and the error doesn’t give any useful information, just says:

exit status 1
Error compiling for board Generic ESP8266 Module.

Please mind there isn’t any logic for checking the time and toggling the relay yet.

(Code in Attachment)

Do you guys have any clue what’s going on?

By the way, I’m new to this forum, so please tell me if I’ve done something wrong! :slight_smile:

And also sorry for my English, but I’m Italian and 13.

RebootINATOR.ino (10.1 KB)

Please post the full error message

I'm so sorry, i didn't see there's more! Now it may be a bit more useful...

Arduino: 1.8.12 (Windows 10), Board: "Generic ESP8266 Module, 80 MHz, Flash, Disabled, All SSL ciphers (most compatible), ck, 26 MHz, 40MHz, DOUT (compatible), 512K (no SPIFFS), 2, nonos-sdk 2.2.1 (legacy), v2 Lower Memory, Disabled, None, Only Sketch, 115200"

In file included from C:\Users\Sergio\Documents\Arduino\RebootINATOR\RebootINATOR.ino:31:0:

C:\Users\Sergio\Documents\Arduino\libraries\NTPClient/NTPClient.h:8:25: error: expected unqualified-id before numeric constant

 #define NTP_PACKET_SIZE 48

                         ^

C:\Users\Sergio\Documents\Arduino\RebootINATOR\RebootINATOR.ino:210:11: note: in expansion of macro 'NTP_PACKET_SIZE'

 const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message

           ^

exit status 1
Error compiling for board Generic ESP8266 Module.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Would you like me to turn on the full verbose output?

Did it compile before you put the time sync code in?

In file included from C:\Users\Sergio\Documents\Arduino\RebootINATOR\RebootINATOR.ino:31:0:

C:\Users\Sergio\Documents\Arduino\libraries\NTPClient/NTPClient.h:8:25: error: expected unqualified-id before numeric constant

 #define NTP_PACKET_SIZE 48
                         ^

C:\Users\Sergio\Documents\Arduino\RebootINATOR\RebootINATOR.ino:210:11: note: in expansion of macro 'NTP_PACKET_SIZE'

 const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
           ^

This says that NTPClient.h defines a macro called "NTP_PACKET_SIZE" and your sketch defines a variable that you want to call "NTP_PACKET_SIZE". The result is:

 const int 48 = 48; // NTP time is in the first 48 bytes of message

The macro expansion has turned you valid statement into an invalid statement. Remove your variable declaration and use the library macro directly.

Proietti:
Did it compile before you put the time sync code in?

Yes, it did. And it worked as expected too, printing the MQTT messages to the terminal.

johnwasser:

In file included from C:\Users\Sergio\Documents\Arduino\RebootINATOR\RebootINATOR.ino:31:0:

C:\Users\Sergio\Documents\Arduino\libraries\NTPClient/NTPClient.h:8:25: error: expected unqualified-id before numeric constant

#define NTP_PACKET_SIZE 48
                        ^

C:\Users\Sergio\Documents\Arduino\RebootINATOR\RebootINATOR.ino:210:11: note: in expansion of macro 'NTP_PACKET_SIZE'

const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
          ^




This says that NTPClient.h defines a macro called "NTP_PACKET_SIZE" and your sketch defines a variable that you want to call "NTP_PACKET_SIZE". The result is:


const int 48 = 48; // NTP time is in the first 48 bytes of message



The macro expansion has turned you valid statement into an invalid statement. Remove your variable declaration and use the library macro directly.

Thank you for your quick answer! Please, correct me if I'm wrong, but why is the example from which I took this code snippet compiling? The part denominated "NTP code" is just copied, along with some Initialization in the first part of the sketch.

Try this NTP library. It was created by the guy with the swiss accent and works well.

Also if you do a search for NTP time there is a video by the swiss guy taking you through the whole thing step by step

The esp8266 core comes with full NTP support and all the real unix time functions including the ability to set a POSIX TZ variable to perform timezone and DST adjustments for local time.
There is no need to use any other 3rd party time library.

--- bill

sergiocarmine:
Thank you for your quick answer! Please, correct me if I'm wrong, but why is the example from which I took this code snippet compiling?

Possibly the example used a different NTP library entirely or the version of the NTP library has changed since the example was written.

Proietti:
Try this NTP library. It was created by the guy with the swiss accent and works well.
GitHub - SensorsIot/NTPtimeESP: Small library for ESP8266 which returns the actual time adjusted by time zone and summer time

Thank you, i'll try that. I didn't find his library when doing research before starting.

sergiocarmine:
Thank you, i'll try that. I didn't find his library when doing research before starting.

NTP is built into to the core.
If you use it, you don't need any 3rd party libraries or any NTP or UDP code in your sketch.
Once initialized, it runs in the background without the sketch having to do anything else.
You can then use the standard unix time library functions that also come with the core (no 3rd party time libraries like Time/TimeLib needed) to get the UTC time or local time with automatic DST time changes if you set the TZ variable.

--- bill

I haven't found a way to retrieve time from the core as you said, but I figured out the problem in my initial sketch: first I was using an NTP library and when I replaced it with the time library I didn't remove the #include statement.
Thank you all for your precious time!