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