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?