03_log_data.ino 2.7 KB

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