Go Down

Topic: expected initializer before '}' token (Read 2284 times) previous topic - next topic

netrosec

Jan 29, 2018, 03:22 am Last Edit: Jan 29, 2018, 10:52 am by netrosec Reason: updated to latest working code
I'm a beginner with the arduino and lua language. editing 2 working scripts to get what i need done.
i have an ldr connected to my esp8266 i need the code to publish to an mqtt topic when a set threshold is read. However, i keep seeing this error: **expected initializer before '}' token**
What am i doing wrong?
Any help is much appreciated.




void setup() {

  pinMode(13, OUTPUT); //pin connected to the relay
  Serial.begin(115200); //sets serial port for communication

  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, wifi_password);


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

  }
  }

  // Debugging - Output the IP Address of the ESP8266
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
 
  // Connect to MQTT Broker
  // client.connect returns a boolean value to let us know if the connection was successful.
  if (client.connect(clientID)) {
    Serial.println("Connected to MQTT Broker!");
    }
 
  else {
    Serial.println("Connection to MQTT Broker failed...");
  }

  void loop()
  {
    // read the value from the sensor:
    sensorValue = analogRead(sensorPin);
    Serial.println(sensorValue); //prints the values coming from the sensor on the screen

    if (sensorValue < 700) //setting a threshold value
      //digitalWrite(2,HIGH); //turn relay ON
      client.publish(mqtt_topic, "Button pressed!");

    else digitalWrite(2, LOW); //turn relay OFF

    delay(100);
  }

Delta_G

Code: [Select]
void loop() }

Look at that line closely.  Find similar lines in other examples.  Notice the difference?
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

netrosec

Haha.. it took me a second to see the problem after you pointed it out. I was using the wrong curly brace. And i posted the wrong part of the code too. Pardon me.

Now this is the error i get: **a function-definition is not allowed here before '{' token**

Delta_G

Looks like you've left a brace off the end of setup.  These braces are important.  They're not just decoration.  You should take some time to familiarize yourself with the purpose they serve. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

netrosec

The braces are all there now. But still getting the error.

Delta_G

#5
Jan 29, 2018, 04:02 am Last Edit: Jan 29, 2018, 04:02 am by Delta_G
The braces are all there now. But still getting the error.
Then the braces aren't all in the right spots.  I'd help but I can't see what you've got now. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

ChrisTenone

You will need another } after the line "Serial.println("Connection to MQTT Broker failed...");"

When you write your code, do a Ctrl-t before verifying it. That will autoformat your code, and things like misplaced braces will stand out.
What, I need to say something else too?

wilfredmedlin

#7
Jan 29, 2018, 06:58 am Last Edit: Jan 29, 2018, 07:04 am by wilfredmedlin
When you write your code, do a Ctrl-t before verifying it.
But before you do that... I find it beneficial to put a carriage return in after "void setup()" and its opening { and ditto with loop(), so it looks like this:

Code: [Select]

void setup()
{

}

void loop()
{

}


In fact, I edited the template and saved it so I get that style as my default. It's the Bare Minimum sketch in the examples folder of the Arduino install folder. (Not your sketchbook.)





Keep Calm and call 0118 999 88199 9119725...... 3

netrosec

You will need another } after the line "Serial.println("Connection to MQTT Broker failed...");"

When you write your code, do a Ctrl-t before verifying it. That will autoformat your code, and things like misplaced braces will stand out.
Bear with me now, I fixed the curly brace error now it says **'Serial' does not name a type**

wilfredmedlin

now it says **'Serial' does not name a type**

So help us to help you by posting the latest code.
Keep Calm and call 0118 999 88199 9119725...... 3

groundFungus

If you make changes to your code, you should post the new code so that we know what you did and can put into our IDE and check it.  

Please post the entire text of the error message.  There is important information missing from your truncated posting of the error.

Please read the "how to use this forum-please read" stickies to see how to format and post code and error messages.

netrosec

If you make changes to your code, you should post the new code so that we know what you did and can put into our IDE and check it. 

Please post the entire text of the error message.  There is important information missing from your truncated posting of the error.

Please read the "how to use this forum-please read" stickies to see how to format and post code and error messages.
Sorry about that.. I have now updated the original question with my current working code.
this is the whole error:

Arduino: 1.8.5 (Windows 10), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, 115200, 4M (3M SPIFFS)"

ldr:40: error: 'Serial' does not name a type

   Serial.println("WiFi connected");
   ^

ldr:41: error: 'Serial' does not name a type
   Serial.print("IP address: ");
   ^

ldr:42: error: 'Serial' does not name a type
   Serial.println(WiFi.localIP());
   ^

ldr:46: error: expected unqualified-id before 'if'
   if (client.connect(clientID)) {

   ^

ldr:50: error: expected unqualified-id before 'else'
   else {

   ^

C:\Users\w4r10ck3r\Desktop\ldr\ldr.ino: In function 'void loop()':

ldr:57: error: 'sensorValue' was not declared in this scope

     sensorValue = analogRead(sensorPin);

     ^

ldr:57: error: 'sensorPin' was not declared in this scope

     sensorValue = analogRead(sensorPin);

                              ^

exit status 1
'Serial' does not name a type

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

Wawa

Put the cursor on the first brace of void setup().
The end of void setup() will be boxed in.
Where does void setup() end....

The block below it will be excluded from setup().
Leo..

wilfredmedlin

Apart from any brace-related issues, you're trying to use the WiFi library but there's no include for it.

Keep Calm and call 0118 999 88199 9119725...... 3

netrosec

Apart from any brace-related issues, you're trying to use the WiFi library but there's no include for it.


I did include it. I just didn't paste that part of the code here.

Go Up