#define BLINKER_WIFI #define BLINKER_MIOT_LIGHT
#include <WiFi.h> #include <WiFiUdp.h> #include <Blinker.h> #include <Servo.h>
const char *auth = "***"; const char *ssid = "***"; const char *pswd = "***";
int port = 6022; byte preamble[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; byte macAddress[] = {0xB0, 0x25, 0xAA, 0x42, 0x8F, 0xCE}; IPAddress ip(192, 168, 1, 107);
WiFiUDP UDP;
BlinkerButton Button("TurnOnPC"); BlinkerButton PreLight("PreLight"); BlinkerButton SufLight("SufLight");
const int turnOnServoPin = 4; const int turnOffServoPin = 5; Servo turnOnServo, turnOffServo;
void UDPSendPacket(IPAddress ip, int port){ UDP.beginPacket(ip, port); UDP.write(preamble, sizeof(preamble)); for (int i = 0; i < 16; i++) UDP.write(macAddress, sizeof(macAddress)); UDP.endPacket(); }
void ButtonCallback(const String & state){ BLINKER_LOG("get button state: ", state); UDPSendPacket(ip, port); }
void spin(char ch, int op){ if (ch == 'p' || ch == 'P'){ if (op == 0){ turnOffServo.write(92); delay(500); turnOffServo.write(81); Serial.println("前灯关"); } else { turnOnServo.write(63); delay(500); turnOnServo.write(81); Serial.println("前灯开"); } } else if (ch == 's' || ch == 'S'){ if (op == 0){ turnOffServo.write(63); delay(500); turnOffServo.write(81); Serial.println("后灯关"); } else { turnOnServo.write(95); delay(500); turnOnServo.write(81); Serial.println("后灯开"); } } }
void PreLightCallback(const String & state){ BLINKER_LOG("get button state: ", state); if (state == BLINKER_CMD_ON){ spin('p', 1); PreLight.print("on"); BlinkerMIOT.powerState("on"); BlinkerMIOT.print(); } else if (state == BLINKER_CMD_OFF){ spin('p', 0); PreLight.print("off"); BlinkerMIOT.powerState("off"); BlinkerMIOT.print(); } }
void SufLightCallback(const String & state){ BLINKER_LOG("get button state: ", state); if (state == BLINKER_CMD_ON){ spin('s', 1); SufLight.print("on"); BlinkerMIOT.powerState("on"); BlinkerMIOT.print(); } else if (state == BLINKER_CMD_OFF){ spin('s', 0); SufLight.print("off"); BlinkerMIOT.powerState("off"); BlinkerMIOT.print(); } }
void miotMode(uint8_t mode){ BLINKER_LOG("need set mode: ", mode);
if (mode == BLINKER_CMD_MIOT_DAY){ UDPSendPacket(ip, port); } else if (mode == BLINKER_CMD_MIOT_NIGHT){ spin('p', 1); PreLight.print("on"); } else if (mode == BLINKER_CMD_MIOT_COLOR){ spin('p', 0); PreLight.print("off"); } else if (mode == BLINKER_CMD_MIOT_WARMTH){ spin('s', 1); SufLight.print("on"); } else if (mode == BLINKER_CMD_MIOT_TV){ spin('s', 0); SufLight.print("off"); } else if (mode == BLINKER_CMD_MIOT_READING){ spin('p', 1); spin('s', 1); PreLight.print("on"); SufLight.print("on"); } else if (mode == BLINKER_CMD_MIOT_COMPUTER){ spin('p', 0); spin('s', 0); PreLight.print("off"); SufLight.print("off"); } BlinkerMIOT.mode(mode); BlinkerMIOT.print(); } void setup() { Serial.begin(115200); BLINKER_DEBUG.stream(Serial);
Blinker.begin(auth, ssid, pswd);
UDP.begin(port);
turnOnServo.attach(turnOnServoPin, Servo::CHANNEL_NOT_ATTACHED, 45, 120); turnOffServo.attach(turnOffServoPin, Servo::CHANNEL_NOT_ATTACHED, 45, 120);
Button.attach(ButtonCallback); PreLight.attach(PreLightCallback); SufLight.attach(SufLightCallback); BlinkerMIOT.attachMode(miotMode); }
void loop() { Blinker.run(); }
|