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