Dear all,
I am trying to move code from ESP8266 to RP2040, both within Arduino framework. Currently, I am struggling with the time functionality - I need to sync time with NTP.
Strangely enough, my code did work initially. But after a while, platformIO did not compile the code anymore and reported conflicts on time variables. I am not aware of any changes that I made.
Do you have any idea, what is going on? I am lost.
Here is my platform.ini:
[env]
platform = raspberrypi
framework = arduino
board_build.core = earlephilhower
monitor_speed = 115200
[env:rpipicow]
board = rpipicow
lib_deps =
https://github.com/PaulStoffregen/Time
; https://github.com/JChristensen/Timezone
This is my Code:
#include <Arduino.h>
#include <WiFi.h>
#include "pico/cyw43_arch.h"
#include <TimeLib.h>
// #include <Timezone.h>
char ssid[] = "ssid"; // your network SSID (name)
char pass[] = "1234567"; // your network password
time_t prevDisplay = 0; // when the digital clock was displayed
/*-------- NTP code ----------*/
// A UDP instance to let us send and receive packets over UDP
WiFiUDP Udp;
// NTP Servers:
char timeServer[] = "time.nist.gov"; // NTP server
unsigned int localPort = 2390; // local port to listen for UDP packets
const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
const int timeZone = 1; // Central European Time
// send an NTP request to the time server at the given address
void sendNTPpacket(char *ntpSrv) {
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.beginPacket(ntpSrv, 123); //NTP requests are to port 123
Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket();
}
time_t getNtpTime()
{
while (Udp.parsePacket() > 0) ; // discard any previously received packets
Serial.println("Transmit NTP Request");
sendNTPpacket(timeServer);
uint32_t beginWait = millis();
while (millis() - beginWait < 1500) {
int size = Udp.parsePacket();
if (size >= NTP_PACKET_SIZE) {
Serial.println("Receive NTP Response");
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
unsigned long secsSince1900;
// convert four bytes starting at location 40 to a long integer
secsSince1900 = (unsigned long)packetBuffer[40] << 24;
secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
secsSince1900 |= (unsigned long)packetBuffer[43];
return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
}
}
Serial.println("No NTP Response :-(");
return 0; // return 0 if unable to get the time
}
void printDigits(int digits){
// utility for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
/* -------- printing functions ---------- */
void digitalClockDisplay(time_t p_time){
// digital clock display of the time
Serial.print(hour(p_time));
printDigits(minute(p_time));
printDigits(second(p_time));
Serial.print(" ");
Serial.print(day(p_time));
Serial.print(" ");
Serial.print(month(p_time));
Serial.print(" ");
Serial.print(year(p_time));
Serial.println();
}
void setup()
{
Serial.begin(115200);
while (!Serial && millis() < 5000);
delay(200);
///////////////////////////////////
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE)
{
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
Serial.print(F("Connecting to SSID: "));
Serial.println(ssid);
int status = WiFi.begin(ssid, pass);
delay(1000);
// attempt to connect to WiFi network
while ( status != WL_CONNECTED)
{
delay(500);
// Connect to WPA/WPA2 network
status = WiFi.status();
}
// you're connected now, so print out the data
Serial.print(F("You're connected to the network, IP = "));
Serial.println(WiFi.localIP());
///////////////////////////////////
delay(250);
Udp.begin(localPort);
Serial.println("waiting for sync");
setSyncProvider(getNtpTime);
}
void loop()
{
///////////////////////////////////
/*
time_t eastern;
TimeChangeRule usEDT = {"EDT", Second, Sun, Mar, 2, -240}; //UTC - 4 hours
TimeChangeRule usEST = {"EST", First, Sun, Nov, 2, -300}; //UTC - 5 hours
Timezone usEastern(usEDT, usEST);
*/
///////////////////////////////////
if (timeStatus() != timeNotSet) {
if (now() != prevDisplay) { //update the display only if time has changed
prevDisplay = now();
// time_t eastern = usEastern.toLocal(now());
digitalClockDisplay(now());
}
}
}
And finally the error log:
Processing rpipicow (board: rpipicow; platform: raspberrypi; framework: arduino)
-------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/raspberrypi/rpipicow.html
PLATFORM: Raspberry Pi RP2040 (1.9.0+sha.ffb6889) > Pico W
HARDWARE: RP2040 133MHz, 264KB RAM, 2MB Flash
DEBUG: Current (blackmagic) External (blackmagic, cmsis-dap, jlink, pico-debug, picoprobe, raspberrypi-swd)
PACKAGES:
- framework-arduinopico @ 1.30600.0+sha.41b0686
- tool-rp2040tools @ 1.0.2
- toolchain-rp2040-earlephilhower @ 5.120300.230911 (12.3.0)
Flash size: 2.00MB
Sketch size: 2.00MB
Filesystem size: 0.00MB
Maximium Sketch size: 2093056 EEPROM start: 0x101ff000 Filesystem start: 0x101ff000 Filesystem end: 0x101ff000
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 53 compatible libraries
Scanning dependencies...
Dependency Graph
|-- Time @ 1.6.1+sha.a18e50d
|-- WiFi @ 1.0.0
Building in release mode
Compiling .pio/build/rpipicow/src/main.cpp.o
Compiling .pio/build/rpipicow/libf0a/LittleFS/LittleFS.cpp.o
Compiling .pio/build/rpipicow/lib584/PicoOTA/PicoOTA.cpp.o
Compiling .pio/build/rpipicow/libae9/Updater/Updater.cpp.o
Compiling .pio/build/rpipicow/libe8f/WiFi/BearSSLHelpers.cpp.o
Compiling .pio/build/rpipicow/libe8f/WiFi/CertStoreBearSSL.cpp.o
Compiling .pio/build/rpipicow/libe8f/WiFi/WiFiClass.cpp.o
Compiling .pio/build/rpipicow/libe8f/WiFi/WiFiClient.cpp.o
In file included from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/LittleFS/src/LittleFS.h:29,
from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/LittleFS/src/LittleFS.cpp:26:
/Users/ludwin/.platformio/packages/framework-arduinopico/cores/rp2040/FS.h: In static member function 'static time_t fs::FS::_defaultTimeCB()':
/Users/ludwin/.platformio/packages/framework-arduinopico/cores/rp2040/FS.h:243:16: error: 'time' was not declared in this scope
243 | return time(nullptr);
| ^~~~
/Users/ludwin/.platformio/packages/framework-arduinopico/cores/rp2040/FS.h:26:1: note: 'time' is defined in header '<ctime>'; did you forget to '#include <ctime>'?
25 | #include <../include/time.h> // See issue #6714
+++ |+#include <ctime>
26 |
In file included from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/LittleFS/src/LittleFS.h:29,
from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/PicoOTA/src/PicoOTA.h:22,
from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/PicoOTA/src/PicoOTA.cpp:20:
/Users/ludwin/.platformio/packages/framework-arduinopico/cores/rp2040/FS.h: In static member function 'static time_t fs::FS::_defaultTimeCB()':
/Users/ludwin/.platformio/packages/framework-arduinopico/cores/rp2040/FS.h:243:16: error: 'time' was not declared in this scope
243 | return time(nullptr);
| ^~~~
/Users/ludwin/.platformio/packages/framework-arduinopico/cores/rp2040/FS.h:26:1: note: 'time' is defined in header '<ctime>'; did you forget to '#include <ctime>'?
25 | #include <../include/time.h> // See issue #6714
+++ |+#include <ctime>
26 |
In file included from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/Updater/src/Updater.h:26,
from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/Updater/src/Updater.cpp:21:
/Users/ludwin/.platformio/packages/framework-arduinopico/cores/rp2040/FS.h: In static member function 'static time_t fs::FS::_defaultTimeCB()':
/Users/ludwin/.platformio/packages/framework-arduinopico/cores/rp2040/FS.h:243:16: error: 'time' was not declared in this scope
243 | return time(nullptr);
| ^~~~
/Users/ludwin/.platformio/packages/framework-arduinopico/cores/rp2040/FS.h:26:1: note: 'time' is defined in header '<ctime>'; did you forget to '#include <ctime>'?
25 | #include <../include/time.h> // See issue #6714
+++ |+#include <ctime>
26 |
*** [.pio/build/rpipicow/lib584/PicoOTA/PicoOTA.cpp.o] Error 1
In file included from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/Updater/src/Updater.h:26,
from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/BearSSLHelpers.h:27,
from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/CertStoreBearSSL.h:23,
from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/CertStoreBearSSL.cpp:20:
/Users/ludwin/.platformio/packages/framework-arduinopico/cores/rp2040/FS.h: In static member function 'static time_t fs::FS::_defaultTimeCB()':
/Users/ludwin/.platformio/packages/framework-arduinopico/cores/rp2040/FS.h:243:16: error: 'time' was not declared in this scope
243 | return time(nullptr);
| ^~~~
/Users/ludwin/.platformio/packages/framework-arduinopico/cores/rp2040/FS.h:26:1: note: 'time' is defined in header '<ctime>'; did you forget to '#include <ctime>'?
25 | #include <../include/time.h> // See issue #6714
+++ |+#include <ctime>
26 |
In file included from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/Updater/src/Updater.h:26,
from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/BearSSLHelpers.h:27,
from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFiClientSecureBearSSL.h:30,
from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFiClientSecure.h:24,
from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFi.h:9,
from src/main.cpp:4:
/Users/ludwin/.platformio/packages/framework-arduinopico/cores/rp2040/FS.h: In static member function 'static time_t fs::FS::_defaultTimeCB()':
/Users/ludwin/.platformio/packages/framework-arduinopico/cores/rp2040/FS.h:243:16: error: 'time' was not declared in this scope
243 | return time(nullptr);
| ^~~~
/Users/ludwin/.platformio/packages/framework-arduinopico/cores/rp2040/FS.h:26:1: note: 'time' is defined in header '<ctime>'; did you forget to '#include <ctime>'?
25 | #include <../include/time.h> // See issue #6714
+++ |+#include <ctime>
26 |
In file included from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/Updater/src/Updater.h:26,
from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/BearSSLHelpers.h:27,
from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/BearSSLHelpers.cpp:23:
/Users/ludwin/.platformio/packages/framework-arduinopico/cores/rp2040/FS.h: In static member function 'static time_t fs::FS::_defaultTimeCB()':
/Users/ludwin/.platformio/packages/framework-arduinopico/cores/rp2040/FS.h:243:16: error: 'time' was not declared in this scope
243 | return time(nullptr);
| ^~~~
/Users/ludwin/.platformio/packages/framework-arduinopico/cores/rp2040/FS.h:26:1: note: 'time' is defined in header '<ctime>'; did you forget to '#include <ctime>'?
25 | #include <../include/time.h> // See issue #6714
+++ |+#include <ctime>
26 |
In file included from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFi.h:15:
/Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFiNTP.h: In member function 'bool NTPClass::waitSet(void (*)(), uint32_t)':
/Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFiNTP.h:88:17: error: 'time' was not declared in this scope
88 | while ((time(nullptr) < 10000000) && (millis() - start < timeout)) {
| ^~~~
/Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFiNTP.h:1:1: note: 'time' is defined in header '<ctime>'; did you forget to '#include <ctime>'?
+++ |+#include <ctime>
1 | /*
/Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFiNTP.h:94:16: error: 'time' was not declared in this scope
94 | return time(nullptr) < 10000000;
| ^~~~
/Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFiNTP.h:94:16: note: 'time' is defined in header '<ctime>'; did you forget to '#include <ctime>'?
*** [.pio/build/rpipicow/libae9/Updater/Updater.cpp.o] Error 1
*** [.pio/build/rpipicow/libf0a/LittleFS/LittleFS.cpp.o] Error 1
*** [.pio/build/rpipicow/libe8f/WiFi/CertStoreBearSSL.cpp.o] Error 1
In file included from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/Updater/src/Updater.h:26,
from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/BearSSLHelpers.h:27,
from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFiClientSecureBearSSL.h:30,
from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFiClientSecure.h:24,
from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFi.h:9,
from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFiClient.cpp:23:
/Users/ludwin/.platformio/packages/framework-arduinopico/cores/rp2040/FS.h: In static member function 'static time_t fs::FS::_defaultTimeCB()':
/Users/ludwin/.platformio/packages/framework-arduinopico/cores/rp2040/FS.h:243:16: error: 'time' was not declared in this scope
243 | return time(nullptr);
| ^~~~
/Users/ludwin/.platformio/packages/framework-arduinopico/cores/rp2040/FS.h:26:1: note: 'time' is defined in header '<ctime>'; did you forget to '#include <ctime>'?
25 | #include <../include/time.h> // See issue #6714
+++ |+#include <ctime>
26 |
In file included from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/Updater/src/Updater.h:26,
from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/BearSSLHelpers.h:27,
from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFiClientSecureBearSSL.h:30,
from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFiClientSecure.h:24,
from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFi.h:9,
from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFiClass.cpp:30:
/Users/ludwin/.platformio/packages/framework-arduinopico/cores/rp2040/FS.h: In static member function 'static time_t fs::FS::_defaultTimeCB()':
/Users/ludwin/.platformio/packages/framework-arduinopico/cores/rp2040/FS.h:243:16: error: 'time' was not declared in this scope
243 | return time(nullptr);
| ^~~~
/Users/ludwin/.platformio/packages/framework-arduinopico/cores/rp2040/FS.h:26:1: note: 'time' is defined in header '<ctime>'; did you forget to '#include <ctime>'?
25 | #include <../include/time.h> // See issue #6714
+++ |+#include <ctime>
26 |
In file included from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFi.h:15:
/Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFiNTP.h: In member function 'bool NTPClass::waitSet(void (*)(), uint32_t)':
/Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFiNTP.h:88:17: error: 'time' was not declared in this scope
88 | while ((time(nullptr) < 10000000) && (millis() - start < timeout)) {
| ^~~~
/Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFiNTP.h:1:1: note: 'time' is defined in header '<ctime>'; did you forget to '#include <ctime>'?
+++ |+#include <ctime>
1 | /*
/Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFiNTP.h:94:16: error: 'time' was not declared in this scope
94 | return time(nullptr) < 10000000;
| ^~~~
/Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFiNTP.h:94:16: note: 'time' is defined in header '<ctime>'; did you forget to '#include <ctime>'?
*** [.pio/build/rpipicow/src/main.cpp.o] Error 1
*** [.pio/build/rpipicow/libe8f/WiFi/BearSSLHelpers.cpp.o] Error 1
In file included from /Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFi.h:15:
/Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFiNTP.h: In member function 'bool NTPClass::waitSet(void (*)(), uint32_t)':
/Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFiNTP.h:88:17: error: 'time' was not declared in this scope
88 | while ((time(nullptr) < 10000000) && (millis() - start < timeout)) {
| ^~~~
/Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFiNTP.h:1:1: note: 'time' is defined in header '<ctime>'; did you forget to '#include <ctime>'?
+++ |+#include <ctime>
1 | /*
/Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFiNTP.h:94:16: error: 'time' was not declared in this scope
94 | return time(nullptr) < 10000000;
| ^~~~
/Users/ludwin/.platformio/packages/framework-arduinopico/libraries/WiFi/src/WiFiNTP.h:94:16: note: 'time' is defined in header '<ctime>'; did you forget to '#include <ctime>'?
*** [.pio/build/rpipicow/libe8f/WiFi/WiFiClient.cpp.o] Error 1
*** [.pio/build/rpipicow/libe8f/WiFi/WiFiClass.cpp.o] Error 1
=========================================== [FAILED] Took 2.09 seconds ===========================================