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)