Skip to Content
Technical Articles
Author's profile photo Siva rama Krishna Pabbraju

Upload sensor data to IoT Service Cockpit– Cloud Foundry using Python

 There are three steps involved in uploading Sensor data to IOT Service Cockpit

  1. Prepare Sensor data
  2. Create Sensor details in IoT Service Cockpit – Cloud Foundry                      https://sap.github.io/cloud-s4ext/week-5/unit-2/                       https://developers.sap.com/tutorials/iot-express-2-create-device-model.html [ Follow steps mentioned in above links to create capabilities, Device type, Sensor and Device]
  3. Upload data to the Device created in the IoT Service Cockpit

 

This blog focuses on “Upload data to the Device created in the IoT Service Cockpit”.

 

Note down below information from the IoT Service Cockpit.

  1. Instance Configuration [ It is host id of the SCP IoT Service Cockpit URL]If URL is  eg, https://d3ffgzx-7dx8-x35e-bxz3a-3zzx2be7b27dab.eu10.cp.iot.sap/dd3ffgzx-7dx8-x35e-bxz3a-3zzx2be7b27dab/iot/cockpit/#/tenant/5125678Then Instance Configuration = ‘d3ffgzx-7dx8-x35e-bxz3a-3zzx2be7b27dab.eu10.cp.iot.sap’
  2. User id [User id used to login into IoT Service Cockpit]
  3. Password [Password used to login into IoT Service Cockpit]
  4. Alternative id for the Device
  5. Alternative id for the Capability
  6. Alternative id for the Sensor
  7. Id for the Device

 

We have three steps to upload data to IoT Service Cockpit.

  1. Read the Sensor data into Json array from local file
  2. Retrieve RSA Certificate of the Device
  3. Upload the Sensor data to the IoT Service Cockpit

 

1. Read the Sensor data into Json Array from Local file:

import pandas as pd

#Upload excel data from Local Folder into a data frame
df = pd.read_excel('D:\IOT\IOT_File.xlsx')

#Convert data from data frame into Json format
xs = df.to_json(orient='records')

 

2. Retrieve RSA Certificate of the Device

import requests
import json

#define variables
#config_instance = ‘ ‘     #replace with your Instance Configuration and uncomment the line
#my_device       = ‘ ‘     #replace with your device id and uncomment the line
certfile_name    = './cert.pem' 

#Config_instance is a variable which holds value of Instance Configuration
#my_device is a variable which holds value of device id
request_url = 'https://' + config_instance + '/iot/core/api/v1/devices/' + my_device + '/authentications/clientCertificate/pem'

headers = {'Content-Type' : 'application/json'}

#User is a variable with value of the User 
#password is a variable with value of password 
response = requests.get(request_url, headers=headers, auth=(user, password))

status_code = response.status_code
#print(str(status_code) + " " + str(response.text))

if (status_code == 200):
    try:
        json_payload = json.loads(response.text)
        
        #Get Secret password
        secret       = json_payload['secret']
        
        #Get pem certificate
        pem          = json_payload['pem']
        
        #print('secret: ' + secret)
        # print('pem: ' + pem)
        #Open file ’cert.pem’ in Write mode
        #File will be created if file doesn’t exist. If exists, it will be overwritten
        #File will be created in the current working directory of the python 
        certfile     = open("cert.pem", "w")
        
       #Write pem certificate into the file
        certfile.write(pem)
        
       #Close the fie
       certfile.close()

       #open file ‘convert_pem.sh’ in Write mode
       #File will be created if file doesn’t exist. If exists, it will be overwritten
       #File will be created in the current working directory of the python 
       pem_script = open("convert_pem.sh", "w")

       #Write Secret Password into the file as a comment
       pem_script.write("echo 'Please use pass phrase " + secret + " for the certificate import      
       from " + certfile_name + " in the conversion !'\n\n")

       #Write openssl command to get rsa certificate key
       #certfile_name is a variable = './cert.pem'
       pem_script.write("openssl rsa -in " + certfile_name + " -out credentials.key\n")
       
       #Write openssl command to get x509 certificate 
       #certfile_name is a variable = './cert.pem' 
       pem_script.write("openssl x509 -in " + certfile_name + " -out credentials.crt\n")
       
       #Close the file
       pem_script.close()

       #Catch exceptions in ValueError and print them
       except (ValueError) as e:
                print(e)
       else:
         exit(0)

This Step has additional sub steps. From the above logic we have downloaded cert.pem file and convert_pem.sh file. Now, we need to run openssl commands as per the file convert_pem.sh.

convert_pem.sh file will look like below one

 

Install openssl software from below link

https://developers.sap.com/tutorials/iot-cf-install-openssl.html

 

Click on openssl application and follow below steps

 

  1. Enter first command line as below                                                                                              rsa -in C:\Users\132384\eclipse-workspace\CheckValues\cert.pem -out credentials.key

In my case Current working folder of python is ‘C:\Users\132384\eclipse-            workspace\CheckValues’

So cert.pem file exists in this folder. So, replace ./cert.pem with entire path

 

2. It will ask for the password     

Enter the Secret password mentioned in the convert_pem.sh file.

In my case it will be ‘8t2LlfshXy5Lhxo4oMfW0k8b5sjHr9Z9vQwI’

When you copy paste the password, it will not display anything. Just press enter after copy paste of the password

 

3.  RSA key file is generated with file name “credentials.key” in the current working directory of the  python

 

4. Similarly, repeat steps to create “credentials.crt” file for below command

x509 -in ./cert.pem -out credentials.crt

 

Now, we will have two files credentials.key and credentials.crt created in the working directory of the python

 

3. Upload the Sensor data to the IoT Service Cockpit

import requests
import json
import pandas as pd

#Upload excel data from Local Folder into a data frame
df = pd.read_excel('D:\IOT\IOT_File.xlsx') 

#Convert data from data frame into Json format
xs = df.to_json(orient='records')


#Declare Variables
#config_instance          = 'd3189576-7df8-435e-bf3a-302be7b27dab.eu10.cp.iot.sap'
#user                     = 'xxxxxx'
#password                 = 'sap12345'       
#alternateId_4_device     = 'ed8d2012-a7e6-452a-83d1-93e21cc7cc89'
#alternateId_4_capability = '44d82e82-603c-4c64-a644-96dcb744cc55'
#alternateId_4_sensor     = '75da8da6-cc64-4827-837f-561d500cb2a5'
 

#config_instance is a variable with value Instance Configuration
#alternateId_4_device is a variable with value Alternative id of the device
#alternateId_4_capability is a variable with value Alternative id of the capability
#alternateId_4_sensor is a variable with value Alternative id of the sensor
request_url =  'https://' + config_instance + '/iot/gateway/rest/measures/' + alternateId_4_device

#xs fil is dataframe in json format
payload = '{ \"capabilityAlternateId\" : \"' + alternateId_4_capability + '\", \"measures\" :' + xs  + ', \"sensorAlternateId\": \"' + alternateId_4_sensor + '\" }'

#print(payload)

headers={'Content-Type' : 'application/json'}

#As files credentials.crt and credentials.key are available in the current working directory of #python
#we can directly use the files names as ./credentials.crt and ./credentials.key
response = requests.post(request_url, data=payload, headers=headers, cert=('./credentials.crt', './credentials.key'))

print(response.status_code)
print(response.text)

 

References:

https://github.com/SAP/iot-starterkit/blob/master/cf/samples/python-samples/templates-iots-cf/template-upstream-rest/iots-cf-template-upstream-rest.py

 

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.