Slave Code :
#include <Wire.h>
String data = "";String CpyCommand;
volatile bool process_it=false;
String p;
int x=0;
void setup()
{
Wire.begin(8);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
Serial.begin(115200);
}
void loop()
{
if(!data.equals(String("")))
{
CpyCommand=data;
Serial.println("Command Recieved...");
x++;
data="";
if(x>99) //Test
x=0;
p="Hello World Limit Is 62Byte";
process_it=true;
}
else
{
//Serial.println("No Command...");
}
}
void receiveEvent(int howMany)
{
data = "";
while( Wire.available()){
data += (char)Wire.read();
}
}
void requestEvent()
{
if(!process_it)
{
CpyCommand.replace("32Byte",String(1000));
CpyCommand+=String("\r\n");
Wire.write(&CpyCommand[0]);
CpyCommand="";
}
else if(process_it)
{
Wire.write(&p[0]);
p="";
process_it=false;
}
}
Master Code :
#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus (address optional for master) //
Serial.begin(115200);
}
int x=0;
void loop() {
String S="Hello World Limit Is 32Byte "+String(x); //Get 32Bytes
byte err=Send2Bus(S,8);
if(err==0)
{
Serial.println("success");
}
else if(err==1)
Serial.println("data too long to fit in transmit buffer"); //Check If Device Is Alive & Send Command
else if(err==2)
Serial.println("received NACK on transmit of data");
else
{
Serial.println("other error");
}
delay(2);
String data = ""; String isInt; String processed;
Wire.requestFrom(8,S.length());
while( Wire.available()){ //Get Wait Time
data += (char)Wire.read();
}
Serial.println(data);
int idex=data.indexOf("Is ");
isInt=data.substring(idex+3,idex+3+4);
Serial.println("Now Waiting 4 .. "+String(isInt.toInt())+"ms ...\nProcessing...");
delay(isInt.toInt()); //Wait For Specfied Time
data="";
delay(2);
x++;
if(x>999){ x=0;}
}
bool Send2Bus(String S,int devid)
{
char *send=&S[0];
Wire.beginTransmission(devid);
Wire.write(send);
byte err=Wire.endTransmission();
}
need to retrieve 32bytes 4 times
On First Read First 32bytes and so on
Can't Get it To Fetch Value from The Slave When The requestfrom() is called again
if Works until slave tells master to wait for specific time and then call requestfrom() to read the sensor
can any body post the corrected code please or the correction.