Function-definition issue

Hello, I see there are other threads about this, but I wasn't sure if resurrecting years-old threads is a bad thing or not.

So clearly I have a syntax error. but I've checked every brace, bracket, and parenthisis and all over google but beating my head over it isn't helping, so I was hoping for some new eyes.

Entire code:

#include <WiFiNINA_Generic.h>
#include <SPI.h>
#include "arduino_secrets.h"
#include <hd44780.h>
#include <Wire.h>
#include <hd44780ioClass/hd44780_I2Cexp.h> // include i/o class header

int sensorPin0 = A0;
int sensorPin1 = A1;
int sensorValue0;
int sensorValue1;
int limit = 380;

hd44780_I2Cexp lcd;

void setup()
{
  lcd.init();                      // initialize the lcd
  // set up the LCD's number of columns and rows:
  lcd.begin(20, 4);
  lcd.backlight();
  Serial.begin(9600);
  while (!Serial)
  {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE)
  {
    Serial.print("Communication with WiFi failed!");
    lcd.setCursor(0, 0);
    lcd.print("Communication with");
    lcd.setCursor(0, 1);
    lcd.print("WiFi failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION)
  {
    Serial.print("Please upgrade the firmware");
    lcd.setCursor (0, 0);
    lcd.print("Please upgrade");
    lcd.setCursor (0, 1);
    lcd.print("the firmware");
   }
}

void loop()
{
 sensorValue0 = analogRead(sensorPin0);
 sensorValue1 = analogRead(sensorPin1);
 Serial.print("Analog Value : ");
 Serial.print(sensorValue0);
 Serial.print(sensorValue1);
 lcd.setCursor(0, 0);
 lcd.print(" ");
 lcd.setCursor (0, 0);
 lcd.print("Analog Value : ");
 lcd.setCursor (0, 1);
 lcd.print(sensorValue0);
 lcd.setCursor (0, 2);
 lcd.print(sensorValue1);
 


  if (sensorValue0 < limit || sensorValue1 < limit)
  {
   digitalWrite(13, HIGH);
   digitalWrite(12, HIGH);
  }

  else
  {
   digitalWrite(13, LOW);
   digitalWrite(12, LOW);
  }
  
  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED)
  {
    Serial.print("Attempting to connect to WPA SSID: ");
    lcd.setCursor (0, 0);
    lcd.print("Attempting to ");
    lcd.setCursor (0, 1);
    lcd.print("WPA SSID: ");
    lcd.setCursor (0, 2);
    Serial.print(ssid);
    lcd.print(ssid);
    lcd.setCursor (0, 3);
    delay (2000);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // wait 5 seconds for connection:
    delay(5000);
  }
  pingResult = WiFi.ping(hostName);
  if (pingResult >= 0)
  {
   Serial.print("SUCCESS! RTT = ");
   Serial.print(pingResult);
   Serial.print(" ms");
   lcd.setCursor (0, 0);
   lcd.print("SUCCESS! RTT = ");
   lcd.setCursor (0, 1);
   lcd.print(pingResult);
   lcd.setCursor (0, 2);
   lcd.print(" ms");
  }
  else
  {
   Serial.print("FAILED! Error code: ");
   Serial.print(pingResult);
   lcd.setCursor(0, 0);
   lcd.print("FAILED! Error code: ");
   lcd.setCursor(1, 1);
   lcd.print(pingResult);
  }

  delay(1000);

 void printWiFiData()
 {
  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP address : ");
  Serial.print(ip);
  lcd.setCursor(0, 0);
  lcd.print("IP address : ");
  lcd.setCursor(0, 1);
  lcd.print(ip);
  
  // you're connected now, so print out the data:
  Serial.print("You're connected to the network");
  lcd.setCursor (0, 0);
  lcd.print("You're connected to ");
  lcd.setCursor (0, 1);
  lcd.print("the network");
  delay (2000);
  lcd.setCursor(0, 0);
  lcd.print(" ");
  printCurrentNet();
  printWiFiData();

  Serial.print("Subnet mask: ");
  Serial.print((IPAddress)WiFi.subnetMask());
  
  Serial.print("Gateway IP : ");
  Serial.print((IPAddress)WiFi.gatewayIP());
  
  // print your MAC address:
  byte mac[6];
  WiFi.macAddress(mac);
  Serial.print("MAC address: ");
 } 

 void printCurrentNet() 
 {
//  print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.print(WiFi.SSID());
  

  // print the MAC address of the router you're attached to:
  byte bssid[6];
  WiFi.BSSID(bssid);
  Serial.print("BSSID: ");
  //    lcd.print("BSSID: ");
  //    lcd.print('/n');
  //    printMacAddress(bssid);
  //print the received signal strength:
  //long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI): ");
  Serial.print(rssi);
  

  //print the encryption type:
  byte encryption = WiFi.encryptionType();
  Serial.print("Encryption Type: ");
  Serial.print(encryption, HEX);
  Serial.print();
 }

//void printMacAddress(byte mac[]) 
// {
//  for (int i = 5; i >= 0; i--) 
//    if (mac[i] < 16) 
//    {
//      Serial.print("0");
//      //        lcd.print("0");
//      //        lcd.print('/n');
//    }  
//     
//    Serial.print(mac[i], HEX);
//    if (i > 0) 
//    {
//      Serial.print(":");
//      //        lcd.print(":");
//      //        lcd.print('/n');
//    }
// }  
    delay(1000);
 
}

and the two parts that result in a " function-definition is not allowed here before '{' token "
are:

void printWiFiData()
 {
  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP address : ");
  Serial.print(ip);
  lcd.setCursor(0, 0);
  lcd.print("IP address : ");
  lcd.setCursor(0, 1);
  lcd.print(ip);
  
  // you're connected now, so print out the data:
  Serial.print("You're connected to the network");
  lcd.setCursor (0, 0);
  lcd.print("You're connected to ");
  lcd.setCursor (0, 1);
  lcd.print("the network");
  delay (2000);
  lcd.setCursor(0, 0);
  lcd.print(" ");
  printCurrentNet();
  printWiFiData();

  Serial.print("Subnet mask: ");
  Serial.print((IPAddress)WiFi.subnetMask());
  
  Serial.print("Gateway IP : ");
  Serial.print((IPAddress)WiFi.gatewayIP());
  
  // print your MAC address:
  byte mac[6];
  WiFi.macAddress(mac);
  Serial.print("MAC address: ");
 } 

 void printCurrentNet() 
 {
//  print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.print(WiFi.SSID());
  

  // print the MAC address of the router you're attached to:
  byte bssid[6];
  WiFi.BSSID(bssid);
  Serial.print("BSSID: ");
  //    lcd.print("BSSID: ");
  //    lcd.print('/n');
  //    printMacAddress(bssid);
  //print the received signal strength:
  //long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI): ");
  Serial.print(rssi);
  

  //print the encryption type:
  byte encryption = WiFi.encryptionType();
  Serial.print("Encryption Type: ");
  Serial.print(encryption, HEX);
  Serial.print();
 }

If I comment those out the rest compiles. I figure this will be something stupid, but I tried all the stupid things I can think of.

Thanks


  delay(1000);
}   //  <<<< Missing
void printWiFiData()

Hello theloniusfunk

The closing bracket for void loop() is at a wrong positie.

Have a nice day and enjoy programming in C++ and learning.

p.s. The Notepad++ editor supports this analysis best

You're putting them in loop().

Move the } from the very end to here:

 }

  delay(1000);
<<<<<<<<<<<<<<<<<<<<
  void printWiFiData()
  {
    // print your board's IP address:

...and the 2 last lines of the program are not required.

delay(1000);

}

Rule: you can't define a function inside of a function.

Yes! Thank you all it is working fine now.

Mark solution and share some love so that others don’t waste time on a solved issue :white_check_mark:

It also marks threads as having a solution for those who actually browse the forum for answers before asking questions.

It only seems like no one does that.