Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

Esp8266 I2C.read JS Mongoose

MheleneMhelene Paris
edited August 29 in Mongoose OS

Hello all,

I'm new to Mongoose and this forum. I'm starting a project with Mongoose and I'm trying to use the I2C library however I have some issues with the READ option.
My device 1 is a NodeMCU 1.0 controlled by Mongoose my device 2 is an Arduino.

My first task, is to send data from NodeMCU to Arduino. This one works without any issue. :)

My code is the following in Mongoose :

load('api_i2c.js');
let i2c = I2C.get();
let addr = 8;
let data = "0";
I2C.write(i2c, addr, data, data.length, true);

My Arduino Code is the following :

void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
}
void loop() {
  delay(100);
}
void receiveEvent(int howMany) {
  Serial.print("Text : ");
  while (Wire.available()>0) {
    reponse = Wire.read();
    Serial.print(reponse);
  }
}

My second task is to send data from Arduino to NodeMCU through I2C.read in Mongoose and wire.write in Arduino IDE and this one doesn't work... :'(

My code is the following in Mongoose :

load('api_i2c.js');
let i2c = I2C.get();
let addr = 8;
let dataReceived = I2C.read(i2c, addr, 2, true); //reading 2 bytes
if (dataReceived) print("Hello : ", JSON.stringify([dataReceived.at(0), dataReceived.at(1)]));

My Arduino Code is the following :

void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Serial.begin(9600);           // start serial for output
}
void loop() {
  delay(100);
  Wire.write("Ok");
}

It doesn't seem to work and the output I receive in the Mongoose console, instead of "Ok" (or whatever I try to send, for that mater) is the following :
"Hello : x00/xff"

Are there any glaring issues with my code? Do you have any ideas on how I could solve my problem?
Thanks in advance!

Comments

Sign In or Register to comment.