Marijn78

Untitled

Jul 7th, 2020
55
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 bridgeWaitMillis = 0;
  4. #include <WiFi.h>
  5. #include <WiFiClientSecure.h>
  6. #include <ETH.h>
  7. WiFiClientSecure client;
  8. static bool eth_connected = false;
  9. const char* host     = "postman-echo.com";
  10. const int httpPort = 443;
  11. unsigned long scanStart;
  12. unsigned long scanDuration;
  13. unsigned int clientStart;
  14. unsigned int clientDuration;
  15. int counter = 0;
  16. const bool debug = true;
  17.  
  18. void WiFiEvent(WiFiEvent_t event) {
  19.   switch (event) {
  20.     case SYSTEM_EVENT_ETH_START:
  21.       Serial.println("ETH Started");
  22.       //set eth hostname here
  23.       ETH.setHostname("esp32-ethernet");
  24.       break;
  25.     case SYSTEM_EVENT_ETH_CONNECTED:
  26.       Serial.println("ETH Connected");
  27.       break;
  28.     case SYSTEM_EVENT_ETH_GOT_IP:
  29.       Serial.print("ETH MAC: ");
  30.       Serial.print(ETH.macAddress());
  31.       Serial.print(", IPv4: ");
  32.       Serial.print(ETH.localIP());
  33.       if (ETH.fullDuplex()) {
  34.         Serial.print(", FULL_DUPLEX");
  35.       }
  36.       Serial.print(", ");
  37.       Serial.print(ETH.linkSpeed());
  38.       Serial.println("Mbps");
  39.       eth_connected = true;
  40.       break;
  41.     case SYSTEM_EVENT_ETH_DISCONNECTED:
  42.       Serial.println("ETH Disconnected");
  43.       eth_connected = false;
  44.       break;
  45.     case SYSTEM_EVENT_ETH_STOP:
  46.       Serial.println("ETH Stopped");
  47.       eth_connected = false;
  48.       break;
  49.     default:
  50.       break;
  51.   }
  52. }
  53.  
  54. void deepSleep() {
  55.   esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * mS_TO_S_FACTOR);
  56.   if (debug) {
  57.     Serial.println("Going to sleep now for " + String(TIME_TO_SLEEP) + " milliseconds");
  58.     Serial.println("");
  59.   }
  60.   if (debug) Serial.flush();
  61.   esp_deep_sleep_start();
  62. }
  63.  
  64. bool clientConnect() {
  65.   clientStart = millis();
  66.   if (!client.connected()) {
  67.     if (debug) Serial.println("Starting connection to host " + String(host) + " port " + String(httpPort));
  68.     if (!client.connect(host, httpPort)) {
  69.       Serial.println("clientConnect connection failed");
  70.       return false;
  71.     } else {
  72.       clientDuration = millis() - clientStart;
  73.       if (debug) Serial.println("Duration clientConnect " + String(clientDuration));
  74.       return true;
  75.     }
  76.   } else {
  77.     return true;
  78.   }
  79. }
  80.  
  81. String getRequest(String url2) {
  82.   unsigned int wait = 1000;
  83.   if (url2.substring(1, 7).equals("lights")) {
  84.     wait = 100;
  85.   }
  86.   while (millis() < bridgeWaitMillis + wait) {
  87.   }
  88.   if (!clientConnect()) {
  89.     return "error";
  90.   }
  91.   bridgeWaitMillis = millis();
  92.   client.print(String("GET ") + url2 + " HTTP/1.1\r\n" +
  93.                "Host: " + host + "\r\n" +
  94.                "\r\n");
  95.   String json;
  96.   String line;
  97.   int retCode = 0;
  98.   while (client.connected()) {
  99.     line = client.readStringUntil('\n');
  100.     if (line.startsWith("HTTP/1.1")) {
  101.       retCode = line.substring(9, 12).toInt(); // Get HTTP return code
  102.       if (retCode != 200) {
  103.         if (debug) Serial.println("http return code: " + String(retCode));
  104.         return "error";
  105.       }
  106.     }
  107.     if (line == "\r")break;
  108.   }
  109.   while (client.available()) {
  110.     Serial.write(client.read());
  111.   }
  112.   return json;
  113. }
  114.  
  115. void setSerial() {
  116.   Serial.begin(115200);
  117.   while (!Serial) continue;
  118. }
  119.  
  120. void runTestApplication() {
  121.   String request_url = "/get?foo1=bar1&foo2=bar2";;
  122.   String myJSON;
  123.   for (int x = 0; x < 10; x++) {
  124.     myJSON = getRequest(request_url);
  125.     //if (debug) Serial.print("myJSON: " + myJSON);
  126.     Serial.println("");
  127.   }
  128.   Serial.println("closing connection\n");
  129.   client.stop();
  130. }
  131.  
  132. void setup() {
  133.   if (debug) setSerial();
  134.   WiFi.onEvent(WiFiEvent);
  135.   ETH.begin();
  136.   delay(10000);
  137.   runTestApplication();
  138.   if (debug) Serial.println("Cyletime: " + String(millis()));
  139.   deepSleep();
  140. }
  141.  
  142. void loop() {
  143.  
  144. }
RAW Paste Data