Skip to content

Instantly share code, notes, and snippets.

@gn-spawn
Created June 28, 2021 13:38
Show Gist options
  • Save gn-spawn/b1dbef1d5c767a497822100712ebfbe4 to your computer and use it in GitHub Desktop.
Save gn-spawn/b1dbef1d5c767a497822100712ebfbe4 to your computer and use it in GitHub Desktop.
m5atom_smartlock
#include "M5Atom.h"
#include <ESP32Servo.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
const char* ssid = "xx";
const char* password = "xx";
WebServer server(80);
Servo servo;
// 状態保持
uint8_t FSM = 0;
uint8_t DisBuff[2 + 5 * 5 * 3];
// サーボのピン番号
const int SERVO_PIN = 25;
// サーバールーティング
void handleRoot() {
server.send(200, "text/plain", "hello from esp32!");
}
void handleOpen() {
server.send(200, "text/html", "<h1>open<h1>");
servo.write(180);
}
void handleClose() {
server.send(200, "text/html", "<h1>close<h1>");
servo.write(0);
}
void setBuff(uint8_t Rdata, uint8_t Gdata, uint8_t Bdata)
{
DisBuff[0] = 0x05;
DisBuff[1] = 0x05;
for (int i = 0; i < 25; i++)
{
DisBuff[2 + i * 3 + 0] = Rdata;
DisBuff[2 + i * 3 + 1] = Gdata;
DisBuff[2 + i * 3 + 2] = Bdata;
}
}
void setup()
{
M5.begin(true, false, true);
delay(10);
setBuff(0xff, 0x00, 0x00);
M5.dis.displaybuff(DisBuff);
// サーボ初期化
servo.attach(SERVO_PIN);
servo.write(90);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("m5atom")) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/open", handleOpen);
server.on("/close", handleClose);
server.on("/inline", []() {
server.send(200, "text/plain", "this works as well");
});
server.begin();
Serial.println("HTTP server started");
}
void loop()
{
server.handleClient();
if (M5.Btn.wasPressed())
{
switch (FSM)
{
case 0:
servo.write(0);
delay(500);
servo.write(90);
setBuff(0x40, 0x00, 0x00);
break;
case 1:
servo.write(180);
delay(500);
servo.write(90);
setBuff(0x00, 0x40, 0x00);
break;
default:
break;
}
M5.dis.displaybuff(DisBuff);
FSM++;
if (FSM >= 2)
{
FSM = 0;
}
}
delay(50);
M5.update();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment