forked from hallard/remora_soft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsensors.cpp
More file actions
135 lines (121 loc) · 3.6 KB
/
sensors.cpp
File metadata and controls
135 lines (121 loc) · 3.6 KB
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
// **********************************************************************************
// SHT31 sensor module management headers file for remora project
// **********************************************************************************
// Creative Commons Attrib Share-Alike License
// You are free to use/extend but please abide with the CC-BY-SA license:
// http://creativecommons.org/licenses/by-sa/4.0/
//
// Written by Mathieu DELBOS
//
// History : V1.00 2020-05-08 - First release
//
// All text above must be included in any redistribution.
//
// **********************************************************************************
#include "./sensors.h"
#include "./SHT3x.h"
#ifdef MOD_SENSORS
unsigned long sensors_last_meas=0;// second since last sensor reading
SHT3x sht30(SHT30_ADDRESS);
SENData senData;
#endif
/* ======================================================================
Function: sensors_setup
Purpose : prepare and init stuff, configuration, ..
Input : -
Output : true if sensor module found, false otherwise
Comments: -
====================================================================== */
bool sensors_setup(void)
{
bool ret = false;
Debug("Initializing sensor SHT30...Searching...");
Debugflush();
#if defined (ESP8266)
// Sepecific ESP8266 to set I2C Speed
Wire.setClock(100000);
#endif
// Détection du SHT30
if (!i2c_detect(SHT30_ADDRESS))
{
Debugln("Not found!");
Debugflush();
return (false);
}
else
{
Debug("Setup...");
Debugflush();
// et l'initialiser
sht30.SetAddress(SHT30_ADDRESS);
sht30.SetMode(SHT3x::Single_HighRep_NoClockStretch);
sht30.Begin();
Debugln("OK!");
Debugflush();
sensors_First_reading(); //first reading initiate senData structure
}
#if defined (ESP8266)
// Sepecific ESP8266 to set I2C Speed
Wire.setClock(400000);
#endif
// ou l'a trouvé
return (true);
}
/* ======================================================================
Function: sensors_read
Purpose :
Input : -
Output : e
Comments: -
====================================================================== */
void sensors_read(void)
{
Wire.setClock(100000);
sht30.UpdateData();
Wire.setClock(400000);
senData.ptemp=senData.temp;
senData.phum=senData.hum;
senData.ptime=senData.time;
senData.temp=sht30.GetTemperature();
senData.hum=sht30.GetRelHumidity();
//https://github.com/arduino-libraries/NTPClient
senData.time=timeClient.getEpochTime();
if ((senData.time/ 86400L)!=(senData.ptime/ 86400L)) { //changement de jour
senData.tmin=senData.temp;
senData.tmax=senData.temp;
senData.hmin=senData.hum;
senData.hmax=senData.hum;
} else {
if (senData.temp < senData.tmin)
senData.tmin=senData.temp;
if (senData.temp > senData.tmax)
senData.tmax=senData.temp;
if (senData.hum < senData.hmin)
senData.hmin=senData.hum;
if (senData.hum > senData.hmax)
senData.hmin=senData.hum;
}
Debugln("sensor read");
Debugflush();
}
/* ======================================================================
Function: sensors_First_reading
Purpose : prepare and init stuff, configuration, ..
Input : -
Output : true if sensor module found, false otherwise
Comments: -
====================================================================== */
void sensors_First_reading(void)
{
sht30.UpdateData();
senData.temp=sht30.GetTemperature();
senData.hum=sht30.GetRelHumidity();
senData.time=timeClient.getEpochTime();
senData.ptemp=senData.temp;
senData.phum=senData.hum;
senData.ptime=senData.time;
senData.tmin=senData.temp;
senData.tmax=senData.temp;
senData.hmin=senData.hum;
senData.hmax=senData.hum;
}