This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Easee EV Charger
#9
Code:
-- Load modules
require('json')
require('ssl.https')
require('ltn12')

-- Set credentials for Easee API
ChargerID = storage.get('EaseeAPI_ChargerID')
API_Token = storage.get('EaseeAPI_Token')  --reads API Token from storage, The token is fetched from scheduled script: "Easee_Authenticate"
--log(ChargerID)


function RequestFromEasee(request)
  request_url = 'https://api.easee.cloud/api/' .. request
  local response_body = {}
  local request_body = ""
  local body, code, hdrs, stat = ssl.https.request{
    url = request_url;
    method = "GET";
    headers =
    {
      ["Content-Type"] = "application/json";
      ["Authorization"] = "Bearer " .. API_Token;
    };
    source = ltn12.source.string(request_body);
    sink = ltn12.sink.table(response_body);
  }
  log(code)
  if code == 200 then
    ret = table.concat(response_body)
    ret = json.pdecode(ret)
    return ret
  else
    --API_Token = GetToken() -- request a new token
  end

end


-- Get products, run once to get charger ID
if ChargerID == nil then
chargers = RequestFromEasee('chargers')
Easee_ChargerID = chargers[1]["id"]
--log(Easee_ChargerID, 'ID') -- Check here for your charger ID
storage.set('EaseeAPI_ChargerID', Easee_ChargerID)

end

ChargerState = RequestFromEasee('chargers/' .. ChargerID  .. '/state')
log(ChargerState)
currentChargePower = ChargerState["totalPower"]
currentCableRating = ChargerState["cableRating"]
currentMaxPower = ChargerState["outputCurrent"]
chargerOnline = ChargerState["isOnline"]
currentChargeEnergy = ChargerState["sessionEnergy"]

log(currentChargePower, currentCableRating, currentMaxPower, chargerOnline, currentChargeEnergy)




function WriteToEasee(request, request_body)
  request_url = 'https://api.easee.cloud/api/' .. request
  local response_body = {}
  local content_length

  if type(request_body) == "string" then
    content_length = #request_body
  else
    request_body = ""
  end

  local body, code, hdrs, stat = ssl.https.request{
    url = request_url;
    method = "POST";
    headers =
    {
      ["Content-Type"] = "application/json";
      ["Authorization"] = "Bearer " .. API_Token;
      ["Content-Length"] = content_length;
    };
    source = ltn12.source.string(request_body);
    sink = ltn12.sink.table(response_body);
  }
  log(code)
  if code == 200 then
    ret = table.concat(response_body)
    ret = json.pdecode(ret)
    return ret
  else
    API_Token = GetToken() -- request a new token
  end
end

Code:
require('json')
require('ssl.https')
require('ltn12')

-- Set credentials for Easee API
email ='****************'
password ='*************'

function GetToken()
  request_token_url = 'https://api.easee.cloud/api/accounts/login'
  local response_body = {}
  local request_body = json.encode({
      userName = email,
      password = password,
})
  local body, code, hdrs, stat = ssl.https.request{
    url = request_token_url;
    method = "POST";
    headers =
    {
      ["Content-Type"] = "application/json";
      ["Content-Length"] = #request_body;
    };
    source = ltn12.source.string(request_body);
    sink = ltn12.sink.table(response_body);
  }
  log(code)
  if code == 200 then
    ret = table.concat(response_body)
    ret = json.pdecode(ret)
    log(ret)
    return ret.accessToken
  end
end

-- Request token
if not API_Token then
    API_Token = GetToken()
      log(API_Token, 'api_token')
end


--Store API Token to memory
storage.set('EaseeAPI_Token', API_Token)
Reply


Messages In This Thread
Easee EV Charger - by tomnord - 15.04.2021, 06:31
RE: Easee EV Charger - by admin - 15.04.2021, 13:04
RE: Easee EV Charger - by tomnord - 15.04.2021, 19:56
RE: Easee EV Charger - by P3774n - 11.01.2022, 19:47
RE: Easee EV Charger - by admin - 16.04.2021, 05:15
RE: Easee EV Charger - by Habib - 14.01.2022, 08:31
RE: Easee EV Charger - by tomnord - 28.01.2022, 16:28
RE: Easee EV Charger - by P3774n - 20.09.2022, 15:16
RE: Easee EV Charger - by tomnord - 07.10.2022, 16:51

Forum Jump: