hi all!
i have a problem in mqtt commuication. I have 2x Esp32, by MQTT they send data to nodered. Both have the same program, deferent IP address. if then work together, noe of them lost connection with MQTT broker. If only one is connect everything is OK, communication is cerrect. Esp cennect by ENC28J60Driver to lan
*
The classic Arduino WebClient Ethernet example
This sketch connects to a website (http://arduino.cc) using Ethernet.
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe, based on work by Adrian McEwen
*/
#include <EthernetESP32.h>
#include "PubSubClient.h"
//W5500Driver driver;
ENC28J60Driver driver;
//EMACDriver driver(ETH_PHY_LAN8720);
char server[] = "192.168.0.10";
const char* mqtt_server = "192.168.0.10";
#define CLIENT_ID "MQTT_2"
#define INTERVAL 1000 // 3 sec delay between publishing
EthernetClient client;
PubSubClient mqttClient(client);
const char* startMessageTopic = "Laser";
const char* startMessageTopicCzas = "Laser_Time";
unsigned long Start;
float Lenght;
float Lenght_s;
const int SensorPin = 4; // the number of the pushbutton pin
const int ledPin = 2; // the number of the LED pin
int SensorState = 0;
int lastSensorState = 0;
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 102);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
//EthernetClient client;
void callback(String topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
}
void setup() {
Serial.begin(115200);
delay(500);
while (!Serial);
Ethernet.init(driver);
// start the Ethernet connection:
Ethernet.begin(ip);
delay(1000);
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
if (Ethernet.linkStatus() == LinkON) {
Serial.println("Ethernet cable is connected!!.");
}
// try to configure using IP address instead of DHCP:
Ethernet.begin(ip);
delay(200);
Serial.println(Ethernet.localIP());
mqttClient.setCallback(callback);
mqttClient.setServer(mqtt_server, 1883);
Serial.println(F("MQTT client configured"));
}
void reconnect() {
// Loop until we're reconnected
while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
}
}
void loop() {
if (!mqttClient.connected()) {
reconnect();
}
SensorState= digitalRead(SensorPin);
if (SensorState != lastSensorState){
if (SensorState==HIGH){
digitalWrite(ledPin, HIGH);
mqttClient.publish(startMessageTopic, "false");
Serial.println(F("low"));
Lenght=((millis()-Start));
Lenght_s= Lenght*0.001;
char LenghtString[8];
dtostrf(Lenght_s, 4, 2, LenghtString);
mqttClient.publish(startMessageTopic, LenghtString);
Serial.println(LenghtString);
Serial.println(Lenght);
Serial.println(Lenght_s);
} else {
// turn LED off
digitalWrite(ledPin, LOW);
mqttClient.publish(startMessageTopic, "true");
Serial.println(F("high"));
Start=millis();
}
Dlugosc=0;
}
lastSensorState=SensorState;
delay(50);
mqttClient.loop();
}
how can i solve this problem?