Erst Mal Hallo imForum.
Ich habe hier eine Anwendung die super funktioniert. Ich möchte das Prog jedoch erweitern dass ich 2 topic abonnieren kann wie ich aber das 2. topic einbauen soll " Als C Anfänger schaffe ich das leider nicht. "
Ich hoffe meine Anfrage steht im richtigen Ort und BEDANKE MICH JETZT SCHON FÜR EURE HILFE
#include <ESP8266WiFi.h>
#include <PubSubClient.h> // WiFi
const char *ssid = "Canada"; // Enter your WiFi name
const char *password = "xxxxxx"; // Enter WiFi password// MQTT Broker
const char *mqtt_broker = "192.168.xx.xxx"; // Enter your WiFi or Ethernet IP
const char *topic1 = "solaranzeige/box1/batterie_strom";
const char *topic2 = "solaranzeige/box1/batteriespannung ";
const int mqtt_port = 1883;WiFiClient espClient;
//---------------------------------
String BatAmp = "";
//--------------------------------
PubSubClient client(espClient);void setup() { // Set software serial baud to 115200;
Serial.begin(115200); // connecting to a WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network"); //connecting to a mqtt broker
client.setServer(mqtt_broker, mqtt_port);
client.setCallback(callback);
while (!client.connected()) {
String client_id = "esp8266-client-";
client_id += String(WiFi.macAddress());
Serial.printf("The client %s connects to mosquitto mqtt broker\n", client_id.c_str());
if (client.connect(client_id.c_str())) {
Serial.println("Public emqx mqtt broker connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
// publish and subscribe
client.publish(topic1, "Hello From ESP8266!");
client.subscribe(topic1);
client.publish(topic2, "Hello From ESP8266!");
client.subscribe(topic2);
}
void callback(char *topic1, byte *payload, unsigned int length) {
Serial.print("Message arrived in topic1: ");
Serial.println(topic1);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char) payload[i]);
//----------------------------------------------
BatAmp = (BatAmp+(char) payload[i]);
//---------------------------------------------------
}
Serial.print (" test ");
float NumBatAmp = BatAmp.toFloat();
Serial.println (BatAmp);
Serial.println (NumBatAmp);
BatAmp = '\0';
Serial.println();
Serial.println(" - - - - - - - - - - - -");
}void loop() {
client.loop();
}