-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Chris McKeever
committed
Jul 18, 2020
1 parent
8d7eb77
commit b41c637
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#define DEVICENAME "YOUR-DEVICE-NAME" | ||
|
||
#define LED 1 | ||
#define RELAY 0 | ||
|
||
// set to false to have relay stay on | ||
const bool INCHING = true; | ||
const unsigned long INCHINGMS = 500; | ||
|
||
const String SSID = "SSID"; | ||
const String PASSWORD = "PASSWORD"; | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
#include "constants.h" | ||
#include <ESP8266WiFi.h> | ||
#include <Espalexa.h> | ||
#include <arduino-timer.h> | ||
|
||
String deviceName = DEVICENAME; | ||
byte mac[6]; | ||
|
||
Espalexa espalexa; | ||
void deviceHandler(uint8_t brightness); | ||
|
||
Timer<1, millis, void *> timer; | ||
|
||
void setup(void){ | ||
pinMode(LED, OUTPUT); | ||
pinMode(RELAY, OUTPUT); | ||
|
||
led(LOW); | ||
|
||
relay(LOW); | ||
delay(100); | ||
relay(LOW); | ||
|
||
delay(1000); | ||
|
||
if (connectWifi()) { | ||
espDevice(); | ||
} | ||
|
||
} | ||
|
||
boolean connectWifi() { | ||
log(""); | ||
log("Serching for " + SSID); | ||
|
||
WiFi.mode(WIFI_STA); | ||
WiFi.begin(SSID, PASSWORD); | ||
|
||
WiFi.macAddress(mac); | ||
String hostname = "fesp-" + deviceName + String(mac[3], HEX) + String(mac[4], HEX) + String(mac[5], HEX); | ||
WiFi.hostname(hostname); | ||
|
||
while (WiFi.status() != WL_CONNECTED) { | ||
flash(); | ||
delay(500); | ||
} | ||
|
||
led(HIGH); | ||
log(hostname + " Connected - IP Address: " + WiFi.localIP().toString()); | ||
|
||
return true; | ||
} | ||
|
||
void espDevice() { | ||
log("Adding Alexa device as " + deviceName); | ||
|
||
espalexa.addDevice(deviceName, deviceHandler); | ||
espalexa.begin(); | ||
|
||
log("ESP Device Setup Complete"); | ||
} | ||
|
||
void deviceHandler(uint8_t brightness) { | ||
if (brightness > 0) { | ||
relayOn(brightness); | ||
|
||
if (INCHING) { | ||
timer.in(INCHINGMS, timerCallback); | ||
} | ||
}else { | ||
relayOff(); | ||
} | ||
} | ||
|
||
bool timerCallback(void *) { | ||
relayOff(); | ||
log("Timer Called"); | ||
return false; | ||
} | ||
|
||
void relay(int state) { | ||
pinMode(RELAY, OUTPUT); | ||
digitalWrite(RELAY, state); | ||
} | ||
|
||
void relayOn(int level) { | ||
relay(HIGH); | ||
espalexa.getDevice(0)->setValue(level); | ||
led(LOW); | ||
} | ||
|
||
void relayOff() { | ||
relay(LOW); | ||
espalexa.getDevice(0)->setValue(0); | ||
led(HIGH); | ||
} | ||
|
||
void flash() { | ||
int state = digitalRead(LED) == HIGH ? LOW : HIGH; | ||
led(state); | ||
} | ||
|
||
void led(int state) { | ||
pinMode(LED, OUTPUT); | ||
digitalWrite(LED, state); | ||
} | ||
|
||
void log(String msg) { | ||
Serial.begin(115200); | ||
Serial.println(msg); | ||
Serial.flush(); | ||
Serial.end(); | ||
} | ||
|
||
void loop(void) { | ||
espalexa.loop(); | ||
timer.tick(); | ||
} |
Binary file not shown.