Wish MQTT broker on ESP8266...

Discussion in 'B4R Bugs & Wishlist' started by rbghongade, Feb 14, 2018.

  1. rbghongade

    rbghongade Active Member Licensed User

    Dear friends,
    Here is a library for MQTT broker on ESP8266:
    https://github.com/martin-ger/uMQTTBroker
    Can we have the same with B4R (wrapping the given library?)
    This will eliminate the requirement of having a broker service running on some other device like Raspberry Pi or PC.
    Have tried the example with Wemos D1 R2 and it works great!
    Arduino library attached!
    regards,
     

    Attached Files:

  2. Erel

    Erel Administrator Staff Member Licensed User

    This is an interesting library. I tried to call it from B4R with inline C and it didn't work. I wasn't able to find why it fails.

    It returns true from MQTT_server_start:
    Code:
    Sub Process_Globals
       
    Public Serial1 As Serial
       
    Private wifi As ESP8266WiFi
    End Sub

    Private Sub AppStart
       Serial1.Initialize(
    115200)
       
    Log("AppStart")
       
    If wifi.Connect("dlink") = False Then
           
    Log("Failed to connect to network.")
           
    Return
       
    End If
       
    Log("My ip: ", wifi.LocalIp)
       RunNative(
    "StartBroker"1883)
    End Sub


    #if C
    #include "uMQTTBroker.h"
    void StartBroker (B4R::Object* o) {
      Serial.println("Starting MQTT broker");
    Serial.println(MQTT_server_start(1883, 30, 30));
    }
    #End If
    However I'm unable to connect other clients. The original example does work. I guess that there is a conflict somewhere which I haven't found.
     
  3. Cableguy

    Cableguy Expert Licensed User

    Interesting... Does this mean that we could have a "closed" mqtt circuit, without need of internet?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    That was possible before if you run the broker on an Android device or on a B4J platform.

    I forgot to write that this library is interesting even without a wrapper. Add another ESP8266 and run the broker on it. You don't really need to program it.
     
    Peter Simpson likes this.
  5. Cableguy

    Cableguy Expert Licensed User

    Yes, but I meant, nothing else than "microcontroler2microcontroler", using only B4R for it.

    What I mean, in a real world app, an esp2866 connecting to wan through WiFi, and using Bluetooth and mqtt for LAN Comms, no extras like raspberry pi, old laptop etc..
    With this, this scenario is possible, right?
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    1. Download the attached zip and extract it to the Arduino libraries folder:

    [​IMG]

    2. Run this program from Arduino IDE:
    Code:
    #include <ESP8266WiFi.h>

    #include "uMQTTBroker.h"

    /*
     * Your WiFi config here
     */
    char ssid[] = 
    "dlink";      // your network SSID (name)
    char pass[] = 
    "MyPassword";   // your network password


    unsigned int mqttPort = 
    1883;       // the standard MQTT broker port
    unsigned int max_subscriptions = 
    30;
    unsigned int max_retained_topics = 
    30;

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

      
    Serial.print("Connecting to ");
      
    Serial.println(ssid);
      WiFi.begin(ssid, pass);
     
      
    while (WiFi.status() != WL_CONNECTED) {
        delay(
    500);
        
    Serial.print(".");
      
    }
      Serial.println("");
     
      Serial.println("WiFi connected");
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());

      Serial.println("Starting MQTT broker");
      MQTT_server_start(mqttPort, subscriptions, retained);
    }

    void loop()
    {
    }
    This will be the broker.
    You can connect other clients with:
    Code:
    Sub Process_Globals
       
    Public Serial1 As Serial
       
    Private wifi As ESP8266WiFi
       
    Private mqtt As MqttClient
       
    Private socket As WiFiSocket
    End Sub

    Private Sub AppStart
       Serial1.Initialize(
    115200)
       
    Log("AppStart")
       
    If wifi.Connect("dlink") = False Then
           
    Log("Failed to connect to network.")
           
    Return
       
    End If
       mqtt.Initialize(
    socket.Stream, Array As Byte(192168036), 1883, _
           
    "esp2""mqtt_MessageArrived""mqtt_Disconnected")
       Connect(
    0)
    End Sub

    Sub Connect(u As Byte)
       
    Log("Connecting...")
       
    If mqtt.Connect Then
           
    Log("connected")
           
    Log(mqtt.Subscribe("#"0))
       
    Else
           CallSubPlus(
    "Connect"10000)
       
    End If
    End Sub

    Sub mqtt_Disconnected
       Connect(
    0)
    End Sub

    Sub mqtt_MessageArrived (Topic As String, Payload() As Byte)
       
    Log("Message arrived: ", Topic, " ", Payload)
    End Sub
    Make sure to use a different id for each client and change the ip address as needed.
     

    Attached Files:

    Cableguy likes this.
  7. Cableguy

    Cableguy Expert Licensed User

    Wow!!! That was fast!!
     
  8. cliv

    cliv Member Licensed User

    In Arduino IDE code ...
    Code:
    MQTT_server_start(mqttPort, subscriptions, retained);
    ...must be:

    Code:
    MQTT_server_start(mqttPort, max_subscriptions, max_retained_topics);
     
Loading...