nodemcu and arduino communication

Hi everyone,
i am working on a project that requires reading multiple sensor at the same time, and they are:
Resistive soil moisture sensor.
DHT11 temperature & humidity sensor.
MQ2 gas sensor.
Ky018 LDR module.
C43 water level sensor.

I am using the Nodemcu as a master and an Arduino UNO as a slave for reading these sensors data.
i’ve tried the serial communication method and the Arduino doesn’t read any of these sensors. i am aware that Nodemcu and Arduino have different voltages, and i used resistors as voltage divider and also logic level converter. And nothing comes out.
i’ve also tried the I2C method but it only reads one sensor out of five.

Any help would be appreciated!

arduino_code_slave_.ino (1.17 KB)

esp_code_master_.ino (905 Bytes)

i've tried the serial communication method and the Arduino doesn't read any of these sensors.

Then you did something wrong.

Any help would be appreciated!

And, we would appreciate you reading the stickies at the top of the forum, where you would have learned that posting in this section of the forum requires that you POST YOUR CODE. And, you would have learned HOW. Don't screw that up, now. You've been warned.

//arduino_code(slave)

#include <Wire.h>
#include <TroykaDHT.h>
DHT dht(4, DHT11);//dht(pin,type)
int soil = A0;
int level=A1;
int smoke=A2;
int ldr=A3;
int t=0,h=0;
int smokevalue = 0;
int soilvalue =0;
int levelvalue =0;
int ldrvalue=0;

void setup() {
dht.begin();
Wire.begin(8); // Slave id #8
Wire.onRequest(requestEvent); // fucntion to run when asking for data
Serial.begin(9600); // serial for displaying data on your screen

}

void loop() {
dht.read();
t=dht.getTemperatureC();
h=dht.getHumidity();
smokevalue = analogRead(smoke);
soilvalue = analogRead(soil);
levelvalue = analogRead(level);
ldrvalue=analogRead(ldr);
Serial.print(ldrvalue);
Serial.print(" “);
Serial.print(levelvalue);
Serial.print(” “);
Serial.print(smokevalue);
Serial.print(” “);
Serial.print(soilvalue);
Serial.print(” “);
Serial.print(t);
Serial.print(” ");
Serial.print(h);
Serial.println();
delay(1000);
}

// function: what to do when asked for data
void requestEvent() {
Wire.write(ldrvalue);
Wire.write(levelvalue);
Wire.write(smokevalue);
Wire.write(soilvalue);
Wire.write(t);
Wire.write(h);
}

//nodemcu_code(master)

#include <Wire.h>
int t=0,h=0;
int smokevalue = 0;
int soilvalue =0;
int levelvalue =0;
int ldrvalue=0;
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}

void loop() {
Wire.requestFrom(8,6); // request 3 bytes from slave device #8

//gathers data comming from slave
while (Wire.available()) {
ldrvalue = Wire.read();
levelvalue = Wire.read();
smokevalue = Wire.read();
soilvalue= Wire.read();
t = Wire.read();
h = Wire.read();
}

Serial.print(ldrvalue);
Serial.print(" “);
Serial.print(levelvalue);
Serial.print(” “);
Serial.print(smokevalue);
Serial.print(” “);
Serial.print(soilvalue);
Serial.print(” “);
Serial.print(t);
Serial.print(” ");
Serial.print(h);
Serial.println();
delay(1000);
}

It's obvious that you didn't read the stickies, or that you assumed that they didn't apply to you. They most certainly do.

You need to stop using smiley faces in your code.

PaulS:
It's obvious that you didn't read the stickies, or that you assumed that they didn't apply to you. They most certainly do.

You need to stop using smiley faces in your code.

i have attached the codes above

smokevalue = analogRead(smoke);
soilvalue = analogRead(soil);
levelvalue = analogRead(level);
ldrvalue=analogRead(ldr);

When switching analog pins, it is sometimes necessary to read the pin twice, and throw away the first reading.

void requestEvent() {
Wire.write(ldrvalue);
Wire.write(levelvalue);
Wire.write(smokevalue);
Wire.write(soilvalue);
Wire.write(t);
Wire.write(h);

The write() method expects a byte. If all the values are less than 255, then int is the wrong type for them all. If any values are greater than 255, then they are not being sent correctly.

With the Arduino (slave) connected to the PC, the NodeMCU in a drawer, and the Serial Monitor application opn, what do you see?

  while (Wire.available()) {
    ldrvalue = Wire.read(); 
    levelvalue = Wire.read();
  smokevalue = Wire.read(); 
  soilvalue= Wire.read(); 
   t = Wire.read();
  h = Wire.read();
  }

While? Why on earth are you using a while statement to execute code that should be executed ONCE?

The read() method returns a byte. It is pointless to waste memory storing the one byte value in a two byte variable.