Page 1 of 1

HTTPS digit auth on the ESP32 based WT32-S1 ETH

Posted: Fri Mar 25, 2022 10:13 am
by _Rens_
For a project i am trying to make an HTTPS connection to Phillips TV using their jointspace API. I already got a working prototype with the http version on port 1925, but now i am trying to pair it on port 1926 via https. But i am getting an timeout code on the post request to start the digit auth.
  1. /*
  2.  
  3.    https://github.com/ldijkman/WT32-ETH01-LAN-8720-RJ45-
  4.  
  5.    HTTP client connection to phillips tv api
  6.    https://techtutorialsx.com/2017/05/19/esp32-http-get-requests/
  7.  
  8.    https://techtutorialsx.com/2017/05/20/esp32-http-post-requests/
  9.  
  10.    HTTPclient github
  11.    https://github.com/espressif/arduino-esp32/blob/master/libraries/HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino
  12.    https://github.com/espressif/arduino-esp32/blob/master/libraries/HTTPClient/src/HTTPClient.h
  13.    https://github.com/espressif/arduino-esp32/blob/master/libraries/HTTPClient/src/HTTPClient.cpp
  14. */
  15.  
  16.  
  17. #include <ETH.h>
  18. #include <ArduinoJson.h>    // Json converter
  19. #include <HTTPClient.h>     // Connection http
  20.  
  21. #define ETH_CLK_MODE    ETH_CLOCK_GPIO0_IN  // ETH_CLOCK_GPIO17_OUT
  22. #define ETH_POWER_PIN   16                  // The enable signal for the external crystal oscillator (-1 to disable for internal APLL source)
  23. #define ETH_TYPE        ETH_PHY_LAN8720     // Type of the Ethernet PHY LAN8720
  24. #define ETH_ADDR        1                   // I²C-address of Ethernet PHY
  25. #define ETH_MDC_PIN     23                  // The I²C clock signal for the Ethernet PHY
  26. #define ETH_MDIO_PIN    18                  // The I²C IO signal for the Ethernet PHY
  27. #define NRST            21                  // The nRST (enable) for the Ethernet PHY
  28. static bool eth_connected = false;          // Global variable to pull status of eth.
  29.  
  30. const int httpsPort = 1925;  //HTTPS= 443 and HTTP = 80, Phillips HTTPS= 1926 and HTTP = 1925
  31. int apiVersion[] = {1, 5, 6}; // possible api versions
  32. int apiv = 0;
  33.  
  34.  
  35. StaticJsonDocument<384> doc;
  36. String json;
  37.  
  38.  
  39. HTTPClient http;
  40. WiFiClient client;
  41. HTTPClient httpSecure;
  42.  
  43. void WiFiEvent(WiFiEvent_t event) // used for connecting the ethernet connection
  44. {
  45.   switch (event) {
  46.     case SYSTEM_EVENT_ETH_START:            //set eth hostname here, Called in setup
  47.       Serial.println("ETH Started");
  48.       ETH.setHostname("esp32-ethernet");
  49.       break;
  50.     case SYSTEM_EVENT_ETH_CONNECTED:        // Called when cable is connected
  51.       eth_connected = true;               // Set the flag true if cable is connected
  52.       Serial.println("ETH Connected");
  53.       break;
  54.     case SYSTEM_EVENT_ETH_GOT_IP:           // Called when DHCP gifted the IP
  55.       Serial.print("ETH MAC : ");
  56.       Serial.println(ETH.macAddress());
  57.       Serial.print("IPv4    : ");
  58.       Serial.println(ETH.localIP());
  59.       break;
  60.     case SYSTEM_EVENT_ETH_DISCONNECTED:     // Called when cable is disconnnected
  61.       Serial.println("ETH Disconnected");
  62.       eth_connected = false;
  63.       break;
  64.     case SYSTEM_EVENT_ETH_STOP:             // Not yet seen
  65.       Serial.println("ETH Stopped");
  66.       eth_connected = false;
  67.       break;
  68.     default:
  69.       break;
  70.   }
  71. }
  72.  
  73. void setup()
  74. {
  75.   Serial.begin(115200);
  76.   WiFi.onEvent(WiFiEvent);
  77.   ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE);
  78.   http.begin("tvip", 1925, "/6/ambilight/power");
  79.   httpSecure.begin(client, "tvip", 1926, "", true);
  80.  
  81.  
  82.   JsonArray scope = doc.createNestedArray("scope");
  83.   scope.add("read");
  84.   scope.add("write");
  85.   scope.add("control");
  86.  
  87.   JsonObject device = doc.createNestedObject("device");
  88.   device["device_name"] = "heliotrope";
  89.   device["device_os"] = "Android";
  90.   device["app_name"] = "ESP32api";
  91.   device["type"] = "native";
  92.   device["app_id"] = "app.id";
  93.   device["id"] = "HWvAq8UdGxbGbsH7";
  94.  
  95.   serializeJson(doc, json);
  96. }
  97.  
  98.  
  99.  
  100. void loop()
  101. {
  102.   int httpCode;
  103.   if (eth_connected)
  104.   {
  105.  
  106.     if (apiv == 0)
  107.     {
  108.       for (int i = 0; i < 3; i++)
  109.       {
  110.         Serial.printf("Sending GET api version %s\n", String(apiVersion[i]));
  111.         http.setURL(("/" + String(apiVersion[i]) + "/system"));
  112.         httpCode = http.GET();
  113.         if (httpCode == HTTP_CODE_OK)
  114.         {
  115.           String payload = http.getString();
  116.           Serial.println(payload);
  117.           apiv = apiVersion[i];
  118.           http.end();
  119.           break;
  120.         }
  121.       }
  122.     }
  123.     else if (apiv != 0)
  124.     {
  125.       delay(1000);
  126.       Serial.println("Sending pair request to https");
  127.       httpSecure.setURL(("/" + String(apiv) + "/pair/request"));
  128.  
  129.       httpCode = httpSecure.POST(json);  // returns code -11 aka timeout
  130.       Serial.println(httpCode);
  131.  
  132.       payload = httpSecure.getString();  // no payload
  133.       Serial.println(payload);
  134.     }
  135.   }
  136.   else {
  137.     Serial.println("Pending");
  138.   }
  139.   delay(1000);
  140. }