Created
March 4, 2018 19:46
-
-
Save superfell/2e68f104c78843672469ad53fe3520b5 to your computer and use it in GitHub Desktop.
ESP2866 triggering a WiFi LED Controller
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[] = "<a network>"; | |
const char WiFiPwd[] = "<a 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 | |
const int DIGITAL_PIN = 12; // Digital pin to be read, the big switch is connected to this | |
WiFiClient client; | |
void setup() | |
{ | |
initHardware(); | |
setupWiFi(); | |
} | |
void loop() | |
{ | |
while (digitalRead(DIGITAL_PIN) == HIGH) { | |
yield(); | |
} | |
turnOnLEDs(); | |
while (digitalRead(DIGITAL_PIN) == LOW) { | |
yield(); | |
} | |
} | |
void turnOnLEDs() | |
{ | |
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"); | |
} | |
} | |
void setupWiFi() | |
{ | |
WiFi.begin(WiFiName, WiFiPwd); | |
Serial.println("Connecting"); | |
bool led = HIGH; | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
digitalWrite(LED_PIN, led); | |
delay(120); | |
Serial.print("."); | |
led = !led; | |
} | |
Serial.println(); | |
Serial.print("Connected, IP address: "); | |
Serial.println(WiFi.localIP()); | |
digitalWrite(LED_PIN, LOW); | |
} | |
void initHardware() | |
{ | |
Serial.begin(115200); | |
pinMode(DIGITAL_PIN, INPUT_PULLUP); | |
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