Skip to content

Commit bd52617

Browse files
authored
Merge pull request #9 from sinricpro/dev-2.2.4
New Example TemperatureSensor
2 parents 4f74318 + a739c02 commit bd52617

File tree

16 files changed

+455
-14
lines changed

16 files changed

+455
-14
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
# SinricPro (ESP8266 / ESP32 SDK)
3-
## Version 2.2.3
3+
## Version 2.2.4
44
## Installation
55

66
### VS Code & PlatformIO:
@@ -682,7 +682,7 @@ Devices: | [TV](#tv) |
682682

683683
### onTargetTemperature
684684
```C++
685-
bool onTargetTemperatue(const String &deviceId, float &temperature)
685+
bool onTargetTemperature(const String &deviceId, float &temperature)
686686
```
687687
| parameter | type| input value | output value | example |
688688
|--|--|--|--|-- |

examples/GarageDoor/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# GarageDoorExample
2-
![Wiring](https://github.com/sivar2311/GarageDoorExample/raw/master/GarageDoor3_Steckplatine.png)
2+
![Wiring](https://github.com/sinricpro/esp8266-esp32-sdk/raw/master/examples/GarageDoor/GarageDoor3_Steckplatine.png)
33

44
D5: connected to relay
55
D6: connected to reed_switch (OPEN ENDSTOP)
66
D7: connected to reed_switch (CLOSED ENDSTOP)
7+
8+
All resistor values are 10kOhm
246 KB
Loading

examples/temperaturesensor/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Temperature Sensor Example
2+
![Wiring](https://github.com/sinricpro/esp8266-esp32-sdk/raw/master/examples/temperaturesensor/DHT22_Wiring.png)
3+
4+
D5: connected to DHT22 signal and via 10k resistor to +3.3V
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+
* DHT Sensor is connected to D5 on ESP8266 devices / GPIO5 on ESP32 devices
7+
*
8+
* DHT Library used in this example: https://github.com/markruys/arduino-DHT
9+
*
10+
* If you encounter any issues:
11+
* - open serial monitor and check whats happening
12+
* - visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one
13+
*/
14+
15+
// Uncomment the following line to enable serial debug output
16+
//#define ENABLE_DEBUG
17+
18+
#ifdef ENABLE_DEBUG
19+
#define DEBUG_ESP_PORT Serial
20+
#define NODEBUG_WEBSOCKETS
21+
#define NDEBUG
22+
#endif
23+
24+
25+
#include <Arduino.h>
26+
#ifdef ESP8266
27+
#include <ESP8266WiFi.h>
28+
#endif
29+
#ifdef ESP32
30+
#include <WiFi.h>
31+
#endif
32+
33+
#include "SinricPro.h"
34+
#include "SinricProTemperaturesensor.h"
35+
#include "DHT.h"
36+
37+
#define WIFI_SSID "YOUR-WIFI-SSID"
38+
#define WIFI_PASS "YOUR-WIFI-PASSWORD"
39+
#define APP_KEY "YOUR-APP-KEY" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
40+
#define APP_SECRET "YOUR-APP-SECRET" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
41+
#define TEMP_SENSOR_ID "YOUR-DEVICE-ID" // Should look like "5dc1564130xxxxxxxxxxxxxx"
42+
#define BAUD_RATE 9600 // Change baudrate to your need (used for serial monitor)
43+
#define EVENT_WAIT_TIME 60000 // send event every 60 seconds
44+
45+
#ifdef ESP8266
46+
#define DHT_PIN D5
47+
#endif
48+
#ifdef ESP32
49+
#define DHT_PIN 5
50+
#endif
51+
52+
DHT dht; // DHT sensor
53+
54+
bool deviceIsOn; // Temeprature sensor on/off state
55+
float temperature; // actual temperature
56+
float humidity; // actual humidity
57+
float lastTemperature; // last known temperature (for compare)
58+
float lastHumidity; // last known humidity (for compare)
59+
unsigned long lastEvent = (-EVENT_WAIT_TIME); // last time event has been sent
60+
61+
/* bool onPowerState(String deviceId, bool &state)
62+
*
63+
* Callback for setPowerState request
64+
* parameters
65+
* String deviceId (r)
66+
* contains deviceId (useful if this callback used by multiple devices)
67+
* bool &state (r/w)
68+
* contains the requested state (true:on / false:off)
69+
* must return the new state
70+
*
71+
* return
72+
* true if request should be marked as handled correctly / false if not
73+
*/
74+
bool onPowerState(const String &deviceId, bool &state) {
75+
Serial.printf("Temperaturesensor turned %s (via SinricPro) \r\n", state?"on":"off");
76+
deviceIsOn = state; // turn on / off temperature sensor
77+
return true; // request handled properly
78+
}
79+
80+
/* handleTemperatatureSensor()
81+
* - Checks if Temperaturesensor is turned on
82+
* - Checks if time since last event > EVENT_WAIT_TIME to prevent sending too much events
83+
* - Get actual temperature and humidity and check if these values are valid
84+
* - Compares actual temperature and humidity to last known temperature and humidity
85+
* - Send event to SinricPro Server if temperature or humidity changed
86+
*/
87+
void handleTemperaturesensor() {
88+
if (deviceIsOn == false) return; // device is off...do nothing
89+
90+
unsigned long actualMillis = millis();
91+
if (actualMillis - lastEvent < EVENT_WAIT_TIME) return; //only check every EVENT_WAIT_TIME milliseconds
92+
93+
temperature = dht.getTemperature(); // get actual temperature
94+
humidity = dht.getHumidity(); // get actual humidity
95+
96+
if (isnan(temperature) || isnan(humidity)) { // reading failed...
97+
Serial.printf("DHT reading failed!\r\n"); // print error message
98+
return; // try again next time
99+
}
100+
101+
if (temperature == lastTemperature || humidity == lastHumidity) return; // if no values changed do nothing...
102+
103+
SinricProTemperaturesensor &mySensor = SinricPro[TEMP_SENSOR_ID]; // get temperaturesensor device
104+
bool success = mySensor.sendTemperatureEvent(temperature, humidity); // send event
105+
if (success) { // if event was sent successfuly, print temperature and humidity to serial
106+
Serial.printf("Temperature: %2.1f Celsius\tHumidity: %2.1f%%\r\n", temperature, humidity);
107+
} else { // if sending event failed, print error message
108+
Serial.printf("Something went wrong...could not send Event to server!\r\n");
109+
}
110+
111+
lastTemperature = temperature; // save actual temperature for next compare
112+
lastHumidity = humidity; // save actual humidity for next compare
113+
lastEvent = actualMillis; // save actual time for next compare
114+
}
115+
116+
117+
// setup function for WiFi connection
118+
void setupWiFi() {
119+
Serial.printf("\r\n[Wifi]: Connecting");
120+
WiFi.begin(WIFI_SSID, WIFI_PASS);
121+
122+
while (WiFi.status() != WL_CONNECTED) {
123+
Serial.printf(".");
124+
delay(250);
125+
}
126+
IPAddress localIP = WiFi.localIP();
127+
Serial.printf("connected!\r\n[WiFi]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]);
128+
}
129+
130+
// setup function for SinricPro
131+
void setupSinricPro() {
132+
// add device to SinricPro
133+
SinricProTemperaturesensor &mySensor = SinricPro[TEMP_SENSOR_ID];
134+
mySensor.onPowerState(onPowerState);
135+
136+
// setup SinricPro
137+
SinricPro.begin(APP_KEY, APP_SECRET);
138+
SinricPro.restoreDeviceStates(true); // get latest known deviceState from server (is device turned on?)
139+
}
140+
141+
// main setup function
142+
void setup() {
143+
Serial.begin(BAUD_RATE);
144+
dht.setup(DHT_PIN);
145+
146+
setupWiFi();
147+
setupSinricPro();
148+
}
149+
150+
void loop() {
151+
SinricPro.handle();
152+
handleTemperaturesensor();
153+
}

library.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@
1313
"maintainer": true
1414
}
1515
],
16-
"version": "2.2.3",
16+
"version": "2.2.4",
1717
"frameworks": "arduino",
1818
"platforms": [
1919
"espressif8266",
2020
"espressif32"
2121
],
2222
"examples":[
2323
"pio-examples/switch/src/switch.cpp",
24-
"pio-examples/doorbell/src/doorbell.cpp"
24+
"pio-examples/doorbell/src/doorbell.cpp",
25+
"pio-examples/temperaturesensor/src/temperaturesensor.cpp"
2526
],
2627
"license": "CC-BY-SA-4.0"
2728
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SinricPro
2-
version=2.2.3
2+
version=2.2.4
33
author=Boris Jaeger <[email protected]>
44
maintainer=Boris Jaeger <[email protected]>
55
sentence=Library for https://sinric.pro - simple way to connect your device to alexa
Loading
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Temperature Sensor Example
2+
![Wiring](https://github.com/sinricpro/esp8266-esp32-sdk/raw/master/examples/temperaturesensor/DHT22_Wiring.png)
3+
4+
D5: connected to DHT22 signal and via 10k resistor to +3.3V
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
;PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; https://docs.platformio.org/page/projectconf.html
10+
11+
[platformio]
12+
; ============================================================
13+
; chose environment:
14+
; ESP8266
15+
; ESP32
16+
; ============================================================
17+
default_envs = ESP8266
18+
;default_envs = ESP32
19+
20+
[env]
21+
; ============================================================
22+
; Serial configuration
23+
; choose upload speed, serial-monitor speed
24+
; ============================================================
25+
upload_speed = 921600
26+
;upload_port = COM11
27+
monitor_speed = 115200
28+
;monitor_port = COM11
29+
30+
lib_deps =
31+
ArduinoJson@~6.11
32+
WebSockets
33+
34+
build_flags =
35+
; set your debug output (default=Serial)
36+
-D DEBUG_ESP_PORT=Serial
37+
; comment the folowing line to enable Websockets debugging
38+
-D NODEBUG_WEBSOCKETS
39+
; comment the folowing line to enable WiFi debugging
40+
-D NDEBUG
41+
; comment the following line to enable SinricPro debugging
42+
-D NODEBUG_SINRIC
43+
44+
45+
[env:ESP8266]
46+
platform = espressif8266
47+
framework = arduino
48+
; ============================================================
49+
; Board configuration
50+
; choose your board by uncommenting one of the following lines
51+
; ============================================================
52+
;board = gen4iod
53+
;board = huzzah
54+
;board = oak
55+
;board = esp_wroom_02
56+
;board = espduino
57+
;board = espectro
58+
;board = espino
59+
;board = espresso_lite_v1
60+
;board = espresso_lite_v2
61+
;board = esp12e
62+
;board = esp01_1m
63+
;board = esp01
64+
;board = esp07
65+
;board = esp8285
66+
;board = heltec_wifi_kit_8
67+
;board = inventone
68+
;board = nodemcu
69+
board = nodemcuv2
70+
;board = modwifi
71+
;board = phoenix_v1
72+
;board = phoenix_v2
73+
;board = sparkfunBlynk
74+
;board = thing
75+
;board = thingdev
76+
;board = esp210
77+
;board = espinotee
78+
;board = d1
79+
;board = d1_mini
80+
;board = d1_mini_lite
81+
;board = d1_mini_pro
82+
;board = wifi_slot
83+
;board = wifiduino
84+
;board = wifinfo
85+
;board = wio_link
86+
;board = wio_node
87+
;board = xinabox_cw01
88+
;board = esp32doit-devkit-v1
89+
90+
[env:ESP32]
91+
platform = espressif32
92+
framework = arduino
93+
; ============================================================
94+
; Board configuration
95+
; choose your board by uncommenting one of the following lines
96+
; ============================================================
97+
;board = esp32cam
98+
;board = alksesp32
99+
;board = featheresp32
100+
;board = espea32
101+
;board = bpi-bit
102+
;board = d-duino-32
103+
board = esp32doit-devkit-v1
104+
;board = pocket_32
105+
;board = fm-devkit
106+
;board = pico32
107+
;board = esp32-evb
108+
;board = esp32-gateway
109+
;board = esp32-pro
110+
;board = esp32-poe
111+
;board = oroca_edubot
112+
;board = onehorse32dev
113+
;board = lopy
114+
;board = lopy4
115+
;board = wesp32
116+
;board = esp32thing
117+
;board = sparkfun_lora_gateway_1-channel
118+
;board = ttgo-lora32-v1
119+
;board = ttgo-t-beam
120+
;board = turta_iot_node
121+
;board = lolin_d32
122+
;board = lolin_d32_pro
123+
;board = lolin32
124+
;board = wemosbat
125+
;board = widora-air
126+
;board = xinabox_cw02
127+
;board = iotbusio
128+
;board = iotbusproteus
129+
;board = nina_w10

0 commit comments

Comments
 (0)