Skip to content

Commit c935398

Browse files
authored
Create temperaturesensor_AHT10. (#200)
* Create temperaturesensor_AHT10. support for a new AHT10 sensor * AHT Sensor is connected to I2C on ESP8266 devices --> SCL D1 / SDA D2 * To begin, install the necessary library, specifically the Adafruit “AHTX0” package * (you might also get warnings for other libraries missing, likely “Adafruit IO“, and “Adafruit Unified Sensors“: install as needed.) * - see https://github.com/adafruit/Adafruit_AHTX0 * - see https://github.com/adafruit/Adafruit_Sensor * - see https://github.com/adafruit/Adafruit_IO_Arduino * Update temperaturesensor_AHT10.
1 parent 2e5d874 commit c935398

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* Example for how to use SinricPro Temperaturesensor device:
3+
* - setup a temperature sensor device
4+
* - send temperature event to SinricPro server when temperature has changed
5+
*
6+
* AHT Sensor is connected to I2C on ESP8266 devices --> SCL D1 / SDA D2
7+
* To begin, install the necessary library, specifically the Adafruit “AHTX0” package
8+
* (you might also get warnings for other libraries missing, likely “Adafruit IO“, and “Adafruit Unified Sensors“: install as needed.)
9+
* - see https://github.com/adafruit/Adafruit_AHTX0
10+
* - see https://github.com/adafruit/Adafruit_Sensor
11+
* - see https://github.com/adafruit/Adafruit_IO_Arduino
12+
13+
*
14+
* If you encounter any issues:
15+
* - check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md
16+
* - ensure all dependent libraries are installed
17+
* - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide
18+
* - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies
19+
* - open serial monitor and check whats happening
20+
* - check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk
21+
* - visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one
22+
*/
23+
24+
// Uncomment the following line to enable serial debug output
25+
//#define ENABLE_DEBUG
26+
27+
#ifdef ENABLE_DEBUG
28+
#define DEBUG_ESP_PORT Serial
29+
#define NODEBUG_WEBSOCKETS
30+
#define NDEBUG
31+
#endif
32+
33+
34+
#include <Arduino.h>
35+
#ifdef ESP8266
36+
#include <ESP8266WiFi.h>
37+
#endif
38+
#ifdef ESP32
39+
#include <WiFi.h>
40+
#endif
41+
42+
#include "SinricPro.h"
43+
#include "SinricProTemperaturesensor.h"
44+
#include <Adafruit_AHTX0.h>
45+
Adafruit_AHTX0 aht;
46+
47+
48+
#define WIFI_SSID ""
49+
#define WIFI_PASS ""
50+
#define APP_KEY "" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
51+
#define APP_SECRET "" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
52+
#define TEMP_SENSOR_ID "" // Should look like "5dc1564130xxxxxxxxxxxxxx"
53+
#define BAUD_RATE 9600 // Change baudrate to your need (used for serial monitor)
54+
#define EVENT_WAIT_TIME 60000 // send event every 60 seconds
55+
56+
57+
bool deviceIsOn; // Temeprature sensor on/off state
58+
float humidity; // actual humidity
59+
float temperature; // actual temperature
60+
float lastTemperature; // last known temperature (for compare)
61+
float lastHumidity; // last known humidity (for compare)
62+
unsigned long lastEvent = (-EVENT_WAIT_TIME); // last time event has been sent
63+
64+
/* bool onPowerState(String deviceId, bool &state)
65+
*
66+
* Callback for setPowerState request
67+
* parameters
68+
* String deviceId (r)
69+
* contains deviceId (useful if this callback used by multiple devices)
70+
* bool &state (r/w)
71+
* contains the requested state (true:on / false:off)
72+
* must return the new state
73+
*
74+
* return
75+
* true if request should be marked as handled correctly / false if not
76+
*/
77+
bool onPowerState(const String &deviceId, bool &state) {
78+
Serial.printf("Temperaturesensor turned %s (via SinricPro) \r\n", state?"on":"off");
79+
deviceIsOn = state; // turn on / off temperature sensor
80+
return true; // request handled properly
81+
}
82+
83+
/* handleTemperatatureSensor()
84+
* - Checks if Temperaturesensor is turned on
85+
* - Checks if time since last event > EVENT_WAIT_TIME to prevent sending too much events
86+
* - Get actual temperature and humidity and check if these values are valid
87+
* - Compares actual temperature and humidity to last known temperature and humidity
88+
* - Send event to SinricPro Server if temperature or humidity changed
89+
*/
90+
void handleTemperaturesensor() {
91+
if (deviceIsOn == false) return; // device is off...do nothing
92+
93+
unsigned long actualMillis = millis();
94+
if (actualMillis - lastEvent < EVENT_WAIT_TIME) return; //only check every EVENT_WAIT_TIME milliseconds
95+
if (!aht.begin()) {
96+
Serial.printf("Sensor not initialized\r\n");
97+
return;
98+
}
99+
sensors_event_t hum, temp;
100+
aht.getEvent(&hum, &temp);
101+
float humidity = hum.relative_humidity;
102+
float temperature = temp.temperature;
103+
104+
if (isnan(temperature) || isnan(humidity)) { // reading failed...
105+
Serial.printf("AHT reading failed!\r\n"); // print error message
106+
return; // try again next time
107+
}
108+
109+
if (temperature == lastTemperature || humidity == lastHumidity) return; // if no values changed do nothing...
110+
111+
SinricProTemperaturesensor &mySensor = SinricPro[TEMP_SENSOR_ID]; // get temperaturesensor device
112+
bool success = mySensor.sendTemperatureEvent(temperature, humidity); // send event
113+
if (success) { // if event was sent successfuly, print temperature and humidity to serial
114+
Serial.printf("Temperature: %2.1f Celsius\tHumidity: %2.1f%%\r\n", temperature, humidity);
115+
} else { // if sending event failed, print error message
116+
Serial.printf("Something went wrong...could not send Event to server!\r\n");
117+
}
118+
119+
lastTemperature = temperature; // save actual temperature for next compare
120+
lastHumidity = humidity; // save actual humidity for next compare
121+
lastEvent = actualMillis; // save actual time for next compare
122+
}
123+
124+
125+
// setup function for WiFi connection
126+
void setupWiFi() {
127+
Serial.printf("\r\n[Wifi]: Connecting");
128+
WiFi.begin(WIFI_SSID, WIFI_PASS);
129+
130+
while (WiFi.status() != WL_CONNECTED) {
131+
Serial.printf(".");
132+
delay(250);
133+
}
134+
IPAddress localIP = WiFi.localIP();
135+
Serial.printf("connected!\r\n[WiFi]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]);
136+
}
137+
138+
// setup function for SinricPro
139+
void setupSinricPro() {
140+
// add device to SinricPro
141+
SinricProTemperaturesensor &mySensor = SinricPro[TEMP_SENSOR_ID];
142+
mySensor.onPowerState(onPowerState);
143+
144+
// setup SinricPro
145+
SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); });
146+
SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); });
147+
SinricPro.begin(APP_KEY, APP_SECRET);
148+
SinricPro.restoreDeviceStates(true); // get latest known deviceState from server (is device turned on?)
149+
}
150+
151+
// main setup function
152+
void setup() {
153+
Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");
154+
aht.begin();
155+
setupWiFi();
156+
setupSinricPro();
157+
}
158+
159+
void loop() {
160+
SinricPro.handle();
161+
handleTemperaturesensor();
162+
}

0 commit comments

Comments
 (0)