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
#65012 Published a first proof of concept implementation of a basic MQTT Broker running on the ESP8266 at https://github.com/martin-ger/esp_mqtt.

The broker does support:
- a subset of MQTT v3.1.1 (CONNECT, DISCONNECT, SUBSCRIBE, UNSUBSCRIBE, PUBLISH, PING)
- a smaller number of clients (at least 5 have been tested) based on the config of lwip
- up to 100 topic subscriptions
- arbitrary publications
- QoS level 0

The broker does not yet support:
- username, password authentication
- QoS levels other than 0
- non-clear sessions
- retained messages
- TLS

The complete functionality is included in the mqtt directory. The broker is started by simply calling:

Code: Select allmqtt_server_start(1883);


in the user_init() function. The code can be be used in any project that is compiled using the esp-open-sdk. Thanks to Tuan PM for sharing his MQTT client library as a basis with us.

The current demo program is based on and included in the esp_wifi_repeater NAT router (without monitoring, ACLs, etc..), but it uses the router only as implementation environment. Binary firmware can be found in the firmware directory. For infos on flashing, building and using the provided demo program look into the master branch of this project (https://github.com/martin-ger/esp_wifi_repeater/).

Work is ongoing, questions, feedback, and help are welcome.
Last edited by martin_g on Sun Mar 04, 2018 7:01 pm, edited 1 time in total.
User avatar
By martin_g
#66624 The esp_uMQTT_broker has some new features:
  • it now supports retained messages and LWT
  • if contains a "LOCAL client", which means, the broker itself can publish and subscribe topics. This is meant to provide the basis for a local rule engine that can react on MQTT events, e.g. to switch GPIOs or send other messages (MQTT, HTTP,...)
  • the demo can now be compiled using the standard NONOS_SDK (or esp-open-sdk), it is no longer linked to my NAT router lib.
Sources and firmware can be found at:
https://github.com/martin-ger/esp_mqtt
User avatar
By martin_g
#68461 Last weeks have enhanced the esp_uMQTT_broker to become the central node in a small distributed IoT system.

For an IoT system typically you have some MQTT-enabled sensors and actors, an MQTT broker and some kind of a rule engine that links the events together and implements the application logic. While the sensors and actors are often ESP8266-based, the latter are typically "Mosquitto" and "Node Red" running on a Raspberry Pi. There is nothing wrong with this setup, but it requires about 50$ of invest for an IoT application probably just consisting of 3 nodes.

This is where this esp_uMQTT_broker comes into play. It implements a basic MQTT Broker and (NEW) a simple scripted rule engine with event/action statements that links together the MQTT sensors and actors. Scripts are interpreted and can be uploaded over the air - no re-flashing of the ESP required. The esp_uMQTT_broker can act as STA, as AP, or as both. It also can connect to another MQTT broker (i.e. in the cloud) and forward and rewrite topics in both directions.

Code, precompiled binaries, and a little bit more documentation are available on github: https://github.com/martin-ger/esp_mqtt

Here is a demo of a small script to give you an idea of the power of the scripting feature:
Code: Select all% Config params, overwrite any previous settings from the commandline
config ap_ssid       MyAP
config ap_password   stupidPassword
config ntp_server   1.pool.ntp.org
config mqtt_host   martinshome.fritz.box
config speed      160

% Now the initialization, this is done once after booting
on init
do
   println "MQTT Script 1.0 starting"
   subscribe local /test/#
   settimer 1 1000         % once per second
   setvar $1=0
   setvar $2=0
   setvar $3=10

% This is done each time the ESP (re-)connects to another MQTT broker
on mqttconnect
do
   println "(Re-)connected to client"
   subscribe remote $SYS/broker/time

% Now the events, checked whenever something happens

% Here a remote republish, of any local topic starting with "/test/"
on topic local /test/#
do
   publish remote $this_topic $this_data

% Print the time when published from remote
on topic remote $SYS/broker/time
do
   println "Remote time: "|$this_data

% When timer 1 expires, do some stuff
on timer 1
do
   % publish a timestamp locally
   publish local /t/time $timestamp

   % Let the LED on GPIO 2 blink
   gpio_out 2 $1
   setvar $1 = not $1

   % Count occurrences in var $2
   setvar $2=$2+1

   % And if we have reached 10, print that to the console
   if $2 = $3 then
      println "We have reached *"|$2| "* at " |$timestamp
      setvar $3=$2+10
   endif

   % Reload the timer
   settimer 1 1000

% Here a local publication once each day at noon
on clock 12:00:00
do
   publish local /t/2 "High Noon"