MQTT - someburner/esp-rfm69 GitHub Wiki

General

This project uses esp_mqtt. I haven't made an MQTT page on the HTML, so it needs to be configured in the Makefile and by editing some code in mqtt/app.c.

Configuring

First, we need to define some things in the main Makefile

1. Set MQTT_ENABLE to 1

MQTT_ENABLE = 1

2. Set MQTT_SUB_COUNT to the total number of topics you want to SUBSCRIBE to.

For example, we'll use 2.

MQTT_SUB_COUNT = 2

3. For the number of subscriptions you want, list each out with MQTT_SUB_TOPIC0 and replace 0 with 1-N where N is MQTT_SUB_COUNT.

So for example:

MQTT_SUB_TOPIC1		?= esp-rfm69-sub1
MQTT_SUB_TOPIC2		?= esp-rfm69-sub2
  1. Do steps 2 and 3 for your publish topics (not necessary, but for the sake of consistency we'll do it)
MQTT_PUB_COUNT		= 1
MQTT_PUB_TOPIC1		?= esp-rfm69-topic1
  1. Set MQTT_USE_IP
  • If MQTT_USE_IP==1, the code will use the IP address defined by DEFAULT_MQTT_IP as the IP to attempt the connection with
  • If MQTT_USE_IP==0, the code will use the domain defined by MQTT_HOST_NAME. Once the ESP gets an IP address, it will perform an nslookup on the domain, get the IP address, and initialize MQTT with that IP. You may leave both IP and DOMAIN defined. Operation will only change if MQTT_USE_IP is changed.
MQTT_USE_IP		= 0 #<--don't use the ip, instead, use the domain
DEFAULT_MQTT_IP		?= 1.2.3.4
MQTT_HOST_NAME		?= iot.eclipse.org #public broker

6. Test it out!

Using the configuration above, the ESP8266 will connect to a public MQTT broker (provided by Eclipse). To test it out, get an MQTT client (such as Mosquitto) and try publishing to the device.

Subcribe:

To test the subscribe functionality, just open up your client and execute the following with the ESP's uart also displaying:

mosquitto_pub -h iot.eclipse.org -t esp-rfm69-sub1 -m "this is a test"

Publish:

To test the publish functionality, go into mqtt/app.c and uncomment the USER_TEST_MSG define. Then subscribe to the public broker like so:

mosquitto_sub -h iot.eclipse.org -t esp-rfm69-topic1

Your MQTT client should read out the testmsg[] char array in mqtt/app.c

More info

The current configuration for MQTT is obtuse, and really just there as proof-of-concept. Don't expect it to work well. But if you need to add more topics or subs, you'll want to go into mqtt/app.c and modify the void mqtt_setup_topics() function to add topics. If your broker needs username/password to connect, change the MQTT_InitClient function in [mqtt/app.c] (around line 218) to:

MQTT_InitClient(&mqtt_client, "user_dev0", "username", "VerySecretPassword", 120, 1); //last bit sets cleanSession flag