MQTT XDK_Live
Answer
6/5/18 2:42 PM

Hello,

 

I have been working  with XDK Live programming language, and so far I haven´t found so much support as while working on C. I have been trying to reproduce some programs under this programming language, however I am facing a new problem on the connectivity of the device with the Wlan and MQTT.

 

Let's start with the CODE:

// The following code belongs to the main package
package main;
// The XDK110 is the device used
import platforms.xdk110;

// Create a wireless connection named 'wireless'
setup wireless : WLAN {
	ssid = "Wlan";
	psk = "password";
	useDHCP = true;
}

// Create a MQTT instance
setup messaging : MQTT {
	transport = wireless;
	url = "141.99.139.173:1883";
	cleanSession = true;
	clientId = "IPEM";
	var Test = topic("ipem/xdk",1);	
}

// When button one is pressed, send text via MQTT
every button_one.pressed {
	messaging.Test.write("Test Write");
}

// Read and transmit the temperature every 5 seconds
every 5 seconds {
	var myTemperature = environment.temperature.read();
	messaging.Test.write(`Temp: ${myTemperature}`);
}

// Read and transmit the barometric pressure every 15 seconds
every 15 seconds {
	var myPressure = environment.pressure.read();
	messaging.Test.write(`Press: ${myPressure}`);
}


On the previous code I have encountered that I neither can connect with the WLAN. If I try to run just that part of the code, it will never shows on my router that the XDK is connected. Edit: The password on the router is type WPA2-PSK.

Leaving aside the fact that I can't connect to the Wlan, it also shows that my definition while setting up the MQTT presents some problems also. The error is the following " Illegal character in scheme name at index 0: 141.99.139.173:1883 - See error log for details".

My MQTT broker is based on "mosquitto" running on a raspberry pi 3. This service is proved to work (based on some test I did with other computers). It is on the same Wlan with the IP described on the CODE, however, it looks that the way how I define the ulr is not precisely correct.

Additionally, I have a password on my MQTT, however I do not see any field to set it while using the commands for setup messaging : MQTT.

I would appreciate if someone could give me a hand on this as to make it work as expected.

Thank you in advance.

Sincerely

Alejandro Perez

 

0 (0 Votes)
RE: MQTT XDK_Live
Answer
6/5/18 2:44 PM as a reply to Alejandro Perez.

I solved the problem with the MQTT changing this on the code. I made a mistake on the setup:

// Create a MQTT instance
setup messaging : MQTT {
	transport = wireless;
	url = "mqtt://141.99.139.173:1883";
	cleanSession = true;
	clientId = "IPEM";
	var Test = topic("ipem/xdk",1);	
}

I still do not know exactly where to set the password.

 

The problem connecting with the Wlan is still happening. However, now I do not get any error of compilator.

0 (0 Votes)
RE: MQTT XDK_Live
Answer
6/5/18 2:51 PM as a reply to Alejandro Perez.

Update:

I restored and rebooted the PC. Now it works. Attached is the code.

// The following code belongs to the main package
package main;
// The XDK110 is the device used
import platforms.xdk110;

// Create a wireless connection named 'wireless'
setup wireless : WLAN {
	ssid = "wlan";
	psk = "password";
	useDHCP = true;
}

// Create a MQTT instance
setup messaging : MQTT {
	transport = wireless;
	url = "mqtt://192.168.43.107:1883";
	cleanSession = true;
	clientId = "IPEM";
	var Test = topic("dev/test",1);	
}

// When button one is pressed, send text via MQTT
every button_one.pressed {
	messaging.Test.write("Test Write");
}

// Read and transmit the temperature every 5 seconds
every 5 seconds {
	var myTemperature = environment.temperature.read();
	messaging.Test.write(`Temp: ${myTemperature}`);
}

// Read and transmit the barometric pressure every 15 seconds
every 15 seconds {
	var myPressure = environment.pressure.read();
	messaging.Test.write(`Press: ${myPressure}`);
}


 

0 (0 Votes)
RE: MQTT XDK_Live
Answer
6/5/18 3:35 PM as a reply to Alejandro Perez.
Hello Alejandro,

I have tested the code with the same setup as you do (mosquitto broker on Raspberry Pi 3, and subscribing to the topic using mosquitto_sub) after changing the MQTT url and the WiFi credentials and it works properly. I assume you have used the correct SSID and Password for WiFi.

