Arduino and ESP8266 data communication using i2c

I want to send two string values from slave Arduino to the master ESP8266 over i2c.

I can send the value in string format to the ESP8266. Since I have two string values to send, my idea was to send the string values using a comma (,) deliminator ; for example value1 and value2; to send as "value1,value2" to the master device.

At the master device (ESP8266), I used the below code to receive the value;

  while (Wire.available())
{
    char c = Wire.read();
    String receivedvalue= String(c);
Serial.print(receivedvalue);
  }

This gives me the output to the serial monitor with the additional character � (because the actual size of data is less than 13, but the buffer size is 13)

My objective is to parse the received data using the comma (,) deliminator so that I can get the value in two string variables like below;
variableString1= value1
variableString2= value2

Is this the correct way of sending and receiving data? If so, how can I parse and read it properly?

Copy the characters received into your variable until you see the "," then copy the rest into the second into the second variable. Do not save the ,.
It might be easier if you send the first byte as the length then send two messages. Using the comma or any other character you need to be careful as it cannot appear in your string.

1 Like

This code is perfectly useless.
First, you don't read the string value, but only single byte.
Next, you throw out the received value immediately after receive...

1 Like

Do you want to send text "Hello World" or floating point values "1.003,12.4" or integer values "123,456" ?

Does the ESP8266 have a spare Serial TX signal ? Could you use a Serial communication instead ?

The I2C bus works with packets of data. You can not put data in the bus which comes out at the other side, it is not that kind of bus. The I2C bus good for reading a few bytes from a sensor.

1 Like

Thanks. I will try using Serial bus instead. I want to send floating value but value from two sensors. So, I wanted to use an eliminator to isolate the values.

Can you please, give an example of a Cstring-type data item (consisting of comma separated sub-items) that the I2C-Master will receive from I2C-Slave? For example:

"Hello World,134.57"

1 Like

You don't need a string for it. Instead of converting float to string on first arduino and convert string to float on another - you can just send a float as is, in bytes. In that case, additionally, the separation of the two values won't required.

1 Like

The data would look like;
3.1416 for first sensor and 20.245 for second sensor.

Apologize as I am not very good in this language.

I will try to find out how to do it as you suggested.

I am not good in this programing environment.

I think this topic will be helpful for me

Serial Input Basics - updated

1. Assuming that --
The 1st number (3.1416) has 6 ASCII coded symbols including the decimal point.
The 2nd number (20.245) has also 6 ASCII coded symbols including the decimal point.

2. I2C-Master Codes to request Slave for 12 ASCII coded symbols and then extracting the individual float data items.

char myData[13];
char *token;
byte m = Wire.requestFrom(7BitslaveAddress, 12);
for(int i = 0; i<12; 1++)
{
     myData[i] = Wire.read();
}
myData[m] = '\0';   //insert null-byte
token = strtok(myData, ",");
float 1stNumber = atof(token);   
Serial.print("Received 1stNumber is: "); Serial.println(1stNumber, 4); //3.1416

token = strtok(NULL, ",");
float 2ndNumber = atof(token);
Serial.print("Received 2ndNumber is: "); Serial.println(2ndNumber, 3); //20.245

3. I2C-Slave Codes to respond to Master's request and then put comma separated data items into I2C Buffer for onward transmission to Master.

Wire.onRequest(sendEvent);

void sendEvent()
{
    Wire.print(3.1416);
    Wire.print(',');
    Wire.print(20.245);
}
1 Like

Wonderful.

Thanks a lot. Have not tried yet but I know it will work.

1 Like

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