knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.83k stars 1.47k forks source link

2 ESP32 publish on one broker #801

Open BigHitRider93 opened 3 years ago

BigHitRider93 commented 3 years ago

I got 2 ESP32 controllers with some sensors (one for inside the other one for outside). On both is the same programm. How do I have to change the client settings? My code is attached below.

Thank you for the help.

`#include

include

include

include

include

include

include

define SEALEVELPRESSURE_HPA (1013.25)

const char ssid = "RaspberryMQTT"; const char password = "***";

const char* mqtt_server = "192.168.178.60";

WiFiClient espClient; PubSubClient client(espClient); long lastMsg = 0; char msg[50]; char numBuffer [50];

float temperatur = 0; float druck = 0; float luftFeuchte = 0; float hoehe = 0;

char temperaturChar[10]; char druckChar[10]; char luftFeuchteChar[10]; char hoeheChar[10];

Adafruit_BME280 bme; // I2C

void setup() { // put your setup code here, to run once: Serial.begin(115200); bme.begin(0x76);

setup_wifi(); client.setServer(mqtt_server, 1883); }

void setup_wifi() { delay(10); Serial.println(); Serial.println("Connecting to "); Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); if (millis() > 10000) { Serial.println("Verbindung fehlgeschlagen"); ESP.restart(); }

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP adress: ");
Serial.println(WiFi.localIP());

} }

void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect("ESP8266Client")) { Serial.println("connected"); // Subscribe client.subscribe("esp32/output"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } }

}

void loop() {

if (!client.connected()) { reconnect(); } client.loop();

long now = millis(); if (now - lastMsg > 5000) { lastMsg = now;

temperatur = bme.readTemperature();
druck = bme.readPressure();
luftFeuchte = bme.readHumidity();
hoehe = bme.readAltitude(SEALEVELPRESSURE_HPA);

Serial.println(temperatur);

itoa(temperatur, temperaturChar, 10);
itoa(druck, druckChar, 10);
itoa(luftFeuchte, luftFeuchteChar, 10);
itoa(hoehe, hoeheChar, 10);

client.publish("esp32/außentemperatur", temperaturChar);
client.publish("esp32/außendruck", druckChar);
client.publish("esp32/außenluftFeuchte", luftFeuchteChar);
client.publish("esp32/außenhoehe", hoeheChar);

} }

// put your main code here, to run repeatedly:

nambabwe commented 3 years ago

In your void reconnect( ) function, you have a line if (client.connect("ESP8266Client")) { You would want to use two different names here, like "ESP8266ClientInside " and "ESPClientOutside" So the Broker/Server sees two different clients.

GM

On Tue, Dec 22, 2020 at 10:52 AM BigHitRider93 notifications@github.com wrote:

I got 2 ESP32 controllers with some sensors (one for inside the other one for outside). On both is the same programm. How do I have to change the client settings? My code is attached below.

Thank you for the help.

`#include

include

include

include

include

include

include

define SEALEVELPRESSURE_HPA (1013.25)

const char ssid = "RaspberryMQTT"; const char password = "***";

const char* mqtt_server = "192.168.178.60";

WiFiClient espClient; PubSubClient client(espClient); long lastMsg = 0; char msg[50]; char numBuffer [50];

float temperatur = 0; float druck = 0; float luftFeuchte = 0; float hoehe = 0;

char temperaturChar[10]; char druckChar[10]; char luftFeuchteChar[10]; char hoeheChar[10];

Adafruit_BME280 bme; // I2C

void setup() { // put your setup code here, to run once: Serial.begin(115200); bme.begin(0x76);

setup_wifi(); client.setServer(mqtt_server, 1883); }

void setup_wifi() { delay(10); Serial.println(); Serial.println("Connecting to "); Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); if (millis() > 10000) { Serial.println("Verbindung fehlgeschlagen"); ESP.restart(); }

Serial.println("");

Serial.println("WiFi connected");

Serial.println("IP adress: ");

Serial.println(WiFi.localIP());

} }

void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect("ESP8266Client")) { Serial.println("connected"); // Subscribe client.subscribe("esp32/output"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } }

}

void loop() {

if (!client.connected()) { reconnect(); } client.loop();

long now = millis(); if (now - lastMsg > 5000) { lastMsg = now;

temperatur = bme.readTemperature();

druck = bme.readPressure();

luftFeuchte = bme.readHumidity();

hoehe = bme.readAltitude(SEALEVELPRESSURE_HPA);

Serial.println(temperatur);

itoa(temperatur, temperaturChar, 10);

itoa(druck, druckChar, 10);

itoa(luftFeuchte, luftFeuchteChar, 10);

itoa(hoehe, hoeheChar, 10);

client.publish("esp32/außentemperatur", temperaturChar);

client.publish("esp32/außendruck", druckChar);

client.publish("esp32/außenluftFeuchte", luftFeuchteChar);

client.publish("esp32/außenhoehe", hoeheChar);

} }

// put your main code here, to run repeatedly:

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/knolleary/pubsubclient/issues/801, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADEX2XXHCM6Y7AEYTXVI7TLSWDFGRANCNFSM4VF4EJNA .

BigHitRider93 commented 3 years ago

Thank you very much! Now I can see both measurements.