Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Announcements
  3. 💬 Building a MQTT Gateway

💬 Building a MQTT Gateway

Scheduled Pinned Locked Moved Announcements
81 Posts 30 Posters 44.8k Views 28 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    linkinpio
    wrote on last edited by
    #81

    I think I'm ussing the latest one
    d8876975-0cfc-4787-b700-255b1eccec88-image.png
    but as you said I don't think it will fix my issue though.
    But looking at the examples I found that they have a connect function which then can be used to reconnect whenever needed not constantly like it happens now,

    But not sure how I could integrate that functionality in this sketch though, as I'm not that experienced with programming :/
    To you think it will be possible?

    /*
     Reconnecting MQTT example - non-blocking
    
     This sketch demonstrates how to keep the client connected
     using a non-blocking reconnect function. If the client loses
     its connection, it attempts to reconnect every 5 seconds
     without blocking the main loop.
    
    */
    
    #include <SPI.h>
    #include <Ethernet.h>
    #include <PubSubClient.h>
    
    // Update these with values suitable for your hardware/network.
    byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
    IPAddress ip(172, 16, 0, 100);
    IPAddress server(172, 16, 0, 2);
    
    void callback(char* topic, byte* payload, unsigned int length) {
      // handle message arrived
    }
    
    EthernetClient ethClient;
    PubSubClient client(ethClient);
    
    long lastReconnectAttempt = 0;
    
    boolean reconnect() {
      if (client.connect("arduinoClient")) {
        // Once connected, publish an announcement...
        client.publish("outTopic","hello world");
        // ... and resubscribe
        client.subscribe("inTopic");
      }
      return client.connected();
    }
    
    void setup()
    {
      client.setServer(server, 1883);
      client.setCallback(callback);
    
      Ethernet.begin(mac, ip);
      delay(1500);
      lastReconnectAttempt = 0;
    }
    
    
    void loop()
    {
      if (!client.connected()) {
        long now = millis();
        if (now - lastReconnectAttempt > 5000) {
          lastReconnectAttempt = now;
          // Attempt to reconnect
          if (reconnect()) {
            lastReconnectAttempt = 0;
          }
        }
      } else {
        // Client connected
    
        client.loop();
      }
    
    }
    
    
    1 Reply Last reply
    0

    Hello! It looks like you're interested in this conversation, but you don't have an account yet.

    Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

    With your input, this post could be even better 💗

    Register Login
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    6

    Online

    12.1k

    Users

    11.2k

    Topics

    113.4k

    Posts


    Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • MySensors
    • OpenHardware.io
    • Categories
    • Recent
    • Tags
    • Popular