0

While trying to setup an ESP32 as mqtt publisher I am getting strange errors. Here is my code:

#include "mqttSetup.h"
#include <WiFi.h>
#include <PubSubClient.h>

static byte httpFailCounter = 0;
byte counter = 0;

const char *ssid = "SSID";
// const char *password = "xxxxx";
const char *passphrase = "xxxxx";

WiFiClient espClient;

const char *mqtt_server = "192.168.1.39";
const int mqttPort = 1883;
const char *mqttUser = "xxxx";
const char *mqttPassword = "xxxxx";

PubSubClient pubClient(espClient);

pubClient.setServer(mqtt_server, mqttPort); 

void checkConnection()
{
    if (WiFi.status() == WL_CONNECTED)
    {
        counter = 0; //reset counter
    }
    else if (WiFi.status() != WL_CONNECTED)
    {
        WiFi.begin(ssid, passphrase);
    }
    while (WiFi.status() != WL_CONNECTED)
    {
        delay(500);
        Serial.print(".");

        counter++;
        if (counter >= 60)
        {
            ESP.restart();
        }
    }
}

void reconnect()
{
 
    while (!pubClient.connected())
    {
        Serial.print("Attempting MQTT connection...");
        // Create a random client ID
        const char *clientID = "ESP32D-MQTT";
        // Attempt to connect
        if (pubClient.connect(clientID))
        {
            Serial.println("connected");
            // Once connected, publish an announcement...
            pubClient.publish("outTopic", "Hello from ESP");
            // ... and resubscribe
            //   client.subscribe("inTopic");
        }
        else
        {
            Serial.print("failed, rc=");
            Serial.print(pubClient.state());
            Serial.println(" try again in 5 seconds");
            // Wait 5 seconds before retrying
            delay(5000);
        }
    }
}

I don't understand why the compiler is spitting out the following two errors:

this declaration has no storage class or type specifier (referring to my pubClient declaration)

expected a ';'

It doesn't have any issue when I declare the pubClient but at the line :

pubClient.setServer(mqtt_server, mqttPort);

where I use the pubClient it gives the error.

Besides I don't see any ; missing in the code.

Could you please help me figure out the source of error?


Header file for this code chunk

#ifndef MQTTSETUP_H
#define MQTTSETUP_H



void checkConnection();
void reconnect();


#endif

------------ Main sketch---------

#include <Arduino.h>
#include "Wire.h"
#include "tempSensor.h"
#include "mqttSetup.h"




void setup()
{
  Wire.begin();

  Serial.begin(115200);
  while (!Serial)
    ;

  shtBegin();
  checkConnection();
}

void loop()
{
  uint32_t newTime = millis();
  static uint32_t oldTime = 0;
  static uint32_t t2 = 0;

  float temp = 0.0;
  float humid = 0.0;

  char senVals[50];

  if (newTime - oldTime >= 10000)
  {
    oldTime = newTime;

    readsht(temp, humid);
    snprintf(senVals, 50, "Temp: %.2f Humid: %.2f", temp, humid);
    Serial.println(senVals);

  }
}
0
0

Code goes in functions, not the global scope.

Your line

pubClient.setServer(mqtt_server, mqttPort);

should be inside a function. Most probably setup() so it gets called when your program starts.

Incidentally - where are setup() and loop() - the two functions that are required by Arduino code?

5
  • This code chunk is contained in a separate .cpp file. I was trying to keep the main sketch clean from setup and function definitions for different components. That's why you don't see the main sketch. I intended to call only a publish function(not yet implemented in my code) in the main sketch. I will update my post with the header file and the main sketch
    – Zaffresky
    Jan 22 at 14:18
  • @Zaffresky Then you should wrap that line in a function which you can then call from setup().
    – Majenko
    Jan 22 at 14:19
  • I have updated my post with main sketch and the relevant header file. I don't fully understand why I should put the line "PubClient.setServer(mqtt_server, mqttPort);" in a new function. My understanding was that the PubClinet.serServer function is already defined in the PubSubClient library and in my separate mqttSetup.cpp file I was calling this function so that this file would be self-contained and I wouldn't need to call any PubSubClient library functions directly in the main sketch. Rather I would only call my function publishMQTT() in the main sketch. Is this approach wrong?
    – Zaffresky
    Jan 22 at 14:30
  • 1
    @Zaffresky Because code goes in functions not in the global scope. Anything you call during the sequence of the execution of your program must go in a function. The only things that can go in the global scope are variable declarations (with or without an immediate assignment).
    – Majenko
    Jan 22 at 14:41
  • I see it now. I was not considering the scope in that seperate .cpp file. I mistakenly assumed that the concept of scope was applicable only to the main sketch. Thanks for clarifying
    – Zaffresky
    Jan 22 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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