Compile error using PubSubClient

Hello! Newbie here and hoping somebody can help me....

I have a Feather ESP32 board which I am trying to use with a HX711 and 4 load cells to take weight measurements and publish to HomeAssistant using MQTT. I've previously flashed the board with my code (a while ago now) and it seemed to work fine. However, I need to make some changes to the name of the MQTT topics and when trying to recompile the code, I keep getting a "client does not name a type error". I'm a bit rusty with my coding so I'm not completely sure why this error is happening, nor how to fix it.

Code as follows:

#include <PubSubClient.h>
#include <Arduino.h>
#include <HX711.h>
#include <WiFi.h>
#include <WiFiClient.h>


HX711 scale;                          // Initiate HX711 library
WiFiClient wifiClient;                // Initiate WiFi library
PubSubClient mqttClient(wifiClient);      // Initiate PubSubClient library

//WiFi Settings
#define WIFI_NETWORK "mywifinetwork"
#define WIFI_PASSWORD "mypassowrd"
#define WIFI_TIMEOUT 20000


// MQTT Settings
char HOSTNAME[] = "bed-sensor";
//char MQTT_SERVER[] = "192.168.1.236";
char STATE_TOPIC[] = "home/bedroom/bed";
char STATE_RAW_TOPIC[] = "home/bedroom/bed/raw";
char AVAILABILITY_TOPIC[] = "home/bedroom/bed/available";
char TARE_TOPIC[] = "home/bedroom/bed/tare";
char mqtt_username[] = "homeassistant";
char mqtt_password[] = "mqttPassword";

// HX711 Pins
const int LOADCELL_DOUT_PIN = 4; // Remember these are ESP GPIO pins, they are not the physical pins on the board.
const int LOADCELL_SCK_PIN = 5;
int calibration_factor = -11000; // Defines calibration factor 

void callback(char* topic, byte* payload, unsigned int length) {
  if (strcmp(topic, TARE_TOPIC) == 0) {
    Serial.println("starting tare...");
    scale.wait_ready();
    scale.set_scale();
    scale.tare();       //Reset scale to zero
    Serial.println("Scale reset to zero");
  }
}

void setup() {
  Serial.begin(115200);
  Serial.print("Connecting to WiFi");
  //WiFi.mode(WIFI_STA);
  delay(100);
  WiFi.begin(WIFI_NETWORK, WIFI_PASSWORD);

  unsigned long startAttemptTime = millis();

  while(WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < WIFI_TIMEOUT){
    Serial.print(".");
    delay(100);
  }

  if(WiFi.status() != WL_CONNECTED){
    Serial.println("Failed connection");
  }else{
    Serial.print("Connection succesful");
  }
}

  mqttClient.setServer("192.168.1.236", 1883);                // This is where the error is thrown up
  //mqttClient.setCallback(callback);                       // Also can't get this function to work so I commented it out before; but since we're here, if anybody has any clues why this doesn't work either...!

this should be in setup or in a function, not hanging outside any code block

Welcome to the forum

  mqttClient.setServer("192.168.1.236", 1883);                // This is where the error is thrown up

That code is not in a function. It should be in setup()

Thank you both! I moved the lines of code into setup and now it compiles as expected :raised_hands:

simple :wink:

have fun

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.