Skip to content

Commit af3e3b1

Browse files
committed
update weight and ENVII unit exmaple
1 parent f3f246a commit af3e3b1

File tree

9 files changed

+741
-169
lines changed

9 files changed

+741
-169
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
* Copyright (C) 2008 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software< /span>
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/* Update by K. Townsend (Adafruit Industries) for lighter typedefs, and
18+
* extended sensor support to include color, voltage and current */
19+
20+
#ifndef _ADAFRUIT_SENSOR_H
21+
#define _ADAFRUIT_SENSOR_H
22+
23+
#ifndef ARDUINO
24+
#include <stdint.h>
25+
#elif ARDUINO >= 100
26+
#include "Arduino.h"
27+
#include "Print.h"
28+
#else
29+
#include "WProgram.h"
30+
#endif
31+
32+
/* Intentionally modeled after sensors.h in the Android API:
33+
* https://github.com/android/platform_hardware_libhardware/blob/master/include/hardware/sensors.h */
34+
35+
/* Constants */
36+
#define SENSORS_GRAVITY_EARTH (9.80665F) /**< Earth's gravity in m/s^2 */
37+
#define SENSORS_GRAVITY_MOON (1.6F) /**< The moon's gravity in m/s^2 */
38+
#define SENSORS_GRAVITY_SUN (275.0F) /**< The sun's gravity in m/s^2 */
39+
#define SENSORS_GRAVITY_STANDARD (SENSORS_GRAVITY_EARTH)
40+
#define SENSORS_MAGFIELD_EARTH_MAX (60.0F) /**< Maximum magnetic field on Earth's surface */
41+
#define SENSORS_MAGFIELD_EARTH_MIN (30.0F) /**< Minimum magnetic field on Earth's surface */
42+
#define SENSORS_PRESSURE_SEALEVELHPA (1013.25F) /**< Average sea level pressure is 1013.25 hPa */
43+
#define SENSORS_DPS_TO_RADS (0.017453293F) /**< Degrees/s to rad/s multiplier */
44+
#define SENSORS_GAUSS_TO_MICROTESLA (100) /**< Gauss to micro-Tesla multiplier */
45+
46+
/** Sensor types */
47+
typedef enum
48+
{
49+
SENSOR_TYPE_ACCELEROMETER = (1), /**< Gravity + linear acceleration */
50+
SENSOR_TYPE_MAGNETIC_FIELD = (2),
51+
SENSOR_TYPE_ORIENTATION = (3),
52+
SENSOR_TYPE_GYROSCOPE = (4),
53+
SENSOR_TYPE_LIGHT = (5),
54+
SENSOR_TYPE_PRESSURE = (6),
55+
SENSOR_TYPE_PROXIMITY = (8),
56+
SENSOR_TYPE_GRAVITY = (9),
57+
SENSOR_TYPE_LINEAR_ACCELERATION = (10), /**< Acceleration not including gravity */
58+
SENSOR_TYPE_ROTATION_VECTOR = (11),
59+
SENSOR_TYPE_RELATIVE_HUMIDITY = (12),
60+
SENSOR_TYPE_AMBIENT_TEMPERATURE = (13),
61+
SENSOR_TYPE_VOLTAGE = (15),
62+
SENSOR_TYPE_CURRENT = (16),
63+
SENSOR_TYPE_COLOR = (17)
64+
} sensors_type_t;
65+
66+
/** struct sensors_vec_s is used to return a vector in a common format. */
67+
typedef struct {
68+
union {
69+
float v[3];
70+
struct {
71+
float x;
72+
float y;
73+
float z;
74+
};
75+
/* Orientation sensors */
76+
struct {
77+
float roll; /**< Rotation around the longitudinal axis (the plane body, 'X axis'). Roll is positive and increasing when moving downward. -90°<=roll<=90° */
78+
float pitch; /**< Rotation around the lateral axis (the wing span, 'Y axis'). Pitch is positive and increasing when moving upwards. -180°<=pitch<=180°) */
79+
float heading; /**< Angle between the longitudinal axis (the plane body) and magnetic north, measured clockwise when viewing from the top of the device. 0-359° */
80+
};
81+
};
82+
int8_t status;
83+
uint8_t reserved[3];
84+
} sensors_vec_t;
85+
86+
/** struct sensors_color_s is used to return color data in a common format. */
87+
typedef struct {
88+
union {
89+
float c[3];
90+
/* RGB color space */
91+
struct {
92+
float r; /**< Red component */
93+
float g; /**< Green component */
94+
float b; /**< Blue component */
95+
};
96+
};
97+
uint32_t rgba; /**< 24-bit RGBA value */
98+
} sensors_color_t;
99+
100+
/* Sensor event (36 bytes) */
101+
/** struct sensor_event_s is used to provide a single sensor event in a common format. */
102+
typedef struct
103+
{
104+
int32_t version; /**< must be sizeof(struct sensors_event_t) */
105+
int32_t sensor_id; /**< unique sensor identifier */
106+
int32_t type; /**< sensor type */
107+
int32_t reserved0; /**< reserved */
108+
int32_t timestamp; /**< time is in milliseconds */
109+
union
110+
{
111+
float data[4];
112+
sensors_vec_t acceleration; /**< acceleration values are in meter per second per second (m/s^2) */
113+
sensors_vec_t magnetic; /**< magnetic vector values are in micro-Tesla (uT) */
114+
sensors_vec_t orientation; /**< orientation values are in degrees */
115+
sensors_vec_t gyro; /**< gyroscope values are in rad/s */
116+
float temperature; /**< temperature is in degrees centigrade (Celsius) */
117+
float distance; /**< distance in centimeters */
118+
float light; /**< light in SI lux units */
119+
float pressure; /**< pressure in hectopascal (hPa) */
120+
float relative_humidity; /**< relative humidity in percent */
121+
float current; /**< current in milliamps (mA) */
122+
float voltage; /**< voltage in volts (V) */
123+
sensors_color_t color; /**< color in RGB component values */
124+
};
125+
} sensors_event_t;
126+
127+
/* Sensor details (40 bytes) */
128+
/** struct sensor_s is used to describe basic information about a specific sensor. */
129+
typedef struct
130+
{
131+
char name[12]; /**< sensor name */
132+
int32_t version; /**< version of the hardware + driver */
133+
int32_t sensor_id; /**< unique sensor identifier */
134+
int32_t type; /**< this sensor's type (ex. SENSOR_TYPE_LIGHT) */
135+
float max_value; /**< maximum value of this sensor's value in SI units */
136+
float min_value; /**< minimum value of this sensor's value in SI units */
137+
float resolution; /**< smallest difference between two values reported by this sensor */
138+
int32_t min_delay; /**< min delay in microseconds between events. zero = not a constant rate */
139+
} sensor_t;
140+
141+
class Adafruit_Sensor {
142+
public:
143+
// Constructor(s)
144+
Adafruit_Sensor() {}
145+
virtual ~Adafruit_Sensor() {}
146+
147+
// These must be defined by the subclass
148+
virtual void enableAutoRange(bool enabled) { (void)enabled; /* suppress unused warning */ };
149+
virtual bool getEvent(sensors_event_t*) = 0;
150+
virtual void getSensor(sensor_t*) = 0;
151+
152+
private:
153+
bool _autoRange;
154+
};
155+
156+
#endif
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Description: Use ENV II Unit to read temperature, humidity, atmospheric pressure, and display the data on the screen.
3+
Please install library before compiling:
4+
Adafruit BMP280: https://github.com/adafruit/Adafruit_BMP280_Library
5+
*/
6+
#include <M5Stack.h>
7+
#include <Wire.h>
8+
#include "Adafruit_Sensor.h"
9+
#include <Adafruit_BMP280.h>
10+
#include "SHT3X.h"
11+
SHT3X sht30;
12+
Adafruit_BMP280 bme;
13+
14+
float tmp = 0.0;
15+
float hum = 0.0;
16+
float pressure = 0.0;
17+
18+
void setup() {
19+
M5.begin();
20+
Wire.begin();
21+
M5.Lcd.setBrightness(10);
22+
M5.Lcd.setTextSize(3);
23+
Serial.println(F("ENV Unit(SHT30 and BMP280) test..."));
24+
25+
while (!bme.begin(0x76)){
26+
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
27+
M5.Lcd.println("Could not find a valid BMP280 sensor, check wiring!");
28+
}
29+
M5.Lcd.clear(BLACK);
30+
}
31+
32+
void loop() {
33+
pressure = bme.readPressure();
34+
if(sht30.get()==0){
35+
tmp = sht30.cTemp;
36+
hum = sht30.humidity;
37+
}
38+
Serial.printf("Temperatura: %2.2f*C Humedad: %0.2f%% Pressure: %0.2fPa\r\n", tmp, hum, pressure);
39+
M5.Lcd.setCursor(0, 0);
40+
M5.Lcd.setTextColor(WHITE, BLACK);
41+
42+
M5.Lcd.printf("Temp: %2.1f \r\nHumi: %2.0f%% \r\nPressure:%2.0fPa\r\n", tmp, hum, pressure);
43+
44+
delay(100);
45+
46+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "SHT3X.h"
2+
3+
/* Motor()
4+
5+
*/
6+
SHT3X::SHT3X(uint8_t address)
7+
{
8+
Wire.begin();
9+
_address=address;
10+
}
11+
12+
13+
14+
byte SHT3X::get()
15+
{
16+
unsigned int data[6];
17+
18+
// Start I2C Transmission
19+
Wire.beginTransmission(_address);
20+
// Send measurement command
21+
Wire.write(0x2C);
22+
Wire.write(0x06);
23+
// Stop I2C transmission
24+
if (Wire.endTransmission()!=0)
25+
return 1;
26+
27+
delay(500);
28+
29+
// Request 6 bytes of data
30+
Wire.requestFrom(_address, 6);
31+
32+
// Read 6 bytes of data
33+
// cTemp msb, cTemp lsb, cTemp crc, humidity msb, humidity lsb, humidity crc
34+
for (int i=0;i<6;i++) {
35+
data[i]=Wire.read();
36+
};
37+
38+
delay(50);
39+
40+
if (Wire.available()!=0)
41+
return 2;
42+
43+
// Convert the data
44+
cTemp = ((((data[0] * 256.0) + data[1]) * 175) / 65535.0) - 45;
45+
fTemp = (cTemp * 1.8) + 32;
46+
humidity = ((((data[3] * 256.0) + data[4]) * 100) / 65535.0);
47+
48+
return 0;
49+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef __SHT3X_H
2+
#define __HT3X_H
3+
4+
5+
#if ARDUINO >= 100
6+
#include "Arduino.h"
7+
#else
8+
#include "WProgram.h"
9+
#endif
10+
11+
#include "Wire.h"
12+
13+
class SHT3X{
14+
public:
15+
SHT3X(uint8_t address=0x44);
16+
byte get(void);
17+
float cTemp=0;
18+
float fTemp=0;
19+
float humidity=0;
20+
21+
private:
22+
uint8_t _address;
23+
24+
};
25+
26+
27+
#endif

0 commit comments

Comments
 (0)