|
| 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 | +} |
0 commit comments