MQTT Arduino Nano and Nanoethernetboard V1.0 ENC28j60

Hi there, I tested the link with this post:

in the forum.

But maybe there is something wrong, but I get no message published.

I decreased the code to a minimum, to send only one message.
I get already "-2" as RecallState when I will connect with the MQTT-Broker.

What I do before Ethernet.begin():


//// CS for the Ethernetboard, I insert it before
Ethernet.init(10);

Ethernet.begin(mac,ip);

Are there someone who had success with this example?

the Ethernet WebClient example works?

Did the IPs there actually match your network? If they didn't, post your actual code as you may have changed other things. And the code posted there is hard at the memory limit (probably the biggest problem of the ENC28J60 on the AVR platform).

https://github.com/JAndrassy/EthernetENC/wiki/Settings

Hi, here the code:

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




// Function prototypes
void subscribeReceive(char* topic, byte* payload, unsigned int length);
 
// Set your MAC address and IP address here
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 2);
 
//// Broker is on my pc (MOSQUITTO)
// Make sure to leave out the http and slashes!
const char* server = "localhost";
 
// Ethernet and MQTT related objects
EthernetClient ethClient;
PubSubClient mqttClient(ethClient);






void setup()
{

Ethernet.init(10);

  // Useful for debugging purposes
  Serial.begin(9600);
  
  // Start the ethernet connection
  Ethernet.begin(mac, ip);              
  
  // Ethernet takes some time to boot!
  delay(3000);                          
 
  // Set the MQTT server to the server stated above ^
  mqttClient.setServer(server, 1883);   
 
  // Attempt to connect to the server with the ID "myClientID"
  if (mqttClient.connect("myClientID")) 
  {
    Serial.println("Connection has been established, well done");
 
    // Establish the subscribe event
    mqttClient.setCallback(subscribeReceive);
  } 
  else 
  {
    Serial.println("Looks like the server connection failed...");
  }
}

void loop()
{
  // This is needed at the top of the loop!
  mqttClient.loop();
 
  // Ensure that we are subscribed to the topic "MakerIOTopic"
  mqttClient.subscribe("MakerIOTopic");
 
  // Attempt to publish a value to the topic "MakerIOTopic"
  if(mqttClient.publish("MakerIOTopic", "Hello World"))
  {
    Serial.println("Publish message success");
  }
  else
  {
    Serial.println("Could not send message :(");
  }
 
  // Dont overload the server!
  delay(4000);
}


void subscribeReceive(char* topic, byte* payload, unsigned int length)
{
  // Print the topic
  Serial.print("Topic: ");
  Serial.println(topic);
 
  // Print the message
  Serial.print("Message: ");
  for(int i = 0; i < length; i ++)
  {
    Serial.print(char(payload[i]));
  }
 
  // Print a newline
  Serial.println("");
}

And here the message from serial:

Looks like the server connection failed...

Could not send message :frowning:

Could not send message :frowning:

Could not send message :frowning:
.
.
.

I connected with a switch to my pc, the pc has IP 192.168.0.1,

Server "localhost" is surely wrong. It might be "localhost" on your PC, which means 127.0.0.1 but your Nano cannot connect to 127.0.0.1 because that IP isn't visible from the outside. That's only used for local, in-host connections.

I changed it back to the IP-Adress from EtherntCard of PC, disabled also the networkcable. No success.

SerialOutput ==>

Ethernet.begin(mac,ipfixed)

IPAdresse: 192.168.0.2MQTT client configured

Looks like the server connection failed...

Could not send message :frowning:

is the port open on firewall?

Firewall is deactivated.

I have also an Arduino UNO with the W5100 Ethernet shield. There is the same problem.

Post the code you used for this. As there is additional output it cannot be the posted one.

