I have an ESP12E, 4MB flash loaded with micropython. I would like to read from an I2C sensor. I can do this without trouble when the power pin of the I2C sensor (a BMP280) is connected directly to the power pin of the board.
However, I would like to connect the power pin of the sensor to one of the board's GPIO. This is to power the sensor only when the board is awake. Here is the code I have so far:
Code: Select all
import json
import machine
import time
import ubinascii
# Personal module
import config
power_pin = machine.Pin(4, machine.Pin.OUT)
power_pin.on()
# time.sleep(1)
i2c = machine.I2C(scl=machine.Pin(config.SCL_PIN), sda=machine.Pin(config.SDA_PIN))
temp_sensor = config.setup_sensors(i2c)
power_pin.off()
while True:
power_pin.on()
time.sleep(5)
try:
temp_sensor = config.setup_sensors(i2c)
temperature, pressure, humidity = temp_sensor.values
temperature = float(temperature.replace("C", ""))
pressure = float(pressure.replace("hPa", ""))
humidity = float(humidity.replace("%", ""))
print("Temperature, pressure, humidity:", temperature, pressure, humidity)
except Exception as e:
print("BME280 reading failed with: {}".format(e))
power_pin.off()
payload = {"temperature": temperature, "humidity": humidity, "pressure": pressure}
print(payload)
print("going to sleep")
# machine.deepsleep()
time.sleep(5)
Code: Select all
power on or hard reset
Sensors ready
Sensors ready
Temperature, pressure, humidity: 20.02 657.64 63.16
{'humidity': 63.16, 'pressure': 657.64, 'temperature': 20.02}
going to sleep
Sensors ready
Temperature, pressure, humidity: 20.02 657.64 63.16
{'humidity': 63.16, 'pressure': 657.64, 'temperature': 20.02}
going to sleep
Sensors ready
Temperature, pressure, humidity: 20.02 657.64 63.16
{'humidity': 63.16, 'pressure': 657.64, 'temperature': 20.02}
going to sleep
Sensors ready
Temperature, pressure, humidity: 20.02 657.64 63.16
{'humidity': 63.16, 'pressure': 657.64, 'temperature': 20.02}
going to sleep
Could you please provide an explanation as to why this is happening, and how I can solve this problem?
Kind regards