Corrupt data, serialport, esp8266 with arduino mega

I tried to transfer the data from esp8266 to Arduino mega with using serial port, but not all of the data is reaching. can you help me?

Arduino Code

#include <OneWire.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>
#include <SPI.h>
#include <DallasTemperature.h>
#include <ArduinoJson.h>
#include "Adafruit_GFX.h"
char msg[1024];
OneWire oneWire_one(ONE_WIRE_BUS_1);
OneWire oneWire_two(ONE_WIRE_BUS_2);
OneWire oneWire_three(ONE_WIRE_BUS_3);
DallasTemperature sensors_one(&oneWire_one);
DallasTemperature sensors_two(&oneWire_two);
DallasTemperature sensors_three(&oneWire_three);
String weather;
String weather1;
float temp1, pressure, wind_speed;
int humidity, wind_degree;
void setup() {
   tft.begin();
   Serial.begin(115200);
   Serial3.begin(115200);
   sensors_one.begin();
   sensors_two.begin();
   sensors_three.begin();
}
void loop() {
  weather_data();
   sensors_data();
}

void weather_data(){
  while(Serial3.available()>0){
    Serial3.readBytes(msg,1024);
  }
            Serial.println(msg);
            DynamicJsonDocument doc(1024);
             deserializeJson(doc, msg);
             String weather = doc["weather"][0]["main"];
             String weather1 = doc["weather"][0]["description"];
             float temp1 = (float)(doc["main"]["temp"])- 273.15; // get temperature in °C
             int humidity = doc["main"]["humidity"]; // get humidity in %
             float pressure = (float)(doc["main"]["pressure"]) / 1000; // get pressure in bar
             float wind_speed = doc["wind"]["speed"]; // get wind speed in m/s
             int wind_degree = doc["wind"]["deg"]; // get wind degree in °
            tft.setTextSize(1);
            tft.setCursor(140,230);
            tft.setTextColor(ILI9341_WHITE,ILI9341_BLACK);
            tft.println(temp1);
            tft.setTextSize(1);
            tft.setCursor(140,239);
            tft.setTextColor(ILI9341_WHITE,ILI9341_BLACK);
            tft.println(humidity);
            tft.setTextSize(1);
            tft.setCursor(140,247);
            tft.setTextColor(ILI9341_WHITE,ILI9341_BLACK);
            tft.println(pressure);
            tft.setTextSize(1);
            tft.setCursor(140,255);
            tft.setTextColor(ILI9341_WHITE,ILI9341_BLACK);
            tft.println(wind_speed);
            tft.setTextSize(1);
            tft.setCursor(140,263);
            tft.setTextColor(ILI9341_WHITE,ILI9341_BLACK);
            tft.println(wind_degree);
            tft.setTextSize(1);
            tft.setCursor(140,271);
            tft.setTextColor(ILI9341_WHITE,ILI9341_BLACK);
            tft.println(weather);
            tft.setTextSize(1);
            tft.setCursor(140,279);
            tft.setTextColor(ILI9341_WHITE,ILI9341_BLACK);
            tft.println(weather1);

  }

unsigned long sensors_data() {
  //unsigned long start = micros();
  sensors_one.requestTemperatures();
  sensors_two.requestTemperatures();
  sensors_three.requestTemperatures();
  float tempC = sensors_one.getTempCByIndex(0);
  float tempCC = sensors_two.getTempCByIndex(0);
  float tempCCC = sensors_three.getTempCByIndex(0);
  value=analogRead(lm35);
  temp=(value*4.1);
  temp=temp/10;
    tft.setTextSize(1);
  tft.setCursor(57,4);
  tft.setTextColor(ILI9341_WHITE,ILI9341_BLACK);
  tft.print(temp);
  tft.print(" ");
  tft.print((char)247);
  tft.print("C");
    tft.setTextSize(1);
  tft.setCursor(57,21);
  tft.setTextColor(ILI9341_WHITE,ILI9341_BLACK);
  tft.print(tempC);
  tft.print(" ");
  tft.print((char)247);
  tft.print("C");
    tft.setTextSize(1);
  tft.setCursor(57,37);
  tft.setTextColor(ILI9341_WHITE,ILI9341_BLACK);
  tft.print(tempCC);
  tft.print(" ");
  tft.print((char)247);
  tft.print("C");
      tft.setTextSize(1);
  tft.setCursor(69,53);
  tft.setTextColor(ILI9341_WHITE,ILI9341_BLACK);
  tft.print(tempCCC);
  tft.print(" ");
  tft.print((char)247);
  tft.print("C");

esp8266 code

#include <WiFiClientSecure.h>
#include <ESP8266WiFi.h>
const char* ssid     = "*****";     
const char* password = "*******"; 

const char*  server = "api.openweathermap.org";  // Server URL
unsigned long lastTime = 0;
unsigned long timerDelay = 60000;



WiFiClientSecure client;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200);
  
  //Serial.print("Attempting to connect to SSID: ");
  //Serial.println(ssid);
  WiFi.begin(ssid, password);

  // attempt to connect to Wifi network:
  while (WiFi.status() != WL_CONNECTED) {
    //Serial.print(".");
    // wait 1 second for re-trying
    delay(1000);
  }

  //Serial.print("Connected to ");
  //Serial.println(ssid);
  //Serial.println("\nStarting connection to server...");

}

void loop() {
  if ((millis() - lastTime) > timerDelay) {
    if (WiFi.status() == WL_CONNECTED){
          WiFiClientSecure client;
          client.setInsecure();
          if (!client.connect(server, 443)){
              //Serial.println("Connection failed!");
              }
          else {
            //Serial.println("Connected to server!");
            }
            // Make a HTTP request:
          
          client.println("GET /data/2.5/weather?q=****,**&APPID=****** HTTP/1.0");
          client.println("api.openweathermap.org");
          client.println("Connection: close");
          client.println();
          while (client.connected()) {
            String line = client.readStringUntil('\n');
            if (line == "\r") {
                //Serial.println("headers received");
              break;
            }
          }
            // if there are incoming bytes available
            // from the server, read them and print them:
          while (client.available()) {
            char c = client.read();
            Serial.write(c);
            
          }
        client.stop();
      }
    lastTime = millis(); 
  }
}

esp8266 receives the data properly and can write to the serial port properly, there is no problem in that part, by the way, the baud rate is 115200 every serialport

Please post both full sketches

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Okay. I see. I fixed it

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.