Is the IP of your PC in the same network (e.g. is it's IP address 192.168.0.x)?

Did you try to connect to the Mosquitto on the PC itself (telnet <ip address> 1883)? Post the output you get (no pictures!).

How I do the test with telnet?

With MQTTFX I can publish ans subscribe ...

On a command line. It depends on your operating system on the PC what that means. Modern OSes call it "Terminal", the legacy stuff "Command Prompt".

I guess that's an application, a link to it would help.
If you used there also "localhost" as the server name your test is useless as that's another interface and Mosquitto might still be invisible from the outside.

Link to Mqttfx:

https://softblade.de/en/mqtt-fx/

Telnet are installed, but how are the command in the cmd ?? C:\telnet 127.0.0.1 1883

??

Hi there, very late but I did some test. I played a little bit.

#include <Ethernet.h>
#include <PubSubClient.h>


#define CLIENT_ID "ArduinoMQTT"
#define TOPIC "temperature\keller"
#define PUBLISH_DELAY 5000

uint8_t mac[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
byte ipfixed[] = {192, 168, 0, 2};

//// different ways, for tests
IPAddress mqttServer(192, 168, 0, 1);
byte ipfixedMqtt[] = {192, 168, 0, 1};

EthernetClient ethClient;
PubSubClient mqttClient;


long previousMillis;

void setup() {

  // setup serial communication
  Serial.begin(9600);
  while (!Serial) {};
  Serial.println(F("MQTT Arduino Demo"));
  Serial.println();

  // setup ethernet communication using DHCP

//// I want to use static IP !!!
  Ethernet.begin(mac, ipfixed);
  
  
  // Ethernet.begin(mac)  ;

  // if(Ethernet.begin(mac) == 0) {
  //   Serial.println(F("Unable to configure Ethernet using DHCP"));
  //   for(;;);
  // }

  Serial.println(F("Ethernet configured via DHCP"));
  Serial.print("IP address: ");
  Serial.println(Ethernet.localIP());
  Serial.println();

  // setup mqtt client
  mqttClient.setClient(ethClient);
  //mqttClient.setServer(mqttServer, 1883);
  mqttClient.setServer(ipfixedMqtt, 1883);
  Serial.println(F("MQTT client configured"));


  Serial.println();
  Serial.println(F("Ready to send data"));
  previousMillis = millis();
}

void loop() {

  // it's time to send new data?
  if (millis() - previousMillis > PUBLISH_DELAY) {
    sendData();
    previousMillis = millis();
  }

  mqttClient.loop();
}

void sendData() {

  char msgBuffer[20];

  float t = 20.33;  //dht.readTemperature();
  Serial.print("Temperature: ");
  Serial.println(t);

  delay(1000);

  if (mqttClient.connect(CLIENT_ID)) {

    delay(2000);

    Serial.println("Publish the Topic:");
    // mqttClient.publish(TOPIC, dtostrf(t, 6, 2, msgBuffer));
    mqttClient.publish(TOPIC, "Hallo");
  } else {
    Serial.print("Publish doesnt work, MQTT-State: ");
    Serial.println(mqttClient.state());
  }
}

At next the serial output, for the Arduino Nano ....

MQTT Arduino Demo

Ethernet configured via DHCP
IP address: 5.0.0.0

MQTT client configured

Ready to send data
Temperature: 20.33
Publish doesnt work, MQTT-State: -2
Temperature: 20.33
Publish doesnt work, MQTT-State: -2

And now eith Arduino Uno:

MQTT Arduino Demo

Ethernet configured via DHCP
IP address: 192.168.0.2

MQTT client configured

Ready to send data
Temperature: 20.33
Publish doesnt work, MQTT-State: -2
Temperature: 20.33
Publish doesnt work, MQTT-State: -2

My Question: Why works she static IP at UNO with W5100, and not at NANO with ENC28j60 ??

  • Why I get only MQTT-State "-2"?

I tried with the CMD's on the local machine to sub and pub data, that works ==>

How I switch off the DHCP-function?

you are using static IP

Yes,

  • I use a static IP-Adress
  • the Broker is running, I did tests with the CMD-line (see screenshot)
  • one test is with Arduino Nano and Ethernetshield V1.0 (ENC28j60)
  • one test is with Arduino UNO and Ethernetshield W5100
  • I think the firewallsettings are OK

I read the the both ethernetshields are difficult. Should I take for ENC28j60 the Lib "EnthernetENC.h" ??

But in first step for me:
==> Why I can't communicate with the Mosquitto Broker ? My system is Windows 10. Who can give me advice?

Are there problems with protocol, or could PubSub for arduino not communicate with Mosquitto?
Or is somewhere a hidden setting (Mosquitto or controller) ??

Has somewhere an hint or an idea? Or is there some more debuginformation possible, to find out what are the problems?

About the connection:

  • I set my ethernetconnection (adapter) to a static IP (192.168.0.1, Subnet: 255.255.255.0, Gateway: 192.168.0.1)
  • Controller should have static IP 192.168.0.2

PubSubClient communicates perfectly well with Mosquitto so that is not the problem

both shields and libraries are OK. there is nothing difficult.
try the basic examples. the WebClient example and the WebServer example

OK, I try both with both shields and post it.

You have to use the external IP for this command to make any sense. So forget the 127.0.0.1 for everything that should work also outside that machine.
The command is

telnet <external IP address> 1883

I heard that Microsoft had the horrible idea to remove telnet from newer OS releases, so I hope your PC still has it.

I guess you used 127.0.0.1 there too. That doesn't check an outside call.