New User MQTT help

Hello,

does anyone have a code thing to do a hello world to a local mqtt server?

i've been searching for 2 days now with no luck on this little board.

thanks a ton!

Have you looked at the PubSubClient library ?

yes, but can't get any of them to work. complete and udder newb here. i've been using pi zeros to transmit bme280 sensor data to my local mqtt server i setup last week. before that i've been using rrdtool to log sensor data and crontab jobs to update a local website for tacking house tempatures and stuff. but i'm getting annoyed at pi for randomly freezing. figured arduino that have single propose would do a better job.

What are the symptoms of them failing ?

they all have import ethernet, i'm on wireless

#include <SPI.h>
#include <WiFiNINA.h>
#include <PubSubClient.h>

WiFiClient espClient;
PubSubClient client(espClient);

#include "arduino_secrets.h" 
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS;     // the WiFi radio's status

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // you're connected now, so print out the data:


}

void loop() {
  // check the network connection once every 10 seconds:
  delay(15000);
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ArduinoSensor1";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("ArduinoSensor1", "hello world");
      // ... and resubscribe
      client.subscribe("greenBottles/#");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

here is what i'm trying, but don't see anything in the debug screen. i don't even know if i'm doing this right.

Please start by posting your code in the recommended way to make it easier to copy for examination

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

If you get errors when compiling please copy them from the IDE using the "Copy error messages" button and paste the clipboard here in code tags

Which Arduino board are you using ?

i'm using an arduino nano 33 iot

the code a posted has no errors, it's a example from the "WiFININA" libary. i've been trying to assemble the mqtt stuff to it so i can get the debug window in node red to see "hello world"

more of a prof of concept before i remove one of my sensors and try playing with that.

Welcome to the forum.

One issue is that you are not setting a MQTT server/broker. Have a look at the PubSubClient examples.

client.setServer(server, 1883); // server is IP address of your MQTT broker