Arduino wire.requestFrom() read different strings ?

Hello everyone,
I am tryin to connect my Arduino Mega to NodeMCU by I2C. The master is NodeMCU and the slave is Arduino. I would like to make the Arduino send different text messages to the NodeMCU, and with each one I would like the NodeMCU to do a different thing.

I've made a counter that increases in the loop of Arduino and whenever it reaches a different number I made the Arduino send a different message to the NodeMCU using the requestEvent in Arduino (of course when the NodeMCU calls requestFrom).

The problem is, I don't seem to be receiving the words in the NodeMCU. Also, the requestFrom takes a parameter of bytes size and each word has different size, how can I take different words each time and perform a different thing.

My code looks something like that:
Arduino Code:

int sec = 0; // increases by 1 in Arduino's loop
void requestEvent()
{
  Wire.write("Hello$");
  delay(2000);
  if (sec == 10) {
    Wire.write("Play$");
    current_mood = "bored";
  }
  if (sec == 30 && petted_flag != 1) {
    Wire.write("Sad$");
    current_mood = "sad";
  }
  if (sec >= 10 && sec <= 29) {
    Wire.write("Happy$");
    current_mood = "happy";
    petted_flag = 1;
  }
 
}

NodeMCU Code:

void loop() {
 Wire.beginTransmission(8); /* begin with device address 8 */
 Wire.write("Hello Arduino");  /* sends hello string */
 Wire.endTransmission();    /* stop transmitting */
 
 Wire.requestFrom(8, 13); /* request & read data of size 13 from slave */
 while(Wire.available()){
  char c = Wire.read();
  if (c == '

Please help I've been trying to do this for the past two days.
Thank you very much :)){
    logged_uid = "123123123";
   
      if (pet_status == "Play") {
        Serial.println("i'm in nodeMCU play");
        delay(1000);
        if (logged_uid != "NUN") {
          sendMessage(logged_uid, "Hey+Buddy");
          delay(5000);
        }
      }

if (pet_status == "Happy") {
        Serial.println("i'm happy");
        delay(1000);
          if (logged_uid != "NUN") {
            mood = "happy";
            points += 10;
            delay(5000);
          }
      }
 
      if (pet_status == "Sad") {
        Serial.println("i'm sad");
delay(1000);
          if (logged_uid != "NUN") {
            mood = "sad";
            delay(5000);
          }
      }
     
      pet_status = "";
      logged_uid = "NUN";
      delay(800);
  } else {
      pet_status += String(c);
  }
}
Serial.println();
delay(1000);
}


Please help I've been trying to do this for the past two days.
Thank you very much :)

The requestEvent function is called by an interrupt service routine, do not use delay, print, etc, and declare variables as volatile. Only use a single Wire.write, and make it the same number of bytes each time.

I would think what you'd want to do is have the NODEMCU periodically send a short message that basically requests a status byte. Most of the time the Arduino will return a byte that says "IDLE". But, when it needs to do something else, it returns a different byte that tells the NODEMCU to do something different. That something different might include sending a different message to transfer a specific type of data in one direction or the other. That way, both ends of the link always stay in-sync, and both know what it expect next.

Tell me names of three different ASCII coded text messages that you want the NodeMCU to receive from MEGA.

RayLivingston:
I would think what you'd want to do is have the NODEMCU periodically send a short message that basically requests a status byte. Most of the time the Arduino will return a byte that says "IDLE". But, when it needs to do something else, it returns a different byte that tells the NODEMCU to do something different. That something different might include sending a different message to transfer a specific type of data in one direction or the other. That way, both ends of the link always stay in-sync, and both know what it expect next.

Thank you all for answering,
@RayLivingston could please explain more ? Kind of not getting what you mean.

@GolamMostafa in the above code, I've sent "Happy", "Sad" and "Play" for example.

Wire.requestFrom is always going to return 13 bytes of data, regardless of how many the slave sends. pet_status likely has leftover characters from your last requestFrom.

Hello everyone,
I am trying to connect my Arduino Mega to NodeMCU by I2C. The master is NodeMCU and the slave is Arduino. I would like to make the Arduino send different text messages to the NodeMCU, and with each one, I would like the NodeMCU to do a different thing.

I've made a counter that increases in the loop of Arduino and whenever it reaches a different number I made the Arduino send a different message to the NodeMCU using the requestEvent in Arduino (of course when the NodeMCU calls requestFrom).

The problem is that sometimes the master reads the same massage that has been sent to him from the slave twice. In my case, in the NodeMCU master code, it gets inside the if statements; if(pet_status=="Play") or if(pet_status=="Sleep") twice instead of once. In other words, when the loop in the NodeMCU code returns to its beginning after it got inside the if statement for the first time (for example inside of - if(pet_status=="Play") ), the wire.read() inside the while reads again the same message "Play" and gets inside the if statement -if(pet_status=="Play") , again for the second time.

