Error: expected constructor, destructor, or type conversion before '('

Hi pls help with my error!

#include <PubSubClient.h>

const int trig_pin = 25;
const int echo_pin = 26;

// Sound speed in air
#define SOUND_SPEED 340
#define TRIG_PULSE_DURATION_US 10
#include <WiFi.h>
#include <PubSubClient.h>

//wifi and mqtt parameter
const char* ssid = "papaya";
const char* password = "alice123";
const char* mqttServer = "127.0.0.1:1880";
const int mqttPort = 1880;

long ultrason_duration;
float distance_cm;




WiFiClient espClient;
PubSubClient client(espClient);
 
void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println("Connected to the WiFi network");
 
  client.setServer(mqttServer, mqttPort);
  client.setCallback(callback);
 
  while (!client.connected()) {
    Serial.println("Connecting to MQTT...");
 
//  if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
    if (client.connect("ESP8266Client" )) {
      
      Serial.println("connected");  
 
    } else {
      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000);
    }
  }
  client.publish("test", "Hello from ESP8266");
  client.subscribe("test");
}
 
void callback(char* topic, byte* payload, unsigned int length) {
 
  Serial.print("Message arrived in topic: ");
  Serial.println(topic);
 
  Serial.print("Message:");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
  Serial.println("-----------------------"); 
}
 
void loop() {
  Serial.begin(115200);
  pinMode(trig_pin, OUTPUT); // We configure the trig as output
  pinMode(echo_pin, INPUT); // We configure the echo as input
}


  // Set up the signal
  digitalWrite(trig_pin, LOW);
  delayMicroseconds(2);
 // Create a 10 µs impulse
  digitalWrite(trig_pin, HIGH);
  delayMicroseconds(TRIG_PULSE_DURATION_US);
  digitalWrite(trig_pin, LOW);

  // Return the wave propagation time (in µs)
  ultrason_duration = pulseIn(echo_pin, HIGH);

//distance calculation
  distance_cm = ultrason_duration * SOUND_SPEED/2 * 0.0001;

  // We print the distance on the serial port
  Serial.print("Distance (cm): ");
  Serial.println(distance_cm);

  delay(1000);
}
  client.loop();
}

the error result is
C:\Users\DEVINA\OneDrive\Documents\Arduino\sketch_jun19a\sketch_jun19a.ino:78:15: error: expected constructor, destructor, or type conversion before '(' token
digitalWrite(trig_pin, LOW);
^
C:\Users\DEVINA\OneDrive\Documents\Arduino\sketch_jun19a\sketch_jun19a.ino:79:20: error: expected constructor, destructor, or type conversion before '(' token
delayMicroseconds(2);
^
C:\Users\DEVINA\OneDrive\Documents\Arduino\sketch_jun19a\sketch_jun19a.ino:81:15: error: expected constructor, destructor, or type conversion before '(' token
digitalWrite(trig_pin, HIGH);
^
C:\Users\DEVINA\OneDrive\Documents\Arduino\sketch_jun19a\sketch_jun19a.ino:82:20: error: expected constructor, destructor, or type conversion before '(' token
delayMicroseconds(TRIG_PULSE_DURATION_US);
^
C:\Users\DEVINA\OneDrive\Documents\Arduino\sketch_jun19a\sketch_jun19a.ino:83:15: error: expected constructor, destructor, or type conversion before '(' token
digitalWrite(trig_pin, LOW);
^
C:\Users\DEVINA\OneDrive\Documents\Arduino\sketch_jun19a\sketch_jun19a.ino:86:3: error: 'ultrason_duration' does not name a type
ultrason_duration = pulseIn(echo_pin, HIGH);
^~~~~~~~~~~~~~~~~
C:\Users\DEVINA\OneDrive\Documents\Arduino\sketch_jun19a\sketch_jun19a.ino:89:3: error: 'distance_cm' does not name a type
distance_cm = ultrason_duration * SOUND_SPEED/2 * 0.0001;
^~~~~~~~~~~
C:\Users\DEVINA\OneDrive\Documents\Arduino\sketch_jun19a\sketch_jun19a.ino:92:3: error: 'Serial' does not name a type
Serial.print("Distance (cm): ");
^~~~~~
C:\Users\DEVINA\OneDrive\Documents\Arduino\sketch_jun19a\sketch_jun19a.ino:93:3: error: 'Serial' does not name a type
Serial.println(distance_cm);
^~~~~~
C:\Users\DEVINA\OneDrive\Documents\Arduino\sketch_jun19a\sketch_jun19a.ino:95:8: error: expected constructor, destructor, or type conversion before '(' token
delay(1000);
^
C:\Users\DEVINA\OneDrive\Documents\Arduino\sketch_jun19a\sketch_jun19a.ino:96:1: error: expected declaration before '}' token
}
^

exit status 1

Compilation error: expected constructor, destructor, or type conversion before '(' token

Your loop is ended at this brace. All lines after that is beyond on any function.

1 Like

Thank you by deleting the brace it worked but now i have a new error code.

C:\Users\DEVINA\OneDrive\Documents\Arduino\sketch_jun19a\sketch_jun19a.ino:96:3: error: 'client' does not name a type
client.loop();
^~~~~~
C:\Users\DEVINA\OneDrive\Documents\Arduino\sketch_jun19a\sketch_jun19a.ino:97:1: error: expected declaration before '}' token
}
^

exit status 1

Compilation error: 'client' does not name a type

Because you have the same error futher in loop.
The number of open and close braces must match.

1 Like

Where did you ge the code from ?

so what can i do for now? sorry im a newbie in the area

i got the code from other forum too.. any idea what's wrong with the code?

The number of open and close braces must match.

1 Like

You have a random bit of code outside of any function;


client.loop();
}

Maybe find a tutorial on basic coding and study it. And with such basic errors in the code you found, who knows if the rest of it actually works......

2 Likes

thank you srnet i have succeeded in uploading the code but why does my serial monitor said
failed with state -2Connecting to MQTT...

failed with state -2Connecting to MQTT...

failed with state -2Connecting to MQTT...

failed with state -2Connecting to MQTT...

failed with state -2Connecting to MQTT...

failed with state -2Connecting to MQTT...

failed with state -2Connecting to MQTT...

thank you b707 i have succeeded in uploading the code but why does my serial monitor said
failed with state -2Connecting to MQTT...

failed with state -2Connecting to MQTT...

failed with state -2Connecting to MQTT...

failed with state -2Connecting to MQTT...

failed with state -2Connecting to MQTT...

failed with state -2Connecting to MQTT...

failed with state -2Connecting to MQTT...

Do not post in the Uncategorized category !!

Topic moved to a more suitable location on the forum.

okay will do, anyway do you have any idea why my serial monitor shows that result?

PubSubClient state -2 means "No connection to the server"
Does your MQTT server alive?

Also, you not need to set port in server address, try just

how to check if my mqtt server is alive or not?

tyy

What is your MQTT server?

i used the localhost from node red ip address

But what is your MQTT server?
It should be a separate program that will respond to MQTT requests, such as mosquito.
Did you installed it?

oh yes i used mosquitto. have installed and checked on the command prompt and the mosquitto is already active