Using tplinkcloud API from ESP32
Hi Folks,
So I've got my first device up and running which is great, but I would like to control it from an ESP32 I'm using locally. So I've been trying to access the TP Link Cloud API and I'm not having much luck.
I've found snippets of code around on Github that claim to do this, but its a bit foggy and I dont feel like there is a lot of good clear official documentation.
I can access it from the Kasa application, and I can even access the API using curl, but from the ESP32 I always seem to get a http 400.
The login step to get the token looks like this:
const char *tplinkCertificate = "-----BEGIN CERTIFICATE-----\n" \ "MIIDjjCCAnagAwIBAgIQAzrx5qcRqaC7KGSxHQn65TANBgkqhkiG9w0BAQsFADBh\n" \ "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n" \ "d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBH\n" \ "MjAeFw0xMzA4MDExMjAwMDBaFw0zODAxMTUxMjAwMDBaMGExCzAJBgNVBAYTAlVT\n" \ "MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j\n" \ "b20xIDAeBgNVBAMTF0RpZ2lDZXJ0IEdsb2JhbCBSb290IEcyMIIBIjANBgkqhkiG\n" \ "9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuzfNNNx7a8myaJCtSnX/RrohCgiN9RlUyfuI\n" \ "2/Ou8jqJkTx65qsGGmvPrC3oXgkkRLpimn7Wo6h+4FR1IAWsULecYxpsMNzaHxmx\n" \ "1x7e/dfgy5SDN67sH0NO3Xss0r0upS/kqbitOtSZpLYl6ZtrAGCSYP9PIUkY92eQ\n" \ "q2EGnI/yuum06ZIya7XzV+hdG82MHauVBJVJ8zUtluNJbd134/tJS7SsVQepj5Wz\n" \ "tCO7TG1F8PapspUwtP1MVYwnSlcUfIKdzXOS0xZKBgyMUNGPHgm+F6HmIcr9g+UQ\n" \ "vIOlCsRnKPZzFBQ9RnbDhxSJITRNrw9FDKZJobq7nMWxM4MphQIDAQABo0IwQDAP\n" \ "BgNVHRMBAf8EBTADAQH/MA4GA1UdDwEB/wQEAwIBhjAdBgNVHQ4EFgQUTiJUIBiV\n" \ "5uNu5g/6+rkS7QYXjzkwDQYJKoZIhvcNAQELBQADggEBAGBnKJRvDkhj6zHd6mcY\n" \ "1Yl9PMWLSn/pvtsrF9+wX3N3KjITOYFnQoQj8kVnNeyIv/iPsGEMNKSuIEyExtv4\n" \ "NeF22d+mQrvHRAiGfzZ0JFrabA0UWTW98kndth/Jsw1HKj2ZL7tcu7XUIOGZX1NG\n" \ "Fdtom/DzMNU+MeKNhJ7jitralj41E6Vf8PlwUHBHQRFXGU7Aj64GxJUTFy8bJZ91\n" \ "8rGOmaFvE7FBcf6IKshPECBV1/MUReXgRPTqh5Uykw7+U0b6LJ3/iyK5S9kJRaTe\n" \ "pLiaWN0bfVKfjllDiIGknibVb63dDcY3fe0Dkhvld1927jyNxF1WW6LZZm6zNTfl\n" \ "MrY=\n" \ "-----END CERTIFICATE-----\n"; const char* api_url = "https://wap.tplinkcloud.com"; const char* uuid = "myUUID"; const char* cloudUserName = "myUsername"; const char* cloudPassword = "myPassword"; char body[2048]; sprintf(body,"{ \"method\":\"login\",\"params\":{\"appType\":\"Kasa_Android\",\"cloudUserName\":\"%s\",\"cloudPassword\":\"%s\",\"terminalUUID\":\"%s\"}}", cloudUserName, cloudPassword, uuid); HTTPClient http; // http.begin("https://postman-echo.com/post", postmanCertificate); http.begin("https://wap.tplinkcloud.com", tplinkCertificate); http.addHeader("Content-Type", "application/json"); http.setUserAgent("Dalvik/2.1.0 (Linux; U; Android 6.0.1; A0001 Build/M4B30X)"); // tlsVersion: "TLSv1.1" ? int result = http.POST(body); Serial.print("Response Code: "); Serial.println(result); String payload = http.getString(); Serial.print("Payload: "); Serial.println(payload); http.end();
As you can see, I tried testing with postman-echo and it seems to work, echoing back the paylod. One site suggested the TLS version could be the reason its been rejected but before I dig into the bowels of the C libraries that do this, is there anything else obvious I'm missing ?
Is there any better documentation ?
Does anyone have suggestions for debugging this ?