-->
Page 1 of 2

Trying to fix error message

PostPosted: Mon Jan 31, 2022 10:52 am
by NAP1947
The following sketch generates the message " 'getHours' was not declared in this scope". The sketch was downloaded from an Instructables project and no one has identified the issue. I believe it may be that I am missing an ESP8266 Library but have not been able to figure out which one it is. Being new I hope I have done this correctly and asked in the right place.

Any guidance would be appreciated.
#include <Servo.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WiFiUdp.h>

ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'

WiFiUDP UDP; // Create an instance of the WiFiUDP class to send and receive

IPAddress timeServerIP; // time.nist.gov NTP server address
const char* NTPServerName = "time.nist.gov";

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

byte NTPBuffer[NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

void setup() {
myservo.attach(5); // attaches the servo on pin 5 aka D1 to the servo object

//open the lid by default
Serial.println("opening the lid");
for (pos = 95; pos >= 0; pos -= 1) { // goes from 95 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}

Serial.begin(115200); // Start the Serial communication to send messages to the computer
delay(10);
Serial.println("\r\n");

startWiFi(); // Try to connect to some given access points. Then wait for a connection

startUDP();

if(!WiFi.hostByName(NTPServerName, timeServerIP)) { // Get the IP address of the NTP server
Serial.println("DNS lookup failed. Rebooting.");
Serial.flush();
ESP.reset();
}
Serial.print("Time server IP:\t");
Serial.println(timeServerIP);

Serial.println("\r\nSending NTP request ...");
sendNTPpacket(timeServerIP);
}

unsigned long intervalNTP = 60000; // Request NTP time every minute
unsigned long prevNTP = 0;
unsigned long lastNTPResponse = millis();
uint32_t timeUNIX = 0;

unsigned long prevActualTime = 0;

void loop() {
unsigned long currentMillis = millis();

if (currentMillis - prevNTP > intervalNTP) { // If a minute has passed since last NTP request
prevNTP = currentMillis;
Serial.println("\r\nSending NTP request ...");
sendNTPpacket(timeServerIP); // Send an NTP request
}

uint32_t time = getTime(); // Check if an NTP response has arrived and get the (UNIX) time
if (time) { // If a new timestamp has been received
timeUNIX = time;
Serial.print("NTP response:\t");
Serial.println(timeUNIX);
lastNTPResponse = currentMillis;
} else if ((currentMillis - lastNTPResponse) > 3600000) {
Serial.println("More than 1 hour since last NTP response. Rebooting.");
Serial.flush();
ESP.reset();
}

uint32_t actualTime = timeUNIX + (currentMillis - lastNTPResponse)/1000;
uint32_t easternTime = timeUNIX - 18000 + (currentMillis - lastNTPResponse)/1000;
if (actualTime != prevActualTime && timeUNIX != 0) { // If a second has passed since last print
prevActualTime = actualTime;
Serial.printf("\rUTC time:\t%d:%d:%d ", getHours(actualTime), getMinutes(actualTime), getSeconds(actualTime));
Serial.printf("\rEST (-5):\t%d:%d:%d ", getHours(easternTime), getMinutes(easternTime), getSeconds(easternTime));
Serial.println();
}

// 7:30am
if(getHours(easternTime) == 7 && getMinutes(easternTime) == 30 && getSeconds(easternTime) == 0){
//open the lid
Serial.println("opening the lid");
for (pos = 95; pos >= 0; pos -= 1) { // goes from 95 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}



// midnight
if(getHours(easternTime) == 59 && getMinutes(easternTime) == 0 && getSeconds(easternTime) == 0){
//close the lid
Serial.println("closing the lid");
for (pos = 0; pos <= 95; pos += 1) { // goes from 0 degrees to 95 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}

/*
// testing
if(getHours(easternTime) == 12 && getMinutes(easternTime) == 45 && getSeconds(easternTime) == 0){
//close the lid
Serial.println("closing the lid");
for (pos = 0; pos <= 95; pos += 1) { // goes from 0 degrees to 95 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
//open the lid
Serial.println("opening the lid");
for (pos = 95; pos >= 0; pos -= 1) { // goes from 95 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
*/
}

void startWiFi() { // Try to connect to some given access points. Then wait for a connection
wifiMulti.addAP("ssid_from_AP_1", "your_password_for_AP_1"); // add Wi-Fi networks you want to connect to
//wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
//wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");

Serial.println("Connecting");
while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(250);
Serial.print('.');
}
Serial.println("\r\n");
Serial.print("Connected to ");
Serial.println(WiFi.SSID()); // Tell us what network we're connected to
Serial.print("IP address:\t");
Serial.print(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
Serial.println("\r\n");
}

void startUDP() {
Serial.println("Starting UDP");
UDP.begin(123); // Start listening for UDP messages on port 123
Serial.print("Local port:\t");
Serial.println(UDP.localPort());
Serial.println();
}

uint32_t getTime() {
if (UDP.parsePacket() == 0) { // If there's no response (yet)
return 0;
}
UDP.read(NTPBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
// Combine the 4 timestamp bytes into one 32-bit number
uint32_t NTPTime = (NTPBuffer[40] << 24) | (NTPBuffer[41] << 16) | (NTPBuffer[42] << 8) | NTPBuffer[43];
// Convert NTP time to a UNIX timestamp:
// Unix time starts on Jan 1 1970. That's 2208988800 seconds in NTP time:
const uint32_t seventyYears = 2208988800UL;
// subtract seventy years:
uint32_t UNIXTime = NTPTime - seventyYears;
return UNIXTime;
}

void sendNTPpacket(IPAddress& address) {
memset(NTPBuffer, 0, NTP_PACKET_SIZE); // set all bytes in the buffer to 0
// Initialize values needed to form NTP request
NTPBuffer[0] = 0b11100011; // LI, Version, Mode
// send a packet requesting a timestamp:
UDP.beginPacket(address, 123); // NTP requests are to port 123
UDP.write(NTPBuffer, NTP_PACKET_SIZE);
UDP.endPacket();
}

Re: Trying to fix error message

PostPosted: Tue Feb 01, 2022 11:29 am
by rpiloverbd
This error occurs when you do not declare variables before the setup function.

https://forum.arduino.cc/t/solved-varia ... tup/471208

Re: Trying to fix error message

PostPosted: Tue Feb 01, 2022 12:04 pm
by btidey
I can't see any declaration of getHours.

As this is being used in the context of NTP functionality, getHours is probably referencing the function that is include in the NTPClient library which is not included.

The NTPClientLib should be installed via library manager if not already done and then

#include <NTPClient.h>

should be added at the top of the code along with the other includes

Re: Trying to fix error message

PostPosted: Tue Feb 01, 2022 12:14 pm
by NAP1947
Thank you for your responses, tells me how much I have yet to learn, I would never have figured out the library requirement, however;
NTPClientlib was not installed so I have done that successfully.
I then added #include <NTPClient.h> as the first line in the include section of the sketch.
When I compile I get the same error as previously.