Last active
March 3, 2024 02:24
-
-
Save ksasao/b27bfce67244f273f3b6a13f097a38d1 to your computer and use it in GitHub Desktop.
I tried using ENV IV with M5UnitENV v1.0.0 library, however it did not read temperature and humidity properly in my environment, so I modified it to work.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/m5stack/M5Unit-ENV | |
#include "M5Atom.h" | |
#include "M5UnitENV.h" | |
SHT4X sht4; | |
BMP280 bmp; | |
// I2C Address for M5Atom Grove Connector | |
#define SDA_PIN 26 | |
#define SCL_PIN 32 | |
void setup() { | |
M5.begin(true, false,true); | |
delay(10); // delay10ms. 延迟10ms | |
if (!sht4.begin(&Wire, SHT40_I2C_ADDR_44, SDA_PIN, SCL_PIN, 400000U)) { | |
Serial.println("Couldn't find SHT4x"); | |
while (1) delay(1000); | |
} | |
// You can have 3 different precisions, higher precision takes longer | |
//sht4.setPrecision(SHT4X_HIGH_PRECISION); | |
//sht4.setHeater(SHT4X_NO_HEATER); | |
if (!bmp.begin(&Wire, BMP280_I2C_ADDR, SDA_PIN, SCL_PIN, 400000U)) { | |
Serial.println("Couldn't find BMP280"); | |
while (1) delay(1); | |
} | |
// Default settings from datasheet. | |
bmp.setSampling(BMP280::MODE_NORMAL, // * Operating Mode. | |
BMP280::SAMPLING_X2, // * Temp. oversampling | |
BMP280::SAMPLING_X16, // * Pressure oversampling | |
BMP280::FILTER_X16, // * Filtering. | |
BMP280::STANDBY_MS_500); // * Standby time. | |
} | |
float cTemp; | |
float humidity; | |
void readSHT4(){ | |
uint8_t readbuffer[6]; | |
Wire.beginTransmission(0x44); // SHT40_I2C_ADDR_44 | |
Wire.write(0xFD); // SHT4x_NOHEAT_HIGHPRECISION | |
Wire.endTransmission(true); | |
delay(10); // duration for SHT4x_NOHEAT_HIGHPRECISION | |
Wire.requestFrom(0x44, 6); | |
for (int i = 0; i < 6; i++) { | |
readbuffer[i] = Wire.read(); | |
} | |
if (readbuffer[2] != crc8(readbuffer, 2) || readbuffer[5] != crc8(readbuffer + 3, 2)) | |
{ | |
Serial.println("* CRC error *"); | |
} | |
float t_ticks = (uint16_t)readbuffer[0] * 256 + (uint16_t)readbuffer[1]; | |
float rh_ticks = (uint16_t)readbuffer[3] * 256 + (uint16_t)readbuffer[4]; | |
cTemp = -45 + 175 * t_ticks / 65535; | |
humidity = -6 + 125 * rh_ticks / 65535; | |
humidity = min(max(humidity, (float)0.0), (float)100.0); | |
} | |
void loop() { | |
readSHT4(); | |
Serial.println("-----SHT4X-----"); | |
Serial.print("Temperature: "); | |
Serial.print(cTemp); | |
Serial.println(" degrees C"); | |
Serial.print("Humidity: "); | |
Serial.print(humidity); | |
Serial.println("% rH"); | |
Serial.println("-------------\r\n"); | |
if (bmp.update()) { | |
Serial.println("-----BMP280-----"); | |
Serial.print(F("Temperature: ")); | |
Serial.print(bmp.cTemp); | |
Serial.println(" degrees C"); | |
Serial.print(F("Pressure: ")); | |
Serial.print(bmp.pressure); | |
Serial.println(" Pa"); | |
Serial.print(F("Approx altitude: ")); | |
Serial.print(bmp.altitude); | |
Serial.println(" m"); | |
Serial.println("-------------\r\n"); | |
} | |
delay(1000); | |
delay(50); | |
M5.update(); // Read the press state of the key. 读取按键按下状态 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment