BME280 gets wrong values after Ethernet.begin(mac)

Hi community!
I will try to briefly comment my project and my issue. Then I will post the code and the errors. This is my first post.
My project is a PCB with an STM32 blue pill, a BME280 sensor and a 128x32 SSD1306 display connected at B6/B7 I2C bus. I got also a W5500 DEVMO USR-ES1 ethernet module to connect to a MQTT server. This project aims to publish temperature and humidity to an IoT platform and to detect fire. My core is the one from stm32duino.

Sensor, display and W5500 are powered from the 3.3V pin, and the blue pill is powered with 5V from the PC. Both voltages have two caps in order to stabilize them. The project gets an IP and posts to the MQTT server without any problem, but I do have a problem with the sensor. Sensor gives correct values only before the Ethernet.begin(mac) call.

I list some possible errors, things that Ive tried, notes and some thoughts:

  • Not enough power (strange, bme280 just requires 1mA)
  • They just share GND and +3.3V
  • There is no drop of voltage
  • After pulling down reset pin (PA4 PIN) on the ethernet module I keep getting wrong values
  • Colisions between libraries(?)

Things pending to try:
-Power the bme280 with 5V instead
-Try another I2C channel
-Try another bme280 library

//##########################################################
include <Ethernet.h> // GitHub - arduino-libraries/Ethernet: Ethernet Library for Arduino
#include <MQTT.h> // GitHub - 256dpi/arduino-mqtt: MQTT library for Arduino
//#include <SPI.h>
//#include <Wire.h>
//#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define SEALEVELPRESSURE_HPA (1013.25)
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
#define SIZE_HIST 20 // 10min
#define SENSIBILITY 7 // ºC/min
#define FIXED_TEMP 80 // ºC
#define SAMPLE_TIME 30000 // ms
#define PUB_TIME 60000 // ms

#define BME_MISO PB7
#define BME_MOSI PB6
#define LED_ PC13

//byte mac = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte mac = { 0xAA, 0xDD, 0x44, 0x11, 0x99, 0x77 };
EthernetClient net;
//byte ip = {192, 168, 1, 142};
//IPAddress ip(192, 168, 1, 142); // Set the static IP address to use if the DHCP fails to assign
MQTTClient client(1024); //1kb
int eth_flag=0;
int mqtt_flag=0;
unsigned long lastMillis_fire = 0;
unsigned long lastMillis_mqtt = 0;

Adafruit_BME280 bme;

char buffer_t[10], buffer_h[10], buffer_p[10];
int historical[SIZE_HIST];
int delta[SIZE_HIST];
int fire_alarm=0;

float bme_pressure;

void setup(){
Serial.begin(9600);
bme.begin(0x76);
//display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
delay(2000);
pinMode(LED_, OUTPUT);

digitalWrite(LED_, HIGH);
delay(1000);
digitalWrite(LED_, LOW);
delay(1000);
digitalWrite(LED_, HIGH);
delay(1000);
digitalWrite(LED_, HIGH);
delay(2000);

pinMode(PA4, OUTPUT);

dtostrf(bme.readTemperature(),6,2,buffer_t);
dtostrf(bme.readHumidity(),6,2,buffer_h);
bme_pressure=bme.readPressure()/100.0 + 70.0;
dtostrf(bme_pressure,8,2,buffer_p);
dtostrf(bme.readTemperature(),6,2,buffer_t);
dtostrf(bme.readHumidity(),6,2,buffer_h);
bme_pressure=bme.readPressure()/100.0 + 70.0;
dtostrf(bme_pressure,8,2,buffer_p);
//
Serial.println("Ti");
Serial.println(buffer_t);
Serial.println(buffer_h);
Serial.println(buffer_p);
Ethernet.init(PA4); //get IP via DHCP
//Ethernet.begin(mac);
delay(2000);
eth_connect();
Serial.print("Local IP is ");
Serial.println(Ethernet.localIP());
//Ethernet.begin(mac,ip);
client.begin("acme.thinger.io", net); //Broker address, port and client
//client.onMessage(messageReceived);
//mqtt_connect();

}

void loop(){
float bme_t;
float bme_h;
float bme_p;

if (millis() - lastMillis_mqtt > PUB_TIME) {
lastMillis_mqtt = millis();

bme_t=bme.readTemperature();
bme_h=bme.readHumidity();
bme_p=bme.readPressure()/100.0 + 70.0;
dtostrf(bme_t,6,2,buffer_t);
delay(10);
dtostrf(bme_h,6,2,buffer_h);
delay(10);
dtostrf(bme_p,8,2,buffer_p);
delay(10);
eth_connect();
mqtt_connect();

Serial.println("T2");
client.publish("/temperature", buffer_t);
client.publish("/humidity", buffer_h);
client.publish("/pressure", buffer_p);

client.disconnect();
}
Ethernet.maintain();
delay(10);
}

void mqtt_connect(){
int mqtt_attempts=0;
mqtt_flag=0;
while ( (!client.connect("mqtt_2", "adrian_test", "cXFguAbmzha")) && (!(mqtt_attempts==10)) ){ // clientId, username, password
Serial.println(".");
delay(100);
mqtt_attempts++;
if(mqtt_attempts==10){
mqtt_flag=1;
Serial.println("Mqtt connect fail");
//NVIC_SystemReset();
}
}

//client.subscribe("/temperature");
//client.subscribe("/humidity");
//client.subscribe("/pressure");
//client.unsubscribe("/temperature");
}

void eth_connect(){
int w5500=0;
int eth_reconnect=0;
delay(2000);
dtostrf(bme.readTemperature(),6,2,buffer_t);
dtostrf(bme.readHumidity(),6,2,buffer_h);
bme_pressure=bme.readPressure()/100.0 + 70.0;
dtostrf(bme_pressure,8,2,buffer_p);
dtostrf(bme.readTemperature(),6,2,buffer_t);
dtostrf(bme.readHumidity(),6,2,buffer_h);
bme_pressure=bme.readPressure()/100.0 + 70.0;
dtostrf(bme_pressure,8,2,buffer_p);
//
Serial.println("Ti2");
Serial.println(buffer_t);
Serial.println(buffer_h);
Serial.println(buffer_p);
delay(2000);
Ethernet.begin(mac);
Serial.println("eth begin");
dtostrf(bme.readTemperature(),6,2,buffer_t);
dtostrf(bme.readHumidity(),6,2,buffer_h);
bme_pressure=bme.readPressure()/100.0 + 70.0;
dtostrf(bme_pressure,8,2,buffer_p);
dtostrf(bme.readTemperature(),6,2,buffer_t);
dtostrf(bme.readHumidity(),6,2,buffer_h);
bme_pressure=bme.readPressure()/100.0 + 70.0;
dtostrf(bme_pressure,8,2,buffer_p);
//
Serial.println("Ti3");
Serial.println(buffer_t);
Serial.println(buffer_h);
Serial.println(buffer_p);
Serial.println("LOW");
digitalWrite(PA4, LOW);
delay(5000);
Serial.println("HIGH");
digitalWrite(PA4, HIGH);
delay(5000);
dtostrf(bme.readTemperature(),6,2,buffer_t);
dtostrf(bme.readHumidity(),6,2,buffer_h);
bme_pressure=bme.readPressure()/100.0 + 70.0;
dtostrf(bme_pressure,8,2,buffer_p);
dtostrf(bme.readTemperature(),6,2,buffer_t);
dtostrf(bme.readHumidity(),6,2,buffer_h);
bme_pressure=bme.readPressure()/100.0 + 70.0;
dtostrf(bme_pressure,8,2,buffer_p);
//
Serial.println("Ti4");
Serial.println(buffer_t);
Serial.println(buffer_h);
Serial.println(buffer_p);

while((!(Ethernet.hardwareStatus() == EthernetW5500)) && (!eth_reconnect) ){
if(Ethernet.linkStatus() == LinkOFF)
Serial.println("N-L");
if(Ethernet.hardwareStatus() == EthernetNoHardware)
Serial.println("N-HW");
delay(100);
w5500++;
if(w5500==3) eth_reconnect=1;
else eth_reconnect=0;
}
w5500=0;

//Serial.print("Local IP is ");
//Serial.println(Ethernet.localIP());
delay(10);
}

void messageReceived(String &topic, String &payload) {
//Serial.println("Incoming: from topic "" + topic + "" Payload "" + payload + """);
}

//#########################################################

The could at the beginning pulls to serial the values and after Ethernet.begin(mac) they are wrong, even if I reset the module, as long as there is no reset in the ethernet library:

It sounds like the 3.3V is being overloaded. A power supply the Arduino is NOT!

I think it might not be the best idea as you said, to power things from STM32. As I have seen, ethernet module consumption could go till 200mA, display till 25mA and BM280 1mA. I would think that if there is not enough power, the ethernet module would not work, but it works, and there is not drop of voltage.

Hi there,
I got the problem. Theres no problem with the stm32 going out of resources, thres no problem of power nor problems with library collisions. I had a defective via in my pcb. STM32 is suitable to work with a ethernet w5500 module, and a display and a bme280 at the same i2c bus. Thanks for all the replies and concerns. They guy who replied has any idea.