expected initializer before '}' token

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);
}

void loop() }

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

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

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.

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

netrosec:
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.

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.

ChrisTenone:
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:

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.)

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.

Bear with me now, I fixed the curly brace error now it says 'Serial' does not name a type

netrosec:
now it says 'Serial' does not name a type

So help us to help you by posting the latest code.

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.

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.

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.

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..

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

wilfredmedlin:
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.

Please read the "how to use this forum-please read" stickies to see how to format and post code and error messages. Following the guidelines in the stickies helps us help you.

Do not change a previous post with new code, post the new code in a new post. That keeps the continuity of the thread.

Post the whole code, including the library includes, defines, variable declarations, etc.

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

That makes it harder for us to test your code.
Also, don't change your original post. Then the answers given don't make sense.
Post the full code with code tags. Read the forum rules first.
Leo..

Wawa:
That makes it harder for us to test your code.
Also, don't change your original post. Then the answers given don't make sense.
Post the full code with code tags. Read the forum rules first.
Leo..

Here's the whole thing

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// WiFi
// Make sure to update this for your own WiFi network!
const char* ssid = "<./>";
const char* wifi_password = "warfcity";

// MQTT
// Make sure to update this for your own MQTT Broker!
const char* mqtt_server = "192.168.0.55";
const char* mqtt_topic = "laser";
// The client id identifies the ESP8266 device. Think of it a bit like a hostname (Or just a name, like Greg).
const char* clientID = "esp8266";

// Initialise the WiFi and MQTT Client objects
WiFiClient wifiClient;
PubSubClient client(mqtt_server, 1883, wifiClient); // 1883 is the listener port for the Broker

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);
}
}

The } that closes setup() is actually after the one closing loop(), so loop is inside setup.

So move the very very last } to be just above "void loop()"

wilfredmedlin:
The } that closes setup() is actually after the one closing loop(), so loop is inside setup.

So move the very very last } to be just above "void loop()"

ok i see that. Fixing that generated this error:

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

sensorValue = analogRead(sensorPin);

^

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

sensorValue = analogRead(sensorPin);

^

ldr:61: error: 'else' without a previous 'if'

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

^

exit status 1
'sensorValue' was not declared in this scope