I am connecting a NodeMCU with an Arduino UNO board with the I2C protocol. The Uno board will read Analog values and send them to the NodeMCU.
First I am trying to build a code to make sure I can send more than 1 value from the Arduino board to the NodeMCU. I decided to use an array to see if I could make this happen, but unfortunately I am not receiving the values I want.
I am using the NodeMCU as a master and the Arduino as a slave.
Code for Arduino (slave):
#include <Wire.h>
void setup() {
Wire.begin(8); /* join i2c bus with address 8 */
Wire.onRequest(requestEvent); /* register request event */
Serial.begin(9600); /* start serial for debug */
}
void loop() {
delay(100);
Serial.println();
}
// function that executes whenever data is requested from master
void requestEvent() {
byte x[2]={25,0};
Serial.println(x[0]);
Serial.println(x[1]);
Wire.write(x,2);
}
Code for NodeMCU (master):
#include <Wire.h>
#include <ESP8266WiFi.h>
void setup() {
Serial.begin(9600); /* begin serial for debug /
Wire.begin(D1, D2); / join i2c bus with SDA=D1 and SCL=D2 of NodeMCU */
}
void loop() {
byte x[2];
Wire.requestFrom(8, 2); /* request & read data of size 2 from slave */
while(Wire.available()){
x [0] = Wire.read();
x [1] =Wire.read();
Serial.println(x[0]);
Serial.println(x[1]);
Serial.println();
delay(100);
}
The same code works when I send only one value (for example x=43;), Any suggestions can help, I am really just a beginner when it comes to programming.