-2

I have an issue while transferring the values of string from UART to buffer. I am using ESP8266 to receive strings on serial from STM32 device. I have total 600 string and i have CSV file that is being transmitted from the STM32 device. I have used proper filtering for every row on the NodeMCU side... I am having no clue why after 300 string the value transmitted to the buffer gets changed as well as the string read on UART also changes exactly after 300/305 string being transmitted. please let me know if there's any mistake in the code.

int i,j,k,l=0;
int httpCode=0;

String fields[24] = {"","month","year","hours","minutes","seconds","rimin","riavg","rimax","yimin","yiavg","yimax","bimin","biavg","bimax","nimin","niavg","nimax","eimin","eiavg","eimax","simin","siavg","simax"};
String element = "API_key=123&mac=0fa&day=";
String postApi1[300];
String postApi2[300];
String str= "";
String str_tx = "";

char char_array[128];
char* token; 
char* rest = char_array;

WiFiClient client;

HTTPClient http;



void setup()
{
  Serial.begin(230400);
  s.begin(9600);
  pinMode(13, INPUT);
  pinMode(15, OUTPUT);
}

void loop() 
{
 if (s.available() >0)
 {
  for(j=0;j<300;j++)
  {
   i=0;
   str = s.readStringUntil('\n');
   Serial.println(str);   
   Serial.println("j");
   postApi1[j]= str;
   Serial.println(postApi1[j]);
   str = "";
  }
  for(k=0;k<300;k++)
  {
   i=0;
   str = s.readStringUntil('\n');
   Serial.println(str);   
   Serial.println("k");
   postApi2[k]= str;
   Serial.println(postApi2[k]);
   str = "";
  }
  
 }

}

Here is the code i am using and following is the Serial output of the code. Serial-output of the above code

the busybee
  • 10,755
  • 3
  • 13
  • 30
Naren2312
  • 1
  • 2
  • 1
    Place enough of a sample from the link directly into your post to illustrate what you are trying to convey, do not leave it in the link. Most people will not bother/want to click the link. – ryyker Jul 27 '20 at 12:47
  • Is this code using `cs50`? I ask because there is not `String` `C type` but there is a typedef for it in `cs50` – ryyker Jul 27 '20 at 12:49
  • What flow control are you using? Serial requires flow control match at sender and receiver – stark Jul 27 '20 at 12:56
  • It seems you are using Arduino library (you should point that), also you should provide information on the device (esp8266 can not work stand alone, and device configuration is important). Also you should provide some example input data (your input strings). – Pablo Yaggi Jul 27 '20 at 14:04
  • Yes I am using arduino compiler for the code, My input string is a row from the CSV file as shown in the serial output monitor. the device I am using is Nodemcu – Naren2312 Jul 28 '20 at 07:13

2 Answers2

0

I think that the readStringUntil function waits for the terminator char. Is the '\n' sent also after 300th character? Also, try to avoid Serial.println in the read function, since the print function is quite slow, you might miss some characters. I would also avoid two for loops, instead, declare a variable, count number of received bytes and according to this save the "str" into "postApi1" or "postApi2". Also, what is the purpose of "i" variable?

Andrej
  • 26
  • 2
  • i is the variable i was using earlier and forgot to comment it out. I dont have any issue to store all the strings read into single postApi buffer but it gives the same output after 300 strings not character. '\n' is sent after every string read. Thankyou for your response i will try to respond with the edited code and forward you the serial output over here – Naren2312 Jul 28 '20 at 08:54
-1

Hey @Andrej I tried your suggestion but it worked as follows

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <SoftwareSerial.h>
#include <ESP8266HTTPClient.h>

#include <WiFiClient.h>

#include <stdio.h>

#include <string.h>

using namespace std;

ESP8266WiFiMulti WiFiMulti;

SoftwareSerial s(13,15); //RX TX
double buff[24]; //Initialized variable to store recieved data
double val;

char c;

int i,j,k,l=0;
int httpCode=0;

String fields[24] = {"","month","year","hours","minutes","seconds","rimin","riavg","rimax","yimin","yiavg","yimax","bimin","biavg","bimax","nimin","niavg","nimax","eimin","eiavg","eimax","simin","siavg","simax"};
String element = "API_key=123&mac=0fa&day=";
String postApi[616];
String str= "";
String str_tx = "";

char char_array[128];
char* token; 
char* rest = char_array;

WiFiClient client;

HTTPClient http;



void setup()
{
  Serial.begin(230400);
  s.begin(9600);
  pinMode(13, INPUT);
  pinMode(15, OUTPUT);
  //Serial.setDebugOutput(true);
  //ESP.wdtDisable();
 
  //Serial.begin(115200);
  // Serial.setDebugOutput(true);

  Serial.println();
  Serial.println();
  Serial.println();

  for (uint8_t t = 4; t > 0; t--) {
    Serial.printf("[SETUP] WAIT %d...\n", t);
    Serial.flush();
    delay(1000);
  }

  WiFi.mode(WIFI_STA);
  WiFiMulti.addAP("ECLP", "p@@$w0rD");

  if ((WiFiMulti.run() == WL_CONNECTED))
  {
    Serial.print("WIFI connected");
  }
}

void loop() 
{
 if (s.available() >0)
 {
  for(j=0;j<616;j++)
  {
   i++;
   str = s.readStringUntil('\n');
   //Serial.println(str);   
   //Serial.println(j);
   postApi[j]= str;
   //Serial.println(postApi[j]);
   str = "";
  }
  
 }
 if(i == 616)
 {
  i=0;
  for(k=0;k<616;k++)
  {
    Serial.println(k);
    Serial.print(" : ");
    Serial.print(postApi[k]);
  }
 }
}

And receive on the output the following thing Serial monitor output

Just for the ref i printed "k" value with output to know at which string the data loss occurs in buffer

the busybee
  • 10,755
  • 3
  • 13
  • 30
Naren2312
  • 1
  • 2
  • Please don't post additions to your question as an answer. This is not the way StackOverflow works. Please take the [tour] and read "[ask]"! – the busybee Jul 28 '20 at 10:36
  • Thanks @thebusybee for pointing this out i will take care of this next time i post my query. – Naren2312 Jul 29 '20 at 07:11