Hello everyone,
I am trying to connect to my Tesla Powerwall gateway through local network. I can get the authorization token with the following code:
When I try to get the charge percentage, I use the following code:
When I try to use the token with this function:
I get the following result in the logs:
* arg: 1
* string: send
* arg: 2
* nil
* arg: 3
* string: wantread
I cannot understand what is that wantread error, how can I solve this issue? I tried in postman and it works.
Thanks
I am trying to connect to my Tesla Powerwall gateway through local network. I can get the authorization token with the following code:
When I try to get the charge percentage, I use the following code:
Code:
function getAccessToken()
IP = '10.51.10.224'
data = '{"username":"customer","password":"XXXXX","email":"my.adderss@gmail.com","force_sm_off":false}'
socket = require('socket')
ssl = require('ssl')
json = require('json')
params = {
mode = 'client',
protocol = 'tlsv1_2',
verify = 'none'
}
client = socket.tcp()
client:settimeout(1)
res, err = client:connect(IP, 443)
ctx = ssl.newcontext(params)
client = ssl.wrap(client, ctx)
client:settimeout(1)
res, err = client:dohandshake()
req =
'POST /api/login/Basic HTTP/1.1' .. '\r\n' .. 'Host: ' .. IP .. '\r\n' .. 'User-Agent: curl/7.64.1' .. '\r\n' ..
'Accept: */*' .. '\r\n' .. 'Connection: close' .. '\r\n' .. 'Content-Type: application/json' .. '\r\n' ..
'Content-Length: ' .. #data .. '\r\n\r\n' .. data
res, err = client:send(req)
res, err, partial = client:receive('*a')
local headers, response_body = res:match("^(.-)\r\n\r\n(.*)$")
local token = json.decode(response_body)['token']
client:close()
return token
end
Code:
function getSoe()
IP = '10.51.10.224'
socket = require('socket')
ssl = require('ssl')
json = require('json')
params = {
mode = 'client',
protocol = 'tlsv1_2',
verify = 'none'
}
token = getAccessToken()
client = socket.tcp()
client:settimeout(1)
res, err = client:connect(IP, 443)
ctx = ssl.newcontext(params)
client = ssl.wrap(client, ctx)
client:settimeout(1)
res, err = client:dohandshake()
req = 'GET /api/system_status/soe HTTP/1.1' .. '\r\n' .. 'Host: ' .. IP .. '\r\n' .. 'User-Agent: curl/7.64.1' ..
'\r\n' .. 'Accept: */*' .. '\r\n' .. 'Connection: close' .. '\r\n' .. 'Authorization: Bearer ' .. token ..
'\r\n'
res, err = client:send(req)
res, err, partial = client:receive('*a')
log('send', res, err)
if res == nil then
return nil
end
end
* arg: 1
* string: send
* arg: 2
* nil
* arg: 3
* string: wantread
I cannot understand what is that wantread error, how can I solve this issue? I tried in postman and it works.
Thanks