03_log_data.ino 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include <WiFi.h>
  2. #include <WiFiClientSecure.h>
  3. #define PORT 443
  4. void getSomething() {
  5. showProgress(PROGRESS_GREEN);
  6. String content = String("GET / HTTP/1.1\r\n")
  7. + "Host: example.com\r\n"
  8. + "User-Agent: ESP32/FunHouse/tarka-outdoor-weather-station" + "\r\n"
  9. + "Connection: close" + "\r\n";
  10. WiFiClientSecure client;
  11. client.setInsecure();
  12. debug("Connectiong to: example.com");
  13. showProgress(PROGRESS_GREEN);
  14. if(!client.connect("example.com", 443)) {
  15. debug("Connection failed!");
  16. ERR;
  17. }
  18. debug("Connection established. Sending headers & body.");
  19. showProgress(PROGRESS_GREEN);
  20. client.print(content);
  21. // Wait for things to happen
  22. showProgress(PROGRESS_GREEN);
  23. delay(500);
  24. while(client.available()) {
  25. showProgress(PROGRESS_GREEN);
  26. String line = client.readStringUntil('\n');
  27. debug("Read line: %s", line);
  28. }
  29. hideProgress();
  30. debug("===== Done =====");
  31. }
  32. boolean uploadData(
  33. float temperature,
  34. float pressure,
  35. float humidity,
  36. float light
  37. ) {
  38. showProgress(PROGRESS_GREEN);
  39. String data = String("{")
  40. + "\t\"temperature\": " + temperature + ",\n"
  41. + "\t\"pressure\": " + pressure + ",\n"
  42. + "\t\"humidity\": " + humidity + ",\n"
  43. + "\t\"light\": " + light + ",\n"
  44. + "\t\"time\": { \".sv\": \"timestamp\" }\n"
  45. + "}";
  46. debugln("Content body: ", data);
  47. String content = String("POST ") + RTDB_URL + " HTTP/1.1\r\n"
  48. + "Host: " + RTDB_HOST + "\r\n"
  49. + "User-Agent: ESP32/FunHouse/tarka-outdoor-weather-station" + "\r\n"
  50. + "Content-Type: application/x-www-form-urlencoded" + "\r\n"
  51. + "Accept: */*" + "\r\n"
  52. + "Content-Length: " + data.length() + "\r\n"
  53. + "Connection: close" + "\r\n"
  54. + "\r\n"
  55. + data;
  56. debugln("HTTP request: ", content);
  57. WiFiClientSecure client;
  58. client.setInsecure();
  59. showProgress(PROGRESS_GREEN);
  60. debugln("Connectiong to: ", RTDB_HOST);
  61. if(!client.connect(RTDB_HOST.c_str(), PORT)) {
  62. debug("Connection failed!");
  63. showError();
  64. return false;
  65. }
  66. debug("Connection established. Sending headers & body.");
  67. showProgress(PROGRESS_GREEN);
  68. client.print(content);
  69. // Wait for things to happen
  70. showProgress(PROGRESS_GREEN);
  71. delay(500);
  72. while(client.available()) {
  73. showProgress(PROGRESS_GREEN);
  74. String line = client.readStringUntil('\n');
  75. debugln("Read line: ", line);
  76. }
  77. debug("===== Done =====");
  78. hideProgress();
  79. return true;
  80. }