Do note that, if you only implement the WiFi resource, but do not use it in the context of MQTT, then the WiFi will never be used, hence WiFi is not implemented in the generated code.

Regarding the username and the password for the broker, unfortunately, these two configuration-items are not implemented yet. But, to amend that in the generated C code, you can go to the base directory of your project, and inside the file ConnectivityMQTTMessaging.c you will find the setup of your MQTT Resource.

There, within ConnectivityMQTTMessaging_Enable() , you can add the following code beneath the assignment of the clientId on line 69:

 
StringDescr_T username;
StringDescr_wrap(&clientId, MQTT_USERNAME);
mqttSession.username = username;

StringDescr_T password;
StringDescr_wrap(&clientId, MQTT_PASSWORD);
mqttSession.password = password;

Then, declare the variables MQTT_USERNAME and MQTT_PASSWORD similarly to MQTT_CLIENT_ID above the function:

 
static const char* MQTT_USERNAME = "myUsername";
static const char* MQTT_USERNAME = "myPassword";

Keep in mind that the generated code will be completely reverted every time you make a change to the application.x file and save it. This is because the transpiler from XDK Live to C is not aware of any changes you make to the generated C code.

As such, I recommend that you try things out without username and password until everything works as expected, and then copy the generated code into an own project and modify the code from there.

And finally, I would like to mention that XDK Live is still at an early point in development, and not all functionality is available, and things are always subject to change in the future.

Please tell me if this was helpful, and do not hesitate to ask further questions.

Kind regards,
Alex
+1 (1 Vote)
RE: MQTT XDK_Live
Answer
6/7/18 12:31 PM as a reply to Alexander Sawtschuk.

Hello Alexander,

Thank you for your time and your advices. I will proceed as you suggested later on with th C code.

Best regards,

Alejandro P

0 (0 Votes)
RE: MQTT XDK_Live
Answer
6/7/18 1:38 PM as a reply to Alejandro Perez.
Hello Alejandro,

I am glad to hear that my advice is helpful.

If there is anything else I can help you with, feel free to ask.

Kind regards,
Alex
0 (0 Votes)
RE: MQTT XDK_Live
Answer
6/13/18 6:53 AM as a reply to Alexander Sawtschuk.

Hello Alejandro,

this is what I did to get username and password support in the default Workbench SendDataOverMQTT project:

Edit:

  • SendDataOverMQTT.h

and add:

/**
 * MQTT_USERNAME is the username to connect to the broker.
 */
#define MQTT_USERNAME	"YOURUSERNAME"

/**
 * MQTT_PASSWORD is the password to connect to the broker.
 */
#define MQTT_PASSWORD	"YOURPASSWORD"

I put it after:

/**
 * MQTT_BROKER_PORT is the MQTT server port number.
 */
#define MQTT_BROKER_PORT	UINT16_C(1883)

for example:

 

/**
 * MQTT_BROKER_PORT is the MQTT server port number.
 */
#define MQTT_BROKER_PORT	UINT16_C(1883)

/**
 * MQTT_USERNAME is the username to connect to the broker.
 */
#define MQTT_USERNAME	"my_awesome_username"

/**
 * MQTT_PASSWORD is the password to connect to the broker.
 */
#define MQTT_PASSWORD	"mysupersecretpassword"

/**
 * DEVICE_NAME is the device name
 */
#define DEVICE_NAME			"AN_MQTT_IOTA"	//

 

Then edit:

  • SendDataOverMQTT.c

and add what comes after // added by Antonio Nardella

 

                // set Connect information
                SessionPtr->MQTTVersion = 3;
                SessionPtr->keepAliveInterval = 100;
                SessionPtr->cleanSession = true;
                SessionPtr->will.haveWill = false;
                // added by Antonio Nardella
                StringDescr_T username;
                StringDescr_wrap(&username, MQTT_USERNAME);
                SessionPtr->username = username;
                StringDescr_T password;
                StringDescr_wrap(&password, MQTT_PASSWORD);
                SessionPtr->password = password;

This is how you get username and password in the demo MQTT demo with the Bosch Workbench 3.3.1


Cheers,

Antonio

+1 (1 Vote)
RE: MQTT XDK_Live
Answer
6/19/18 11:27 AM as a reply to Antonio Nardella.

Hello Antonio,

 

Thank you very much for your answer. I will try it out as well.

 

Best regards,

Alejandro P

0 (0 Votes)
RE: MQTT XDK_Live
Answer
9/6/18 1:25 PM as a reply to Alejandro Perez.

What would be the equivalent way to do this in 3.4? I've tried the following snippet but there's clearly no fields called .username or .password (or username or password):

 

static MQTT_Connect_T MqttConnectInfo =
        {
                .ClientId = APP_MQTT_CLIENT_ID,
                .BrokerURL = APP_MQTT_BROKER_HOST_URL,
                .BrokerPort = APP_MQTT_BROKER_HOST_PORT,
                .CleanSession = true,
                .KeepAliveInterval = 100,
                username = MQTT_USERNAME,
                password = MQTT_PASSWORD,
        };/**< MQTT connect parameters */

 

0 (0 Votes)
RE: MQTT XDK_Live
Answer
9/6/18 3:21 PM as a reply to Kallum Burgin.
Hello Kallum,


Please take a look at the MqttSession_S struct in the Serval_Mqtt.h library.

All fields of the struct MQTT_Connect_T are present in the struct MQTT_Session_S . You can the interface by navigating to it over the following folderpath:

SDK > xdk110 > Libraries > ServalStack > 3rd-party > ServalStack > api

Please let me know if this was helpful, and do not hesitate to ask further questions.

Kind regards,
Franjo
0 (0 Votes)
RE: MQTT XDK_Live
Answer
9/6/18 4:27 PM as a reply to Franjo Stjepandic.

That's true, but not the other way around (the fields of MQTT_Session_S aren't within MQTT_Connect_T). Therefore you can't pass a username/password from the user app to the mqtt connection. I have edited the SDK/xdk110/Common/source/Connectivity/MQTT.c and at line ~420 add:

 

MqttSession.MQTTVersion = 3;
MqttSession.keepAliveInterval = connect->KeepAliveInterval;
MqttSession.cleanSession = connect->CleanSession;
MqttSession.will.haveWill = false;
MqttSession.onMqttEvent = MqttEventHandler;

StringDescr_T mqttusername;
StringDescr_wrap(&mqttusername, "myusername");
MqttSession.username = mqttusername;
StringDescr_T mqttpassword;
StringDescr_wrap(&mqttpassword, "mypassword");
MqttSession.password = mqttpassword;

 

This is obviously less than ideal as the username and password are coded into the SDK rather than the program.

0 (0 Votes)
RE: MQTT XDK_Live
Answer
9/7/18 1:26 PM as a reply to Kallum Burgin.
Hello Kallum,

you are indeed correct, that this approach is less ideal.

In that regard, I would ask you to go more into detail about your use case. So that I am might able to propose a more fitting solution.

Kind regards,
Franjo
0 (0 Votes)
RE: MQTT XDK_Live
Answer
9/12/18 1:07 PM as a reply to Franjo Stjepandic.

Hi Franjo,

I have a case where I connect an XDK to Cumulocity over MQTT.

Upon the first connection the XDK receives device credentials (registration mode). The device credentials are used in the following phase (operation mode) when measurements are sent using the device credentials .

So credentials are changing from device to device and should not be hard coded.

Do you have any advice how to achieve this?

With the PAHO client code  it was possible to do this.

Kind regards

Christof

0 (0 Votes)
RE: MQTT XDK_Live
Answer
9/13/18 3:12 PM as a reply to Christof Strack.
Hello Christof,

just for clarification about the way you are going to connect the XDK with Cumulocity over MQTT. Are you going to establish a connection to Cumulocity registration mode over MQTT, followed by receiving credentials to connect to the operation mode?

If you were able to accomplish this with the third party MQTT Paho Client library, it is also possible with the Serval Stack implementation of MQTT. This implementation also integrates username and password as configuration items into the struct MqttSession_T .

As such, you would also need to port the implementation you made with the MQTT Paho client library, into an implementation with the Serval MQTT library.

Besides, since this is a whole new topic which does not relate to the initial topic of this thread, it is hard to find for users with similar issues.
To give other users the opportunity to benefit from this, I would kindly ask you to open up new threads for new issues.

Please let me know if this was helpful and feel free to ask if you have further questions.

Kind regards,
Franjo
0 (0 Votes)