0

I'll post the whole code if you really need to see it, but my question is pretty simple.

In my sketch I define some parameters:

#define SSID "Kaywinnet"
#define PASSWORD "806194edb8"
#define MQTT_SERVER { 192, 168, 1, 124 }

And, the sketch compiles just fine. But, rather than putting my net parameters into every sketch, I wanted to just use a .h file so that if something changes, I only have to change it once.

Here's the problem. When I put these three lines into a .h file, I get an error when the first parameter is used in the sketch:

expected primary-expression before '.' token

Here is the code where I use the value:

//Initialize ESPHelper
netInfo homeNet = {
  .mqttHost = mqtt_server,
  .mqttUser = "",
  .mqttPass = "",
  .mqttPort = 1883,
  .ssid = SSID,
  .pass = password
};
ESPHelper myESP(&homeNet);

The error is on the .ssid= line.

So, why does it compile when I define the parameters in the sketch, and gives an error when I put them into a .h file??

Here's the ino file:

    /*****

  Projects/esp8266/MQTT nodes/light2.ino
  10/3/2017- Clone of studio.ino

  IDE settings:
  Board: "Generic 8266 Module"
  Flash Mode: "DOUT"

*****/


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

//#include <Kaywinnet.h>
#define SSID "Kaywinnet"
#define password "806194edb8"
#define mqtt_server "192.168.1.124"

//#define toggle    //Using toggle switch.
#define pushbutton    //Using pushbutton switch


#define NODENAME "light2"
const char *cmdTopic =  NODENAME "/light";
const char *statusTopic = NODENAME "/light/status";
const char *connectName =  NODENAME "lights";
#define OTAPASSWORD "xxxx"



//Defaults to DHCP, if you want a static IP, configure below
//#define STATIC_IP
#ifdef STATIC_IP
IPAddress ip(192, 168, 1, 50);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
#endif



//Initialize ESPHelper
netInfo homeNet = {
  .mqttHost = mqtt_server,
  .mqttUser = "",
  .mqttPass = "",
  .mqttPort = 1883,
  .ssid = SSID,
  .pass = password
};
ESPHelper myESP(&homeNet);


//Declare an object of class WiFiClient
WiFiClient esplight2;
PubSubClient client(esplight2);


#define esp8266-01    //Either sonoff or esp8266-01

//Define the GPIO pins on the 8266-01
#ifdef esp8266-01
const int buttonPin = 0;    //GPIO0
const int relayPin = 2;     //GPIO2
#endif

#ifdef Sonoff
const int buttonPin = 0;     //GPIO0
const int extraPin = 14;
const int ledPin = 13;
const int relayPin = 12;
const bool ledOFF = HIGH;
const bool ledON = LOW;
#endif

int switchVal = 0;
int currentVal = 0;
bool relayState;
String payLoad;


// * *************** RelayControl, remember the state ***************
//
void RelayControl(bool state) {
  //Serial.print("relayState: ");
  //Serial.println(relayState);

  digitalWrite(relayPin, state ? HIGH : LOW);
  relayState = state;

  //Feedback over MQTT
  if (relayState) {
    client.publish(statusTopic, "on");    //Tell Node Red about the current status
    Serial.println(String(statusTopic) + "= on.");
  } else {
    client.publish(statusTopic, "off");
    Serial.println(String(statusTopic) + "= off.");
  }
  Serial.println();
}



void timeLoop (long int startMillis, long int interval) { // the delay function
  while (millis() - startMillis < interval) {}
}




/*
 * ************** blink the LED ************************
   We assume the LED is on after connecting.
*/
void blinkLED() {
#ifdef esp8266-01
  return;   //No led on the esp to blink..
#endif
#ifdef Sonoff
  digitalWrite(ledPin, ledOFF);
  delay(500);
  digitalWrite(ledPin, ledON);
#endif
}




/*
 * ******************** set up WiFi ****************
*/
void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(SSID);

  //Connect to WiFi network so we can reach the MQTT broker and publish messages to topics.
  WiFi.mode(WIFI_STA);
  WiFi.begin(SSID, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("WiFi connected - ESP IP address: ");
  Serial.println(WiFi.localIP());
}



