| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- #include <WiFi.h>
- #include <WiFiClientSecure.h>
- #define PORT 443
- void getSomething() {
- showProgress(PROGRESS_GREEN);
- String content = String("GET / HTTP/1.1\r\n")
- + "Host: example.com\r\n"
- + "User-Agent: ESP32/FunHouse/tarka-outdoor-weather-station" + "\r\n"
- + "Connection: close" + "\r\n";
- WiFiClientSecure client;
- client.setInsecure();
- debug("Connectiong to: example.com");
- showProgress(PROGRESS_GREEN);
- if(!client.connect("example.com", 443)) {
- debug("Connection failed!");
- ERR;
- }
- debug("Connection established. Sending headers & body.");
- showProgress(PROGRESS_GREEN);
- client.print(content);
- // Wait for things to happen
- showProgress(PROGRESS_GREEN);
- delay(500);
- while(client.available()) {
- showProgress(PROGRESS_GREEN);
- String line = client.readStringUntil('\n');
- debug("Read line: %s", line);
- }
- hideProgress();
- debug("===== Done =====");
- }
- boolean uploadData(
- float temperature,
- float pressure,
- float humidity,
- float light
- ) {
- showProgress(PROGRESS_GREEN);
- String data = String("{")
- + "\t\"temperature\": " + temperature + ",\n"
- + "\t\"pressure\": " + pressure + ",\n"
- + "\t\"humidity\": " + humidity + ",\n"
- + "\t\"light\": " + light + ",\n"
- + "\t\"time\": { \".sv\": \"timestamp\" }\n"
- + "}";
- debugln("Content body: ", data);
- String content = String("POST ") + RTDB_URL + " HTTP/1.1\r\n"
- + "Host: " + RTDB_HOST + "\r\n"
- + "User-Agent: ESP32/FunHouse/tarka-outdoor-weather-station" + "\r\n"
- + "Content-Type: application/x-www-form-urlencoded" + "\r\n"
- + "Accept: */*" + "\r\n"
- + "Content-Length: " + data.length() + "\r\n"
- + "Connection: close" + "\r\n"
- + "\r\n"
- + data;
- debugln("HTTP request: ", content);
- WiFiClientSecure client;
- client.setInsecure();
- showProgress(PROGRESS_GREEN);
- debugln("Connectiong to: ", RTDB_HOST);
- if(!client.connect(RTDB_HOST.c_str(), PORT)) {
- debug("Connection failed!");
- showError();
- return false;
- }
- debug("Connection established. Sending headers & body.");
- showProgress(PROGRESS_GREEN);
- client.print(content);
- // Wait for things to happen
- showProgress(PROGRESS_GREEN);
- delay(500);
- while(client.available()) {
- showProgress(PROGRESS_GREEN);
- String line = client.readStringUntil('\n');
- debugln("Read line: ", line);
- }
- debug("===== Done =====");
- hideProgress();
- return true;
- }
|