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: 
jpenninkhof
Product and Topic Expert
Product and Topic Expert

It looks like someone with quite some experience tinkering with Arduino's and the likes decided to sit down on a rainy sunday afternoon to write Santa a wishlist with everything he would like to see in the next Micro Controller Unit. However, that wishlist wasn't sent to Lapland, but to China, where the good folks of EspressIf have materialize this wishlist in a brilliant little ARM based controller that is clocked at 80Mhz and can be overclocked to 160Mhz (For reference, Arduino's run on 16Mhz), it has WiFi built-in, has 64KBytes of instruction RAM, 96KBytes of data RAM and 64KBytes boot ROM and has a Winbond W25Q40BVNIG SPI flash for your code. On top of that, it has GPIO, I2C, ADC, SPI, PWM and actually some other nice acronyms. One of the biggest advantages is the fact that it is really dirt-cheap though. With a price of only $4, it is actually cheap enough to be put in everything and anything.

It would of course be nice to get it to work with the SAP HANA Cloud Platform. As a proof of concept, I have tried to make a very easy setup of an ESP8266 and flashed it with simple firmware that connects to WiFi and sends temperature sensor data to the new SAP HANA Cloud IoT services. The aim is to have the chip to work in an autonomous mode, meaning that there will be no brokers or bridge involved that are taking messages from the ESP8266 to send it to the SAP HANA Cloud Platform.

his requires the chip to speak HTTPS though, and SSL has been a bit of the achilles heel of the chip. I guess that is also the reason why prashantha.hj in his blog “ ESP8266 and HCP Internet Of Things Services (BETA) - Part 2” is desribing to overcome the limitations of the ESP8266 by adding a PHP bridge into the game.

The good news is that with the help of Faye and John Lee of EspressIf and gkreimer of Hybris Labs, I managed to squeeze a working SSL implementation into Martin d’Allens’ esphttpclient. So let’s see if this tiny 3$ chip is now enterprise grade material.

Circuitry

To connect and ESP8266 to the Hana cloud, it would of course be nice if it had any data to send there. For this experiment I have paired the ESP8266-12 (but could have been any version) with a DHT22 temperature and humidity sensor. This sensor can also be very easily purchase off eBay, AliExpress of Banggood. The nice thing about the DHT22 is that it doesn’t really require a pull-up resistor or any other circuitry to be connected to make it work and the data pin can be connected directly to the ESP8266. The DHT22 does like a voltage of 5V to be more accurate, but it will work if you connect it to the same 3.3V power supply as the ESP8266 is connected to as well. I’m using a bread power supply that provides both 3.3V and 5V, so I’ll just connect it to the 5V power line. Please find a diagram below.

To be able to flash the ESP8266, it is of course convenient to have a serial or USB link as well. The dotted lines indicate how those pins are connected. I’m using a CH340 based USB adapter to connect the ESP8266 to my computer so that I’ll be able to flash it. You can find an image on the right, and also for this component there are plenty sellers available on eBay, AliExpress and Banggood.

Once everything is connected it can be flashed with firmware that reads the DHT22 and sends it into the cloud. To get the ESP8266 in flash mode, the GPIO0 pin should be connected to ground when it is booted.

SAP HANA Cloud Plaform

prashantha.hj  has described perfectly and comprehensively what needs to be done to setup the SAP Hana IoT services in his blog ESP8266 and HCP Internet Of Things Services (BETA) - Part 1. In order for the firmware to work, I have taken a quite similar approach and have configured one device type ESP8266 (screenshot 1) that is capable of sending and “Environment” message type (screenshot 2). I have also created one device ESP8266-01 and linked it to the ESP8266 device type (screenshot 3)

1: Device Type

2: Message Type

3:Device

4: IoT Services Cockpit

Please note that while you’re setting up the firmware later, you’ll need the device ID, but also a valid access token for that device. You can generate an access token in the “Authorization” tab of the device (screenshot 3).

Firmware

Now there is a http-client that can speak SSL and now the SDK also supports SSL (since SDK version 1.1.1), the firmware can actually be kept relatively simple. I have made a little sample application, which is available on github: https://github.com/jpenninkhof/esp_hcp_temperature.

To check it out, use the usual “git clone”:


git clone https://github.com/jpenninkhof/esp_hcp_temperature.git

The webclient, the hcp-client, as well as the dht22 and uart driver are linked into this project as modules in the lib directory. After you have cloned the project, the lib directory is still empty though. To pull the modules into the lib directory, you will have to execute two more git commands from the project directory:


cd esp_hcp_temperature
git submodule init
git submodule update

The first command initializes your local configuration files and the second actually downloads the modules needed for this project.

The default makefile in the project is a makefile that is meant for compiling the firmware from Eclipse (more info here). If you are compiling the SDK from linux (as described here), you will have to rename Makefile.linux to Makefile.

The main logic in the firmware, that does the actual updating is in this part of the user_main.c:


r = DHTRead();
if(r->success)
{
    int temp = (int)(r->temperature * 10);
    int hum = (int)(r->humidity * 10);
    os_sprintf(temp_str, "%d.%d",(int)(temp / 10), temp - ((int)(temp / 10)) * 10);
    os_sprintf(hum_str, "%d.%d",(int)(hum / 10), hum - ((int)(hum / 10)) * 10);
    os_printf("Temperature: %s *C, Humidity: %s %%\r\n", temp_str, hum_str);
    if (lastTemp != temp || lastHum != hum)
    {
        lastTemp = temp;
        lastHum = hum;
        wifi_get_ip_info(STATION_IF, &ipConfig);
        os_sprintf(data,
                "{\"mode\":\"sync\",\"messageType\":\"%s\",\"messages\":[{"
                    "\"Humidity\": %s,"
                    "\"Temperature\": %s"
                "}]}",
                HCP_MESSAGETYPE, hum_str, temp_str);
        hcp_send(HCP_ACCOUNT, HCP_LANDSCAPEHOST, HCP_DEVICEID, HCP_DEVICETOKEN, data, hcp_callback);
    } else {
        os_printf("No changes since last reading.\r\n");
    }
} else {
    os_printf("Error reading temperature and humidity.\r\n");
}

In this piece of code you can see that it tries to read the temperature from the DHT22 thermometer and if there are changes, the temperature and humidity is sent to the HANA Cloud Platform. In line 15 the message is formulated. As I have configured a message type on the HANA Cloud Platform, that contains parameters Humidity and Temperature, a message is formulated containing both variables. Eventually in line 21, the data is sent to the HANA Cloud platform.

To send IoT data to the HANA Cloud Platform, you need to pass the account name, landscape host, device ID, and the device’s access token. Please refer to the “HANA Cloud Platform” section for more information. In line 21, you can see that these variables are not hard-coded. Instead these parameters are configured along with the wifi settings in the configuration file include/user_config.h.

Demo time

To show that it can actually work, please have a look at the video below. On my breadboard, you’ll find the DHT22 sensor on the far left, the ESP8266 in the middle and a breadboard power module on the right. Please note that besides the connections made in the “citcuitry” section, I have added some extra wires to link it up to the USB port of my computer.

Once the power is switched on, you’ll immediately see the ESP8266’s blue led blink up, meaning that it is trying to find a connection with the Wifi network. Once a connection is established, it will wait until it is allowed to measure temperature and send it to the SAP HANA Cloud Platform. Once it has measured the temperature, it will send it over through https, and the response of the SAP HANA Cloud platform is displayed.

Visualize it

SAP has made the IoT Starter Kit available, which provides a few examples/templates to quickly get started with the IoT services. To display the data coming from the ESP8266 I have used the starter kit to read the data from the IoT Services tables and to view them in a line-chart.

To get the visualization application going, you clone the official starter kit GIT repository using:


git clone https://github.com/SAP/iot-starterkit.git

Once you have cloned it, it is best to import it into Eclipse. The Java/SAPUI5 application used for visualization can be found in directory iot-starterkit/src/apps/java/consumption/com.sap.iot.starterkit.ui. In that directory you’ll find a src/main/webapp/index.html that slightly needs to be modified so that it displays your device and values.

In this section, the device ID needs to be replaced with your device ID:


oSettingsModel.setData({
    "deviceId" : "51a6bc16-79ba-497e-9632-f976067360da",
    "deviceTypeId" : "2a877eada3d2c8557f07",
    "fromDeviceMessageTypeId" : "1",
    "toDeviceMessageTypeId" : "2",
});

Once you have that in place you will need to adjust the “graph” settings to match the values of our message type as well as our data values, change Slider Value to Temperature.


function createMeasureFeed() {
    return new sap.viz.ui5.controls.common.feeds.FeedItem({
        "uid" : "primaryValues",
        "type" : "Measure",
        "values" : [ "Temperature" ]
    });
}

And:


function createDataSet() {
    return new sap.viz.ui5.data.FlattenedDataset({
        dimensions : [ {
            name : "Timestamp",
            value : "{sensor>G_CREATED}"
        } ],
        measures : [ {
            name : "Temperature",
            value : "{sensor>C_TEMPERATURE}"
        } ],
        data : {
            path : "sensor>/"
        }
    });
}

Once these changes have been made, you can click on the project name com.sap.iot.starterkit.ui in the project explorer and right click Run As Maven Build. Eclipse will then pop-up with a dialog in which you can enter goals. To build the application, you should enter goal “clean install”, which will generate a WAR file in the target directory.

This war file can be deployed on the SAP Hana Cloud Platform through the Account Cockpit, by selecting “Java applications” from the menu, and then clicking the “Deploy Application” from the “Java Applications” panel. When you have uploaded the war file, it is not necessary to start it yet, as it needs to be bound to the data sets collected by the IoT service.

To bind the applicaton to the IoT dataset, you’ll have to select the application and pick “Data Source Bindings” from the navigation menu. If there are any bindings, delete them first. Then click on “New Binding” to create a binding to the IoT dataset. During creation the data source should be “Default”, meaning that this is the default data source from the app. The “Schema ID” will have to be the schema of the IoT service. In my case, I had to select “p508741trial.iotmms.web”.

Once the data source has been bound to the IoT service, the application can be started and launched in a browser. If you have had the ESP8266 running for a while, something similar to this should be the result:

Shameless plug

I have written a few other blogs on getting started with the ESP8266. They are going in depth on how to setup a tool chain on Linux and Windows and how to connect the ESP8266 with MQTT for home automation. If you're interested, please feel free to browse around:

7 Comments
Labels in this area