Quantcast
Channel: SCN : Blog List - All Communities
Viewing all articles
Browse latest Browse all 2548

Direct publishing to HCP IOT Services with ESP8266 and Arduino IDE (2.0.0)

$
0
0

Existing Work

In this blog i will explain how to connect the ESP8266 with HCP IOT, by developing a simple sketch in the Arduino IDE. It is not meant as a standalone post, rather as a follow up to the great blogs by Prashantha H J, Jan Penninkhof, Rui Nogueira. They describe in detail how to wire an ESP8266, how to connect via a PHP gateway, a plain C program based on the Esspressiv SDK, and how to connect this to the HCP IOT Service and build a nice looking SAP UI5 Frontend.

 

For Reference, for those that want some background information, here are the links to those blog posts:

ESP8266 and HCP Internet Of Things Services (BETA) - Part 1

Connecting an ESP8266 to the SAP HANA Cloud Platform

RaspberryPi on SAP HCP - IoT blog series part 1: Setting-up your RaspberryPi

 

Arduino IDE and ESP8266

The folks at https://github.com/esp8266/Arduino, have done great work in bridging the Arduino IDE with ESP8266, making it very simple to develop sketches and deploy them to an ESP8266. As noted in the blogs by Jan and Prashantha, up to recently it did not support HTTPS connections, so connecting to HCP IOT required some workarounds.

Yet, few months ago HTTPS support has been included and as a bonus, a couple of weeks ago a built in HTTP Client has been included. This makes the whole story very easy to implement and brings it closer to a much wider community of makers.

When you download it, make sure to take the GIT version (not stable, nor staging), and follow the instructions to install it. Also make sure to use Arduino 1.6.5 and not 1.6.6 as it brought some incompatible changes. (This is as of 12.Dec.2015, perhaps soon the GIT version will be stable and can be directly installed from Arduino's BoardManager)

 

ESP8266 to HCP IOT Service

The whole code to make this call is quite simple. Assuming that you have followed the posts above or already know the details, you should have your deviceID, authentication Token, messageId, variableName and of course the host name of your IOT MMS server.

 

(The whole sketch can be downloaded from my git Repository: vlast3k/HCPIOT_ESP8266_ArduinoIDE · GitHub (just make sure to store it in a directory called 'HCPIOT_ESP8266_ArduinoIDE'))

 

In this example i am using the simplified syntax for calling the MMS server, that is - all parameters are sent in the URL, thus - no JSON needs to be constructed manually.

 

This syntax implies that you should build an url like this:

sprintf(url, "https://%s/com.sap.iotservices.mms/v1/api/http/data/%s/%s/sync?%s=%d",                host, deviceId, messageId, variableName, value);

where host, deviceId and messageId are self-explanatory. "variableName" is the name of the variable in your message, whose "value" you want to update. With this syntax multiple variables can be sent as well.

 

Then sending the HTTPS call is simple:

 

 HTTPClient http;  http.begin(url);  http.addHeader("Content-Type",  "application/json;charset=UTF-8");  http.addHeader("Authorization", String("Bearer ") + token);  http.POST("");
 Serial.printf("Payload: [%s]\n", http.getString().c_str());

In case it is Ok, the response should be something like

Payload: [52
{"msg":"1 message(s) received from device [c5c73d69-6a19-4c7d-9da3-b32198ba71f9]"}
0
]

If there was a problem to establish connection the response code is <0, with the following meanings

#define HTTPC_ERROR_CONNECTION_REFUSED  (-1)
#define HTTPC_ERROR_SEND_HEADER_FAILED  (-2)
#define HTTPC_ERROR_SEND_PAYLOAD_FAILED (-3)
#define HTTPC_ERROR_NOT_CONNECTED       (-4)
#define HTTPC_ERROR_CONNECTION_LOST     (-5)
#define HTTPC_ERROR_NO_STREAM           (-6)
#define HTTPC_ERROR_NO_HTTP_SERVER      (-7)

Else - the response code is a standard HTTP response code.

 

Conclusion

I believe that those new enhancements to this framework will make a big step towards enabling it for various developers to do interesting things. Previously a bit more expertise was required to use this functionality (e.g. as Jan described in his post of using the Esspresiv SDK to make HTTPS calls), or using some bridge to do the needful.

Also I would recommend to use some of the existing development boards for ESP8266. A month ago a very appealing board was released - the Wemos D1 mini. It costs 4$ at Aliexpress, which is an astonishing low price, given the fact that the components if bought separately cost more. I do not want to make advertisement of this board, but i am just excited of it (so i bought 15 already ).

 

I hope this blog will be useful and of course in case of problems or questions - feel free to comment.


Viewing all articles
Browse latest Browse all 2548

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>