Measurement Simulation Using Python and MQTT

    This article provides the solution to simulate measurements from Cumulocity edge and Cumulocity Cloud. Python scripts are attached to generate data for following scenarios:

  1. Simulate New Measurements

  2. Simulate Measurements from Existing data

1.Simulate New Measurements:

    Attached file SimulateNew.py can be used for creating new measurements in bulk. This Python script creates the device if device is not present and sends the given measurements to the device.

    Apart from SimulateNew.py file, following 2 files are required for measurements generation:

  • Data sheet  ( to provide the details about the required device name, measurement data and wait time between measurements)
  • Configuration file ( to provide the tenant and other configurations)

1.1 Pre-requisite:

    Valid tenant, a user, and a password in order to access Cumulocity Application (Edge/Cloud)

1.2 Steps to Execute:

 1. Create a CSV file with following columns and required data in rows. File name can be any name, which we will passed as argument to the python scripts

Device Name Fragment Type Unit Measurement Wait Time in Sec
         

Example: Below is the CSV data to simulate measurements for device:NewRebort1 with fragment type:c8y_TemperatureMeasurement and temperature in Celsius 

  1. Configure the tenant and other details in config.properties file:

Following are the details about each key:

  • brokerAddress = For Edge tenant, add the URL without http( example: 192.168.56.120) and for Cloud version, prefix “mqtt.” Before the url(Example: mqtt.adamos-dev.com)
  • port = 1883
  • user =  for edge username should be like “edge/{tenantusername}” and for Cloud version, it should be like “{tenantName}/{tenantUsername}”
  • password = {tenantPassword}
  • headerRowIncluded = yes / no (if yes, it consider data from second row by ignoring first row as header row)
  • repeatAfterEndOfFile = yes/no (if yes, it repeats from starting after reaching end of file)
  1. Run the SimulateNew.py python file by passing csv file path as argument, which will connect to the broker and create devices(if not present) and sends measurements.

          To stop the execution in between, press Ctrl+Break keys

  4. Created measurements can be verified in the Cumulocity devices page as shown in below picture:

2.Simulate Measurements from Existing data

    Attached file SimulateExisting.py will be used to simulating existing measurements. Apart from the python file following 2 files are required:

  • Measurement file  ( Downloaded measurement data in the form of CSV file from Cumulocity tenant)
  • Configuration file ( to provide the tenant and other configurations)

2.1 Pre-requisite:

  • Valid tenant, a user, and a password in order to access Cumulocity
  • Device with measurements should be available in Cumulocity Tenant

2.2 Download the Existing Measurement data:

  Following Steps are useful to download the measurements from existing data:

  1. Login to cumulocity tenant and navigate to Device Management page
  2. In All devices tab, click on the device name to navigate to device details page

  1. Select the required time range from the measurement section as shown in the below picture

  1. Download the CSV file using the “Download CSV” option displayed in the top right corner

  1. Downloaded CSV will have data like below:

2.3 Send Measurements Using Python

  1. Configure tenant details and other keys in config.properties file. More details are provided in section:1.2.

  1. Run the “SimulateExisting.py” python file by passing Downloaded csv file path as argument. Python script creates the device if the device is not present and sends the measurements as per the inputted CSV file in the same interval as per the test data. Press Ctrl+Break to stop the script running in middle

  1.  Created measurements can be verified in the device details page of selected device

 

 

config.properties (185 Bytes)

NewData.csv (238 Bytes)

SimulateExisting.py (3.87 KB)