My code looks something like that:
Arduino Code:

int sec = 0; // increases by 1 in Arduino's loop

void requestEvent() {
 
  if (sec == 10) {
    //Serial.println("in arduino play");
    Wire.write("Play$        ");
    Wire.write("$            ");
    petted_flag = 0;
    pet_is_sleeping = 0;
    current_mood = "bored";
  }
  if (sec == 30 && petted_flag != 1) {
    Wire.write("Sad$         ");
    Wire.write("$            ");
    current_mood = "sad";
  }
  if (sec >= 10 && sec <= 29 && petted_flag != 1 && distance < 3) {
    Wire.write("Happy$       ");
    Wire.write("$            ");
    current_mood = "happy";
    petted_flag = 1;
  }

  if (sec == 40) {
    Wire.write("Sleep$       ");
    current_mood = "sleepy";
    sleep_flag = 0;
    turn_on_ldr_flag = 1;
    pet_is_sleeping = 0;
    ldrValue = 10000;
  }
  if (sec == 60 && sleep_flag != 1) {
    Wire.write("Sad$         ");
    current_mood = "sad";
    turn_on_ldr_flag = 0;
    pet_is_sleeping = 0;
    ldrValue = 10000;
  }
  if (sec >= 40 && sec <= 59 && sleep_flag != 1 && ldrValue < 600) {
    Wire.write("Happy$       ");
    current_mood = "off";
    pet_is_sleeping = 1;
    sleep_flag = 1;
    turn_on_ldr_flag = 0;
    ldrValue = 10000;
  }
}

NodeMCU Code:

void loop() {
  Wire.beginTransmission(8); /* begin with device address 8 */
  Wire.write("Hello Arduino");  /* sends hello string */
  Wire.endTransmission();    /* stop transmitting */
  
  Wire.requestFrom(8, 13); /* request & read data of size 13 from slave */
  pet_status = "";
  
  while(Wire.available()){
    char c = Wire.read();
	
    if (c == '

Please :frowning: help me solve the problem!!! Thank you very very much :kissing:){

logged_uid = getLoggedUid();
     
      if (pet_status == "Play") {
        Serial.println("i'm in nodeMCU play");
        delay(1000);
        if (logged_uid != "NUN") {
          sendMessage(logged_uid, "Hey+Buddy,+Please+pet+me.");
          delay(5000);
        }
        pet_status = "";
      }
 
      if (pet_status == "Sleep") {
        Serial.println("i'm in nodeMCU sleep");
        delay(1000);
        if (logged_uid != "NUN") {
          sendMessage(logged_uid, "Hey+Buddy,+I+want+to+sleep.");
          delay(5000);
        }
      }
 
      if (pet_status == "Happy") {
        Serial.println("i'm happy");
        delay(1000);
       
      }
 
      if (pet_status == "Sad") {
        Serial.println("i'm sad");
      }

pet_status = "";
      delay(800);
    } else {
        pet_status += String(c);
    }

}
Serial.println();
delay(1000);
}


Please :( help me solve the problem!!! Thank you very very much :kissing:

Is this some type of school assignment? Looks very similar to this post Arduino wire.requestFrom() read different strings ?

I think it is the same person.

david_2018:
Is this some type of school assignment? Looks very similar to this post Arduino wire.requestFrom() read different strings ?

@david_2018
yes, we are working on the same project,
we will be glad if you can help us solve the problem that is mentioned In this post.

Have a look at this webpage I2C - Two-Wire Peripheral Interface - for Arduino there is a section on Wire.requestFrom()

How are you connecting the Mega to the NodeMCU? The NodeMCU runs at 3.3 volts and the I2C pins may not be 5 volt tolerant.

david_2018:
Have a look at this webpage I2C - Two-Wire Peripheral Interface - for Arduino there is a section on Wire.requestFrom()

How are you connecting the Mega to the NodeMCU? The NodeMCU runs at 3.3 volts and the I2C pins may not be 5 volt tolerant.

Each one of the NodeMCU and the Arduino Mega are connected to the computer through the USB cable and the NodeMCU and the Arduino mega are connected together with the I2C pins

Topics on the same project merged

If it turns out that there is only one user and not two on the same team then both users will be banned

UKHeliBob:
Topics on the same project merged

If it turns out that there is only one user and not two on the same team then both users will be banned

@UKHeliBob - we are not the same person, I should have posted my question there... Sorry, I am new here...

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