summaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 224daeb03b30b4724ebb04bc461cedfbb470793e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>

const char *ssid = "";
const char *password = "";
const char *host = "";
const int port = 8000;
const char *sensor_1 = "Home_bme280_balkon";
const char *sensor_2 = "Villa_bme280_base1";

void setup()
{
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }

  if (WiFi.status() == WL_CONNECTED)
  {
    Serial.println("\nOK !");
  }


  //=====================================================
  String sensorKeys = sensor_1;
  sensorKeys += ",";
  sensorKeys += sensor_2;

  WiFiClient client;
  HTTPClient http;

  String url = "http://" + String(host) + ":" + String(port) + "/";
  Serial.println("Sending POST to: " + url);
  Serial.println("Data: " + sensorKeys);

  http.begin(client, url);
  http.addHeader("Content-Type", "text/plain");
  http.setTimeout(10000);


  // Отправляем POST с нашими ключами
  int httpCode = http.POST(sensorKeys);

  // Анализируем ответ
  if (httpCode > 0)
  {
    Serial.printf("Response code: %d\n", httpCode);

    if (httpCode == HTTP_CODE_OK)
    {
      String payload = http.getString();
      Serial.println("Server response: \n" + payload);
    }
  }
  else
  {
    Serial.printf("POST failed, error: %s\n", http.errorToString(httpCode).c_str());
  }

  http.end();

  //=====================================================

  // if (client.connect(host, 8000))
  // {
  //   client.println("GET / HTTP/1.1");
  //   client.print("Host: ");
  //   client.println(host);
  //   client.println("Connection: close");
  //   client.println();
  //   Serial.println("Отправили запрос");

  //   delay(500);

  //   while (client.available())
  //   {
  //     String line = client.readStringUntil('\r');
  //     Serial.print(line);
  //   }
  // }
}

void loop() {}