REST API Samples

HTML / Javascript (jquery) Examples

Get all devices

<html>

<head>

<title>smart-me REST API Sample</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

</head>

<body>

<h1>Get all devices</h1>

<ul id="DeviceList">

</ul>


<script type="text/javascript">

var smartmeUserName = "YOUR_USERNAME";

var smartmePassword = "YOUR_PASSWORD";


function GetAllDevices() {

var targetUrl = "https://api.smart-me.com/api/Devices/";


$.ajax({

url: targetUrl,

type: "get",

cache: false,

headers: {

"Authorization": "Basic " + btoa(smartmeUserName + ":" + smartmePassword)

},

dataType: "json",

error: function(jqXHR, exception) {

alert(exception);

},

success: function(json) {


json.forEach(function(element) {

// Do something

$("#DeviceList").append($("<li>").text(element.Name + ": " + element.CounterReadingImport + " " + element.CounterReadingUnit));

});

}

});

}


// On document load

$(document).ready(function() {


// Get all smart-me devices

GetAllDevices();


});

</script>


</body>

</html>

Create and update device

<html>


<head>

<title>smart-me REST API Sample</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

</head>


<body>

<h1>Create and update devices</h1>


<button type="button" id="CreateNewDeviceButton">Create new device</button>


<script type="text/javascript">

var smartmeUserName = "YOUR_USERNAME";

var smartmePassword = "YOUR_PASSWORD";


var counterValueImport = 0.0;

var counterValueExport = 0.0;

var activePowerTempValue = 0.1;


// The ID of the device

var deviceID = null;


function GetElectricityDevice(id, power, energyImport, energyExport) {

var deviceObject = new Object();


if (id != null) {

// Object with an ID updates the existing device. Object without an ID creates a new one

deviceObject.ID = id;

}


// Add all data

deviceObject.Name = "My test device";

deviceObject.Serial = 12345678;

deviceObject.DeviceEnergyType = "MeterTypeElectricity"; // MeterTypeWater for Water, MeterTypeGas for Gas, MeterTypeHeat for Heat


// Set active power in kW

deviceObject.ActivePower = power;


// Set countervalues import (in kWh)

deviceObject.CounterReading = energyImport;


// Set countervalues export (in kWh)

deviceObject.CounterReadingExport = energyExport;


return deviceObject;

}


function CreateUpdateDevice(deviceObject) {


var targetUrl = "https://api.smart-me.com/api/Devices/";


var postData = JSON.stringify(deviceObject);


$.ajax({

url: targetUrl,

type: "post",

cache: false,

headers: {

"Authorization": "Basic " + btoa(smartmeUserName + ":" + smartmePassword)

},

dataType: "json",

contentType: "application/json; charset=utf-8",

data: postData,

error: function(jqXHR, exception) {

alert(exception);

},

success: function(json) {


// The device was created or updated

deviceID = json.Id;

$('#CreateNewDeviceButton').text("Update device");


alert("OK! device ID: " + json.Id);

}

});

}


// On document load

$(document).ready(function() {


$('#CreateNewDeviceButton').click(function(e) {

// No Postback

e.preventDefault();


// Add some random values

activePowerTempValue += 0.1;

counterValueImport += 1.0;

counterValueExport += 0.5;


var electricityDevice = GetElectricityDevice(deviceID, activePowerTempValue, counterValueImport, counterValueExport);

// Send to cloud

CreateUpdateDevice(electricityDevice);


});


});

</script>


</body>


</html>

Create or update devices

Heat meter

API Method: 'POST /api/devices'

Payload:

{

"Id": "b0ec0030-9f97-4c3c-897f-8175074177bc",

"Name": "Wärme Zähler Test",

"Serial": 2233445566,

"DeviceEnergyType": "MeterTypeHeat",

"ActivePower": 5.89,

"CounterReading": 1234.56,

}

To create a device don't send a ID.