Last active
October 19, 2023 22:40
-
-
Save kakopappa/5e2c591be631436b8313bd777b5948cc to your computer and use it in GitHub Desktop.
This file contains 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
#include "max6675.h" // From https://github.com/adafruit/MAX6675-library | |
int CS0 = 11; // CS pin of MAX6675 | |
int SO = 12; // SO pin of MAX6675 | |
int SCK = 13; // SCK pin of MAX6675 | |
int units = 0; // Units to readout temp (0 = ÀöF, 1 = ÀöC) | |
float error = 0.0; // Temperature compensation error | |
MAX6675 temp0(CS0,SO,SCK,units,error); | |
#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) | |
#include <WiFi.h> | |
#endif | |
#include "SinricPro.h" | |
#include "SinricProTemperaturesensor.h" | |
#define WIFI_SSID "" // Your WiFI SSID name | |
#define WIFI_PASS "" // Your WiFi Password. | |
#define APP_KEY "" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx" (Get it from Portal -> Secrets) | |
#define APP_SECRET "" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx" (Get it from Portal -> Secrets) | |
#define TEMP_SENSOR_ID "" // Should look like "5dc1564130xxxxxxxxxxxxxx" (Get it from Portal -> Devices) | |
#define BAUD_RATE 115200 // Change baudrate to your need (used for serial monitor) | |
#define EVENT_WAIT_TIME 60000 // send event every 60 seconds | |
float temperature; // actual temperature | |
float humidity; // actual humidity | |
float lastTemperature; // last known temperature (for compare) | |
void handleTemperaturesensor() { | |
if (SinricPro.isConnected() == false) { | |
Serial.printf("Not connected to Sinric Pro...!\r\n"); | |
return; | |
} | |
static unsigned long last_millis; | |
unsigned long current_millis = millis(); | |
if (last_millis && current_millis - last_millis < EVENT_WAIT_TIME) return; | |
last_millis = current_millis; | |
temperature = temp0.read_temp(5); // Read the temp 5 times and return the average value to the var | |
Serial.printf("Temperature: %2.1f Celsius\r\n", temperature); | |
if (isnan(temperature)) { // reading failed... | |
Serial.printf("MAX6675 reading failed!\r\n"); // print error message | |
return; // try again next time | |
} | |
if (temperature == lastTemperature) { | |
Serial.printf("Temperature did not changed. do nothing...!\r\n"); | |
return; | |
} | |
SinricProTemperaturesensor &mySensor = SinricPro[TEMP_SENSOR_ID]; // get temperaturesensor device | |
bool success = mySensor.sendTemperatureEvent(temperature, -1); // send event | |
if (success) { | |
Serial.printf("Sent!\r\n"); | |
} else { | |
Serial.printf("Something went wrong...could not send Event to server!\r\n"); // Enable ENABLE_DEBUG to see why | |
} | |
if(temperature > 99) { | |
mySensor.sendPushNotification("Water is ready to make a cup of tea!"); | |
} | |
lastTemperature = temperature; // save actual temperature for next compare | |
} | |
// 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]); | |
} | |
bool onPowerState(const String &deviceId, bool &state) { | |
return true; // request handled properly | |
} | |
// setup function for SinricPro | |
void setupSinricPro() { | |
// add device to SinricPro | |
SinricProTemperaturesensor &mySensor = SinricPro[TEMP_SENSOR_ID]; | |
mySensor.onPowerState(onPowerState); | |
// 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"); | |
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