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.
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...
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.
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:
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. 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.