This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
temperaturesensor.ino
227 lines (184 loc) · 7.88 KB
/
temperaturesensor.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/****************************************************************************************************************************
temperaturesensor.ino
For ESP32/ESP8266 boards
Based on and modified from SinricPro libarary (https://github.com/sinricpro/)
to support other boards such as SAMD21, SAMD51, Adafruit's nRF52 boards, etc.
Built by Khoi Hoang https://github.com/khoih-prog/SinricPro_Generic
Licensed under MIT license
**********************************************************************************************************************************/
/*
Example for how to use SinricPro Temperaturesensor device:
- setup a temperature sensor device
- send temperature event to SinricPro server when temperature has changed
DHT Sensor is connected to D5 on ESP8266 devices / GPIO5 on ESP32 devices
DHT Library used in this example: https://github.com/markruys/arduino-DHT
If you encounter any issues:
- check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md
- ensure all dependent libraries are installed
- see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide
- see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies
- open serial monitor and check whats happening
- check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk
- visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one
*/
#if !(defined(ESP8266) || defined(ESP32))
#error This code is intended to run on the ESP32/ESP8266 boards ! Please check your Tools->Board setting.
#endif
// Uncomment the following line to enable serial debug output
//#define ENABLE_DEBUG
#ifdef ENABLE_DEBUG
#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG
#endif
#if (ESP8266)
#include <ESP8266WiFi.h>
#elif (ESP32)
#include <WiFi.h>
#endif
#include "SinricPro_Generic.h"
#include "SinricProTemperaturesensor.h"
#include "DHT.h" // https://github.com/adafruit/DHT-sensor-library
#define WIFI_SSID "YOUR-WIFI-SSID"
#define WIFI_PASS "YOUR-WIFI-PASSWORD"
#define APP_KEY "YOUR-APP-KEY" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET "YOUR-APP-SECRET" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
#define TEMP_SENSOR_ID "YOUR-DEVICE-ID" // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define BAUD_RATE 115200 // Change baudrate to your need (used for serial monitor)
#define EVENT_WAIT_TIME 60000 // send event every 60 seconds
// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic, connect pin 1 to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHT_PIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
#if (ESP8266)
#define DHT_PIN D5
#elif (ESP32)
#define DHT_PIN 5
#endif
// Uncomment whatever type you're using!
//#define DHT_TYPE DHT11 // DHT 11
#define DHT_TYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHT_TYPE DHT21 // DHT 21 (AM2301)
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHT_PIN, DHT_TYPE);
bool deviceIsOn; // Temeprature sensor on/off state
float temperature; // actual temperature
float humidity; // actual humidity
float lastTemperature; // last known temperature (for compare)
float lastHumidity; // last known humidity (for compare)
unsigned long lastEvent = (-EVENT_WAIT_TIME); // last time event has been sent
/* bool onPowerState(String deviceId, bool &state)
Callback for setPowerState request
parameters
String deviceId (r)
contains deviceId (useful if this callback used by multiple devices)
bool &state (r/w)
contains the requested state (true:on / false:off)
must return the new state
return
true if request should be marked as handled correctly / false if not
*/
bool onPowerState(const String &deviceId, bool &state)
{
(void) deviceId;
Serial.println("TemperatureSensor turned " + String(state ? "on" : "off"));
deviceIsOn = state; // turn on / off temperature sensor
return true; // request handled properly
}
/* handleTemperatatureSensor()
- Checks if Temperaturesensor is turned on
- Checks if time since last event > EVENT_WAIT_TIME to prevent sending too much events
- Get actual temperature and humidity and check if these values are valid
- Compares actual temperature and humidity to last known temperature and humidity
- Send event to SinricPro Server if temperature or humidity changed
*/
void handleTemperaturesensor()
{
// device is off...do nothing
if (deviceIsOn == false)
return;
unsigned long actualMillis = millis();
if (actualMillis - lastEvent < EVENT_WAIT_TIME)
return; //only check every EVENT_WAIT_TIME milliseconds
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
// Read temperature as Celsius (the default)
temperature = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
// temperature = dht.readTemperature(true);
humidity = dht.readHumidity(); // get actual humidity
if (isnan(temperature) || isnan(humidity))
{
// reading failed...
Serial.println("DHT reading failed"); // print error message
return; // try again next time
}
// Check if any reads failed and exit early (to try again).
if (temperature == lastTemperature || humidity == lastHumidity)
return;
SinricProTemperaturesensor &mySensor = SinricPro[TEMP_SENSOR_ID]; // get temperaturesensor device
bool success = mySensor.sendTemperatureEvent(temperature, humidity); // send event
if (success)
{
// if event was sent successfuly, print temperature and humidity to serial
Serial.println("Temperature: " + String(temperature, 1) + " Celsius\tHumidity: " + String(humidity, 1) + " %");
}
else
{
// if sending event failed, print error message
Serial.println("Something went wrong...Could not send Event to server!");
}
lastTemperature = temperature; // save actual temperature for next compare
lastHumidity = humidity; // save actual humidity for next compare
lastEvent = actualMillis; // save actual time for next compare
}
// setup function for WiFi connection
void setupWiFi()
{
Serial.print("\n[Wifi]: Connecting");
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(250);
}
Serial.print("\n[WiFi]: IP-Address is ");
Serial.println(WiFi.localIP());
}
// setup function for SinricPro
void setupSinricPro()
{
// add device to SinricPro
SinricProTemperaturesensor &mySensor = SinricPro[TEMP_SENSOR_ID];
mySensor.onPowerState(onPowerState);
// setup SinricPro
SinricPro.onConnected([]()
{
Serial.println("Connected to SinricPro");
});
SinricPro.onDisconnected([]()
{
Serial.println("Disconnected from SinricPro");
});
SinricPro.begin(APP_KEY, APP_SECRET);
}
// main setup function
void setup()
{
Serial.begin(BAUD_RATE);
while (!Serial);
Serial.println("\nStarting temperaturesensor on " + String(ARDUINO_BOARD));
Serial.println("Version : " + String(SINRICPRO_VERSION_STR));
dht.begin();
setupWiFi();
setupSinricPro();
}
void loop()
{
SinricPro.handle();
handleTemperaturesensor();
}