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
//
5
// Shows how to send and receive Json data
6
//
7
 
8
#include <Arduino.h>
9
#if defined(ESP32) || defined(LIBRETINY)
10
#include <AsyncTCP.h>
11
#include <WiFi.h>
12
#elif defined(ESP8266)
13
#include <ESP8266WiFi.h>
14
#include <ESPAsyncTCP.h>
15
#elif defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350)
16
#include <RPAsyncTCP.h>
17
#include <WiFi.h>
18
#endif
19
 
20
#include <ESPAsyncWebServer.h>
21
 
22
static AsyncWebServer server(80);
23
 
24
#if ASYNC_JSON_SUPPORT == 1
25
static AsyncCallbackJsonWebHandler *handler = new AsyncCallbackJsonWebHandler("/json2");
26
#endif
27
 
28
void setup() {
29
  Serial.begin(115200);
30
 
31
#if ASYNCWEBSERVER_WIFI_SUPPORTED
32
  WiFi.mode(WIFI_AP);
33
  WiFi.softAP("esp-captive");
34
#endif
35
 
36
#if ASYNC_JSON_SUPPORT == 1
37
  //
38
  // sends JSON using AsyncJsonResponse
39
  //
40
  // curl -v http://192.168.4.1/json1
41
  //
42
  server.on("/json1", HTTP_GET, [](AsyncWebServerRequest *request) {
43
    AsyncJsonResponse *response = new AsyncJsonResponse();
44
    JsonObject root = response->getRoot().to<JsonObject>();
45
    root["hello"] = "world";
46
    response->setLength();
47
    request->send(response);
48
  });
49
 
50
  // Send JSON using AsyncResponseStream
51
  //
52
  // curl -v http://192.168.4.1/json2
53
  //
54
  server.on("/json2", HTTP_GET, [](AsyncWebServerRequest *request) {
55
    AsyncResponseStream *response = request->beginResponseStream("application/json");
56
    JsonDocument doc;
57
    JsonObject root = doc.to<JsonObject>();
58
    root["foo"] = "bar";
59
    // serializeJson(root, Serial);
60
    // Serial.println();
61
    request->send(response);
62
  });
63
 
64
  // curl -v -X POST -H 'Content-Type: application/json' -d '{"name":"You"}' http://192.168.4.1/json2
65
  // curl -v -X PUT -H 'Content-Type: application/json' -d '{"name":"You"}' http://192.168.4.1/json2
66
  //
67
  // edge cases:
68
  //
69
  // curl -v -X POST -H "Content-Type: application/json" -d "1234" -H "Content-Length: 5" http://192.168.4.1/json2 => rx timeout
70
  // curl -v -X POST -H "Content-Type: application/json" -d "1234" -H "Content-Length: 2" http://192.168.4.1/json2 => 12
71
  // curl -v -X POST -H "Content-Type: application/json" -d "1234" -H "Content-Length: 4" http://192.168.4.1/json2 => 1234
72
  // curl -v -X POST -H "Content-Type: application/json" -d "1234" -H "Content-Length: 10" http://192.168.4.1/json2 => rx timeout
73
  // curl -v -X POST -H "Content-Type: application/json" -d "12345678" -H "Content-Length: 8" http://192.168.4.1/json2 => 12345678
74
  // curl -v -X POST -H "Content-Type: application/json" -d "123456789" -H "Content-Length: 8" http://192.168.4.1/json2 => 12345678
75
  // curl -v -X POST -H "Content-Type: application/json" -d "123456789" -H "Content-Length: 9" http://192.168.4.1/json2 => 413: Content length exceeds maximum allowed
76
  handler->setMaxContentLength(8);
77
  handler->setMethod(HTTP_POST | HTTP_PUT);
78
  handler->onRequest([](AsyncWebServerRequest *request, JsonVariant &json) {
79
    serializeJson(json, Serial);
80
    Serial.println();
81
    AsyncJsonResponse *response = new AsyncJsonResponse();
82
    JsonObject root = response->getRoot().to<JsonObject>();
83
    root["hello"] = json.as<JsonObject>()["name"];
84
    response->setLength();
85
    request->send(response);
86
  });
87
 
88
  server.addHandler(handler);
89
 
90
  // New Json API since 3.8.2, which works for both Json and MessagePack bodies
91
  // curl -v -X POST -H 'Content-Type: application/json' -d '{"name":"You"}' http://192.168.4.1/json3
92
 
93
  server.on("/json3", HTTP_POST, [](AsyncWebServerRequest *request, JsonVariant &json) {
94
    Serial.printf("Body request : ");
95
    serializeJson(json, Serial);
96
    Serial.println();
97
    AsyncJsonResponse *response = new AsyncJsonResponse();
98
    JsonObject root = response->getRoot().to<JsonObject>();
99
    root["hello"] = json.as<JsonObject>()["name"];
100
    response->setLength();
101
    request->send(response);
102
  });
103
#endif
104
 
105
  server.begin();
106
}
107
 
108
static uint32_t lastHeapTime = 0;
109
static uint32_t lastHeap = 0;
110
 
111
void loop() {
112
#ifdef ESP32
113
  uint32_t now = millis();
114
  if (now - lastHeapTime >= 500) {
115
    uint32_t heap = ESP.getFreeHeap();
116
    if (heap != lastHeap) {
117
      lastHeap = heap;
118
      async_ws_log_w("Free heap: %" PRIu32, heap);
119
    }
120
    lastHeapTime = now;
121
  }
122
#endif
123
}