Logic Machine Forum
Local connection to powerwall API - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Local connection to powerwall API (/showthread.php?tid=4751)



Local connection to powerwall API - LucioB81 - 05.05.2023

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


RE: Local connection to powerwall API - admin - 08.05.2023

Your second script has an incorrect IP address - 1.1.1.1 instead of 10.51.10.224


RE: Local connection to powerwall API - LucioB81 - 08.05.2023

(08.05.2023, 06:18)admin Wrote: Your second script has an incorrect IP address - 1.1.1.1 instead of 10.51.10.224

Ahem my bad, I wanted to obfuscate that as well but I didn't do the first one.   Rolleyes
Should have used the right one from the start, it's in a private network anyways