Arduino IoT with SAP HANA Cloud Platform
Enter into IoT world with SAP HANA Cloud Platform and Arduino.
Components and supplies
1
Arduino UNO
1
ESPDUINO
Apps and platforms
1
SAP Cloud Platform IoT for the Neo environment
1
Arduino IDE
Project description
Code
Untitled file
arduino
1//Andrea Martignoni 2//martignoni.a@gmail.com 3// post data from a device upstream to SAP HANA Cloud Platform IoT Services 4 5// the program is based on the HTTPS example for ESP8266 - see below 6 7#include <ESP8266WiFi.h> 8#include <WiFiClientSecure.h> 9#include <Wire.h> 10 11// ========== start configuration ========== 12// WiFi configuration 13const char* ssid = "YOURwifiNETWORK"; 14const char* password = "Password"; 15 16// SAP HCP specific configuration 17const char* host = "YOURiotSERVICE.hanatrial.ondemand.com"; 18String device_id = "DEVICEid"; 19String message_type_id = "MESSAGEtypeID"; 20String oauth_token="AUTHtoken"; 21 22//fixed values but you directly read from a sensor 23String temperatureC="22.0"; 24String temperatureF="70.01"; 25String humidty="45.00"; 26String pressure="955.000"; 27String dateTime = ""; 28String TimeDate = ""; 29String Date =""; 30 31//NTP Time server 32const char* NTPhost = "1.it.pool.ntp.org"; 33 34 35 36// ========== end configuration ============ 37 38String url = "https://YOURurl.hanatrial.ondemand.com/com.sap.iotservices.mms/v1/api/http/data/" + device_id; 39 40// just an example payload corresponding to the other Starterkit examples 41String post_payload = "{\\"mode\\":\\"async\\", \\"messageType\\":\\"" + message_type_id + "\\", \\"messages\\":[{\\"temperatureC\\":"+temperatureC+", \\"temperatureF\\":"+temperatureF+", \\"humidity\\":"+temperatureC+", \\"pressure\\":"+temperatureC+", \\"value\\":\\"20\\", \\"timestamp\\":1413191650}]}"; 42 43const int httpsPort = 443; //HTTP port 44 45void setup() { 46 Serial.begin(115200); 47 Serial.println(); 48 Wire.begin(9); 49 Serial.print("connecting to "); 50 Serial.println(ssid); 51 WiFi.begin(ssid, password); 52 while (WiFi.status() != WL_CONNECTED) { 53 delay(500); 54 Serial.print("."); 55 } 56 57 // Use WiFiClient class to create TCP connections 58 WiFiClient Client; 59 const int httpPort = 13; 60 //Connect to NTP Server 61 if (!Client.connect(NTPhost, httpPort)) { 62 Serial.println("connection failed to NTP"); 63 // return; 64 } 65 66 // Use WiFiClientSecure class to create TLS connection 67 WiFiClientSecure client; 68 Serial.print("connecting to "); 69 Serial.println(host); 70 if (!client.connect(host, httpsPort)) { 71 Serial.println("connection failed"); 72 return; 73 } 74//NTP 75 // Read all the lines of the reply from server and print them to Serial 76 // expected line is like : Date: Thu, 01 Jan 2015 22:00:14 GMT 77 char buffer[12]; 78 String dateTime = ""; 79 80 while(Client.available()) 81 { 82 String line = Client.readStringUntil('\ '); 83 84 if (line.indexOf("Date") != -1) 85 { 86 Serial.print("=====>"); 87 } else 88 { 89 // Serial.print(line); 90 // date starts at pos 7 91 Date= line.substring (7); 92 Serial.println(Date); 93 TimeDate = line.substring(7); 94 Serial.println(TimeDate); 95 // time starts at pos 14 96 TimeDate = line.substring(7, 15); 97 TimeDate.toCharArray(buffer, 10); 98 //sendStrXY("UTC Date/Time:", 4, 0); 99 //sendStrXY(buffer, 5, 0); 100 TimeDate = line.substring(16, 24); 101 TimeDate.toCharArray(buffer, 10); 102 Serial.println(TimeDate); 103 //sendStrXY(buffer, 6, 0); 104 } 105 } 106 //END NTP 107 Serial.print("requesting URL: "); 108 Serial.println(url); 109 110 // using HTTP/1.0 enforces a non-chunked response 111 client.print(String("POST ") + url + " HTTP/1.0\ \ 112" + 113 "Host: " + host + "\ \ 114" + 115 "Content-Type: application/json;charset=utf-8\ \ 116" + 117 "Authorization: Bearer " + oauth_token + "\ \ 118" + 119 "Content-Length: " + post_payload.length() + "\ \ 120\ \ 121" + 122 post_payload + "\ \ 123\ \ 124"); 125 126 Serial.println("request sent"); 127 128 Serial.println("reply was:"); 129 Serial.println("=========="); 130 while (client.connected()) { 131 String line = client.readStringUntil('\ 132'); 133 Serial.println(line); 134 } 135 Serial.println("=========="); 136 Serial.println("closing connection"); 137} 138 139void loop() { 140}
SAP IoT HCP Starter Kit
Untitled file
arduino
1//Andrea Martignoni 2//martignoni.a@gmail.com 3// post data from a device upstream to SAP HANA Cloud Platform IoT Services 4 5// the program is based on the HTTPS example for ESP8266 - see below 6 7#include <ESP8266WiFi.h> 8#include <WiFiClientSecure.h> 9#include <Wire.h> 10 11// ========== start configuration ========== 12// WiFi configuration 13const char* ssid = "YOURwifiNETWORK"; 14const char* password = "Password"; 15 16// SAP HCP specific configuration 17const char* host = "YOURiotSERVICE.hanatrial.ondemand.com"; 18String device_id = "DEVICEid"; 19String message_type_id = "MESSAGEtypeID"; 20String oauth_token="AUTHtoken"; 21 22//fixed values but you directly read from a sensor 23String temperatureC="22.0"; 24String temperatureF="70.01"; 25String humidty="45.00"; 26String pressure="955.000"; 27String dateTime = ""; 28String TimeDate = ""; 29String Date =""; 30 31//NTP Time server 32const char* NTPhost = "1.it.pool.ntp.org"; 33 34 35 36// ========== end configuration ============ 37 38String url = "https://YOURurl.hanatrial.ondemand.com/com.sap.iotservices.mms/v1/api/http/data/" + device_id; 39 40// just an example payload corresponding to the other Starterkit examples 41String post_payload = "{\\"mode\\":\\"async\\", \\"messageType\\":\\"" + message_type_id + "\\", \\"messages\\":[{\\"temperatureC\\":"+temperatureC+", \\"temperatureF\\":"+temperatureF+", \\"humidity\\":"+temperatureC+", \\"pressure\\":"+temperatureC+", \\"value\\":\\"20\\", \\"timestamp\\":1413191650}]}"; 42 43const int httpsPort = 443; //HTTP port 44 45void setup() { 46 Serial.begin(115200); 47 Serial.println(); 48 Wire.begin(9); 49 Serial.print("connecting to "); 50 Serial.println(ssid); 51 WiFi.begin(ssid, password); 52 while (WiFi.status() != WL_CONNECTED) { 53 delay(500); 54 Serial.print("."); 55 } 56 57 // Use WiFiClient class to create TCP connections 58 WiFiClient Client; 59 const int httpPort = 13; 60 //Connect to NTP Server 61 if (!Client.connect(NTPhost, httpPort)) { 62 Serial.println("connection failed to NTP"); 63 // return; 64 } 65 66 // Use WiFiClientSecure class to create TLS connection 67 WiFiClientSecure client; 68 Serial.print("connecting to "); 69 Serial.println(host); 70 if (!client.connect(host, httpsPort)) { 71 Serial.println("connection failed"); 72 return; 73 } 74//NTP 75 // Read all the lines of the reply from server and print them to Serial 76 // expected line is like : Date: Thu, 01 Jan 2015 22:00:14 GMT 77 char buffer[12]; 78 String dateTime = ""; 79 80 while(Client.available()) 81 { 82 String line = Client.readStringUntil('\r'); 83 84 if (line.indexOf("Date") != -1) 85 { 86 Serial.print("=====>"); 87 } else 88 { 89 // Serial.print(line); 90 // date starts at pos 7 91 Date= line.substring (7); 92 Serial.println(Date); 93 TimeDate = line.substring(7); 94 Serial.println(TimeDate); 95 // time starts at pos 14 96 TimeDate = line.substring(7, 15); 97 TimeDate.toCharArray(buffer, 10); 98 //sendStrXY("UTC Date/Time:", 4, 0); 99 //sendStrXY(buffer, 5, 0); 100 TimeDate = line.substring(16, 24); 101 TimeDate.toCharArray(buffer, 10); 102 Serial.println(TimeDate); 103 //sendStrXY(buffer, 6, 0); 104 } 105 } 106 //END NTP 107 Serial.print("requesting URL: "); 108 Serial.println(url); 109 110 // using HTTP/1.0 enforces a non-chunked response 111 client.print(String("POST ") + url + " HTTP/1.0\ \ 112" + 113 "Host: " + host + "\ \ 114" + 115 "Content-Type: application/json;charset=utf-8\ \ 116" + 117 "Authorization: Bearer " + oauth_token + "\ \ 118" + 119 "Content-Length: " + post_payload.length() + "\ \ 120\ \ 121" + 122 post_payload + "\ \ 123\ \ 124"); 125 126 Serial.println("request sent"); 127 128 Serial.println("reply was:"); 129 Serial.println("=========="); 130 while (client.connected()) { 131 String line = client.readStringUntil('\n'); 132 Serial.println(line); 133 } 134 Serial.println("=========="); 135 Serial.println("closing connection"); 136} 137 138void loop() { 139}
Comments
Only logged in users can leave comments