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.

Local connection to powerwall API
#1
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:
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
When I try to use the token with this function:
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
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
Reply


Messages In This Thread
Local connection to powerwall API - by LucioB81 - 05.05.2023, 20:18

Forum Jump: