A place users can post their projects. If you have a small project and would like your own dedicated place to post and have others chat about it then this is your spot.

User avatar
By martin_g
#74360 Just use the provided sample script and replace the WiFi client initialization with the SoftAP init:

If you are looking for pre-built (and a far more powerful) MQTT Broker of the ESP,you might consider the other project: https://github.com/martin-ger/esp_mqtt

Code: Select all/*
 * uMQTTBroker demo for Arduino
 *
 * The program starts a broker, subscribes to anything and publishs a topic every second.
 * Try to connect from a remote client and publish something - the console will show this as well.
 */

#include <ESP8266WiFi.h>

#include "uMQTTBroker.h"

/* Set these to your desired credentials. */
const char *ssid = "ESPap";
const char *password = "thereisnospoon";

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

void data_callback(uint32_t *client /* we can ignore this */, const char* topic, uint32_t topic_len, const char *data, uint32_t lengh) {
  char topic_str[topic_len+1];
  os_memcpy(topic_str, topic, topic_len);
  topic_str[topic_len] = '\0';

  char data_str[lengh+1];
  os_memcpy(data_str, data, lengh);
  data_str[lengh] = '\0';

  Serial.print("received topic '");
  Serial.print(topic_str);
  Serial.print("' with data '");
  Serial.print(data_str);
  Serial.println("'");
}

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

  Serial.print("Configuring access point...");
  /* You can remove the password parameter if you want the AP to be open. */
  WiFi.softAP(ssid, password);

  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);

/*
 * Register the callback
 */
  MQTT_server_onData(data_callback);

/*
 * Start the broker
 */
  Serial.println("Starting MQTT broker");
  MQTT_server_start(mqttPort, max_subscriptions, max_retained_topics);

/*
 * Subscribe to anything
 */
  MQTT_local_subscribe((unsigned char *)"#", 0);
}

int counter = 0;

void loop()
{
  String myData(counter++);

/*
 * Publish the counter value as String
 */
  MQTT_local_publish((unsigned char *)"/MyBroker/count", (unsigned char *)myData.c_str(), myData.length(), 0, 0);
 
  // wait a second
  delay(10000);
}
User avatar
By kaszanka
#76630 Hi everybody!

I have one question in matter of uMqttBroker. I'm setting up my esp8266 as softAP and running uMqttBroker on it. Next I'm connecting to esp8266 with my smartphone and connecting to broker with "IoT MQTT Panel" app. In that scenario everything works good. I'm recieving messages on my esp8266.

After above test, I wanted to do something else. I'm running softAP and uMqttBroker on esp8266. I want to connect to that broker but using PubSubClient library runing on exactly the same esp8266 - then I receive error during connection with rc=-2 : MQTT_CONNECT_FAILED - the network connection failed.

The reason I want to do something like that is only one. During normal operation my device uses PubSubClient to connect to MQTTbroker somewhere in the internet. I have written quite long callback function for that. I wanted to use uMqttBroker library only in case, if there is no internet connection. Then esp start softAP and uMqttBroker and i'm able to communicate with it. If i manage to connect to that uMqttBroker using PubSubClient then I will be able to handle mqtt messages exactly the same as i use internet broker. I dont want to double callback functions for PubSubClient and uMqttBroker, because if I want to change something there will be a need to do it in two places. Second thing are differences between arguments
uMqttBroker: void data_callback(uint32_t *client /* we can ignore this */, const char* topic, uint32_t topic_len, const char *data, uint32_t lengh)
PubSubClient: void callback(char* topic, byte* payload, unsigned int len)

Is there a chance to connect with that uMqttBroker using PubSubClient on the same esp8266 module?
User avatar
By martin_g
#76716 I would see two options:

The hard (in terms of configuration) and expensive (in terms of RAM) way: installing my open-lwip with loopback support.

The quick way: Why not calling the pubsub callback function from the uMQTTBroker callback? Converting the params should be straight forward.
User avatar
By kaszanka
#76756
martin_g wrote:The quick way: Why not calling the pubsub callback function from the uMQTTBroker callback? Converting the params should be straight forward.


I came up with that idea after adding post and I did that :) But I have another question: Is there any function to stop uMqttBroker?