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.
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.
#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
// 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
}
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.