Created
March 5, 2018 02:55
-
-
Save superfell/395de3b2ea328b1a4222ec949fae5a32 to your computer and use it in GitHub Desktop.
ESP2866 trigger LED Light Controller on button push, Stays in low power/deepSleep mode until button is pressed.
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 <ESP8266WiFi.h> | |
////////////////////// | |
// WiFi Definitions // | |
////////////////////// | |
const char WiFiName[] = "<network>"; | |
const char WiFiPwd[] = "<password>"; | |
/////////////////////////// | |
// LED Light Controller // | |
/////////////////////////// | |
const char LEDControllerName[] = "ESP_953C75.localdomain"; | |
const int LEDControllerPort = 5577; | |
// Control Sequences to send | |
const byte LEDTurnOn[] = { 113, 35, 15, 163 }; | |
const byte LEDTurnOff[] = { 113, 35, 15, 164 }; | |
const byte LEDSeq[] = { 81, 255, 0, 0, 0, 255, 255, 255, 0, 0, 0, 255, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 5, 59, 255, 15, 232 }; | |
///////////////////// | |
// Pin Definitions // | |
///////////////////// | |
const int LED_PIN = 5; // Thing's onboard, green LED | |
WiFiClient client; | |
void setup() | |
{ | |
initHardware(); | |
setupWiFi(); | |
turnOnLEDs(); | |
} | |
void loop() { | |
delay(1000); | |
Serial.println("About to call deepSleep: "); | |
ESP.deepSleep(0, WAKE_RF_DEFAULT); // goto sleep until the RST/DTR pin is signals. | |
Serial.println("After deep sleep: "); | |
} | |
void turnOnLEDs() | |
{ | |
digitalWrite(LED_PIN, HIGH); | |
if (client.connect(LEDControllerName, LEDControllerPort)) { | |
Serial.println("Connected to LED Controller"); | |
int numBytes = client.write(LEDTurnOn, sizeof(LEDTurnOn)); | |
Serial.print("Sent power on: "); | |
Serial.print(numBytes); | |
Serial.println(" bytes sent"); | |
numBytes = client.write(LEDSeq, sizeof(LEDSeq)); | |
Serial.print("Sent light seq: "); | |
Serial.print(numBytes); | |
Serial.println(" bytes sent"); | |
client.stop(); | |
Serial.println("Disconnecting"); | |
} else { | |
Serial.println("Couldn't connect"); | |
digitalWrite(LED_PIN, HIGH); | |
delay(500); | |
digitalWrite(LED_PIN, LOW); | |
delay(500); | |
digitalWrite(LED_PIN, HIGH); | |
delay(500); | |
digitalWrite(LED_PIN, LOW); | |
} | |
digitalWrite(LED_PIN, LOW); | |
} | |
void setupWiFi() | |
{ | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(WiFiName, WiFiPwd); | |
Serial.println("Connecting"); | |
bool led = HIGH; | |
int count = 0; | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
digitalWrite(LED_PIN, led); | |
delay(120); | |
Serial.print("."); | |
led = !led; | |
count++; | |
if (count % 10 == 0) { | |
Serial.printf("wifi status: %d\n", WiFi.status()); | |
} | |
if (count % 100 == 0) { | |
WiFi.printDiag(Serial); | |
} | |
} | |
Serial.println(); | |
Serial.print("Connected, IP address: "); | |
Serial.println(WiFi.localIP()); | |
digitalWrite(LED_PIN, LOW); | |
} | |
void initHardware() | |
{ | |
Serial.begin(115200); | |
pinMode(LED_PIN, OUTPUT); | |
digitalWrite(LED_PIN, HIGH); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment