Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinyu-Zhao committed Jul 19, 2022
1 parent 14b5aec commit b1b5692
Show file tree
Hide file tree
Showing 7 changed files with 488 additions and 461 deletions.
4 changes: 2 additions & 2 deletions examples/Unit_ENVIII_M5StickC/Unit_ENVIII_M5StickC.ino
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
display them on the display screen
请连接端口,读取温度、湿度和大气压强并在显示屏上显示
*/
#include <Adafruit_BMP280.h>
// #include <Adafruit_BMP280.h>
#include <M5StickC.h>

#include "Adafruit_Sensor.h"
// #include "Adafruit_Sensor.h"
#include "M5_ENV.h"

SHT3X sht30;
Expand Down
97 changes: 48 additions & 49 deletions src/DHT12.cpp
Original file line number Diff line number Diff line change
@@ -1,60 +1,59 @@
/*
DHT12.cpp - Library for DHT12 sensor.
v0.0.1 Beta
Created by Bobadas, July 30,2016.
Released into the public domain.
DHT12.cpp - Library for DHT12 sensor.
v0.0.1 Beta
Created by Bobadas, July 30,2016.
Released into the public domain.
*/
#include "DHT12.h"

DHT12::DHT12(uint8_t scale,uint8_t id)
{
if (id==0 || id>126) _id=0x5c;
else _id=id;
if (scale==0 || scale>3) _scale=CELSIUS;
else _scale=scale;
DHT12::DHT12(uint8_t scale, uint8_t id) {
if (id == 0 || id > 126)
_id = 0x5c;
else
_id = id;
if (scale == 0 || scale > 3)
_scale = CELSIUS;
else
_scale = scale;
}

uint8_t DHT12::read()
{
Wire.beginTransmission(_id);
Wire.write(0);
if (Wire.endTransmission()!=0) return 1;
Wire.requestFrom(_id, (uint8_t)5);
for (int i=0;i<5;i++) {
datos[i]=Wire.read();
};
delay(50);
if (Wire.available()!=0) return 2;
if (datos[4]!=(datos[0]+datos[1]+datos[2]+datos[3])) return 3;
return 0;
uint8_t DHT12::read() {
Wire.beginTransmission(_id);
Wire.write(0);
if (Wire.endTransmission() != 0) return 1;
Wire.requestFrom(_id, (uint8_t)5);
for (int i = 0; i < 5; i++) {
datos[i] = Wire.read();
};
delay(50);
if (Wire.available() != 0) return 2;
if (datos[4] != (datos[0] + datos[1] + datos[2] + datos[3])) return 3;
return 0;
}

float DHT12::readTemperature(uint8_t scale)
{
float resultado=0;
uint8_t error=read();
if (error!=0) return (float)error/100;
if (scale==0) scale=_scale;
switch(scale) {
case CELSIUS:
resultado=(datos[2]+(float)datos[3]/10);
break;
case FAHRENHEIT:
resultado=((datos[2]+(float)datos[3]/10)*1.8+32);
break;
case KELVIN:
resultado=(datos[2]+(float)datos[3]/10)+273.15;
break;
};
return resultado;
float DHT12::readTemperature(uint8_t scale) {
float resultado = 0;
uint8_t error = read();
if (error != 0) return (float)error / 100;
if (scale == 0) scale = _scale;
switch (scale) {
case CELSIUS:
resultado = (datos[2] + (float)datos[3] / 10);
break;
case FAHRENHEIT:
resultado = ((datos[2] + (float)datos[3] / 10) * 1.8 + 32);
break;
case KELVIN:
resultado = (datos[2] + (float)datos[3] / 10) + 273.15;
break;
};
return resultado;
}

float DHT12::readHumidity()
{
float resultado;
uint8_t error=read();
if (error!=0) return (float)error/100;
resultado=(datos[0]+(float)datos[1]/10);
return resultado;
float DHT12::readHumidity() {
float resultado;
uint8_t error = read();
if (error != 0) return (float)error / 100;
resultado = (datos[0] + (float)datos[1] / 10);
return resultado;
}

40 changes: 20 additions & 20 deletions src/DHT12.h
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
/*
DHT12.h - Library for DHT12 sensor.
v0.0.1 Beta
Created by Bobadas, July 30,2016.
Released into the public domain.
DHT12.h - Library for DHT12 sensor.
v0.0.1 Beta
Created by Bobadas, July 30,2016.
Released into the public domain.
*/
#ifndef DHT12_h
#define DHT12_h
#ifndef DHT12_h
#define DHT12_h
#include "Arduino.h"
#include "Wire.h"

#define CELSIUS 1
#define KELVIN 2
#define FAHRENHEIT 3
#define CELSIUS 1
#define KELVIN 2
#define FAHRENHEIT 3

class DHT12
{
public:
DHT12(uint8_t scale=0,uint8_t id=0);
float readTemperature(uint8_t scale=0);
float readHumidity();
private:
uint8_t read();
uint8_t datos[5];
uint8_t _id;
uint8_t _scale;
class DHT12 {
public:
DHT12(uint8_t scale = 0, uint8_t id = 0);
float readTemperature(uint8_t scale = 0);
float readHumidity();

private:
uint8_t read();
uint8_t datos[5];
uint8_t _id;
uint8_t _scale;
};

#endif
Loading

0 comments on commit b1b5692

Please sign in to comment.