Using Rest APIs in OpenEMS

Hello All :),
I would like use OpenEMS to monitor a PV device via Rest APIs. I’m not able to find satisfactory documentation about the setup and use of the controllers available. Specifically:

How do I start the API service? [Is it running by default/ do I make a change in the config files]
When I save a controller in the Apache ConfigManager, I get webSocket errors.

In general, are there any code samples/Documentation/Guides for how to use the various components? How do I go about getting familiar with the OpenEMS environment?

Warm Regards,
Dipti

Hello,
no there are not so many documentations out, yet. Cause most of us are full with workload :D.
just start the alphabeticSchedular and the “Rest Api”, than you can talk to openems by rest.

for example http://localhost:8084/rest/channel/meter0/ActivePower with an meter with id “meter0” you get so the ActivePower.

“When I save a controller in the Apache ConfigManager, I get webSocket errors.” this is a bit to less information to help you ;).

1 Like

You example above does not work. “http://localhost:8084/rest/channel/meter0/ActivePower
Authentication Failed. See my full question below, please.

I am trying to use the REST-Api controller to get data from my system, as documented here

I am unable to get authentication to work. The documentation mentions using admin or user as passwords but neither work. We do actually have a user authentication system with users/passwords but none of those work either.

The Document says to use the chrome add-on Talend API. If I try the recommended format
http://x:user@192.168.1.10:8084/rest/channel/_sum/EssSoC
Talend does nothing, fails… does not send a request at all.

If I remove x:user and send
http://192.168.1.10:8084/rest/channel/_sum/EssSoC
Talend sends the command and Edge responds
{“jsonrpc”:“2.0”,“id”:“00000000-0000-0000-0000-000000000000”,“error”:{“code”:1003,“message”:“Authentication failed”,“data”:}}

Using a web browser to send the request
http://x:user@192.168.1.10:8084/rest/channel/_sum/EssSoC
gets the same response as above
{“jsonrpc”:“2.0”,“id”:“00000000-0000-0000-0000-000000000000”,“error”:{“code”:1003,“message”:“Authentication failed”,“data”:}}

In conclusion:
Talend doesn’t work at all if I put in the x:user

Using a browser sends the command to Edge but authentication fails.

Please, how do I get this to work? How do I authenticate with the REST-Api controller

Thanks,
James

1 Like

I’m not at all technical so these are just guesses, please let me know how far wrong I am.

What does the "x:user " relate to? it kinda looks like a file path but also doesn’t and I don’t think a file path has a place in a command like this.

I believe that the authentication we use is only on the backend whereas the REST API is in edge. So I don’t see that it will be the same authentication system/credentials.

Which leaves me to question:
*What authentication is the API looking for?
*Where is the API verifying the data for authentication ?
If we can find that maybe we can begin to understand the issue?

Auth

you need to Auth @ the Edge Device locally so x:user is okay

x:user does not work, neither does x:admin, also mentioned in the documentation. Can you please tell what will work?

Thanks.

Hi,
You should add an HTTP Basic Auth header with the username and password to your request. The HTTP standard has changed and putting the password and user directly in the URL won’t work anymore.
Send the request to http://192.168.1.10:8084/rest/channel/_sum/EssSoC with the Basic Auth header.

Best

3 Likes

Try this, it will work: http://admin:admin@127.0.0.1:8084/rest/channel/meter0/ActivePower
and also I discussed about Rest Api endpoints before, it might be helpful: Using REST Api to get static info of the devices

This: http://admin:admin@127.0.0.1:8084/rest/channel/meter0/ActivePower will NOT work, as the above poster noted, you can no longer put the username:address in the url. It has to be added as he instructed, such as below.

You have to encode the username:password in Base64 using Basic Authentication Header Generator
In this case I encoded x:admin

Then use an api tester (Talend addon to chrome) as below to construct the GET message. (the address of my Edge server is 192.168.2.12)

4 Likes

Additionally, here is some python code I wrote to set a value and read it back. I read it back as the response to the POST gives no indication that it was successful. Also note that “name”: “power” is not documented anywhere I could find. All the channels revealed on the display are Read Only (eg _PropertyPower). I guessed at the word ‘power’ by looking at the debug example and looking in the code.

I hope this helps someone…

import requests
from requests.auth import HTTPBasicAuth
import time

url = "http://192.168.2.12:8084"
getEndpoint = "/rest/channel/ctrlFixActivePowerRESTapi/_PropertyPower"
postEndpoint = "/jsonrpc"
auth = HTTPBasicAuth('x', 'admin')
headers = {
'Content-Type': 'application/json'
}

#power in watts
power_W = 12345

print (power_W)

data = {
"method": "updateComponentConfig",
"params": {
"componentId": "ctrlFixActivePowerRESTapi",
"properties": [{
"name": "power",
"value": power_W
}]
}
}

postresponse = requests.post(url+postEndpoint, headers=headers, json=data, auth=auth)
print(postresponse.status_code)
print(postresponse.json())

#need a small delay before sending the next request, this for OEMS to process the power setting request
time.sleep(1)

getresponse = requests.get(url+getEndpoint, auth=auth)
print (getresponse.status_code)
print (getresponse.json())
3 Likes

Basically same as here:

1 Like