Skip to content

Instantly share code, notes, and snippets.

@PinkPandaKatie
Created February 1, 2021 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PinkPandaKatie/21bafce98903c11b7f204775d7f259f3 to your computer and use it in GitHub Desktop.
Save PinkPandaKatie/21bafce98903c11b7f204775d7f259f3 to your computer and use it in GitHub Desktop.
ESP8266 Wifi-controlled outlet
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
static const char* WIFINET = "notmyactualssidlol";
static const char* WIFIPASS = "ormyrealpassword";
static const uint8_t LED = D0;
static const uint8_t RELAY1 = D7;
static const uint8_t RELAY2 = D6;
uint8_t curbits = 0;
WiFiUDP udp;
void setpins() {
digitalWrite(RELAY1, (curbits & 0x01) ? LOW : HIGH);
digitalWrite(RELAY2, (curbits & 0x02) ? LOW : HIGH);
digitalWrite(LED, (curbits & 0x80) ? LOW : HIGH);
}
void setup() {
setpins();
pinMode(LED, OUTPUT);
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
WiFi.begin(WIFINET, WIFIPASS);
udp.begin(5747);
}
void loop() {
int size;
if ((size = udp.parsePacket()) != 0) {
IPAddress raddr = udp.remoteIP();
uint16_t rport = udp.remotePort();
if (size == 2) {
uint8_t clearbits = udp.read();
uint8_t togglebits = udp.read();
curbits = (curbits & ~clearbits) ^ togglebits;
udp.beginPacket(raddr, rport);
udp.write(curbits);
udp.endPacket();
}
} else {
// Reduce power by halting until interrupt is received
asm("waiti 0");
}
uint32_t ctime = millis();
if (WiFi.isConnected()) {
setpins();
} else {
digitalWrite(LED, ((millis() % 500) > 250) ? HIGH : LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment