Skip to content

Instantly share code, notes, and snippets.

@sivar2311
Last active March 22, 2020 05:45
Show Gist options
  • Save sivar2311/dea45f6c2bc281960398eaa3bd5b9ced to your computer and use it in GitHub Desktop.
Save sivar2311/dea45f6c2bc281960398eaa3bd5b9ced to your computer and use it in GitHub Desktop.
Kreutzschaltung mit 3 Schaltern und 1 Lampe
/*
* If you encounter any issues:
* - check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md
* - ensure all dependent libraries are installed
* - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide
* - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies
* - open serial monitor and check whats happening
* - check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk
* - visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one
*/
// 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>
#include <ESP8266WiFi.h>
#include "SinricPro.h"
#include "SinricProLight.h"
#define WIFI_SSID "YOUR-WIFI-SSID"
#define WIFI_PASS "YOUR-WIFI-PASSWORD"
#define APP_KEY "YOUR-APP-KEY" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET "YOUR-APP-SECRET" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
#define LIGHT_ID "YOUR-DEVICE-ID" // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define BAUD_RATE 9600 // Change baudrate to your need
#define POWER_STATE_PIN D3 // PIN to get the current powerState (LOW = OFF, HIGH = ON);
#define POWER_CHANGE_PIN D4 // PIN to change the powerState; To change the power state this pin must be inverted
#define DEBOUNCE_TIME 100 // Entprell-Zeit in ms
/**
* Logic Table
*
* D3 | D4 | state | turn on | turn off
* -------+--------+--------------+-------------+-------------
* LOW | LOW | Power is OFF | set D4 HIGH | ---
* LOW | HIGH | Power is OFF | set D4 LOW | ---
* HIGH | LOW | Power is ON | --- | set D4 HIGH
* HIGH | HIGH | Power is ON | --- | set D4 LOW
*/
bool lastPowerState; // stores the last known state to keep track of changes
unsigned long lastPowerStateChange = 0;
bool onPowerState(const String &deviceId, bool &state) {
bool currentState = digitalRead(POWER_STATE_PIN); // get the current state
if (currentState != state) { // if the currentState is different to desired state
digitalWrite(POWER_CHANGE_PIN, !digitalRead(POWER_CHANGE_PIN)); // flip POWER_CHANGE_PIN
}
lastPowerState = state; // update lastPowerState
Serial.printf("Device turned %s\r\n", state?"ON":"OFF");
return true; // request handled properly
}
void setupWiFi() {
Serial.printf("\r\n[Wifi]: Connecting");
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 %s\r\n", localIP.toString().c_str());
}
void setupSinricPro() {
// get a new Light device from SinricPro
SinricProLight &myLight = SinricPro[LIGHT_ID];
myLight.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);
SinricPro.begin(APP_KEY, APP_SECRET);
}
void setupPINs() {
pinMode(POWER_STATE_PIN, INPUT);
pinMode(POWER_CHANGE_PIN, OUTPUT);
}
// main setup function
void setup() {
Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");
setupPINs();
setupWiFi();
setupSinricPro();
}
// function to handle manual state changes
void checkPowerState() {
unsigned long actualMillis = millis();
if (lastPowerStateChange - actualMillis < DEBOUNCE_TIME) return;
bool currentPowerState = digitalRead(POWER_STATE_PIN);
if (currentPowerState != lastPowerState) { // if state have changed
SinricProLight &myLight = SinricPro[LIGHT_ID];
myLight.sendPowerStateEvent(currentPowerState); // send event to SinricPro
lastPowerState = currentPowerState; // update lastPowerState
lastPowerStateChange = actualMillis;
}
}
void loop() {
SinricPro.handle();
checkPowerState();
}
/*
* If you encounter any issues:
* - check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md
* - ensure all dependent libraries are installed
* - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide
* - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies
* - open serial monitor and check whats happening
* - check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk
* - visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one
*/
// Uncomment the following line to enable serial debug output
//#define ENABLE_DEBUG
#include "TelnetDebug.h"
#ifdef ENABLE_DEBUG
#define DEBUG_ESP_PORT TelnetDebug
#define NODEBUG_WEBSOCKETS
#define NDEBUG
#endif
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "SinricPro.h"
#include "SinricProLight.h"
#define WIFI_SSID "YOUR-WIFI-SSID"
#define WIFI_PASS "YOUR-WIFI-PASSWORD"
#define APP_KEY "YOUR-APP-KEY" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET "YOUR-APP-SECRET" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
#define LIGHT_ID "YOUR-DEVICE-ID" // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define BAUD_RATE 9600 // Change baudrate to your need
#define HOSTNAME "lightswitch" // hostname
#define POWER_STATE_PIN D3 // PIN to get the current powerState (LOW = OFF, HIGH = ON);
#define POWER_CHANGE_PIN D4 // PIN to change the powerState; To change the power state this pin must be inverted
#define DEBOUNCE_TIME 100 // Debounce-Time in ms
/**
* Logic Table
*
* D3 | D4 | state | turn on | turn off
* -------+--------+--------------+-------------+-------------
* LOW | LOW | Power is OFF | set D4 HIGH | ---
* LOW | HIGH | Power is OFF | set D4 LOW | ---
* HIGH | LOW | Power is ON | --- | set D4 HIGH
* HIGH | HIGH | Power is ON | --- | set D4 LOW
*/
bool lastPowerState; // stores the last known state to keep track of changes
unsigned long lastPowerStateChange = 0;
bool onPowerState(const String &deviceId, bool &state) {
bool currentState = digitalRead(POWER_STATE_PIN); // get the current state
if (currentState != state) { // if the currentState is different to desired state
digitalWrite(POWER_CHANGE_PIN, !digitalRead(POWER_CHANGE_PIN)); // flip POWER_CHANGE_PIN
}
lastPowerState = state; // update lastPowerState
TelnetDebug.printf("Device turned %s\r\n", state?"ON":"OFF");
return true; // request handled properly
}
void setupWiFi() {
Serial.printf("\r\n[Wifi]: Connecting");
WiFi.begin(WIFI_SSID, WIFI_PASS);
WiFi.hostname(HOSTNAME);
WiFi.begin();
while (WiFi.status() != WL_CONNECTED) {
Serial.printf(".");
delay(250);
}
TelnetDebug.begin();
IPAddress localIP = WiFi.localIP();
TelnetDebug.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", localIP.toString().c_str());
}
void setupSinricPro() {
// get a new Light device from SinricPro
SinricProLight &myLight = SinricPro[LIGHT_ID];
myLight.onPowerState(onPowerState);
// setup SinricPro
SinricPro.onConnected([](){ TelnetDebug.printf("Connected to SinricPro\r\n"); });
SinricPro.onDisconnected([](){ TelnetDebug.printf("Disconnected from SinricPro\r\n"); });
SinricPro.restoreDeviceStates(true);
SinricPro.begin(APP_KEY, APP_SECRET);
}
void setupPINs() {
pinMode(POWER_STATE_PIN, INPUT);
pinMode(POWER_CHANGE_PIN, OUTPUT);
}
// main setup function
void setup() {
Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");
setupPINs();
setupWiFi();
setupSinricPro();
}
// function to handle manual state changes
void checkPowerState() {
unsigned long actualMillis = millis();
if (lastPowerStateChange - actualMillis < DEBOUNCE_TIME) return;
bool currentPowerState = digitalRead(POWER_STATE_PIN);
if (currentPowerState != lastPowerState) { // if state have changed
SinricProLight &myLight = SinricPro[LIGHT_ID];
myLight.sendPowerStateEvent(currentPowerState); // send event to SinricPro
lastPowerState = currentPowerState; // update lastPowerState
lastPowerStateChange = actualMillis;
}
}
void loop() {
TelnetDebug.handle();
SinricPro.handle();
checkPowerState();
}
#ifndef _TELNETDEBUG_
#define _TELNETDEBUG_
#include <Arduino.h>
#include <Print.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#else
#error Only for ESP8266 or ESP32
#endif
#define DEFAULT_TELNET_PORT 23
#define BUFFER_LEN 128
#define debPrintf(...) if (TelnetDebug.isConnected()) TelnetDebug.printf(__VA_ARGS__); Serial.printf(__VA_ARGS__)
#define debPrint(...) if (TelnetDebug.isConnected()) TelnetDebug.print(__VA_ARGS__); Serial.print(__VA_ARGS__)
#define debPrintln(...) if (TelnetDebug.isConnected()) TelnetDebug.println(__VA_ARGS__); Serial.println(__VA_ARGS__)
class TelnetDebugClass : public Print {
public:
TelnetDebugClass();
~TelnetDebugClass() { if (_telnetServer) delete _telnetServer; }
void begin(int port = DEFAULT_TELNET_PORT, bool serialMirror = true);
bool isConnected();
void handle();
virtual size_t write(uint8_t);
private:
bool _isConnected = false;
bool _serialMirror = false;
WiFiServer* _telnetServer = nullptr;
WiFiClient _telnetClient;
};
TelnetDebugClass::TelnetDebugClass() {}
void TelnetDebugClass::begin(int port, bool serialMirror) {
_telnetServer = new WiFiServer(port);
_telnetServer->begin();
_telnetServer->setNoDelay(true);
_serialMirror = serialMirror;
}
void TelnetDebugClass::handle() {
// neuer Client?
if (_telnetServer->hasClient()) {
_telnetClient = _telnetServer->available();
if (!_telnetClient) return;
Serial.print("Neuer Client: ");
Serial.println(_telnetClient.remoteIP());
_telnetClient.setNoDelay(true);
_telnetClient.flush();
// puffer leeren
while (_telnetClient.available()) {
_telnetClient.read();
}
_telnetClient.printf("*** WELCOME TO TELNET_DEBUG ***\r\n");
}
_isConnected = (_telnetClient && _telnetClient.connected());
if (!_isConnected) return;
while (_telnetClient.available()) {
_telnetClient.read();
}
}
bool TelnetDebugClass::isConnected() {
return _isConnected;
}
size_t TelnetDebugClass::write(uint8_t c) {
static char buf[BUFFER_LEN];
static int pos;
if (pos < BUFFER_LEN && c!='\n') {
buf[pos++] = c;
} else {
buf[pos++] = c;
if (_serialMirror) Serial.write(buf, pos);
_telnetClient.write(buf, pos);
size_t written = pos;
pos = 0;
return written;
}
return 1;
}
TelnetDebugClass TelnetDebug = TelnetDebugClass();
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment