[SOLVED] Serial1 getting cut Arduino Mega

Hello,

This is my code

if (Serial.available() > 0) {
    Serial1.write(Serial.read());
  }

  String wifiData;
  while (Serial1.available() > 0) {
    delay(50);
    char c = Serial1.read();
    wifiData += c;
  }
  if (wifiData != "") {
    Serial.print(wifiData);
  }

I'm trying to receive messages from an esp8266 but I'm getting the output on the Serial console cutted.

Re: sdf

It should say Received: sdf

I even think the problem is on the Serial1.write

Even if I do

if (Serial.available() > 0) {
    char c = Serial.read();
    //Serial1.write(c);
    Serial.write(c);
  }

Still getting cutted off

It's some of your code. Post it all or make a complete compiles we can run it ourselves sketch that demonstrates the problem.

What is the roll of the delay(50) in the snippet you did post?

  while (Serial1.available() > 0) {
    delay(50);
    char c = Serial1.read();
    wifiData += c;
  }

a7

I tried to add the delay so maybe it would help, really doesn't need to be there.

Main sketch:

#include <SoftwareSerial.h>

//SoftwareSerial wifi(2,3);

#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>

#include <Wire.h>
#include <RTClib.h>

#define WIDTH 36
#define HEIGHT 8
#define PIN 6

Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(WIDTH, HEIGHT, PIN, NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG, NEO_GRB + NEO_KHZ800);
RTC_DS1307 rtc;

#include "icons.h"
#include "numbers.h"
#include "matrix_draw_utils.h"
#include "modes.h"

Mode mode = time;

void setup() {
  Serial.begin(9600);
  //wifi.begin(9600);
  Serial1.begin(9600);

  Serial1.println("[REQUEST] weather");

  Wire.begin();

  if (rtc.begin()) {
    rtc.adjust(DateTime(__DATE__, __TIME__));
  }

  matrix.begin();
  matrix.show();
  matrix.setBrightness(5);
}

int changeModeInterval = 75;
int changeModeCooldown = changeModeInterval;

int weatherInterval = 1000;
int weatherCooldown = weatherInterval;

void loop() {
  if (Serial.available() > 0) {
    delay(10);
    char c = Serial.read();
    Serial1.write(c);
  }

  String wifiData;
  while (Serial1.available() > 0) {
    delay(10);
    char c = Serial1.read();
    wifiData += c;
  }
  if (wifiData != "") {
    Serial.print(wifiData);
  }

  matrix.fillScreen(0);

  if (mode == time) {
    drawIcon(CLOCK_X, CLOCK_Y, CLOCK_C, CLOCK_N, 0);
    drawTime();
    drawIcon(TIME_MODE_X, TIME_MODE_Y, TIME_MODE_C, TIME_MODE_N, 0);
  }
  else if (mode == weather) {
    drawIcon(WEATHER_MODE_X, WEATHER_MODE_Y, WEATHER_MODE_C, WEATHER_MODE_N, 0);
  }
  else if (mode == cronometer) {
    drawIcon(CRONOMETER_MODE_X, CRONOMETER_MODE_Y, CRONOMETER_MODE_C, CRONOMETER_MODE_N, 0);
  }

  if (digitalRead(3) == HIGH) {
    changeMode();
  }
  if (changeModeCooldown < changeModeInterval) {
    changeModeCooldown++;
  }

  if (weatherCooldown == weatherInterval) {
    //Serial1.println("[REQUEST] weather");
    weatherCooldown = 0;
  }
  if (weatherCooldown < weatherInterval) {
    weatherCooldown++;
  }

  matrix.show();
}

void changeMode() {
  if (changeModeCooldown == changeModeInterval) {
    if (mode == time) {
    mode = weather;
    }
    else if (mode == weather) {
      mode = cronometer;
    }
    else if (mode == cronometer) {
      mode = time;
    }
    changeModeCooldown = 0;
  }
}

This is the code on the ESP8266, I have problems on receiving on both sides.

void handleSerial() {
  if (Serial.available() > 0) {
    String read = Serial.readString();

    a = read;

    Serial.println("Received: " + read);

    if (read.indexOf("lal ") != -1) {
      read.replace("lal ", "");

      longitude = read.toInt();
    }

    if (read.indexOf("lol ") != -1) {
      read.replace("lol ", "");

      latitude = read.toInt();
    }

    if (read.indexOf("[ACTION] ") != -1) {
      read.replace("[ACTION] ", "");

      if (read.equals("setup_mode")) {
        if (mode == normal) {
          startSetupMode();
          initSetupServer();
        }
      }
      if (read.equals("normal_mode")) {
        if (mode == set) {
          startNormalMode();
          initNormalServer();
        }
      }
    }

    if (read.indexOf("[REQUEST] ") != -1) {
      read.replace("[REQUEST] ", "");

      if (read.equals("weather")) {
        Serial.println("[REQUEST] weather " + weather());
      }
    }

  }
}

