[Submission] MQTT Library and Sample

Hi Claudio,

It fails with FQDNs, hostnames are probably fine, I haven’t tried but I wanted my device to be able to connect outside of my domain so the FQDN is most useful to me.

I have it working without an issue with the public IP, but it would be nice to use FQDNs.

Many thanks

Andy

Ok @mcinnes01 :
Which is the public URL of the server you are trying to connect to (if you can disclose it) ?

Hi @duffo64

I can’t disclose my server however this is open and using the exact same setup that I have running:

http://test.mosquitto.org/ >> tcp://test.mosquitto.org/ If you use the chrome extension MQTTlens you can subscribe and publish so you can see what is going on with your particle.

Minus the use of FQDN I’ve put together a cool little test with a universal windows 10 app running on my phone interpreting voice commands and publish to an mqtt topic. Then my photon is subscribed to the topic and processes the messages from the queue controlling the colour of the onboard led.

It would be interesting to know if you can get the FQDN working?

Many thanks

Andy

@mcinnes01

test.mosquitto.org (lean and mean) IS an FQDN, so at this point some sort of misunderstanding is around…

@duffo64

That is the same structure as the FQDN / url that is assigned for a particular port on my server. If I try to connect to my equivalent of test.mosquitto.org it fails, but if I connect to the public IP of my server everything is fine.

Many thanks

Andy

Have tried every example in this thread and I’m always getting the same error!

In file included from ../hal/src/stm32f2xx/platform_headers.h:19:0,
             from ./inc/application.h:32,
             from MQTT.cpp:2:
../hal/src/stm32f2xx/deepsleep_hal_impl.h:22:80: warning: 'externally_visible' attribute ignored [-Wattributes]
#define retained  __attribute__((externally_visible, section(".retained_user")))
                                                                                ^
MQTT.cpp:231:78: note: in expansion of macro 'retained'
bool MQTT::publish(char* topic, uint8_t* payload, unsigned int plength, bool retained) {

		../hal/src/stm32f2xx/deepsleep_hal_impl.h:22:80: error: section attribute not allowed for '<anonymous>'
 #define retained  __attribute__((externally_visible, section(".retained_user")))
                                                                                ^
	MQTT.cpp:231:78: note: in expansion of macro 'retained'
 bool MQTT::publish(char* topic, uint8_t* payload, unsigned int plength, bool retained) {
                                                                          ^
MQTT.cpp: In member function 'bool MQTT::publish(char*, uint8_t*, unsigned int, bool)':

		../hal/src/stm32f2xx/deepsleep_hal_impl.h:22:19: error: expected primary-expression before '__attribute__'
 #define retained  __attribute__((externally_visible, section(".retained_user")))
               ^
	MQTT.cpp:241:13: note: in expansion of macro 'retained'
     if (retained) {
         ^
		../hal/src/stm32f2xx/deepsleep_hal_impl.h:22:19: error: expected ')' before '__attribute__'
 #define retained  __attribute__((externally_visible, section(".retained_user")))
               ^
	MQTT.cpp:241:13: note: in expansion of macro 'retained'
     if (retained) {
         ^
make[1]: *** [../build/target/user/platform-6MQTT.o] Error 1
make: *** [user] Error 2

Error: Could not compile. Please review your code.

Hi @DanielSheard

This happened because there is a macro in deepsleep_hal_impl.h that has the same name (retained) of a parameter in the MQTT::publish method.

In MQTT.cpp rename it from

bool MQTT::publish(char* topic, uint8_t* payload, unsigned int plength, bool retained)

to

bool MQTT::publish(char* topic, uint8_t* payload, unsigned int plength, bool mqretained)

Do the same thing in the body of the method, of course.

An unfortunate case of name clash.

1 Like

Hi @DanielSheard I tried that but the same error persists.

Don’t know you, but I am not using the web IDE, only Particle Dev.
I needed only a find/replace in mqtt.cpp, recompile, flash.

Hi,
I had a working code using MQTT lib. Today I tried to compile the code without modifications, and the same error of duplicated macro appears!, so, I just fixed the name of param (retained), and everything compiled ok. The problem is after flash my Photon it goes into magenta, and my program never starts. How can I see the errors?, I’m using Particle Dev on MAC, and why my program stop working? I suspect is something related with the some kind of change in Photon online compiler or libs?
:frowning:

Forget it!, I just flash an example again from WEB UI, and then try again my full example from my local Particle Dev and it works again!

I think sometimes WEB and Local dont work properly :S

I’m having a hard time finding a source for this, but I just discovered that Particle Dev doesn’t issue a system firmware update automatically if your Photon’s system version different than your built application’s, whereas the Web IDE and CLI do take care of this for you. The result is that an update from Particle Dev can sometimes send the device into safe mode, where it breathes magenta, until a system firmware update is issued.

Working on making that work…

@Shadow6363 I managed to solve the issue and I have put a template code here. In summary what I have done was to keep checking if the Core is connected to the MQTT broker and if not, reset the WiFi connection (by turing the WiFi off then on) and try to reconnect to the broker. I have the Particle-Core running continuously for weeks now without any issue nor having to reset it.

3 Likes

Hey guys i am new to spark i have an old spark core with a relay shield. Ive managed to make it work flawlessly with blynk app but now i want to test mqtt on it to control the relays using openhab framework. I am using on my core the default spark.ino and i am using the blynk library so how do i add this mqtt library? sorry for the newbie question. Thanks in advance.

Hey kyngston, this worked good for me, but I had one issue. My messages appear on CloudMQTT, but if I leave the websocket UI and go back, they’re gone whereas the messages from owntracks on my test android device stay (or the latest update does at least). One thing I did modify, since I’m using this with the Electron, is rather than keeping the connection up, I connect, send my data, and disconnect. Then after 5 minutes, I repeat.

Was wondering if you had any idea what might cause that to happen though with the data disappearing, I can’t access it since it doesn’t stay.

Has anyone thought about porting this library to Particle?

I worked on it a little (fixing all those retain issues), but now I’m stuck with the different assumptions about the WifiClient. Maybe one of the C/C++ guys is willing to take a look?

The library itself is really stable! I tested it on the esp8266 for weeks and it runs fine.

I was trying to use MQTT Client library “https://github.com/hirotakaster/MQTT
” with Arduino IDE and encountered following error:
In file included from D:\arduino-1.6.8\libraries\MQTT\MQTT.h:57:0,
from D:\arduino-1.6.8\libraries\MQTT\MQTT.cpp:1:
C:\Users\admin\AppData\Local\Arduino15\packages\RedBear\hardware\STM32F2\0.2.9\cores\RedBear_Duo/redbear_sdk/F_wiring/inc/spark_wiring_usbserial.h:57:2: error: ‘os_mutex_recursive_t’ does not name a type
os_mutex_recursive_t get_mutex()

It got fix with following changes in spark_wiring_usbserial.h: (In case someone wants to use it)
Add following after Line 32
#if PLATFORM_THREADING
#include “concurrent_hal.h”
#include <stddef.h>
#include <mutex>
#include <functional>
#include <type_traits>
#endif /* PLATFORM_THREADING */

@subh2020, you need to post this question in the Redbear forum.

@peekay123, Thanks for pointing out and input.
I will try to repost.

1 Like