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 Message Pack 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("/msgpack2");
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 MessagePack using AsyncMessagePackResponse
39
  //
40
  // curl -v http://192.168.4.1/msgpack1
41
  //
42
  server.on("/msgpack1", HTTP_GET, [](AsyncWebServerRequest *request) {
43
    AsyncMessagePackResponse *response = new AsyncMessagePackResponse();
44
    JsonObject root = response->getRoot().to<JsonObject>();
45
    root["hello"] = "world";
46
    response->setLength();
47
    request->send(response);
48
  });
49
 
50
  // Send MessagePack using AsyncResponseStream
51
  //
52
  // curl -v http://192.168.4.1/msgpack2
53
  //
54
  // Save file: curl -v http://192.168.4.1/msgpack2 -o msgpack.bin
55
  //
56
  server.on("/msgpack2", HTTP_GET, [](AsyncWebServerRequest *request) {
57
    AsyncResponseStream *response = request->beginResponseStream("application/msgpack");
58
    JsonDocument doc;
59
    JsonObject root = doc.to<JsonObject>();
60
    root["name"] = "Bob";
61
    serializeMsgPack(root, *response);
62
    request->send(response);
63
  });
64
 
65
  // POST file:
66
  //
67
  // curl -v -X POST -H 'Content-Type: application/msgpack' --data-binary @msgpack.bin http://192.168.4.1/msgpack2
68
  //
69
  handler->setMethod(HTTP_POST | HTTP_PUT);
70
  handler->onRequest([](AsyncWebServerRequest *request, JsonVariant &json) {
71
    Serial.printf("Body request /msgpack2 : ");  // should print: Body request /msgpack2 : {"name":"Bob"}
72
    serializeJson(json, Serial);
73
    Serial.println();
74
    AsyncMessagePackResponse *response = new AsyncMessagePackResponse();
75
    JsonObject root = response->getRoot().to<JsonObject>();
76
    root["hello"] = json.as<JsonObject>()["name"];
77
    response->setLength();
78
    request->send(response);
79
  });
80
 
81
  server.addHandler(handler);
82
 
83
  // New Json API since 3.8.2, which works for both Json and MessagePack bodies
84
  //
85
  // curl -v -X POST -H 'Content-Type: application/json' -d '{"name":"You"}' http://192.168.4.1/msgpack3
86
  // curl -v -X POST -H 'Content-Type: application/msgpack' --data-binary @msgpack.bin http://192.168.4.1/msgpack3
87
  //
88
  server.on("/msgpack3", HTTP_POST, [](AsyncWebServerRequest *request, JsonVariant &json) {
89
    Serial.printf("Body request /msgpack3 : ");  // should print: Body request /msgpack3 : {"name":"Bob"}
90
    serializeJson(json, Serial);
91
    Serial.println();
92
    AsyncJsonResponse *response = new AsyncJsonResponse();
93
    JsonObject root = response->getRoot().to<JsonObject>();
94
    root["hello"] = json.as<JsonObject>()["name"];
95
    response->setLength();
96
    request->send(response);
97
  });
98
#endif
99
 
100
  server.begin();
101
}
102
 
103
// not needed
104
void loop() {
105
  delay(100);
106
}