Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
architectSAP
Active Contributor

Inspired by rui.nogueira's blog RaspberryPi on SAP HANA Cloud Platform I have been wondering whether I could connect my vintage Lego Mindstorms NXT with its myriad of sensor to the HCP Internet of Things Services leveraging a similarly aged Raspberry Pi A.

In fact, this turned out to be relatively simple to achieve. My Raspbrry Pi had already been equipped with a WiFi and a Bluetooth dongle so that it was quickly connected to my Mindstorms NXT following this excellent blog on leJOS on the Raspberry Pi - including USB and Bluetooth communication. I had already been running my Raspberry Pi from a power bank, so this was a truly wireless scenario from the start:

The only remaining task was to write some Java code for the Raspberry Pi to read a sensor from the Mindstorms NXT (in this case a touch sensor in conjunctions with an ultrasonic proximity sensor) and send the event to the HCP Internet of Things Services (which I had already set-up for a hands-on session during TechEd 2015 in Barcelona😞

import lejos.nxt.TouchSensor;

import lejos.nxt.SensorPort;

import lejos.nxt.UltrasonicSensor;

import java.net.URL;

import java.net.HttpURLConnection;

import java.io.DataOutputStream;

import java.io.InputStream;

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class Mindstorms {

     public static void main(String[] args) {

     System.out.println("Please press button");

     TouchSensor touch = new TouchSensor(SensorPort.S1);

     while (!touch.isPressed()) {

     }

     System.out.println("Button pressed");

     UltrasonicSensor dist = new UltrasonicSensor(SensorPort.S2);

     HttpURLConnection con = null;

     try {

          URL url = new URL("https://iotmmssYourHCPIDtrial.hanatrial.ondemand.com/com.sap.iotservices.mms/v1/api/http/data/YourDeviceType");

          con = (HttpURLConnection)url.openConnection();

          con.setRequestMethod("POST");

          con.setRequestProperty("authorization", "Bearer YourAuthCode");

          con.setRequestProperty("content-type", "application/json;charset=utf-8");

          con.setUseCaches(false);

          con.setDoOutput(true);

          DataOutputStream wr = new DataOutputStream (con.getOutputStream());

          wr.writeBytes("{\"mode\":\"sync\",\"messageType\":\"YourMessageType\",\"messages\":[{\"distance\":\"" + dist.getDistance() + "\"}]}");

          wr.close();

          InputStream is = con.getInputStream();

          BufferedReader rd = new BufferedReader(new InputStreamReader(is));

          StringBuilder response = new StringBuilder();

          String line;

          while((line = rd.readLine()) != null) {

               response.append(line);

               response.append('\r');

          }

          rd.close();

          System.out.println(response);

          } catch (Exception e) {

               e.printStackTrace();

          }

     }

  }

And there we go. On the press of a button sensor on a Lego Mindstorms device, we receive a message in the HCP Internet of Things Services with the distance of the person pressing the button:

As you can see from the Application Data view, I have been pressing the button sensor on my Mindstorms NXT first leaning to it and then with an extended arm:

From here of course, the options are endless. All Lego Mindstorms sensors can be connected to the HCP Internet of Things Services in the same way, and in parallel, control commands to the Lego Mindstorms can be send as well. Also, in my next blog, I describe how to Display your Lego Mindstorms sensor data on a Fiori Overview Page and deploy it to the HCP Portal Fi....

Labels in this area