Wish MQTT broker on ESP8266...

rbghongade

Active Member
Licensed User
Longtime 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,
 

Attachments

  • uMQTTBroker.zip
    48.5 KB · Views: 481

Erel

B4X founder
Staff member
Licensed User
Longtime 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:
B4X:
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.
 

Cableguy

Expert
Licensed User
Longtime User
Interesting... Does this mean that we could have a "closed" mqtt circuit, without need of internet?
 

Cableguy

Expert
Licensed User
Longtime 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?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Download the attached zip and extract it to the Arduino libraries folder:

SS-2018-02-14_14.01.49.png


2. Run this program from Arduino IDE:
B4X:
#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:
B4X:
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(192, 168, 0, 36), 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", 1000, 0)
   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.
 

Attachments

  • uMQTTBroker.zip
    41.3 KB · Views: 445

cliv

Member
Licensed User
Longtime User
In Arduino IDE code ...
B4X:
MQTT_server_start(mqttPort, subscriptions, retained);
...must be:

B4X:
MQTT_server_start(mqttPort, max_subscriptions, max_retained_topics);
 
Top