/*
 * *************** MQTT callback ********************
*/
void callback(String topic, byte* message, unsigned int length) {
  Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");

  String messageTemp;
  for (int i = 0; i < length; i++) {
    //Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  messageTemp.trim();            // trim the white space off the string

  Serial.println(messageTemp);

  // If a message is received with the topic 'cmdTopic', check if the message is either on or off.
  if (topic == cmdTopic) {
    if (messageTemp == "on") {
      Serial.println("Light2 is ON");
      RelayControl(HIGH);
      blinkLED();

    }
    else if (messageTemp == "off") {
      Serial.println("Light2 is OFF");
      RelayControl(LOW);
      blinkLED();

    }
  }
}





/*
 * **************** Connect to MQTT broker **************
   This functions reconnects your ESP8266 to the MQTT broker and subscribes to topics.
*/
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");

    // Attempt to connect
    if (client.connect(connectName)) {
      Serial.println("connected");
#ifdef Sonoff
      digitalWrite(ledPin, ledON);
#endif
      // Subscribe or resubscribe to a topic
      client.subscribe(cmdTopic);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}





/*
 * *************** SETUP ********************
*/
void setup() {

  //espHelper
  myESP.OTA_enable();
  myESP.OTA_setPassword(OTAPASSWORD);
  myESP.OTA_setHostname(NODENAME);
  myESP.begin();

#ifdef Sonoff
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, ledOFF);     //The Sonoff LED is low-true.  This starts with it off.
#endif

  pinMode(relayPin, OUTPUT);
  RelayControl(LOW);  //Start with the heat off

  pinMode(buttonPin, INPUT_PULLUP);

  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);

  //Save the initial switch position.
  currentVal = digitalRead(buttonPin);

  Serial.print("cmdTopic= ");
  Serial.println((String(cmdTopic)));
  Serial.print("statusTopic= ");
  Serial.println((String(statusTopic)));
  Serial.print("connectName= ");
  Serial.println((String(connectName)));
  Serial.println("...............");
}



/*
 * *************** LOOP ***********
   The loop function begins with making sure that your ESP is connected to the broker.
*/
void loop() {
  myESP.loop();  //run the loop() method - this keeps the network services running

  if (!client.connected()) {
    reconnect();
  }
  if (!client.loop())
    client.connect(connectName);


#ifdef toggle
  /*
   * ******** Toggle switch attached **********
  */
  //Has the switch position changed from the last loop?
  //If yes, the save the new setting and toggle the heat.
  switchVal = digitalRead(buttonPin);
  if (switchVal != currentVal) {
    delay(500);
    switchVal = digitalRead(buttonPin);
    if (switchVal != currentVal) {   //debounce
      currentVal = switchVal;
      relayState = !relayState;
      RelayControl(relayState);

      /*    //For simultaneous control over MQTT
            if(relayState){
                client.publish("studio/heat","on");
              }else{
                client.publish("worksgop/light","off");
              }
      */

    }
  }
#endif



#ifdef pushbutton
  /*
   * ***** Momentary pushbuttom switch attached *******
  */
  //When the switch input goes low, toggle the light.
  switchVal = digitalRead(buttonPin);
  if (switchVal == LOW) {
    delay(500);
    switchVal = digitalRead(buttonPin);
    if (switchVal == LOW) {   //debounce
      relayState = !relayState;
      RelayControl(relayState);
      delay(1000);            //Delay if the button is held too long.
    }
  }
#endif

  yield();
}

And here is Kaywinnet.h

#define ssid "Kaywinnet"
#define password "806194edb8"
#define mqtt_server "192.168.1.124"
5
  • We need the complete sketch, both .h and .ino. This is a recurrent error with Arduino IDE (and UECIDE sometimes).
    – user31481
    Nov 22, 2017 at 4:44
  • Did you #include the .h file in your sketch before you used the macros?
    – Majenko
    Nov 22, 2017 at 10:38
  • Majenko- Yes. The include line is at the same place in he sketch as the internal defines. If I uncomment the include and comment the three defines right under it, compile fails. Nov 23, 2017 at 17:41
  • Why is SSID lowercased in Kaywinnet.h?
    – gone
    Dec 23, 2017 at 3:42
  • Yeti and per1234 found the error - DOH! Thanks... Dec 24, 2017 at 16:48

2 Answers 2

1

Here's the ino file:
...

#define SSID "Kaywinnet"

...

  .ssid = SSID,

...
And here is Kaywinnet.h

#define ssid "Kaywinnet"

...
The error is on the .ssid= line.

C++ is case sensitive. You changed the macro name from SSID to ssid when you moved the defines to the .h file.

0

why does it compile when I define the parameters in the sketch, and gives an error when I put them into a .h file??

easy:

1) go back to the non-fancy version where you put the strings back into the code.

2) get it to compile.

3) then extract the strings to macros.

by going through that, you will know precisely the mistakes you made.

1
  • You lost me on this one. What do you mean to 'extract the strings to macros'? Nov 23, 2017 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.