Go Down

Topic: a function-definition is not allowed here before '{' token (Read 223 times) previous topic - next topic

7101334

I'm trying to use an NTP server to call the time. I have no meaningful coding experience and am using code I found online somewhere: it's not working.

FULL CODE:
https://docs.google.com/document/d/1ikZR3GM3ocxr04pDFdGIv29WoQnv3m1WuJHIXcztwfE/edit?usp=sharing

PROBLEM PORTION:

Code: [Select]
  {
    Serial.begin(115200);
    Serial.println();
    Serial.println();

    // We start by connecting to a WiFi network
    Serial.print("Connecting to ");
    Serial.println(ssid);
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, pass);

    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
    Serial.println("");

    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());

    Serial.println("Starting UDP");
    udp.begin(localPort);
    Serial.print("Local port: ");
    Serial.println(udp.localPort());
  }

  //=======================================================================
  //  send an NTP request to the time server at the given address
  //=======================================================================
//***This is where the error occurs****
  unsigned long sendNTPpacket(IPAddress & address)
  {
    Serial.println("sending NTP packet...");
    // 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(address, 123); //NTP requests are to port 123
    udp.write(packetBuffer, NTP_PACKET_SIZE);
    udp.endPacket();
  }


Full error message:

Code: [Select]
Arduino: 1.8.9 (Windows 8.1), 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"

C:\Users\7101334\Documents\Arduino\Project\Project.ino: In function 'void setup()':

Project:72:3: error: a function-definition is not allowed here before '{' token

  {

  ^

C:\Users\7101334\Documents\Arduino\Project\Project.ino: At global scope:

Project:107:3: error: expected declaration before '}' token

} }

  ^

exit status 1
a function-definition is not allowed here before '{' token

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


As I didn't write the code to begin with, I really don't understand how to get it working properly.

UKHeliBob

This error is usually the result of not having matching pairs of { and } somewhere in the program.

Auto Format the code in the IDE and put each { and } on its own line to make the layout more obvious then check the { } pairs
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

sterretje

If the code exceeds the character limit, you can attach it. For this type of errors we do need the full code.

First thing to do is use tools -> autoformat (you might already have done that) and check that every function starts at the beginning of a line. If not, you have a } missing or in the wrong place.

Example of error
Code: [Select]
void setup()
  ...
   ...

  void loop()
  {
    ...
    ...
  }
}
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

johnwasser

Here is a helpful guide to bracket errors:

Common compiler errors caused by mismatched brackets:

"does not name a type"  or 
"expected declaration before" or
"expected unqualified-id before" or
"expected initializer before"
Usually means you forgot a '{' or put in an extra '}' in the previous function.  Since all of the open brackets have been closed, the compiler is looking for further global declarations (variables or functions).  If it finds something that looks like executable code instead of a global declaration it emits an error.  Make sure that the brackets in the preceding function are in matching pairs '{' followed by '}'.

"a function-definition is not allowed here before '{' token"
(can cause: "'functionName' was not declared in this scope")
Usually means you forgot a '}' or put in an extra '{' in the previous function.  Since a set of brackets has not been closed yet the compiler is looking for more code to put in the function.  You can't declare a function inside a function so if the compiler finds a function declaration it emits an error.  Make sure that the brackets in the preceding function are in matching pairs '{' followed by '}'.

"expected '}' at end of input"
Usually means you forgot a '}' or put in an extra '{' in the last function in the sketch.  Since a set of brackets has not been closed yet, the compiler is looking for more code to put in the function.  When it hits the end of the file instead, it emits an error. Make sure that the brackets in the last function are in matching pairs '{' followed by '}'.

"expected primary-expression before '}' token"
Usually means you have an incomplete statement before a '}'.  The block statement (between '{' and matching '}') can only contain complete statements.  Complete statements always end with ';' (or '}' for a block statement).
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

7101334

The problem is definitely with line 72
Code: [Select]
  unsigned long sendNTPpacket(IPAddress & address)
I don't see any indication that it's due to unclosed brackets; both a manual examination and CTRL + F found the same number of each type of bracket (25).

