Advertisement
Guest User

ESPAsyncWebServer large arrays

a guest
May 2nd, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <AsyncTCP.h>
  3. #include <ESPAsyncWebServer.h>
  4.  
  5. const char *ssid = "";
  6. const char *password = "";
  7. int counter_wifi = 0;
  8. AsyncWebServer server(80);
  9.  
  10. const uint16_t SAMPLESPERSEND = 512;
  11. const uint8_t SAMPLESSEND = 16; // 512*16 = 8196
  12. const uint8_t SAMPLESNUMSIZE = 8; // as your nums are 0.271184 so 8 chars
  13.  
  14. float myMeasureVals [SAMPLESPERSEND * SAMPLESSEND];
  15. char myValueArray[SAMPLESPERSEND * (SAMPLESNUMSIZE + 1) + 2 + 1] = {'\0'};
  16. char numBufferArray[SAMPLESNUMSIZE + 1] = {'\0'}; // Converter helper array
  17.  
  18. void setup() {
  19.   Serial.begin(500000);
  20.   Serial.println();
  21.   Serial.println();
  22.   WiFi.begin(ssid, password);
  23.   while (WiFi.status() != WL_CONNECTED)
  24.   {
  25.     delay(500);
  26.     Serial.print(".");
  27.     counter_wifi++;
  28.     if (counter_wifi >= 10) { //after 5 seconds timeout - reset board (on unsucessful connection)
  29.       ESP.restart();
  30.     }
  31.   }
  32.   Serial.println("WiFi connected.");
  33.   Serial.println("IP Address: ");
  34.   Serial.println(WiFi.localIP());
  35.  
  36.  
  37.   server.on("/array", HTTP_GET, [](AsyncWebServerRequest * request) {
  38.    
  39.     for (uint8_t j = 0; j < SAMPLESSEND; j++) {
  40.       if (j == 0) strcat (myValueArray, "[");
  41.       for (uint16_t i = 0; i < SAMPLESPERSEND; i++) {
  42.         // float myTempValue = function2GetValues(); // direct froma function
  43.         float myTempValue = myMeasureVals [i * j];
  44.         // dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf);
  45.         dtostrf(myTempValue, 2, 6, numBufferArray);
  46.         strcat (myValueArray, numBufferArray);
  47.         strcat (myValueArray, ",");
  48.       }
  49.       if (j == SAMPLESSEND - 1) myValueArray [strlen(myValueArray) - 1] = ']'; // overwrite the last ',' with ']'
  50.       Serial.println(myValueArray);
  51.       request->send(myValueArray);
  52.       myValueArray[0] = '\0';  // empty array for next part
  53.     }
  54.  
  55.   });
  56.  
  57.   DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*");
  58.   server.begin();
  59.  
  60. }
  61.  
  62. void loop() {
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement