Marijn78

Untitled

Jul 7th, 2020
62
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define mS_TO_S_FACTOR 1000  /* Conversion factor for milliseconds to seconds */
  2. int TIME_TO_SLEEP = 3000;
  3. unsigned int bridgeMillis = 0;
  4. #include <WiFi.h>
  5. const char* ssid     = "<SSID>";
  6. const char* password = "<PW>";
  7. #include <WiFiClientSecure.h>
  8. IPAddress local_IP(<local_ip>);
  9. IPAddress gateway(<gateway>);
  10. IPAddress subnet(<subnet>);
  11. IPAddress primaryDNS(<dns1>);
  12. IPAddress secondaryDNS(<dns2>);
  13. WiFiClientSecure client;
  14. const char* host     = "<ip>";
  15. const int httpPort = 443;
  16. const char* hueBridgeHost = "<ip>";
  17. const char* hueBridgeUser = "<api_key>";
  18. unsigned long scanStart;
  19. unsigned long scanDuration;
  20. unsigned int clientStart;
  21. unsigned int clientDuration;
  22. int counter = 0;
  23. unsigned const int wifiTimeoutSeconds = 5;
  24. RTC_DATA_ATTR int timeoutCount = 0;
  25. const bool debug = true;
  26.  
  27. void deepSleep() {
  28.   esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * mS_TO_S_FACTOR);
  29.   if (debug) {
  30.     Serial.println("Going to sleep now for " + String(TIME_TO_SLEEP) + " milliseconds");
  31.     Serial.println("");
  32.   }
  33.   if (debug) Serial.flush();
  34.   esp_deep_sleep_start();
  35. }
  36.  
  37. bool wifiConnect() {
  38.   scanStart = millis();
  39.   if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
  40.     if (debug) Serial.println("STA Failed to configure");
  41.     return false;
  42.   }
  43.   if (debug) {
  44.     Serial.println("Connecting to " + String(ssid));
  45.   }
  46.   WiFi.begin(ssid, password);
  47.   while (WiFi.status() != WL_CONNECTED) {
  48.     delay(500);
  49.     if (debug) Serial.print(".");
  50.     delay(500);
  51.     counter++;
  52.     if (counter >= wifiTimeoutSeconds) { //after x seconds timeout - reset board
  53.       if (debug) Serial.println("Wifi Connection Timeout..");
  54.       timeoutCount++;
  55.       return false;
  56.     }
  57.   }
  58.   if (debug) {
  59.     Serial.println("");
  60.     Serial.println("WiFi connected!");
  61.   }
  62.   scanDuration = millis() - scanStart;
  63.   if (debug) {
  64.     Serial.print("Duration wifiConnect: ");
  65.     Serial.println(String(scanDuration));
  66.   }
  67.   return true;
  68. }
  69.  
  70. bool clientConnect() {
  71.   clientStart = millis();
  72.   if (!client.connected()) {
  73.     if (debug) Serial.println("Starting connection to host " + String(host) + " port " + String(httpPort));
  74.     if (!client.connect(host, httpPort)) {
  75.       Serial.println("clientConnect connection failed");
  76.       delay(500); //new, for led visibility..
  77.       return false;
  78.     } else {
  79.       clientDuration = millis() - clientStart;
  80.       if (debug) Serial.println("Duration clientConnect " + String(clientDuration));
  81.       return true;
  82.     }
  83.   } else {
  84.     return true;
  85.   }
  86. }
  87.  
  88. String getRequest(String url2) {
  89.   unsigned int wait = 1000;
  90.   if (url2.substring(1, 7).equals("lights")) {
  91.     wait = 100;
  92.   } else if (url2.substring(1, 7).equals("groups")) {
  93.     wait = 500;
  94.   } else if (url2.substring(1, 7).equals("scenes")) {
  95.     wait = 500;
  96.   } else if (url2.substring(1, 8).equals("sensors")) {
  97.     wait = 500;
  98.   }
  99.  
  100.   while (millis() < bridgeMillis + wait) {
  101.   }
  102.   if (!clientConnect()) {
  103.     return "error"; //TODO!
  104.   }
  105.   bridgeMillis = millis();
  106.   client.print(String("GET ") + "/api/" + hueBridgeUser + url2 + " HTTP/1.1\r\n" +
  107.                "Host: " + hueBridgeHost + "\r\n" +
  108.                "\r\n");
  109.   String json;
  110.   String line;
  111.   int retCode = 0;
  112.   while (client.connected()) {
  113.     line = client.readStringUntil('\n');
  114.     if (line.startsWith("HTTP/1.1")) {
  115.       retCode = line.substring(9, 12).toInt(); // Get HTTP return code
  116.       if (retCode != 200) {
  117.         if (debug) Serial.println("http return code: " + String(retCode));
  118.         return "error";
  119.       }
  120.     }
  121.     if (line == "\r")break;
  122.   }
  123.   while (client.available()) {
  124.     line = client.readStringUntil('\n');
  125.     line = client.readStringUntil('\n');
  126.     json = line;
  127.     line = client.readStringUntil('\n');
  128.     line = client.readStringUntil('\n');
  129.     if (line == "\r")break;
  130.   }
  131.   return json;
  132. }
  133.  
  134. void setSerial() {
  135.   Serial.begin(115200);
  136.   while (!Serial) continue;
  137. }
  138.  
  139. void runApplication() {
  140.   if (wifiConnect()) {
  141.     String api = "/lights";
  142.     String request_url = api + "/1";
  143.     String myJSON = getRequest(request_url);
  144.     if (debug) Serial.println("myJSON: " + myJSON);
  145.    
  146.     request_url = api + "/2";
  147.     myJSON = getRequest(request_url);
  148.     if (debug) Serial.println("myJSON: " + myJSON);
  149.    
  150.     request_url = api + "/3";
  151.     myJSON = getRequest(request_url);
  152.     if (debug) Serial.println("myJSON: " + myJSON);
  153.    
  154.     request_url = api + "/4";
  155.     myJSON = getRequest(request_url);
  156.     if (debug) Serial.println("myJSON: " + myJSON);
  157.    
  158.     request_url = api + "/5";
  159.     myJSON = getRequest(request_url);
  160.     if (debug) Serial.println("myJSON: " + myJSON);
  161.    
  162.     request_url = api + "/6";
  163.     myJSON = getRequest(request_url);
  164.     if (debug) Serial.println("myJSON: " + myJSON);
  165.    
  166.     request_url = api + "/8";
  167.     myJSON = getRequest(request_url);
  168.     if (debug) Serial.println("myJSON: " + myJSON);
  169.    
  170.     request_url = api + "/9";
  171.     myJSON = getRequest(request_url);
  172.     if (debug) Serial.println("myJSON: " + myJSON);
  173.    
  174.     request_url = api + "/11";
  175.     myJSON = getRequest(request_url);
  176.     if (debug) Serial.println("myJSON: " + myJSON);
  177.    
  178.   } else {
  179.     if (debug) Serial.println("NOT WifiConnect");
  180.   }
  181. }
  182.  
  183. void setup() {
  184.   if (debug) setSerial();
  185.   runApplication();
  186.   if (debug) Serial.println("Cyletime: " + String(millis()));
  187.   deepSleep();
  188. }
  189.  
  190. void loop() {
  191.  
  192. }
RAW Paste Data