Subversion Repositories ESP8266_P1_Meter

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 raymond 1
// SPDX-License-Identifier: LGPL-3.0-or-later
2
// Copyright 2016-2026 Hristo Gochkov, Mathieu Carbou, Emil Muratov, Will Miles
3
 
4
#include <Arduino.h>
5
#include <AsyncTCP.h>
6
#include <StreamString.h>
7
#include <WiFi.h>
8
 
9
#include <functional>
10
#include <string>
11
 
12
#define WIFI_SSID     "IoT"
13
#define WIFI_PASSWORD ""
14
 
15
void fetchAsync(const char *host, std::function<void(const StreamString *)> onDone) {
16
  Serial.printf("[%s] Fetching: http://%s...\n", host, host);
17
 
18
  // buffer where we will accumulate the received data
19
  StreamString *content = new StreamString();
20
 
21
  // reserve enough space to avoid reallocations
22
  content->reserve(32 * 1024);
23
 
24
  // create a new client
25
  AsyncClient *client = new AsyncClient();
26
 
27
  // register a callback when the client disconnects
28
  client->onDisconnect([content, host, onDone](void *arg, AsyncClient *client) {
29
    Serial.printf("[%s] Disconnected.\n", host);
30
    onDone(content);
31
    delete client;
32
    delete content;
33
  });
34
 
35
  // register a callback when an error occurs
36
  client->onError([host, onDone](void *arg, AsyncClient *client, int8_t error) {
37
    Serial.printf("[%s] Error: %s\n", host, client->errorToString(error));
38
  });
39
 
40
  // register a callback when data arrives, to accumulate it
41
  client->onData([host, content](void *arg, AsyncClient *client, void *data, size_t len) {
42
    Serial.printf("[%s] Received %u bytes...\n", host, len);
43
    content->write((const uint8_t *)data, len);
44
  });
45
 
46
  // register a callback when we are connected
47
  client->onConnect([host](void *arg, AsyncClient *client) {
48
    Serial.printf("[%s] Connected!\n", host);
49
 
50
    // send request
51
    client->write("GET / HTTP/1.1\r\n");
52
    client->write("Host: ");
53
    client->write(host);
54
    client->write("\r\n");
55
    client->write("User-Agent: ESP32\r\n");
56
    client->write("Connection: close\r\n");
57
    client->write("\r\n");
58
  });
59
 
60
  Serial.printf("[%s] Connecting...\n", host);
61
 
62
  if (!client->connect(host, 80)) {
63
    Serial.printf("[%s] Failed to connect!\n", host);
64
    delete client;
65
    delete content;
66
    onDone(nullptr);
67
  }
68
}
69
 
70
void setup() {
71
  Serial.begin(115200);
72
  while (!Serial) {
73
    continue;
74
  }
75
 
76
  // connect to WiFi
77
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
78
  while (WiFi.status() != WL_CONNECTED) {
79
    delay(500);
80
  }
81
  Serial.println("Connected to WiFi!");
82
  Serial.println(WiFi.localIP());
83
 
84
  // fetch asynchronously 2 websites:
85
 
86
  // equivalent to curl -v --raw http://www.time.org/
87
  fetchAsync("www.time.org", [](const StreamString *content) {
88
    if (content) {
89
      Serial.printf("[www.time.org] Fetched website:\n%s\n", content->c_str());
90
    } else {
91
      Serial.println("[www.time.org] Failed to fetch website!");
92
    }
93
  });
94
 
95
  // equivalent to curl -v --raw http://www.google.com/
96
  fetchAsync("www.google.com", [](const StreamString *content) {
97
    if (content) {
98
      Serial.printf("[www.google.com] Fetched website:\n%s\n", content->c_str());
99
    } else {
100
      Serial.println("[www.google.com] Failed to fetch website!");
101
    }
102
  });
103
}
104
 
105
void loop() {
106
  delay(100);
107
}