Last active
February 16, 2025 06:03
-
-
Save kakopappa/702cfa1c79899b450d682d891a59202c to your computer and use it in GitHub Desktop.
SinricPro two temp sensors example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
#include <Arduino.h> | |
#if defined(ESP8266) | |
#include <ESP8266WiFi.h> | |
#elif defined(ESP32) || defined(ARDUINO_ARCH_RP2040) | |
#include <WiFi.h> | |
#endif | |
#include "SinricPro.h" | |
#include "SinricProTemperaturesensor.h" | |
#define WIFI_SSID "" | |
#define WIFI_PASS "" | |
#define APP_KEY "" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx" | |
#define APP_SECRET "" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx" | |
#define TEMP_SENSOR_ID_1 "" // Should look like "5dc1564130xxxxxxxxxxxxxx" | |
#define TEMP_SENSOR_ID_2 "" // Should look like "5dc1564130xxxxxxxxxxxxxx" | |
#define EVENT_WAIT_TIME 60000 // send event every 60 seconds | |
unsigned long lastEvent = (-EVENT_WAIT_TIME); // last time event has been sent | |
void updateSinricProServer(float humidity1, float temperature1, float humidity2, float temperature2) { | |
unsigned long actualMillis = millis(); | |
if (actualMillis - lastEvent < EVENT_WAIT_TIME) return; // send only 1 min | |
SinricProTemperaturesensor &mySensor1 = SinricPro[TEMP_SENSOR_ID_1]; | |
mySensor1.sendTemperatureEvent(temperature1, humidity1); | |
SinricProTemperaturesensor &mySensor2 = SinricPro[TEMP_SENSOR_ID_2]; | |
mySensor2.sendTemperatureEvent(temperature2, humidity2); | |
lastEvent = actualMillis; | |
Serial.printf("\r\n[updateSinricProServer]: Sent!"); | |
} | |
// setup function for WiFi connection | |
void setupWiFi() { | |
Serial.printf("\r\n[Wifi]: Connecting"); | |
#if defined(ESP8266) | |
WiFi.setSleepMode(WIFI_NONE_SLEEP); | |
WiFi.setAutoReconnect(true); | |
#elif defined(ESP32) | |
WiFi.setSleep(false); | |
WiFi.setAutoReconnect(true); | |
#endif | |
WiFi.begin(WIFI_SSID, WIFI_PASS); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.printf("."); | |
delay(250); | |
} | |
IPAddress localIP = WiFi.localIP(); | |
Serial.printf("connected!\r\n[WiFi]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]); | |
} | |
// setup function for SinricPro | |
void setupSinricPro() { | |
// add device to SinricPro | |
SinricProTemperaturesensor &mySensor1 = SinricPro[TEMP_SENSOR_ID_1]; | |
SinricProTemperaturesensor &mySensor2 = SinricPro[TEMP_SENSOR_ID_2]; | |
// setup SinricPro | |
SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); }); | |
SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); }); | |
//SinricPro.restoreDeviceStates(true); // Uncomment to restore the last known state from the server. | |
SinricPro.begin(APP_KEY, APP_SECRET); | |
} | |
// main setup function | |
void setup() { | |
Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n"); | |
aht.begin(); | |
setupWiFi(); | |
setupSinricPro(); | |
} | |
void loop() { | |
SinricPro.handle(); | |
handleTemperaturesensor(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dear Dr. kakopappa,
I used this code, and I found it interesting. I am not an expert in this field; I work on home automation for fun and out of personal interest. This code alone has limited functionality because it only shows the soil moisture level remotely. I found another code on this site for controlling a relay via Sinric Pro. Combining these two codes could make them more practical, which is exactly what I am looking for.
Thank you. [email protected]