Found the solution,

The matrix.show() was making all the problem, I simply added a 50 loop cycles cooldown and it works now.

I'll leave the code if anyone needs it

Arduino Code

#include <SoftwareSerial.h>

//SoftwareSerial wifi(10,11);

#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>

#include <Wire.h>
#include <RTClib.h>

#define WIDTH 36
#define HEIGHT 8
#define PIN 6

Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(WIDTH, HEIGHT, PIN, NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG, NEO_GRB + NEO_KHZ800);
RTC_DS1307 rtc;

#include "icons.h"
#include "numbers.h"
#include "matrix_draw_utils.h"
#include "modes.h"

Mode mode = time;

void setup() {
  Serial.begin(9600);
  //wifi.begin(9600);
  Serial1.begin(9600);

  Wire.begin();

  if (rtc.begin()) {
    rtc.adjust(DateTime(__DATE__, __TIME__));
  }

  matrix.begin();
  matrix.show();
  matrix.setBrightness(5);
}

int changeModeInterval = 75;
int changeModeCooldown = changeModeInterval;

int weatherInterval = 1000;
int weatherCooldown = weatherInterval;

int showInterval = 50;
int showCooldown = showInterval;

void loop() {
  if (Serial.available()) {
    char read = Serial.read();
    Serial.write(read);
    Serial1.write(read);
  }

  String wifiData;
  while (Serial1.available() > 0) {
    char c = Serial1.read();
    wifiData += c;
  }
  if (wifiData != "") {
    Serial.print(wifiData);
  }


  matrix.fillScreen(0);

  if (mode == time) {
    drawIcon(CLOCK_X, CLOCK_Y, CLOCK_C, CLOCK_N, 0);
    drawTime();
    drawIcon(TIME_MODE_X, TIME_MODE_Y, TIME_MODE_C, TIME_MODE_N, 0);
  }
  else if (mode == weather) {
    drawIcon(WEATHER_MODE_X, WEATHER_MODE_Y, WEATHER_MODE_C, WEATHER_MODE_N, 0);
  }
  else if (mode == cronometer) {
    drawIcon(CRONOMETER_MODE_X, CRONOMETER_MODE_Y, CRONOMETER_MODE_C, CRONOMETER_MODE_N, 0);
  }

  if (digitalRead(3) == HIGH) {
    changeMode();
  }
  if (changeModeCooldown < changeModeInterval) {
    changeModeCooldown++;
  }

  if (weatherCooldown == weatherInterval) {
    weatherCooldown = 0;
  }
  if (weatherCooldown < weatherInterval) {
    weatherCooldown++;
  }

  if (showCooldown == showInterval) {
    matrix.show();
    showCooldown = 0;
  }
  if (showCooldown < showInterval) {
    showCooldown++;
  }
  
}

void changeMode() {
  if (changeModeCooldown == changeModeInterval) {
    if (mode == time) {
    mode = weather;
    }
    else if (mode == weather) {
      mode = cronometer;
    }
    else if (mode == cronometer) {
      mode = time;
    }
    changeModeCooldown = 0;
  }
}

ESP8266 code

void handleSerial() {
  String read = "";
  boolean b = false;

  while (Serial.available()) {
    read = Serial.readString();
    b = true;
  }
  if (b) {
    a = read;

    Serial.println("Received: " + read);

    if (read.indexOf("lal ") != -1) {
      read.replace("lal ", "");

      longitude = read.toInt();
    }

    if (read.indexOf("lol ") != -1) {
      read.replace("lol ", "");

      latitude = read.toInt();
    }

    if (read.indexOf("[ACTION] ") != -1) {
      read.replace("[ACTION] ", "");

      if (read.equals("setup_mode")) {
        if (mode == normal) {
          startSetupMode();
          initSetupServer();
        }
      }
      if (read.equals("normal_mode")) {
        if (mode == set) {
          startNormalMode();
          initNormalServer();
        }
      }
    }

    if (read.indexOf("[REQUEST] ") != -1) {
      read.replace("[REQUEST] ", "");

      if (read.equals("weather")) {
        Serial.println("[REQUEST] weather " + weather());
      }
    }
  }
}