Need help with I2C

I have a problem which i was struggling with for the past 4 days, I'm a beginner. I am trying to make arduino nano 33 ble sense send it's onboard temperature, pressure and humidity sensor through I2C to a NodeMcu v0.9 which then sends the data to the arduino Iot cloud. I'm using the <I2C_Anything.h> and <Wire.h> libraries. Everything but the connection between the Arduino and Nodemcu works fine, may i have some help? Here is the arduino (Master) code:

#include <Arduino_LPS22HB.h>
#include <Arduino_HTS221.h>
#include <Wire.h>
#include <I2C_Anything.h>

const byte SLAVE_ADDRESS = 42;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  while (!Serial);


  if (!BARO.begin()) {
    Serial.println("Failed to initialize pressure sensor!");
    while (1);
  }

  if (!HTS.begin()) {
    Serial.println("Failed to initialize humidity temperature sensor!");
    while (1);
  }
}

void loop() {

  float pressure = BARO.readPressure();
  Serial.print("Pressure = ");
  Serial.print(pressure);
  Serial.println(" kPa");

  float temperature = BARO.readTemperature();
  Serial.print("Temperature = ");
  Serial.print(temperature);
  Serial.println(" C");

  float htstemperature = HTS.readTemperature();
  Serial.print("Temperature = ");
  Serial.print(htstemperature);
  Serial.println(" °C");

  float humidity    = HTS.readHumidity();
  Serial.print("Humidity    = ");
  Serial.print(humidity);
  Serial.println(" %");

Wire.beginTransmission (SLAVE_ADDRESS);
    I2C_writeAnything (temperature);
    I2C_writeAnything (humidity);
    I2C_writeAnything (pressure);
    Wire.endTransmission (); 

  delay(100);
}

And here is the NodeMcu (Reciever) code:

#include "arduino_secrets.h"
/* 
  Sketch generated by the Arduino IoT Cloud Thing  

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  float htstemperature;
  float humidity;
  float pressure;
  float temperature;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"
#include <Wire.h>
#include <Wire.h>
#include <I2C_Anything.h>

const byte MY_ADDRESS = 42;

void setup() {
  Wire.begin (MY_ADDRESS);
  Wire.begin(D1, D2);
  Serial.begin (9600);
  Wire.onReceive(receiveEvent);
  delay(1500); 
  initProperties();
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();

}

void loop() {
  ArduinoCloud.update();
  // Your code h 
  
}

void receiveEvent (int howMany){

if (howMany >= (sizeof temperature) + (sizeof humidity)+ (sizeof pressure))
   {
  I2C_readAnything (temperature);
   I2C_readAnything (humidity);
   I2C_readAnything (pressure);
   }
}

The temperature, humidity and pressure float are defined on another tab.
Also this is my first time using the forums so i may have put this in the wrong page.

Welcome to the forum.

Nano 33 BLE Sense: a nRF52840 processor with Mbed. The onboard sensors are on a different I2C bus than the I2C bus at A4 and A5 for external sensors.
NodeMcu v0.9: ESP8266.

Does the ESP8266 support I2C Slave mode ? I don't think so: https://arduino-esp8266.readthedocs.io/en/3.1.2/libraries.html#i2c-wire-library
Please don't make the Nano 33 BLE Sense the I2C Slave. Projects fail that have two processors with a I2C bus between them.

Can you buy a ESP32 and buy those sensors and do your whole project with a ESP32 ? That will work. That is normal project.

The I2C bus is not a fail-safe or sturdy bus. It is meant to get data from sensors that are on the same board (or very close). It is a good bus if you know its limitation, and it causes a lot of trouble if you think that you can put data in at one end and assume that the data comes out at the other end.

Thanks, but do you think i can use serial or/and a adafruit feather HUZZAH because i'm not currently able to buy anything.

nevermind, I'll just buy the sensors separately.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.