Last active
September 21, 2020 07:17
-
-
Save mhama/6c8c8cc1a0076e1c68ed5feec9d605a4 to your computer and use it in GitHub Desktop.
Wifi Configurator for M5StickC using WiFiManager (development branch) see: https://qiita.com/mhama/items/ff5ae397a853aa4f8d48
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <M5StickC.h> | |
#include <WiFiManager.h> | |
WiFiManager wifiManager; | |
bool isWifiConfigSucceeded = false; | |
void showMessage(String msg) | |
{ | |
M5.Lcd.setRotation(1); | |
M5.Lcd.setCursor(0, 10, 2); | |
M5.Lcd.fillScreen(BLACK); | |
M5.Lcd.println(msg); | |
} | |
void setupWiFi() | |
{ | |
wifiManager.setAPCallback(configModeCallback); | |
// clicking power button at boot time to enter wifi config mode | |
bool doManualConfig = false; | |
showMessage("Push POWER button to enter Wifi config."); | |
for(int i=0 ; i<200 ; i++) { | |
M5.update(); | |
if (M5.Axp.GetBtnPress()) { | |
doManualConfig = true; | |
break; | |
} | |
delay(10); | |
} | |
if (doManualConfig) { | |
Serial.println("wifiManager.startConfigPortal()"); | |
if (wifiManager.startConfigPortal()) { | |
isWifiConfigSucceeded = true; | |
Serial.println("startConfigPortal() connect success!"); | |
} | |
else { | |
Serial.println("startConfigPortal() connect failed!"); | |
} | |
} else { | |
showMessage("Wi-Fi connecting..."); | |
Serial.println("wifiManager.autoConnect()"); | |
if (wifiManager.autoConnect()) { | |
isWifiConfigSucceeded = true; | |
Serial.println("autoConnect() connect success!"); | |
} | |
else { | |
Serial.println("autoConnect() connect failed!"); | |
} | |
} | |
if (isWifiConfigSucceeded) { | |
showMessage("Wi-Fi connected."); | |
} else { | |
showMessage("Wi-Fi failed."); | |
} | |
} | |
void setup() | |
{ | |
Serial.begin(115200); | |
Serial.println("M5StickC started."); | |
M5.begin(); | |
setupWiFi(); | |
} | |
void configModeCallback (WiFiManager *myWiFiManager) { | |
Serial.println("Entered config mode"); | |
Serial.println(WiFi.softAPIP()); | |
Serial.println(myWiFiManager->getConfigPortalSSID()); | |
showMessage("Please connect to this AP\nto config WiFi\nSSID: " + myWiFiManager->getConfigPortalSSID()); | |
} | |
void accessGoogle() { | |
WiFiClient client; | |
if (client.connect("google.com", 80)) { | |
client.println("GET / HTTP/1.0"); | |
client.println(); | |
String body = client.readString(); | |
showMessage("google.com: " + body.substring(0, 100)); | |
Serial.println("google.com: " + body); | |
} | |
} | |
void loop() | |
{ | |
if (WiFi.isConnected()) { | |
showMessage("SSID: " + WiFi.SSID() + "\nIP: " + WiFi.localIP().toString()); | |
delay(3000); | |
accessGoogle(); | |
delay(30000); | |
} else { | |
showMessage("Not Connected."); | |
} | |
delay(500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment