ESP8266 and HCP Internet Of Things Services (BETA) – Part 2
Part-1 touched upon ESP8266, it’s advantages and limitations. We also prepared HCP IoT Services to accept and store sensor data from ESP8266.
Overcoming Limitations of ESP8266
There are two limitations that need to be addressed.
Connecting to office/corporate network if your office/corporate network is enabled with WPA2-Enterprise security. This device is not yet capable of connecting to such networks. Easiest solution I found was to use my cellular network 3G connection and enabling hotspot in the phone.
ESP8266 cannot use https hence it cannot POST message to Hana Cloud Platform IOT services on its own. To overcome this, I used a PHP web server in an Android tablet and written a small PHP script to accept HTTP GET/POST request from ESP8266 and relay it to HCP IoT Services over HTTPS.
UPDATE: PLEASE NOTE THIS LIMITATION IS NOW POSSIBLE TO OVERCOME Without this PHP Webserver bridge. Please read through the blogs below.
- Blog from Vladimir Savchenko
- Blog from Jan Penninkhof
Note: This is just one way of doing this. You might take a different approach to handle this limitation. If you do not have access to an Android device, you can still use your computer to host a PHP web server and follow the steps mentioned below. Or you might use a tomcat server and write a Servlet to do the same.
Steps:
Download PHP Webserver from Google Play Store. I downloaded a free application Palapa Web Server.
Download Certificate: We have to use certificate from HCP in PHP Script.
Go to https://account.hanatrial.ondemand.com/cockpit using google chrome.
Download it as Base 64 certificate in your computer. Let’s call it as hana64.cer.
Write a PHP Script to accept GET/POST request from ESP8266:
ESP8266 will send request to this PHP script with following parameters.
device: This the device id to which the message has to be sent.
message: Message in JSON format.
token: OAuth token that has to be used to send message to HCP.
<?php
$device = "";
$message = "";
$token = "";
if ($_REQUEST["device"] || $_REQUEST["message"] || $_REQUEST["token"]) {
$device = $_REQUEST["device"];
$message = $_REQUEST["message"];
$token = $_REQUEST["token"];
}
$url = "https://iotmmspXXXXXXXXXXXtrial.hanatrial.ondemand.com/com.sap.iotservices.mms/v1/api/http/data/" . $device;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$array_data = json_decode($message);
$data_string = json_encode($array_data, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/hana64.cer");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36',
'Authorization: Bearer ' . $token,
'Content-Type: application/json;charset=UTF-8'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
// Get the response and close the channel.
$response = curl_exec($ch);
var_dump($response);
curl_close($ch);
?>
Copy Certificate and PHP file to the PHP Server root location. (Note: Please change the URL to point to your host)
For Palapa Web Server I used, the root location is Device/pws/www.
Now you can call HCP_IOT_HTTPS_Relay.php hosted in PHP Web Server from ESP8266 with device id, message and token.
You can get the host and port of the server from web server Admin page
You can use chrome app Advanced REST Client to test our HTTPS Relay. Your payload will be like
token=4da8eb85cc10bd5499612899b85c4e7c8&device=90c8ca3d-b1a7-4496-889f-f9305b2892e2&message={“mode”:”sync”, “messageType”:”1″, “messages”:[{“temperature”:34, “humidity”:42}]}
Now our HTTPS Relay is ready.
Next Steps:
We have to program ESP8266 to read temperature and humidity from the sensor and fill those values in the message json. To do this we need Arduino IDE and some configuration which will do in Part-3.
Dear Prashantha,
Thanks for the blog. I am trying to call IOTMMS service wth Arduino+ESP8266 combination, and came across this blog. Its very informative and saved a lot of time for me.
I followed this blog for the http->https relay and installed certificate as described in the blog. It didn't work, and on dumping curl_error($ch) I see the error as "SSL certificate problem: unable to get local issuer certificate".
Did you face this issue? Any hints how to resolve?Thanks.
Cool...its resolved, by setting CURLOPT_SSL_VERIFYPEER false. Adding it here for the benefit of others following this blog, in case they face same issue.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
Hi Krishnakumar,
I will check this once again and update this blog with more information on this.
Regards,
Prashanth
Another high quality blog from you Prashantha!
I am looking forward Part 3.
Thank you, keep going on!!
Regards
Basar Ozgur
Hi Basar Ozgur,
I was on vacation and could not work on part 3 of this blog series. I will write it as soon as I get some time this week.
Regards,
Prashanth
Hi Basar Ozgur,
I have published Part-3 of this blog series. Please have a look.
Regards,
Prashanth
Hi Prashantha,
Thank you for your effort.
Regards