1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
| #include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer.h> #include <ESP8266mDNS.h>
const char *ssid = "WIFI-SSID"; const char *password = "WIFI-PSK";
ESP8266WebServer server ( 80 );
#define LED_PIN 16 #define POWER_PIN 4 #define RESET_PIN 5 #define BUZZER_PIN 15
#define LONG 5000 #define SHORT 1000
bool powerSwitch = false; bool resetSwitch = false;
void setup ( void ) { pinMode ( LED_PIN, OUTPUT ); pinMode(BUZZER_PIN, OUTPUT); pinMode(LED_PIN, OUTPUT);
digitalWrite ( LED_PIN, HIGH ); digitalWrite ( BUZZER_PIN, LOW );
Serial.begin ( 115200 ); WiFi.begin ( ssid, password ); Serial.println ( "" );
Serial.println ( "WiFi initializing" );
int connection_count = 0; // Wait for connection while ( WiFi.status() != WL_CONNECTED ) { if (connection_count > (10 * 2) ) { digitalWrite ( BUZZER_PIN, HIGH ); connection_count--; } delay ( 500 );
Serial.print ( "." );
connection_count++;
if (connection_count > (10 * 2) ) { digitalWrite ( BUZZER_PIN, LOW ); delay ( 500 ); Serial.print ( "." ); } }
Serial.println ( "" ); Serial.print ( "Connected to " ); Serial.println ( ssid ); Serial.print ( "IP address: " ); Serial.println ( WiFi.localIP() );
if ( MDNS.begin ( "esp8266" ) ) { Serial.println ( "MDNS responder started" ); }
server.on ( "/ping", handlePing ); server.on ( "/power", handlePower ); server.on ( "/reset", handleReset ); server.onNotFound ( handleNotFound ); server.begin(); Serial.println ( "HTTP server started" );
// 接続OK音 digitalWrite ( LED_PIN, LOW ); digitalWrite ( BUZZER_PIN, HIGH ); delay ( 1500 ); digitalWrite ( BUZZER_PIN, LOW );
Serial.println ( "setup complete!" );
}
void loop ( void ) { server.handleClient(); }
void handleNotFound() { digitalWrite ( LED_PIN, HIGH ); String message = "File Not Found\n\n"; message += "URI: "; message += server.uri(); message += "\nMethod: "; message += ( server.method() == HTTP_GET ) ? "GET" : "POST"; message += "\nArguments: "; message += server.args(); message += "\n";
for ( uint8_t i = 0; i < server.args(); i++ ) { message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n"; }
server.send ( 404, "text/plain", message ); digitalWrite ( LED_PIN, LOW ); }
void handlePing() { digitalWrite ( LED_PIN, HIGH ); // digitalWrite(BUZZER_PIN, HIGH);
char temp[400]; int sec = millis() / 1000; int min = sec / 60; int hr = min / 60;
snprintf ( temp, 400, "{ \"uptime\": \"%02d:%02d:%02d\", \"uptimeMills\": \"%02d\" }\n" , hr, min % 60, sec % 60, millis() ); server.send ( 200, "text/html", temp ); digitalWrite ( LED_PIN, LOW ); // digitalWrite(BUZZER_PIN, LOW ); }
void handlePower() { if (server.arg("type") == "long") { doSwitch(LONG, POWER_PIN); sendJson("POWER", "long"); } else { doSwitch(SHORT, POWER_PIN); sendJson("POWER", "short"); } }
void handleReset() { doSwitch(SHORT, RESET_PIN); sendJson("RESET", "short"); }
void sendJson(String target, String duration) { String json = ""; json += "{ ""result"": ""ok"", ""target"": """ + target + """, ""duration"": """ + duration + """}\n"; server.send ( 200, "application/json", json ); }
void doSwitch(int duration, int pin) { onoff(true, pin); delay(duration); onoff(false, pin); delay(100); // 一応リレーの切替速度 の問題で待っている。そんな連打はしないけれども }
void onoff(bool onoff, int pin) { if (onoff == true) { Serial.println("Switch ON"); pinMode(pin, OUTPUT); digitalWrite(BUZZER_PIN, HIGH); digitalWrite(LED_PIN, HIGH); } else { Serial.println("Switch OFF"); pinMode(pin, INPUT); digitalWrite(BUZZER_PIN, LOW); digitalWrite(LED_PIN, LOW); } }
|