Arduino IoT with SAP HANA Cloud Platform

Arduino IoT with SAP HANA Cloud Platform © CC0

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

  • 6,570 views
  • 11 comments
  • 9 respects

Components and supplies

Apps and online services

About this project

Introduction

Did you ever think about starting an Internet of Things project that can be scaled also to Enterprise grade level? SAP HANA Cloud Platform is an enterprise solution in the cloud that enable you in creating IoT projects and a lot of nice staff.

SAP HCP is also available as free trial.

Getting started

First you have to create a new trial account in case you don't have one:

Then you have to Enable Internet of Things Services and Go to the service:

Now you have to Install Message Management Service from Internet of Things Services Cockpit:

Add your user to the role Iot-MMS-User:

Preparing the Interface

Now we need to prepare interface for registering our devices. First create Message type (in my case I prepared for sending data from my existing project):

Then create device type:

Creating the device

And finally create the device:

With these simple steps, we have prepared HCP for receiving data from our Arduino. Data will come up in a table that we can use for creating a HTML5 app or for other purposes:

Using advanced features of HCP we can also enable predictive algorithms for forecasting data.

Code

Untitled fileArduino
//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() {
}

Comments

Similar projects you might like

IoT Blink - Getting started with IoT

Project showcase by AppShed Support

  • 2,026 views
  • 0 comments
  • 11 respects

IoT Made Easy w/ UNO, ESP-01, ThingSpeak & MIT App Inventor

Project tutorial by MJRoBot

  • 17,870 views
  • 15 comments
  • 35 respects
Add projectSign up / Login