Zum Inhalt springen

MQTT über Ethernet Shield W5100

Projekte und Tutorials für Arduino Foren StartHardware-Forum MQTT über Ethernet Shield W5100

Schlagwörter: 

  • Dieses Thema hat 7 Antworten und 2 Teilnehmer, und wurde zuletzt aktualisiert vor 4 Wochen, 1 Tag von Gina wyllie.
Ansicht von 8 Beiträgen - 1 bis 8 (von insgesamt 8)
  • Autor
    Beiträge
  • #6706 Antworten
    JanS
    Teilnehmer

    Hallo zusammen, ich versuche im Moment mein Arduino Uno, mithilfe eines W5100, mit einem Mosquitto Broker auf meinem Windows Pc zu verbinden. Wenn ich aber versuche die Verbindung aufzubauen erscheint nur ein Verbindungsfehler (state: -2). Vielleicht weiß ja einer von euch, was für ein Fehler ich bei den IP Adressen gemacht habe und welche ich verwenden muss. Vielen Dank im Vorraus
    Mein Code:
    #include <SPI.h>
    #include <Ethernet.h>
    #include <PubSubClient.h>
    #include <DHT.h>

    #define ARDUINO_CLIENT_ID “arduino_1” // Client ID for Arduino pub/sub
    #define PUB_TEMP “arduino_1/sensor/temperature_celsius” // MTTQ topic for temperature [C]
    #define PUB_HUMID “arduino_1/sensor/humidity” // MTTQ topic for humidity
    #define SUB_LED “arduino_1/led” // MTTQ topic for LED
    #define PUBLISH_DELAY 3000 // Publishing delay [ms]

    // Hardware setup details
    const int ledPin = 3;
    const int sensorPin = 5;
    const int sensorType = DHT11;

    // Networking details
    byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; // Ethernet shield (W5100) MAC address
    IPAddress ip(192, 168, 0, 106); // Ethernet shield (W5100) IP address
    IPAddress server(192,106,0,102); // MTTQ server IP address

    DHT dht(sensorPin, sensorType);
    EthernetClient ethClient;
    PubSubClient client(ethClient);

    long previousMillis;

    void setup()
    {
    Serial.begin(9600);

    // LED off
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, LOW);

    // MTTQ parameters
    client.setServer(server, 1883);
    client.setCallback(callback);

    // Ethernet shield configuration
    Ethernet.begin(mac, ip);

    delay(1500); // Allow hardware to stabilize

    previousMillis = millis();
    }

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

    if (millis() – previousMillis > PUBLISH_DELAY)
    {
    previousMillis = millis();
    float humidity = dht.readHumidity(); // humidity
    float tempC = dht.readTemperature(); // temperature [C]
    char tmpBuffer[20];

    // check if any reads failed and exit early (to try again).
    if (isnan(humidity) || isnan(tempC))
    {
    Serial.println(“error reading sensor data”);
    return;
    }
    else
    {
    Serial.print(“[sensor data] temperature[C]: “);
    Serial.print(tempC);
    Serial.print(“, humidity: “);
    Serial.println(humidity);

    client.publish(PUB_TEMP, dtostrf(tempC, 6, 2, tmpBuffer));
    client.publish(PUB_HUMID, dtostrf(humidity, 6, 2, tmpBuffer));
    }
    }

    client.loop();
    }

    void reconnect()
    {
    // Loop until reconnected
    while (!client.connected()) {
    Serial.print(“Attempting MQTT connection … “);
    // Attempt to connect
    if (client.connect(ARDUINO_CLIENT_ID)) {
    Serial.println(“connected”);
    // (re)subscribe
    client.subscribe(SUB_LED);
    } else {
    Serial.print(“Connection failed, state: “);
    Serial.print(client.state());
    Serial.println(“, retrying in 5 seconds”);
    delay(5000); // Wait 5 seconds before retrying
    }
    }
    }

    // sub callback function
    void callback(char* topic, byte* payload, unsigned int length)
    {
    Serial.print(“[sub: “);
    Serial.print(topic);
    Serial.print(“] “);
    char message[length + 1] = “”;
    for (int i = 0; i < length; i++)
    message[i] = (char)payload[i];
    message[length] = ‘\0’;
    Serial.println(message);

    // SUB_LED topic section
    if (strcmp(topic, SUB_LED) == 0)
    {
    if (strcmp(message, “on”) == 0)
    digitalWrite(ledPin, HIGH);
    if (strcmp(message, “off”) == 0)
    digitalWrite(ledPin, LOW);
    }
    }

    #6707 Antworten
    Stefan Hermann
    Administrator

    Hm, komplexes Problem, aber kann es sein, dass du dich einfach bei der IP vertippt hast? Also statt:

    IPAddress ip(192, 168, 0, 106); // Ethernet shield (W5100) IP address
    IPAddress server(192,106,0,102); // MTTQ server IP address

    Das hier verwenden:

    IPAddress ip(192, 168, 0, 106); // Ethernet shield (W5100) IP address
    IPAddress server(192,168,0,102); // MTTQ server IP address

    Kommt mir komisch vor, dass die IP vom Server 192.106.0.102 sein soll. Ich hoffe, das war es, sonst wird es schwierig :-)

    #17406 Antworten
    goldsmithw1107
    Gast

    By guaranteeing top-notch repair service,O General ac installation dubai is able to supply and install O General air conditioning units for the lowest possible prices, keeping customers comfortable and confident. Temperatures may be easily controlled all year long by installing O General Systems that can both heat and cool an area. It is possible to install suitable digital displays and wall panels to regulate the AC heat in businesses. We install O General AC systems throughout Dubai.

    #17407 Antworten
    goldsmithw1107
    Gast

    <div class=”cent_text content-main content-1″ data-username=”Anonymous31950301″>By guaranteeing top-notch repair service,O General ac installation dubai is able to supply and install O General air conditioning units for the lowest possible prices, keeping customers comfortable and confident. Temperatures may be easily controlled all year long by installing O General Systems that can both heat and cool an area. It is possible to install suitable digital displays and wall panels to regulate the AC heat in businesses. We install O General AC systems throughout Dubai.</div>

    #17619 Antworten
    LucasBaker
    Gast

    Check out this https://scamfighter.net/review/unemployedprofessors.com link and find out why some writing services are unlikely to be number one on your list. Just check this site!

    #18670 Antworten
    Marfish
    Gast

    kgj

    #26923 Antworten
    Wawwich
    Gast

    I think that this post is very difficult to understand. Therefore, instead of reading this post, I decide to play games in fnaf games. How about you?

    #31427 Antworten
    Gina wyllie
    Gast

    For businesses looking to make a bold statement and leave a lasting impression, SignFreak offers the expertise, creativity, and professionalism needed to bring their vision to fruition. https://signfreaks.com/

Ansicht von 8 Beiträgen - 1 bis 8 (von insgesamt 8)
Antwort auf: Antwort #6707 in MQTT über Ethernet Shield W5100
Deine Information: