Upload files to "/"
This commit is contained in:
commit
73c19b5360
|
@ -0,0 +1,120 @@
|
|||
#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 "SinricProLock.h"
|
||||
#include "SinricProSwitch.h"
|
||||
|
||||
#define WIFI_SSID "HUDA"
|
||||
#define WIFI_PASS "12345678"
|
||||
#define APP_KEY "7349f04f-c93a-43e8-882f-7613b2752065"
|
||||
#define APP_SECRET "907c6c3a-69fb-49c1-8090-7f554f79fb3c-71cc13cf-17da-4e96-a0c5-2dd1f7bd7670"
|
||||
#define LOCK_ID "66a04c715d818a66facf83ad" // Lock Device ID
|
||||
#define RELAY_ID "66a04c715d818a66facf83ad" // Relay Device ID (for example purposes)
|
||||
#define LOCK_PIN D2 // Pin untuk kunci
|
||||
#define RELAY_PIN D1 // Pin untuk relay
|
||||
#define BAUD_RATE 115200
|
||||
|
||||
// Relay configuration
|
||||
struct RelayInfo {
|
||||
String deviceId;
|
||||
String name;
|
||||
int pin;
|
||||
};
|
||||
|
||||
RelayInfo relays[] = {
|
||||
{RELAY_ID, "Relay 1", RELAY_PIN}
|
||||
};
|
||||
|
||||
const int numRelays = sizeof(relays) / sizeof(relays[0]);
|
||||
|
||||
// Lock state handler
|
||||
bool onLockState(const String &deviceId, bool &lockState) {
|
||||
Serial.printf("Device %s is %s\r\n", deviceId.c_str(), lockState ? "locked" : "unlocked");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Relay state handler
|
||||
bool onPowerState(const String &deviceId, bool &state) {
|
||||
for (int i = 0; i < numRelays; i++) {
|
||||
if (deviceId == relays[i].deviceId) {
|
||||
Serial.printf("Device %s turned %s\r\n", relays[i].name.c_str(), state ? "on" : "off");
|
||||
digitalWrite(relays[i].pin, state ? HIGH : LOW); // HIGH for ON, LOW for OFF
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void setupPins() {
|
||||
pinMode(LOCK_PIN, OUTPUT);
|
||||
digitalWrite(LOCK_PIN, HIGH); // Initialize lock to UNLOCK (assuming LOW is LOCK, adjust if needed)
|
||||
|
||||
for (int i = 0; i < numRelays; i++) {
|
||||
pinMode(relays[i].pin, OUTPUT);
|
||||
digitalWrite(relays[i].pin, HIGH); // Initialize relay to OFF (assuming LOW is ON, adjust if needed)
|
||||
}
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
|
||||
void setupSinricPro() {
|
||||
// Setup lock
|
||||
SinricProLock &myLock = SinricPro[LOCK_ID];
|
||||
myLock.onLockState(onLockState);
|
||||
|
||||
// Setup relay
|
||||
for (int i = 0; i < numRelays; i++) {
|
||||
SinricProSwitch &mySwitch = SinricPro[relays[i].deviceId];
|
||||
mySwitch.onPowerState(onPowerState);
|
||||
}
|
||||
|
||||
// Setup SinricPro
|
||||
SinricPro.onConnected([]() { Serial.printf("Connected to SinricPro\r\n"); });
|
||||
SinricPro.onDisconnected([]() { Serial.printf("Disconnected from SinricPro\r\n"); });
|
||||
SinricPro.begin(APP_KEY, APP_SECRET);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(BAUD_RATE);
|
||||
Serial.printf("\r\n\r\n");
|
||||
|
||||
setupPins();
|
||||
setupWiFi();
|
||||
setupSinricPro();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
SinricPro.handle();
|
||||
}
|
Loading…
Reference in New Issue