-
Notifications
You must be signed in to change notification settings - Fork 0
/
GY-21P.ino
206 lines (164 loc) · 5.2 KB
/
GY-21P.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
on Sep 21, 2020
by MohammedDamirchi
Home
*/
#define SDA 23
#define SCL 22
#include <Wire.h>
//TwoWire I2C_0 = TwoWire(0);*/
#include "HomeSpan.h"
#include "THP.h"
DigooData gy21p;
unsigned long TempTimeout = 30000; // чтение термодатчков
unsigned long TempTime = 0; //
#include <Adafruit_Sensor.h>
#include "Adafruit_Si7021.h"
#include <Adafruit_BMP280.h>
Adafruit_BMP280 bme; // I2C
Adafruit_Si7021 sensor = Adafruit_Si7021();
void HomeKit(){
homeSpan.setApSSID("Sensor-AP");
homeSpan.setApPassword("");
homeSpan.setControlPin(0);
homeSpan.setStatusPin(2);
homeSpan.setLogLevel(1);
homeSpan.setSketchVersion("2.1.1");
homeSpan.enableOTA();
homeSpan.begin(Category::Sensors,"HTP");
new SpanAccessory();
new Service::AccessoryInformation();
new Characteristic::Name("Sensor");
new Characteristic::Manufacturer("Danil");
new Characteristic::SerialNumber("0000001");
new Characteristic::Model("beta");
new Characteristic::FirmwareRevision("0.1");
new Characteristic::Identify();
new Service::HAPProtocolInformation();
new Characteristic::Version("1.1.0");
new THP(&gy21p);
}
void poll_gy21p(){
float bmeTemp = bme.readTemperature();
float bmePress = bme.readPressure()*0.00750062;
float SiHum = sensor.readHumidity();
float SiTemp = sensor.readTemperature();
Serial.print("Temperature(bme): ");
Serial.print(bmeTemp);
Serial.print(" °C\t");
Serial.print("Pressure: ");
Serial.print(bmePress);
Serial.print(" mmHg\t");
Serial.print("Humidity: ");
Serial.print(SiHum, 2);
Serial.print("\tTemperature(Si7021): ");
Serial.println(SiTemp, 2);
if (bmePress != NAN){
gy21p.pressure = bmePress;
}
if (!isnan(bmeTemp) && !isnan(SiTemp)){
gy21p.temperature = (bmeTemp + SiTemp)/2;
gy21p.updated = millis();
gy21p.isNew[0] = true;
}
if (!isnan(bmeTemp) && isnan(SiTemp)){
gy21p.temperature = bmeTemp;
gy21p.updated = millis();
gy21p.isNew[0] = true;
}
if (isnan(bmeTemp) && !isnan(SiTemp)){
gy21p.temperature = SiTemp;
gy21p.updated = millis();
gy21p.isNew[0] = true;
}
if (!isnan(SiHum)){
gy21p.humidity = SiHum;
gy21p.updated = millis();
gy21p.isNew[1] = true;
}
}
void setup() {
Serial.begin(115200);
//I2C_0.begin(SDA , SCL , uint32_t (700000) );
Wire.begin(SDA, SCL, uint32_t (400000));
bme.begin(BMP280_ADDRESS_ALT);
sensor.begin();
poll_gy21p();
HomeKit();
}
void loop() {
if ( (millis() - TempTime) > TempTimeout ){
poll_gy21p();
TempTime = millis();
}
homeSpan.poll();
}
/*
#include <Wire.h>
void setup(){
Wire.begin();
Serial.begin(115200);
while (!Serial);
Serial.println("\nI2C Scanner");
}
void loop(){
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 8; address < 127; address++ ){
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0){
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4) {
Serial.print("Unknow error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000);
}
*/
/*
CustomCharacteristic.AtmosphericPressureLevel = function() {
Characteristic.call(this, 'Air Pressure', CustomCharacteristic.AtmosphericPressureLevel.UUID);
this.setProps({
format: Characteristic.Formats.UINT8,
unit: "mbar",
minValue: 800,
maxValue: 1200,
minStep: 1,
perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
});
this.value = this.getDefaultValue();
};
CustomCharacteristic.AtmosphericPressureLevel.UUID = 'E863F10F-079E-48FF-8F27-9C2605A29F52';
inherits(CustomCharacteristic.AtmosphericPressureLevel, Characteristic);
// courtesy of https://github.com/robi-van-kinobi/homebridge-cubesensors
CustomCharacteristic.AtmosphericPressureSensor = function(displayName, subtype) {
Service.call(this, displayName, CustomCharacteristic.AtmosphericPressureSensor.UUID, subtype);
// Required Characteristics
this.addCharacteristic(CustomCharacteristic.AtmosphericPressureLevel);
// Optional Characteristics
this.addOptionalCharacteristic(Characteristic.StatusActive);
this.addOptionalCharacteristic(Characteristic.StatusFault);
this.addOptionalCharacteristic(Characteristic.StatusLowBattery);
this.addOptionalCharacteristic(Characteristic.StatusTampered);
this.addOptionalCharacteristic(Characteristic.Name);
};
CustomCharacteristic.AtmosphericPressureSensor.UUID = 'B77831FD-D66A-46A4-B66D-FD7EE8DFE3CE';
inherits(CustomCharacteristic.AtmosphericPressureSensor, Service);
*/
//#define CUSTOM_CHAR(NAME,UUID,PERMISISONS,FORMAT,TYPE,DEFVAL,MINVAL,MAXVAL,STATIC_RANGE)