前回ご紹介したesp8266をサーバー機にしてから子機に変更する方法でした。忘れたらは再度見てみてください。
本日ご紹介したいのは前回をもとにしてBlynkの機能を追加することで遠隔操作を可能にする!
まずは配線図を見せたいが、、、実は前回と同じなものになる。一応:
そして スケッチ:
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 |
#define BLYNK_PRINT Serial #include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer.h> #include <FS.h> #include <BlynkSimpleEsp8266.h> // モード切り替えピン const int MODE_PIN = 0; // GPIO0 // serverモード const int MODE_SERVER = 2; // GPIO2 // Wi-Fi設定保存ファイル const char* settings = "/wifi_settings.txt"; // サーバモードでのパスワード const String pass = "12345678"; String auth = "***************";//ご自分のBLYNK アプリで作ったプロジェクトのAUTH boolean serverMode; ESP8266WebServer server(80); /** * WiFi設定 */ void handleRootGet() { String html = ""; html +=F( "<h1>WiFi Setting</h1>"); html += F("<form method='post'>"); html += F("<input type='text' name='ssid' placeholder='ssid'><br>"); html += F("<input type='password' name='pass' placeholder='pass'><br>"); html += F("<input type='password' name='authkey' placeholder='authkey'><br>"); html += F("<input type='submit'><br>"); html +=F("</form>"); server.send(200, "text/html", html); } void handleRootPost() { String ssid = server.arg("ssid"); String pass = server.arg("pass"); String auth = server.arg("authkey"); File f = SPIFFS.open(settings, "w"); f.println(ssid); f.println(pass); f.println(auth); f.close(); String html = ""; html += F("<h1>WiFi Setting</h1>"); html += ssid + "<br>"; server.send(200, "text/html", html); } /** * 初期化(クライアントモード) */ void setup_client() { File f = SPIFFS.open(settings, "r"); String ssid = f.readStringUntil('\n'); String pass = f.readStringUntil('\n'); String auth = f.readStringUntil('\n'); f.close(); ssid.trim(); pass.trim(); auth.trim(); //Serial.println("SSID: " + ssid); // Serial.println("PASS: " + pass); // WiFi.mode(WIFI_STA); // WiFi.begin(ssid.c_str(), pass.c_str()); Blynk.begin(auth.c_str(), ssid.c_str(), pass.c_str()); while (WiFi.status() != WL_CONNECTED) { led12TM(); } //Serial.println(F("WiFi connected")); // Serial.print(F("IP address: ")); //Serial.println(WiFi.localIP()); } /** * 初期化(サーバモード) */ void setup_server() { String ssid = "Server"; /* You can remove the password parameter if you want the AP to be open. */ WiFi.mode(WIFI_AP); WiFi.softAP(ssid.c_str(), pass.c_str()); server.on("/", HTTP_GET, handleRootGet); server.on("/", HTTP_POST, handleRootPost); server.begin(); Serial.println(F("HTTP server started.")); } void led12TM(){ delay( 200 ); digitalWrite( MODE_SERVER, HIGH ); // sets the LED off delay( 200 ); // waits for a second digitalWrite( MODE_SERVER, LOW ); // sets the LED on } /** * 初期化 */ void setup() { Serial.begin(115200); SPIFFS.begin(); // 1秒以内にMODEを切り替える リセットする際にONにしてはいけない // 0 : Server // 1 : Client // ファイルシステム初期化 pinMode(MODE_PIN, INPUT); pinMode(MODE_SERVER, OUTPUT); digitalWrite(MODE_SERVER,LOW); if (digitalRead(MODE_PIN) == 0) { // サーバモード初期化 serverMode=true; setup_server(); } else { serverMode=false; // クライアントモード初期化 setup_client(); } } void loop() { Blynk.run(); delay(100); if(serverMode){ digitalWrite( MODE_SERVER, HIGH ); server.handleClient(); }else if (WiFi.status() != WL_CONNECTED) { led12TM(); }else{ digitalWrite( MODE_SERVER, LOW ); } } |