Quantcast
Channel: SCN : Blog List - All Communities
Viewing all articles
Browse latest Browse all 2548

Connect a Lego Mindstorms NXT to the HCP Internet of Things Services via a Raspberry Pi over Bluetooth

$
0
0

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 an 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:

 

leJOS.png

 

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 as simple as a touch 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 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");

     HttpURLConnection con =null;

     try {

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

          con = (HttpURLConnection)url.openConnection();

          con.setRequestMethod("POST");

          con.setRequestProperty("authorization", "BearerYourAuthCode");

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

          con.setUseCaches(false);

          con.setDoOutput(true);

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

          wr.writeBytes('{"mode":"sync","messageType":"YourMessageType","messages":[{"temperature":"27","timestamp":1413191650}]}');

          wr.close();

          InputStream is = con.getInputStream();

          BufferedReader rd =newBufferedReader(newInputStreamReader(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 on a Lego Mindstorms device, we receive a message in the HCP Internet of Things Services:

BlueCove.png

As you can see from the code, I am faking a 27 degrees meter reading at the button press, and that is reflected in the Application Data view:

Application Data.png

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.


Viewing all articles
Browse latest Browse all 2548

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>