Go Down

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

groundFungus

#15
Jan 29, 2018, 11:12 am Last Edit: Jan 29, 2018, 11:13 am by groundFungus
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.

Wawa

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

netrosec

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

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()"
Keep Calm and call 0118 999 88199 9119725...... 3

netrosec

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


wilfredmedlin

Well those messages are un-ambiguous. Nowhere did you do eg:

Code: [Select]
int sensorValue;



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

netrosec

Well those messages are un-ambiguous. Nowhere did you do eg:

Code: [Select]
int sensorValue;




Sorry i don't understand what you mean.

wilfredmedlin

Sorry i don't understand what you mean.
Ok well you need to back up a bit and look at some basics.

Variables need to be declared; have a read here, then here and the variables part on this page.

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

Go Up