(solved)8x32 led matrix, controlled form browser with ESP-01

Hi!
I have this self-made led-matrix, controlled with pro mini (which I'll switch to teensy once I have this problem solved)
Arduino part of code processes serial data, works in wired serial as well as bluetooth. But, now when I try to add ESP-01 to control from browser, something odd is happening.
When I use any other serial method than ESP-01, it looks like this, smooth scrolling and showing letters, like It's supposed to be:

But, when I try to use ESP-01, text only "blinks" very quickly when shot from browser, and MDNS-part of code is shown on screen once, before sending any data. It looks like extra line feed is fed.
But, according to serial monitor, no extra LN is received on arduino part.
I have feeling esp-part of code is culprit, since bluetooth serial and wired serial works fine
First, ESP:code (program using arduino IDE)
Full article I found this code from and mangled bit:

I hope I didn't forgot any relevant information.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

MDNSResponder mdns;

// Replace with your network credentials
const char* ssid = "KONIG_WS01";
const char* password = "admin";

ESP8266WebServer server(80);

String webPage = "";


void setup(void) {

  webPage += "<h1>ESP8266 Web Server</h1>";
  webPage +=   "<form action='msg'><p>Type your message <input type='text' name='msg' size=100 autofocus> <input type='submit' value='Submit'></form>";


  delay(1000);
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  if (mdns.begin("esp8266", WiFi.localIP())) {
    Serial.println("MDNS responder started");
  }

  server.on("/", []() {
    server.send(200, "text/html", webPage);
  });

    server.on("/msg", []() {
    server.send(200, "text/html", webPage);
  String msg = server.arg("msg");
  Serial.println(msg);    delay(1000);
  });

  
  server.begin();
  Serial.println("HTTP server started");
}

void loop(void) {
  server.handleClient();
}

And then the pro-mini part

//this sketch allows to scroll text and data on 8x8 matrises. When changing matrix size, change matrises value and OCR1A in setup. However, OCR1A is initially se-up for bigger displays, and it works in smaller too so no need change unless screen doesn't update fast enought; increase value then.
/*
   SERIAL INPUT!
  to display text = text("text here",x-pos,y-pos, rot(optional));
  to display values in numbers= value(value, x-pos,y-pos, rot(optional));
  to display pixels=pixel(x-pos,y-pos,mode);   (useful for drawing lines and graphs, use for() loop to create lines better

  BIG thanks for dr.doggy from ETO (electro tech online)

  signed char/char= 1 byte(8 bits), -127->127
  unsigned char/byte= 1 byte(8 bits), 0->255
  int= 2 byte(16 bits), -32768->32767
  unsigned int= 2 byte(16 bits), 0-> 65535
  long = 4 byte(32 bits), -2,147,483,648->2,147,483,647
  unsigned long= 4 byte (32 bits), 4,294,967,295
*/


#include "FontMap.h"  //font is in external file
#define matrises 4          //define number of matrises, 4-22
#define columns matrises*8  //calculate columns based how many matrises are in use
byte displayPointer = 0;                       //for interrupt use...
byte buffer1[columns];                              // buffer for screen,4x8=32
const byte power[8] = {128, 64, 32, 16, 8, 4, 2, 1}; //for each column B10000000->B00000001

char string[40]; //array to store incoming data
char textFromPc[40]; //array to store actual text to show on matrix

char c; 
byte index;
byte textlength; //for dynaminc scroll length
char * strtokIndx; // this is used by strtok() as an index


 byte Yoffset = 0;
byte Rotation = 0;


void draw() //draw loop,DON'T TOUCH!
{
  setcolumn(displayPointer);            //column scanning
  setdata(buffer1[displayPointer]);    //bit banging shift registers
  bitSet(PORTD, 3); //blit latch, high->low
  bitClear(PORTD, 3);
  if (++displayPointer == columns)          //32 LED row sections in total
  { displayPointer = 0;
  }
}



void setcolumn(byte col)         //loop that takes care of column scanning DON'T TOUCH!
{
  int pos;
  for (pos = columns; pos > -1; pos--)
  {
    if (col == pos)
    {
      bitSet(PORTD, 2);  //data high
    }
    else
    {
      bitClear(PORTD, 2); //data low
    }
    bitSet(PORTD, 4);      //blit clock high->low
    bitClear(PORTD, 4);
  }
}


void setdata(byte dat)
{
  byte pos;
  for (pos = 0; pos < 8; pos++)
  {
    if (dat & 128)
    {
      dat -= 128;
      bitSet(PORTD, 2);  //data high
    }
    else
    {
      bitClear(PORTD, 2); //data low
    }
    dat = dat << 1;  //dat <<1==dat *2;
    bitSet(PORTD, 4);     //blit clock
    bitClear(PORTD, 4);

  }
}




void clr()                          //clear display buffer
{
  byte addr;
  for (addr = 0; addr < columns; addr++) // Empty display buffer
    buffer1[addr] = 0;
}

void pixel(signed char x, signed char y, byte cond)
{
  byte pix, msk;
  if (x < 0 || y < 0)
  {
    return; // outside drawing limits negative
  }
  if (x > (columns - 1) || y > 7)
  {
    return; // outside drawing limits positive, x=32, y=8
  }
  pix = power[y];
  msk = buffer1[x]; // get exsisting data


  if (cond == 2)
    pix ^= msk; //XOR data to screen
  if (cond == 1)
  {
    pix = ~pix;
    pix &= msk; // AND data to screen
  }
  if (cond == 0)
    pix |= msk; // OR data to screen
  buffer1[x] = pix; // apply changes
}




void value(char ch, signed char x, signed char y, byte rotation)
{
  byte x1, y1;
  byte disp, disp2;
  for ( x1 = 0; x1 < 8; x1++) // eight rows
  {
    disp = font[x1 + (ch << 3)]; //look data from fontmap, (ch<<3)==(ch *8)
    for (y1 = 0; y1 < 8; y1++) // eight pixels
    {
      disp2 = disp & power[y1];
      if (disp2 > 0)
      {
        if (rotation == 0)
        {
          pixel(x + x1, y + y1, 0); //0 degrees
        }
        if (rotation == 1)
        {
          pixel(7 - (y + y1), (x + x1), 0); //90 degrees, flip to left
        }
        if (rotation == 2)
        {
          pixel(7 - (x + x1), 7 - (y + y1), 0);
        }
        if (rotation == 3)
        {
          pixel(y + y1, 7 - (x + x1), 0); //90 degrees, flip to right
        }
      }
    }
  }
}





void text(char* ch, signed char x, signed char y, byte rotation)  //strput relies on charput
{
  while (*ch )
  {
    value(*ch++, x, y, rotation); //write a string to the display buffer
    x += 7;
  }
}



void setup() //setup runs once
{
  DDRD = DDRD | B11111100; //port registers used to set pin directions
  Serial.begin(115200);
}


void loop() //just sitting here
{

  if (Serial.available() > 0)
  {
    c = Serial.read();

    if ((c != '\r') && (c != '\n')) //Keep these out of our buffer
    {
      string[index++] = c; //Add whatever we receive to the buffer
    }

    else
    {
      textlength = index * 8 + 32;  // *8, since matrises are 8-wide, and 32 to scroll whole screen.
      string[index++] = '\0';//Converts the array into a string
      index = 0; //next time we start from beginning index
      parseData(); //time to parse data received
      //showParsedData(); //for debugging!

    }
  }
  clr();
  text(textFromPc, 31 - millis() / 100 % textlength, Yoffset, Rotation);
  draw();
}




void parseData()  //function for splitting the array
{

  strtokIndx = strtok(string, ",");     // get the first part - the TEXT PART
  strcpy(textFromPc, strtokIndx); // copy it to messageFromPC

  strtokIndx = strtok(NULL, ",");
  Yoffset = atoi(strtokIndx);

  strtokIndx = strtok(NULL, ",");
  Rotation = atoi(strtokIndx);

}


/*
  void showParsedData()
  {
  Serial.print("text ");
  Serial.println(textFromPc);
  Serial.print("plane ");
  Serial.println(Yoffset);
  Serial.print("rotation ");
  Serial.println(Rotation);
  }
*/

Took couple shots, with realterm and arduino serial monitor if these are any help.
Realterm:

Arduino serial monitor:

as you can see, in arduino serial, data indeed ends at 13/10, CR/LF
Code I used to catch data with arduino, which was hooked to other computer so indeed read data from ESP8266:

void setup() 
{
Serial.begin(115200);
}

void loop() 
{
if(Serial.available()>0)
{
  Serial.println(Serial.read(),DEC);
}
}

Solved! Found bug in my receiving part....read on! :slight_smile:

Aha! Seems I have bug In my code, since I debugged bit with serial monitor.
This is code I used to debug, arriving data, what is going to buffer, and what is in buffer:

char string[40]; //array to store incoming data
char textFromPc[40]; //array to store actual text to show on matrix

char c;
byte index;
byte textlength; //for dynaminc scroll length


void setup() //setup runs once
{
  Serial.begin(9600);
}


void loop() //just sitting here
{

  if (Serial.available() > 0)
  {
    c = Serial.read();
    Serial.print("before buffer:  ");
    Serial.println(c, DEC);

    if ((c != '\r') && (c != '\n')) //Keep these out of our buffer
    {
      string[index++] = c; //Add whatever we receive to the buffer
      //Serial.print("going to buffer:  ");
      //Serial.println(c, DEC);
    }
    else
    {
      textlength = index * 8 + 32;  // *8, since matrises are 8-wide, and 32 to scroll whole screen.
      string[index++] = '\0';//Converts the array into a string
      //Serial.print("at buffer, received in ASCII:  ");
      //Serial.println(string);
      index = 0; //next time we start from beginning index
    }
  }
}

Normal, written straight from monitor,when pressed enter:

before buffer:  10 //line feed!
at buffer, received in ASCII:    //nothing, since, LN is ignored

and, with esp:

before buffer: 13    //hmm, carrion return?
at buffer, received in ascii:
before buffer :10
at buffer, received in ascii:

and same test, with character "h":
normal:

before buffer:  104
going to buffer:  104
before buffer:  10
at buffer, received in ASCII:  h

esp:

before buffer: 104
going to buffer:104
before buffer: 13
at buffer , received in ASCII: h
before buffer: 10
at buffer, received in ASCII:

WHILE writing this, I found bug in my arduino part, so this fixed: (LN wasn't terminator in my code!)

    if(c=='\n')
    {
      textlength = index * 8 + 32;  // *8, since matrises are 8-wide, and 32 to scroll whole screen.
      string[index++] = '\0';//Converts the array into a string
      //Serial.print("at buffer, received in ASCII:  ");
      //Serial.println(string);
      index = 0; //next time we start from beginning index

      parseData(); //time to parse data received
      //showParsedData(); //for debugging!

    }