Arduino Mega 2560 + ESP8266 Write data on serial not working?!

Hello everyone I'm new here and I want to ask for your help.

I am working on a project, where I use an Arduino Mega 2560 + integrated ESP8266 Board.
The used board is this one: Click here

I have already managed to write code for both boards, that pretty much does (independently) what I want. The ESP8266 schould fetch the epoch time from an ntp server and pass that value over a serial port.

The Arduino should recieve that serial data passed from the ESP8266 and use the epoch time as basis to adjust an RTC1307 connected via I2c.

For themselves the Chips do what I want from them. I just can't achieve the serial communication between them.

How can I achieve a proper connection between the boards, so that I can read the transmitted data from the ESP8266?

This is the code I used for the ESP8266:

#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

const char *ssid     = "";
const char *password = "";

const long utcOffsetInSeconds = 3600;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);

void setup(){
  Serial.begin(74880);

  WiFi.begin(ssid, password);

  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    //Serial.print ( "." );
  }

  timeClient.begin();
}

void loop() {
  timeClient.update();

  Serial.println(timeClient.getEpochTime());

  delay(3000);
}

ssid and password are properly used. I just deleted the real information. Connections works as intended.

1,2 ON for esp8266 and ATmega communication.
RX3/TX3 switch for ATmega Serial3 connected to the esp8266
3,4 ON for ATmega Serial on USB

This is my Code @ Arduino Mega 2560

#include <Wire.h>
#include <I2C_RTC.h>

#define A1 2
#define B1 3
#define C1 4
#define D1 5
#define A2 6
#define B2 7
#define C2 8
#define D2 9
#define A3 10
#define B3 11
#define C3 12
#define D3 22
#define A4 23
#define B4 24
#define C4 25
#define D4 26
#define A5 27
#define B5 28
#define C5 29
#define D5 30
#define A6 31 
#define B6 32
#define C6 33
#define D6 34
#define A7 35 
#define B7 36
#define C7 37
#define D7 38
#define A8 39
#define B8 40
#define C8 41
#define D8 42
#define A9 43
#define B9 44
#define C9 45
#define D9 46
#define A10 47 
#define B10 48
#define C10 49
#define D10 50

char A[10] = {A1, A2, A3, A4, A5, A6, A7, A8, A9, A10};
char B[10] = {B1, B2, B3, B4, B5, B6, B7, B8, B9, B10};
char C[10] = {C1, C2, C3, C4, C5, C6, C7, C8, C9, C10};
char D[10] = {D1, D2, D3, D4, D5, D6, D7, D8, D9, D10};

static DS1307 RTC;

//Start Setup
void setup()
{
  pinMode(A1, OUTPUT);
  pinMode(B1, OUTPUT);
  pinMode(C1, OUTPUT);
  pinMode(D1, OUTPUT);

  pinMode(A2, OUTPUT);
  pinMode(B2, OUTPUT);
  pinMode(C2, OUTPUT);
  pinMode(D2, OUTPUT);

  pinMode(A3, OUTPUT);
  pinMode(B3, OUTPUT);
  pinMode(C3, OUTPUT);
  pinMode(D3, OUTPUT);

  pinMode(A4, OUTPUT);
  pinMode(B4, OUTPUT);
  pinMode(C4, OUTPUT);
  pinMode(D4, OUTPUT);

  pinMode(A5, OUTPUT);
  pinMode(B5, OUTPUT);
  pinMode(C5, OUTPUT);
  pinMode(D5, OUTPUT);
  
  pinMode(A6, OUTPUT);
  pinMode(B6, OUTPUT);
  pinMode(C6, OUTPUT);
  pinMode(D6, OUTPUT);
  
  pinMode(A7, OUTPUT);
  pinMode(B7, OUTPUT);
  pinMode(C7, OUTPUT);
  pinMode(D7, OUTPUT);
  
  pinMode(A8, OUTPUT);
  pinMode(B8, OUTPUT);
  pinMode(C8, OUTPUT);
  pinMode(D8, OUTPUT);
  
  pinMode(A9, OUTPUT);
  pinMode(B9, OUTPUT);
  pinMode(C9, OUTPUT);
  pinMode(D9, OUTPUT);
  
  pinMode(A10, OUTPUT);
  pinMode(B10, OUTPUT);
  pinMode(C10, OUTPUT);
  pinMode(D10, OUTPUT);

  writenumber(0,9);
  writenumber(1,9);
  writenumber(2,9);
  writenumber(3,9);
  writenumber(4,9);
  writenumber(5,9);
  writenumber(6,9);
  writenumber(7,9);
  writenumber(8,9);
  writenumber(9,9);

  Serial.begin(9600);
  Serial1.begin(78800);
  RTC.begin();

  RTC.setHourMode(CLOCK_H24);

  if (RTC.getHourMode() == CLOCK_H12)
  {
    RTC.setHourMode(CLOCK_H24);
  }

  RTC.setWeek(2);
  RTC.setDate(29, 01, 2024);
  /*RTC.setDay(29);
  RTC.setMonth(1);
  RTC.setYear(2024);*/

  RTC.setHours(9);
  RTC.setMinutes(47);
  RTC.setSeconds(56);

} //End Setup

int day1, day2, mont1, mont2, year1, year2, hour1, hour2, min1, min2;
int day, mont, year, hour, min;

// Start mainloop
void loop()
{
  Serial.print("Start mainloop\n");

  Serial.println(Serial1.read());

  printTimeSerial(); //print time on serial console
  
  writenumber(3,1); //set nixietube 4 to value 1
  writenumber(4,2); //set nixietube 5 to value 1

  //delay(5000);

  writenumber(3,9);
  writenumber(4,9);


//Code for Nixie-digit-calculation
 /* day=RTC.getDay();
  mont=RTC.getMonth();
  year=RTC.getYear();
  hour=RTC.getHours();
  min=RTC.getMinutes();

  hour2 = hour % 10;
  hour = hour / 10;
  hour1 = hour % 10;
  
  writenumber(1, hour1);
  writenumber(2, hour2);
  
  min2 = min % 10;
  min = min / 10;
  min1 = min % 10;

  writenumber(3, min1);
  writenumber(4, min2);

  day2 = day % 10;
  day = day / 10;
  day1 = day % 10;

  writenumber(5, day1);
  writenumber(6, day2);

  mont2 = mont % 10;
  mont = mont / 10;
  mont1 = mont % 10;

  writenumber(7, mont1);
  writenumber(8, mont2);

  year2 = year % 10;
  year = year / 10;
  year1 = year % 10;

  writenumber(9, year1);
  writenumber(10, year2);  */
  
  //delay(5000);
} //End Mainloop

void writenumber(int a, int b) {
  switch (b) {
    case 0:
      digitalWrite(A[a], LOW);
      digitalWrite(B[a], LOW);
      digitalWrite(C[a], LOW);
      digitalWrite(D[a], LOW);
      break;
    case 1:
      digitalWrite(A[a], HIGH);
      digitalWrite(B[a], LOW);
      digitalWrite(C[a], LOW);
      digitalWrite(D[a], LOW);
      break;
    case 2:
      digitalWrite(A[a], LOW);
      digitalWrite(B[a], HIGH);
      digitalWrite(C[a], LOW);
      digitalWrite(D[a], LOW);
      break;
    case 3:
      digitalWrite(A[a], HIGH);
      digitalWrite(B[a], HIGH);
      digitalWrite(C[a], LOW);
      digitalWrite(D[a], LOW);
      break;
    case 4:
      digitalWrite(A[a], LOW);
      digitalWrite(B[a], LOW);
      digitalWrite(C[a], HIGH);
      digitalWrite(D[a], LOW);
      break;
    case 5:
      digitalWrite(A[a], HIGH);
      digitalWrite(B[a], LOW);
      digitalWrite(C[a], HIGH);
      digitalWrite(D[a], LOW);
      break;
    case 6:
      digitalWrite(A[a], LOW);
      digitalWrite(B[a], HIGH);
      digitalWrite(C[a], HIGH);
      digitalWrite(D[a], LOW);
      break;
    case 7:
      digitalWrite(A[a], HIGH);
      digitalWrite(B[a], HIGH);
      digitalWrite(C[a], HIGH);
      digitalWrite(D[a], LOW);
      break;
    case 8:
      digitalWrite(A[a], LOW);
      digitalWrite(B[a], LOW);
      digitalWrite(C[a], LOW);
      digitalWrite(D[a], HIGH);
      break;
    case 9:
      digitalWrite(A[a], HIGH);
      digitalWrite(B[a], LOW);
      digitalWrite(C[a], LOW);
      digitalWrite(D[a], HIGH);
      break;
  }
}

void printTimeSerial(){

  switch (RTC.getWeek())
  {
    case 1:
      Serial.print("SO");
      break;
    case 2:
      Serial.print("MO");
      break;
    case 3:
      Serial.print("DI");
      break;
    case 4:
      Serial.print("MI");
      break;
    case 5:
      Serial.print("DO");
      break;
    case 6:
      Serial.print("FR");
      break;
    case 7:
      Serial.print("SA");
      break;
  }
  Serial.print(" ");
  Serial.print(RTC.getDay());
  Serial.print("-");
  Serial.print(RTC.getMonth());
  Serial.print("-");
  Serial.print(RTC.getYear());

  Serial.print(" ");

  Serial.print(RTC.getHours());
  Serial.print(":");
  Serial.print(RTC.getMinutes());
  Serial.print(":");
  Serial.print(RTC.getSeconds());
  Serial.print(" ");
  Serial.println("");
  delay(1000);
}

to
get time on esp8266

  configTime(TIME_ZONE, "pool.ntp.org");
  time_t now = time(nullptr);
  while (now < SECS_YR_2000) {
    delay(100);
    now = time(nullptr);
  }

with

#include <sntp.h>
#include <TZ.h>

Using Serial3 worked perfectly. Thanks a lot for your fast reply!

the DIP switches fall apart if switched frequently.
I recommend you to put AT firmware 1.7.4 in the esp8266 library and use my WiFiEspAT library in the ATmega.

Thanks but that is a bit too much for me.

I now got a new problem: If i switch the esp8266 to TX3/RX3, then my rtc on sda/scl is not working anymore. Why is that so? I mean, I2C and serial are'nt the same, right?

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