Created
February 22, 2020 09:54
-
-
Save nurikk/c9711a9945e753e658f7597958c1b51f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include "WiFi.h" | |
// #include <WebServer.h> | |
#include <ArduinoJson.h> | |
#include <StreamUtils.h> | |
#include <esp_wifi.h> | |
#include <esp_event.h> | |
#include <esp_log.h> | |
#include <esp_system.h> | |
#include <nvs_flash.h> | |
#include <sys/param.h> | |
#include "nvs_flash.h" | |
#include "esp_eth.h" | |
#include <esp_http_server.h> | |
const char *ssid = "**"; | |
const char *password = "**"; | |
// WebServer server(80); | |
static httpd_handle_t idfServer = NULL; | |
void largeJson(); | |
const int exampleCount = 90; | |
const size_t capacity = 11 * JSON_ARRAY_SIZE(1) + 10 * JSON_OBJECT_SIZE(0) + JSON_OBJECT_SIZE(1) + 2 * JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(8) + JSON_OBJECT_SIZE(13) + 310; | |
DynamicJsonDocument example(capacity); | |
const char *json = "{\"ieeeAddr\":\"000D6F001336867C\",\"last_seen\":\"1581928963\",\"type\":\"Router\",\"powerSource\":\"Main\",\"Interview\":{\"TS\":0,\"State\":4},\"ep\":{\"1\":{\"profId\":260,\"In\":{\"0\":[{}],\"1\":[{}],\"3\":[{}],\"4\":[{}],\"9\":[{}],\"1280\":[{}],\"1282\":[{}],\"65530\":[{}]},\"Out\":{\"3\":[{}],\"25\":[{}]},\"devId\":1027}},\"ManufName\":\"GS\",\"ModelId\":\"SRHMP-I1\",\"st\":{\"linkquality\":76,\"trSeqNum\":24,\"warning\":\"\",\"battery\":100,\"voltage\":4.1},\"supported\":1,\"friendly_name\":\"sirena\",\"Rcf\":true,\"Rtg\":[\"35282\"]}"; | |
DynamicJsonDocument doc(capacity *exampleCount + exampleCount * 3); | |
class CustomWriter : public Stream | |
{ | |
public: | |
CustomWriter(httpd_req_t *req) | |
{ | |
_req = req; | |
} | |
size_t write(uint8_t c) | |
{ | |
httpd_resp_send_chunk(_req, (const char *)c, (ssize_t)1); | |
// httpd_resp_send_chunk(_req, (const char *)"c", 1); | |
return 1; | |
} | |
size_t write(const uint8_t *s, size_t n) | |
{ | |
httpd_resp_send_chunk(_req, (const char *)s, n); | |
// httpd_resp_send_chunk(_req, (const char *)"test", 4); httpd_resp_send | |
return n; | |
} | |
int available() | |
{ | |
return 1; | |
} | |
void clear() | |
{ | |
} | |
int read() | |
{ | |
return 0; | |
} | |
int peek() | |
{ | |
return 0; | |
} | |
void flush() | |
{ | |
httpd_resp_send_chunk(_req, NULL, 0); | |
} | |
private: | |
httpd_req_t *_req; | |
}; | |
/* An HTTP GET handler */ | |
static esp_err_t large_get_handler(httpd_req_t *req) | |
{ | |
CustomWriter writer(req); | |
WriteBufferingStream bufferedWifiClient{writer, 1024}; | |
serializeJson(doc, bufferedWifiClient); | |
bufferedWifiClient.flush(); | |
return ESP_OK; | |
} | |
static const httpd_uri_t large_uri = { | |
.uri = "/large", | |
.method = HTTP_GET, | |
.handler = large_get_handler, | |
.user_ctx = NULL}; | |
static httpd_handle_t start_webserver(void) | |
{ | |
httpd_handle_t server = NULL; | |
httpd_config_t config = HTTPD_DEFAULT_CONFIG(); | |
config.server_port = 81; | |
// Start the httpd server | |
ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port); | |
if (httpd_start(&server, &config) == ESP_OK) | |
{ | |
// Set URI handlers | |
ESP_LOGI(TAG, "Registering URI handlers"); | |
httpd_register_uri_handler(server, &large_uri); | |
return server; | |
} | |
ESP_LOGI(TAG, "Error starting server!"); | |
return NULL; | |
} | |
void setup() | |
{ | |
Serial.begin(115200); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
delay(500); | |
Serial.println("Connecting to WiFi.."); | |
} | |
Serial.println("Connected to the WiFi network"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
// server.begin(); | |
// server.on("/large", largeJson); | |
deserializeJson(example, json); | |
for (int i = 0; i < exampleCount; i++) | |
{ | |
String key = "key" + String(i); | |
doc[key] = example; | |
} | |
idfServer = start_webserver(); | |
} | |
void loop() | |
{ | |
// server.handleClient(); | |
} | |
// void largeJson() | |
// { | |
// WiFiClient client = server.client(); | |
// WriteBufferingStream bufferedWifiClient{client, 1024}; | |
// serializeJson(doc, bufferedWifiClient); | |
// bufferedWifiClient.flush(); | |
// client.flush(); | |
// client.stop(); | |
// Serial.println("done"); | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment