Skip to content

Instantly share code, notes, and snippets.

@ebith
Created Nov 30, 2020
Embed
What would you like to do?
ATOM Liteで作るPCの電源スイッチ
#include <Arduino.h>
#include <WiFi.h>
#include <FastLED.h>
#include <WebServer.h>
#include <ESPmDNS.h>
const int NUM_LEDS = 1;
const int LED_PIN = 27;
static CRGB leds[NUM_LEDS];
const int BUTTON_PIN = 39;
int BUTTON_STATE = 0;
const char* ssid = "**********";
const char* password = "**********";
const int POWER_PIN = 33;
WebServer server(80);
void power() {
digitalWrite(POWER_PIN, HIGH);
delay(500);
digitalWrite(POWER_PIN, LOW);
}
void setup() {
// LED
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(50);
leds[0] = CRGB::Black;
FastLED.show();
// Button
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Serial
Serial.begin(115200);
Serial.flush();
// POWER SW
pinMode(POWER_PIN, OUTPUT);
// Web Server
server.on("/", []() {
server.send(200);
});
server.on("/api/power", HTTP_POST, []() {
server.send(202);
power();
});
server.onNotFound([]() {
server.send(404);
});
Serial.println("");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
server.handleClient();
if (digitalRead(BUTTON_PIN) == LOW && BUTTON_STATE == 0) {
BUTTON_STATE = 1;
power();
} else if (digitalRead(BUTTON_PIN) == HIGH && BUTTON_STATE == 1) {
BUTTON_STATE = 0;
}
} else {
leds[0] = CRGB::Red;
FastLED.show();
WiFi.begin(ssid, password);
int wifiReconnectCount = 0;
while (WiFi.status() != WL_CONNECTED) {
if (wifiReconnectCount >= 300) { ESP.restart(); }
wifiReconnectCount++;
delay(100);
}
MDNS.begin("powerswitch");
server.begin();
leds[0] = CRGB::Green;
FastLED.show();
}
}
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:m5stick-c]
platform = espressif32
board = m5stick-c
framework = arduino
monitor_speed = 115200
board_build.f_cpu = 80000000L
lib_deps =
fastled/FastLED@^3.3.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment