Sending data from Arduino to ESP8266 via I2C communication

Hi everyone,
I'm working on a GPS project. Here my GPS is connected to Arduino NANO and getting Date Time information. Then I tried to sending those data to ESP8266 via I2C communication. I used I2C because I'm hoping to connect two Arduino to ESP8266 and ESP8266 haven't two UARTs. So I used this sketch in my Arduino NANO and got some result.

#include <Wire.h>
#include "SparkFun_Ublox_Arduino_Library.h" 
SFE_UBLOX_GPS myGPS;

#include <SoftwareSerial.h>
SoftwareSerial mySerial(4, 3); 

long lastTime = 0; 

void setup()
{
  Serial.begin(115200);
  while (!Serial); 
  Serial.println("SparkFun Ublox Example");
 do {
    Serial.println("GPS: trying 38400 baud");
    mySerial.begin(38400);
    if (myGPS.begin(mySerial) == true) break;

    delay(100);
    Serial.println("GPS: trying 9600 baud");
    mySerial.begin(9600);
    if (myGPS.begin(mySerial) == true) {
      Serial.println("GPS: connected at 9600 baud, switching to 38400");
      myGPS.setSerialRate(38400);
      delay(100);
    } else {
      delay(2000); 
    }
  } while (1);
  Serial.println("GPS serial connected");

  myGPS.setUART1Output(COM_TYPE_UBX); 
  myGPS.saveConfiguration(); 

  
  Wire.begin(8);
}

void loop()
{
  if (millis() - lastTime > 500)
  {
    lastTime = millis(); 

    long TOW = myGPS.getTimeOfWeek();
    int Year = myGPS.getYear();
    int Month = myGPS.getMonth();
    int Day = myGPS.getDay();
    int Hour = myGPS.getHour();
    int Min = myGPS.getMinute();
    int Second = myGPS.getSecond();
    Serial.print(Year);
    Serial.print(",");
    Serial.print(Month);
    Serial.print(",");
    Serial.print(Day);
    Serial.print(",");
    Serial.print(Hour);
    Serial.print(",");
    Serial.print(Min);
    Serial.print(",");
    Serial.print(Second);
    Serial.print(",");
    Serial.print(TOW);

    Wire.write(Year);
    Wire.write(",");
    Wire.write(Month);
    Wire.write(",");
    Wire.write(Day);
    Wire.write(",");
    Wire.write(Hour);
    Wire.write(",");
    Wire.write(Min);
    Wire.write(",");
    Wire.write(Second);
    Wire.write(",");
    Wire.write(TOW);
    Serial.println();
  }
}

In my Arduino Serial monitor I got this results,

2022,10,10,12,38,38,131936000
2022,10,10,12,38,38,131936200

Then in my ESP8266 I used this sketch to receive data.

#include <Wire.h>
void setup() {
 Serial.begin(9600); 
 Wire.begin(D1, D2); 
}

void loop() {
 Wire.requestFrom(8,10); 
 while(Wire.available()){
    char c = Wire.read();
  Serial.print(c);
 }
 Serial.println();
 delay(100);
}

But I couldn't get better result and my Serial monitor like this,

⸮⸮⸮⸮⸮⸮⸮⸮⸮
⸮⸮⸮⸮⸮⸮⸮⸮⸮
⸮⸮⸮⸮⸮⸮⸮⸮⸮

I tried several times but I couldn't get better result same as my Arduino Serial output. So how can I solve this. Please give me some help.

Thank You.

see the examples of the Wire library

The I2C bus is not just a serial bus, it uses small packages of data. During sending and receiving the Master and Slave keep an eye on each other. The data starts with a "start condition" and ends with a "end condition".
Perhaps my alternative explanation might help.

I am giving below test sketches; where, the ESP8266-I2C Master receives the following message from NANO-I2C Slave at 1-sec interval. You may use these sketches in your application. You can use strtoul() function to extract the individual data items from the received string.

2022,10,10,12,38,38,131936000


Figure-1: Hardware Setup

Master-I2CESP8266 Sketch:

#include <Wire.h>

void setup() 
{
  Wire.begin(D4, D3);   // SDA = D4 (GPIO/DPin-2), SCL = D3 (GPIO-0)
  Serial.begin(115200);
  Serial.print("  ");
}

void loop() 
{
  byte n = Wire.requestFrom(8, 29); //request 29 bytes byte x = Wire.read();
  for(int i = 0; i < n; i++)
  {
    char y = Wire.read();
    Serial.print(y);
  }
  Serial.println();
  delay(1000);
}

Slave-I2CNANO Sketch:

#include <Wire.h>

char myData[] = "2022,10,10,12,38,38,131936000"; //test data
 
void setup()
{
  Serial.begin(115200);
  Wire.begin(8);
  Wire.onRequest(sendEvent);
}

void loop()
{
 
}

void sendEvent()
{
  Wire.write(myData, sizeof myData);
}

Master's Serial Monitor

2022,10,10,12,38,38,131936000
2022,10,10,12,38,38,131936000
2022,10,10,12,38,38,131936000
2022,10,10,12,38,38,131936000

2 Likes

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