Hello, I've tried hard to figure this out myself but have been unable to.
I am using preferences to store a field to NVM but it's giving errors. There's just some syntax it doesn't like. It's an unsigned integer that I'm trying to store (and then retrieve).
I have tried commenting out one error, but then it throws up another so there's more than one error
the error messages are below but in summary:
"error: expected primary-expression before 'fieldname'"
"error: expected primary-expression before 'unsigned'"
I'm compiling it for ESP32 not an Arduino.
Help would be appreciated. Thanks.
#include <WiFi.h> // Library provides Wi-Fi Access
#include <Preferences.h> // Library stores variables in NVM
#include <NTPClient.h> // Library for NTP client
const char* ssid = "Haverstock-Guest";
const char* password = "november2015";
// Configure NTP Client:
WiFiUDP ntpUDP; // Dunno what this line does
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 0, 60000); // Server, Offset, Update Interval
String formattedDate;
String dayStamp;
String timeStamp;
Preferences preferences; // Create an instance of the preferences library called 'preferences'.
void StoreInt(std::string fieldname, unsigned int val) // Store Integer to NVM.
{
// Serial.println("Key:", fieldname, "!"); // For debug Only
preferences.begin("counters",false); // Open the 'counters' namespace
preferences.putUInt( String fieldname, unsigned int val);
//preferences.putUInt("pulseCount19", 202);
preferences.end();
}
//unsigned Int RetrieveInt(std::String fieldname) // Retrieve Integer from NVM
int RetrieveInt(String fieldname)
{
preferences.begin("counters", false);
//unsigned int ret = 202;
Serial.println(fieldname);
unsigned int ret = 20;
//unsigned int ret = preferences.getUInt(fieldname, 0); // Returns 0 if doesn't exist
preferences.end();
return ret;
}
void setup() {
// Connect to Wi-Fi:
WiFi.mode(WIFI_STA); // Set Station mode (not AP)
WiFi.begin(ssid, password);
Serial.println("\nConnecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) // Wait for Wi-Fi to connect
{
Serial.print("."); // Print dots while waiting
delay(200);
}
Serial.println("\nConnected to the WiFi network");
Serial.print("Local ESP32 IP: ");
Serial.println(WiFi.localIP());
// Connected to Wi-Fi
timeClient.begin(); // Initialise NTP Service
// Configure ESP32 IO Ports:
Serial.begin(19200); // Initialise Serial Port for PC console
pinMode(02, OUTPUT); // Config GPIO02 as Built-in LED
pinMode(19, INPUT_PULLUP); // Config GPIO19 as input and enable the internal pull-up resistor
pinMode(21, INPUT_PULLUP); // GPIO21
pinMode(22, INPUT_PULLUP); // GPIO21
pinMode(23, INPUT_PULLUP); // GPIO21
}
void loop() { // Main Code Loop
// Get time/date from NTP function:
while (!timeClient.update()) // Wait for update
{
timeClient.forceUpdate();
}
formattedDate = timeClient.getFormattedDate(); // Example Time Returned : 2023-11-12T13:30:22Z
Serial.println(formattedDate);
// Retreive Counters from NVM
unsigned int pulseCount19 = RetrieveInt("pulseCount19"); // Pulse Counter (All time)
unsigned int previousCount19 = RetrieveInt("previousCount19"); // Previous Period (30 days)
while (0 == 1) // infinite Loop for main program:
{
if (digitalRead(19) == 0) // If DI19 is pressed...
{
pulseCount19++; // ...increment counter
}
Serial.print(formattedDate);
Serial.print(" ");
Serial.print("Counter19:"); // For debug Only
Serial.println(pulseCount19); // For debug Only
delay(500);
} // End of infinite loop
} // End of main program loop
C:\Users\Michael\Documents\Arduino\ESP32_Pulse_Counter\ESP32_Pulse_Counter.ino: In function 'void StoreInt(std::__cxx11::string, unsigned int)':
C:\Users\Michael\Documents\Arduino\ESP32_Pulse_Counter\ESP32_Pulse_Counter.ino:21:32: error: expected primary-expression before 'fieldname'
preferences.putUInt( String fieldname, unsigned int val);
^~~~~~~~~
C:\Users\Michael\Documents\Arduino\ESP32_Pulse_Counter\ESP32_Pulse_Counter.ino:21:43: error: expected primary-expression before 'unsigned'
preferences.putUInt( String fieldname, unsigned int val);
^~~~~~~~
exit status 1
Compilation error: expected primary-expression before 'fieldname'
p.s. I thought maybe I should remove the type definitions as they are implicit, i.e. preferences.putUInt( String fieldname, unsigned int val); becomes preferences.putUInt(fieldname,val); but that still gets upset.