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 <WiFi.h>
7
 
8
// Run a server at the root of the project with:
9
// > python3 -m http.server 3333
10
// Now you can open a browser and test it works by visiting http://192.168.125.122:3333/ or http://192.168.125.122:3333/README.md
11
#define HOST "192.168.125.122"
12
#define PORT 3333
13
 
14
// WiFi SSID to connect to
15
#define WIFI_SSID "IoT"
16
 
17
// 16 slots on esp32 (CONFIG_LWIP_MAX_ACTIVE_TCP)
18
#define MAX_CLIENTS CONFIG_LWIP_MAX_ACTIVE_TCP
19
// #define MAX_CLIENTS 1
20
 
21
size_t permits = MAX_CLIENTS;
22
 
23
void makeRequest() {
24
  if (!permits) {
25
    return;
26
  }
27
 
28
  Serial.printf("** permits: %d\n", permits);
29
 
30
  AsyncClient *client = new AsyncClient;
31
 
32
  client->onError([](void *arg, AsyncClient *client, int8_t error) {
33
    Serial.printf("** error occurred %s \n", client->errorToString(error));
34
    client->close();
35
    delete client;
36
  });
37
 
38
  client->onConnect([](void *arg, AsyncClient *client) {
39
    permits--;
40
    Serial.printf("** client has been connected: %" PRIu16 "\n", client->localPort());
41
 
42
    client->onDisconnect([](void *arg, AsyncClient *client) {
43
      Serial.printf("** client has been disconnected: %" PRIu16 "\n", client->localPort());
44
      client->close();
45
      delete client;
46
 
47
      permits++;
48
      makeRequest();
49
    });
50
 
51
    client->onData([](void *arg, AsyncClient *client, void *data, size_t len) {
52
      Serial.printf("** data received by client: %" PRIu16 ": len=%u\n", client->localPort(), len);
53
    });
54
 
55
    client->write("GET /README.md HTTP/1.1\r\nHost: " HOST "\r\nUser-Agent: ESP\r\nConnection: close\r\n\r\n");
56
  });
57
 
58
  if (client->connect(HOST, PORT)) {
59
  } else {
60
    Serial.println("** connection failed");
61
  }
62
}
63
 
64
void setup() {
65
  Serial.begin(115200);
66
  while (!Serial) {
67
    continue;
68
  }
69
 
70
  WiFi.mode(WIFI_STA);
71
  WiFi.begin(WIFI_SSID);
72
  while (WiFi.status() != WL_CONNECTED) {
73
    delay(500);
74
    Serial.print(".");
75
  }
76
  Serial.println("** connected to WiFi");
77
  Serial.println(WiFi.localIP());
78
 
79
  for (size_t i = 0; i < MAX_CLIENTS; i++) {
80
    makeRequest();
81
  }
82
}
83
 
84
void loop() {
85
  delay(1000);
86
  Serial.printf("** free heap: %" PRIu32 "\n", ESP.getFreeHeap());
87
}