Marijn78

Untitled

Jul 7th, 2020
47
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. String getRequest(String url2) {
  2.   unsigned int wait = 1000;
  3.   if (url2.substring(1, 7).equals("lights")) {
  4.     wait = 100;  
  5.   } else if (url2.substring(1, 7).equals("groups")) {
  6.     wait = 500;    
  7.   } else if (url2.substring(1, 7).equals("scenes")) {
  8.     wait = 500;    
  9.   } else if (url2.substring(1, 8).equals("sensors")) {
  10.     wait = 500;    
  11.   }
  12.  
  13.   while (millis() < bridgeMillis + wait) {
  14.   }
  15.   //bridgeMillis = millis();
  16.   if (!clientConnect("getRequest")) {
  17.     return "error"; //TODO!
  18.   }
  19.   bridgeMillis = millis();
  20.   client.print(String("GET ") + "/api/" + hueBridgeUser + url2 + " HTTP/1.1\r\n" +
  21.                "Host: " + hueBridgeHost + "\r\n" +
  22.                "\r\n");
  23.   String json;
  24.   String line;
  25.   int retCode = 0;
  26.   while (client.connected()) {
  27.     line = client.readStringUntil('\n');
  28.     if (line.startsWith("HTTP/1.1")) {
  29.       retCode = line.substring(9, 12).toInt(); // Get HTTP return code
  30.       if (retCode != 200) {
  31.         if (debug) Serial.println("http return code: " + String(retCode));
  32.         return "error";
  33.       }
  34.     }
  35.     if (line == "\r")break;
  36.   }
  37.   while (client.available()) {
  38.     line = client.readStringUntil('\n');
  39.     line = client.readStringUntil('\n');
  40.     json = line;
  41.     line = client.readStringUntil('\n');
  42.     line = client.readStringUntil('\n');
  43.     if (line == "\r")break;
  44.   }
  45.   return json;
  46. }
RAW Paste Data