Send XDK data to Bosch IoT Things directly
응답
16. 11. 9 오전 9:10

XDK has integrated wifi module, which gives us more options over TI SensorTag. Together with Bosch IoT Suite from INST, one straightforward idea is publishing XDK senser data to Bosch IoT Things backend directly.

However, the existing solutions that I can find still require a proxy or gateway, due to the limitation from the current verion of Serval stack (cannot modify http header). 

In order to get rid of this redundant proxy, I have tried several different approaches (RestClient, HttpClient, Serval_XTcp, third-party library...), and finally figured it out by crafting the http header manually on top of TI simplelink socket library. 

Here is my code snippet, 

void xdkRestClientPostMessage(const char* uripath, char* payload, uint32_t size) {
	int16_t SockID = 0;

	SockID = sl_Socket(SL_AF_INET, SL_SOCK_STREAM, 0);
	if (SockID < 0) {
		printf("Fail to create socket\r\n");
	}

	SlSockAddrIn_t Addr;
	Addr.sin_family = SL_AF_INET;
	Addr.sin_port = sl_Htons((uint16_t) DEST_PORT_NUMBER);
	Addr.sin_addr.s_addr = destAddr;

	int16_t rc = sl_Connect(SockID, (SlSockAddr_t *) &Addr,
			sizeof(SlSockAddrIn_t));
	if (rc < 0) {
		sl_Close(SockID);
		printf("fail to connect (%d)\r\n", rc);
		return;
	}

	int16_t msg_size = snprintf((char*) msg_buf, MSG_MAX_SIZE,
			"POST %s HTTP/1.1\r\n"
			"Content-Type: application/json\r\n"
			"Connection: close\r\n"
			"Host: %s:%d\r\n"
			"Content-Length: %ld\r\n"
			"\r\n"
			"%s"
			"\r\n", uripath, DEST_SERVER, DEST_PORT_NUMBER, size, payload);

	int16_t numSent = sl_Send(SockID, msg_buf, msg_size, 0);
	if (numSent < 0) {
		sl_Close(SockID);
		printf("Fail to send message\r\n");
		return;
	}
	printf("Sent a message (size = %d)\r\n", msg_size);

	sl_Close(SockID);

	return;
}

you need to define below macros, these parameters are required by Things service, please refer to Bosch IoT Things for more information, 

#define DEST_SERVER             "things.apps.bosch-iot-cloud.com"
#define DEST_PORT_NUMBER 	80
#define THINGS_API_TOKEN 	"changethis"      // Things API Token for Rest Call
#define THINGS_USER_AUTH 	"changethis"      // User Basic Authentication code, format like "Basic xxxxx"

Then you can call this function to update XDK data (e.g. temperature) on Things backend, 

char buf[DATA_BUF_SIZE];
int length = snprintf(buf, DATA_BUF_SIZE, "{\"properties\":{\"sensor_value\":%.1f,}}", temperature);
xdkRestClientSendMessage("/api/1/things/<your-things-id>/features/<your-feature-name>", (char*) buf, length);

 

Keep in mind, this is a workaround so far to send data directly from XDK to Bosch IoT Suite services (also works for IoT HUB customized http connectors, see Developer Experience if you are interested), however, we should use official RestClient once it supports customizable http header. 

 

Regards, 

Shaodong 

 

+3 (3 투표)
RE: Send XDK data to Bosch IoT Things directly
응답
16. 11. 9 오전 10:19 as a reply to gavin y.

Hello Shaodong,

thank you very much for sharing your solution with the XDK community. I moved this thread to project exchange.

Costumizing the http header is required for many use cases and I imagine the XDK community members will be very thankful to refer to your solution when approaching this topic.

Kind regards,
Manuel

0 (0 투표)
RE: Send XDK data to Bosch IoT Things directly
응답
17. 1. 17 오전 9:52 as a reply to Manuel Cerny.

Hello - stay tuned - actually there are coming more and more appications which can visualize your XDK data in the Bosch IoT cloud.

Greetings Achim

0 (0 투표)