So here's my code, repeating that this is my first time doing anything like this so it's probably hideous Frankenstein code, and I haven't finished it beyond this error (line 72). Feel free to offer any additional advice while you're in there though.

https://docs.google.com/document/d/1ikZR3GM3ocxr04pDFdGIv29WoQnv3m1WuJHIXcztwfE/edit?usp=sharing

UKHeliBob

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.


AWOL

"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

7101334

We can't even see where it starts.
You could if you clicked/copied the link I posted in my previous message

https://docs.google.com/document/d/1ikZR3GM3ocxr04pDFdGIv29WoQnv3m1WuJHIXcztwfE/edit?usp=sharing

I'll edit it into the OP too

AWOL

You could if you clicked/copied the link I posted in my previous message
No line numbers.

Code: [Select]
  pinMode(15, OUTPUT); //dehumidifier, D8

{

    Serial.begin(115200);
Why is there a brace there?
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

UKHeliBob

Line 109
Then what is
Code: [Select]

  //=======================================================================
  //  send an NTP request to the time server at the given address
  //=======================================================================
  unsigned long sendNTPpacket(IPAddress & address)

doing starting at line 69 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

7101334

I don't know the "why," I didn't write this code and the portion having issues wasn't even "stitched together" by me. I have no meaningful coding experience, basically I know what I want it to do and that it's *supposed* to do that (obtain time from an NTP server), but it doesn't seem to be working out that way.

So I can't offer explanations as to why it was set up this way or why certain parts of code were placed where they were.  Are you guys able to offer ways to fix the problem or another website with better code to incorporate into my sketch? I'd love to take the time to learn to code but this is a small part of a much larger project which is already giving me more than enough to learn; I just want to get this working

I'm using the NodeMCU

gfvalvo

This particular forum is for people seeking help with their own code. If this is code you "found" on the internet and you're not even interested in understanding how it (doesn't) work, then perhaps you should mosey over to Gigs and Collaborations. There you might be able to find somebody who you can pay to do your work for you.
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

7101334

This particular forum is for people seeking help with their own code. If this is code you "found" on the internet and you're not even interested in understanding how it (doesn't) work, then perhaps you should mosey over to Gigs and Collaborations. There you might be able to find somebody who you can pay to do your work for you.
I'm not interested in paying or condescension. I'm happy to do my own research if someone could point me to somewhere more specific than Google, seeing as that's where I got this problem code in the first place.

7101334

I tried replacing all old time / NTP-related code with new code. So far that seems to have worked and presented me with a simpler problem which, again, I have tried to research and experiment with, but have hit a barrier with. Have a feeling it's exceedingly simple to solve and I am just missing the solution.

I need to use either the full time or just the hours from timeClient library to define the variable "hour" in line 76 & below. I don't really know what I'm doing as I'm new to this, so looking up "how to declare a variable" mostly led me to try things like

Code: [Select]
int hour = timeClient.getFormattedTime

int hour() = timeClient.getHours

int hour = getFormattedTime


None of these worked, either saying they weren't declared (getFormattedTime) or that the NTP format couldn't be converted. I can add the error message but I'm pretty sure I'm just a noob approaching this incorrectly.

I am trying to research these things, but it's difficult when you aren't even sure exactly how to phrase it and searching the error code often presents something either unrelated or barely related to your problem. Not trying to use this forum for free labor, and I'm surprised a user with 3,000+ posts would be so unhelpful after how many Youtube videos directed me to this board. To use a car analogy, I'm trying to learn how to change a tire, not the entire theory behind a suspension system - doesn't mean I'm trying to get someone to work for free.

I've attached my code as .ino and .txt files, but the only excerpts you should need to see to know what I'm talking about are below:

Code: [Select]
  timeClient.update();

  Serial.print(daysOfTheWeek[timeClient.getDay()]);
  Serial.print(", ");
  Serial.print(timeClient.getHours());
  Serial.print(":");
  Serial.print(timeClient.getMinutes());
  Serial.print(":");
  Serial.println(timeClient.getSeconds());
  //Serial.println(timeClient.getFormattedTime());

  if (hour > 6 && hour < 11)  // TIME PARAMETERS (Define either lights on or lights off)

Go Up