derapados
Published © CC0

Arduino IoT with SAP HANA Cloud Platform

Enter into IoT world with SAP HANA Cloud Platform and Arduino.

BeginnerProtip8,716
Arduino IoT with SAP HANA Cloud Platform

Things used in this project

Story

Read more

Code

Untitled file

Arduino
//Andrea Martignoni
//martignoni.a@gmail.com
// post data from a device upstream to SAP HANA Cloud Platform IoT Services

// the program is based on the HTTPS example for ESP8266 - see below

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <Wire.h>

// ========== start configuration ==========
// WiFi configuration
const char* ssid = "YOURwifiNETWORK";
const char* password = "Password";

// SAP HCP specific configuration
const char* host = "YOURiotSERVICE.hanatrial.ondemand.com";
String device_id = "DEVICEid";
String message_type_id = "MESSAGEtypeID";
String oauth_token="AUTHtoken";

//fixed values but you directly read from a sensor
String temperatureC="22.0";
String temperatureF="70.01";
String humidty="45.00";
String pressure="955.000";
String dateTime = "";
String TimeDate = "";
String Date ="";

//NTP Time server
const char* NTPhost = "1.it.pool.ntp.org";



// ========== end configuration ============

String url = "https://YOURurl.hanatrial.ondemand.com/com.sap.iotservices.mms/v1/api/http/data/" + device_id;

// just an example payload corresponding to the other Starterkit examples
String post_payload = "{\"mode\":\"async\", \"messageType\":\"" + message_type_id + "\", \"messages\":[{\"temperatureC\":"+temperatureC+", \"temperatureF\":"+temperatureF+", \"humidity\":"+temperatureC+", \"pressure\":"+temperatureC+", \"value\":\"20\", \"timestamp\":1413191650}]}";

const int httpsPort = 443; //HTTP port

void setup() {
  Serial.begin(115200);
  Serial.println();
  Wire.begin(9);
  Serial.print("connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  // Use WiFiClient class to create TCP connections
  WiFiClient Client;
  const int httpPort = 13;
  //Connect to NTP Server
  if (!Client.connect(NTPhost, httpPort)) {
    Serial.println("connection failed to NTP");
   // return;
  }

  // Use WiFiClientSecure class to create TLS connection
  WiFiClientSecure client;
  Serial.print("connecting to ");
  Serial.println(host);
  if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed");
    return;
  }
//NTP
  // Read all the lines of the reply from server and print them to Serial
  // expected line is like : Date: Thu, 01 Jan 2015 22:00:14 GMT
  char buffer[12];
  String dateTime = "";
  
 while(Client.available())
  {
    String line = Client.readStringUntil('\r');

    if (line.indexOf("Date") != -1)
    {
      Serial.print("=====>");
    } else
    {
      // Serial.print(line);
      // date starts at pos 7
      Date= line.substring (7);
      Serial.println(Date);
      TimeDate = line.substring(7);
      Serial.println(TimeDate);
      // time starts at pos 14
      TimeDate = line.substring(7, 15);
      TimeDate.toCharArray(buffer, 10);
      //sendStrXY("UTC Date/Time:", 4, 0);
      //sendStrXY(buffer, 5, 0);
      TimeDate = line.substring(16, 24);
      TimeDate.toCharArray(buffer, 10);
      Serial.println(TimeDate);
      //sendStrXY(buffer, 6, 0);
    }
  }
 //END NTP 
  Serial.print("requesting URL: ");
  Serial.println(url);
  
  // using HTTP/1.0 enforces a non-chunked response
  client.print(String("POST ") + url + " HTTP/1.0\r\n" +
               "Host: " + host + "\r\n" +
               "Content-Type: application/json;charset=utf-8\r\n" +
               "Authorization: Bearer " + oauth_token + "\r\n" +
               "Content-Length: " + post_payload.length() + "\r\n\r\n" +
               post_payload + "\r\n\r\n");
               
  Serial.println("request sent");

  Serial.println("reply was:");
  Serial.println("==========");
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    Serial.println(line);
  }
  Serial.println("==========");
  Serial.println("closing connection");
}

void loop() {
}

SAP IoT HCP Starter Kit

Credits

derapados

derapados

3 projects • 30 followers
https://www.linkedin.com/in/andreamartignoni/
Thanks to Prashantha H J.

Comments