WiFi Manager
WiFi Manager เปนไลบรารตวหนงทชวยใหพฒนาหรอผใชงานสามารถทจะจดการเรอง WiFi ใหกบ
อปกรณไดงายขน
ขอด
1. ม Captive Portal ชวยใหสามารถระบ ssid และ password ใหกบอปกรณไดโดยงาย เพยงแคเลอก
เมนและกรอกขอมลทตองการแลวกดบนทก
2. สะดวกสบายไมตองแกไขโคดโปรแกรมแลวอพโหลดใหมใหยงยาก เขยนโคดอพโหลดใหแคครง
เดยว
3. ชวยดแลเรองการเชอมตอ WiFi เมอมปญหา เชน Access Point หาย ใชงานไมได หรออนเตอรเนต
หลด ตว WiFiManager จะท าการเปลยนโหมดตวเองใหเปน Access Point เพอใหผใชงานสามารถทจะเชอมตอ
อปกรณเขาไปเปลยน ssid และ password ใหใหม
ขอเสย
WiFiManager ชวยดแลการเชอมตอใหไดในระดบหนง แตการเปลยนโหมดจาก Client เปน AP นน
จะมการเรยก ESP.reset() ซงการเรยกบอยๆ อาจจะมผลท าใหอปกรณคาง
ขอควรระวง
WiFiManager ใชงานงายเพยงแคเชอมตอกบอปกรณ แตในขณะทอปกรณนนเปลยนตวเองใหเปน
โหมด Access Point ซงผใชงานคนอนกสามารถมองเหนและเขาถงตวอปกรณได ดงนนควรจะตง password
ของ AP ดวย ในกรณทท าไปใชงานจรง เพอความปลอดภยของตวอปกรณ และมนใจไดวาจะไมมใครสามารถ
เขาไปแกไขหรอสงงานอปกรณได
การตดตงและการใชงาน WiFiManager Library
1. เปดโปรแกรม Arduino IDE
2. เขาไปท Sketch -> Include Library -> Manage Libraries…
3. พมพค าวา wifimanager ในชอง Filter your search… เมอมคนหาเจอแลวใหคลกบน Label ของ
WiFiManager และคลก Install
4. เมอตดตงเรยบรอยแลว กด Close ไดเลย และลองดตวอยาง WiFiManager เขาไปท File -> Examples -
> WiFiManager -> AutoConnectWithFeedbackLED
โคดตวอยาง
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <Ticker.h>
Ticker ticker;
void tick() { // เปนฟงกชนไวสงงานแสดงสถานะ led
int state = digitalRead(BUILTIN_LED); // get the current state of GPIO1 pin
digitalWrite(BUILTIN_LED, !state); // set pin to the opposite state
}
void configModeCallback (WiFiManager *myWiFiManager) { // callback เมอเชอม access point ไมส าเรจ จะเขาสโหมด AP
Serial.println("Entered config mode");
Serial.println(WiFi.softAPIP());
Serial.println(myWiFiManager->getConfigPortalSSID());
ticker.attach(0.2, tick); // ก าหนดสถานะกระพรบของ led ตดดบสลบกน 200 มลลวนาท
}
void setup() {
Serial.begin(115200);
pinMode(BUILTIN_LED, OUTPUT);
ticker.attach(0.6, tick); // ก าหนดสถานะกระพรบของ led ตดดบสลบกน 600 มลลวนาท
WiFiManager wifiManager; // ประกาศใชงาน WiFiManager
wifiManager.resetSettings(); // ฟงกชนส าหรบลบการตงคา ssid, password
wifiManager.setTimeout(180); // ฟงกชนส าหรบก าหนด timeout ของโหมด AP อปกรณ
wifiManager.setAPCallback(configModeCallback); // ก าหนด callback ของ AP ไปทฟงกชน configModeCallback
if (!wifiManager.autoConnect("ESP_NodeMCU")) { // ก าหนดชอ AP ใหอปกรณ เมอเปนโหมด AP, SSID ชอ ESP_NodeMCU
Serial.println("failed to connect and hit timeout");
ESP.reset(); // reset อปกรณ
delay(1000);
}
Serial.println("connected...yeey :)");
ticker.detach(); // ยกเลกการใช tricker สงงานเปลยนสถานะ led
digitalWrite(BUILTIN_LED, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
}
5. ใหเลอก board ทจะใชงาน ในตอนนจะใชเปน NodeMCU โดยคลกท Tools -> Board: “???????” ->
NodeMCU 1.0 และเลอก Port ทเชอมตอ
6. คลก Sketh -> Upload หรอกดคยลด Ctrl+U
7. หลงจาก upload เสรจแลว ดท Serial Monitor โดยคลกทรปแวนขยาย หรอเขาไปท Tools -> Serial
Monitor เมออปกรณเขาสโหมด AP จะได output ดงภาพดานลาง
8. ใหน ามอถอหรอคอมพวเตอรเชอมตอไปท AP ชอ ESP_NodeMCU หลงจากทตอเรยบรอย จะม
Captive Portal เดงขนมา ถาหากไมมการตอบสนอง ใหเปด Browser ขนมา และพมพ address : 192.168.4.1
เมอเขาถงอปกรณไดแลวกตงคาใหกบอปกรณไดเลย
หนา Captive Portal
หนา Configure WiFi และ Configure WiFi (No Scan)
9. ลองตงคา ssid และ password ใหอปกรณ
10. เมอกด Save ฝงอปกรณจะท าการบนทก ssid และ password แลวท าการเชอมตอ Access Point ตามท
ระบ
*WM: 3 เปนตวเลขสถานะของการเชอมตอ Access Point ส าเรจ
การตดตงและการใชงาน WiFiManager Library + EEPROM
EEPROM(Electrical Erasable Programmable Read Only Memory) เปน ROM ชนดหนง ซงถกพฒนา
และออกแบบมาใหสามารถใชงานไดงาย ใชไฟฟาอานเขยนขอมล และสามารถใชงานแทน EPROM ไดเปน
อยางด
ขอควรระวง
1. การอานเขยนเรวๆ มผลกระทบท าใหอปกรณเสอมสภาพเรว
2. การเขยนขอมล สามารถเขยนทบต าแหนงเดมได ดงนนจงตองระวงใหดตอนเขยนขอมล
3. ไฟเลยงอาจสงผลกระทบท าให EEPROM เสยหายได เมอมไฟกระชากบอยๆ
4. การเขยนขอมลลง EEPROM ของอปกรณบางตวจะเขยนโคดตางกน เชน Arduino Uno จะใชแค
ค าสง EEPROM.write() สวน ESP8266 จะมค าสงทเพมขนมา นนคอ EEPROM.commit() ถาหากไมมค าสงน
ขอมลจะไมถกจดเกบลง EEPROM
ขอมลเพมเตม : http://www.comgeeks.net/eeprom/
การตดตง Library
1. ดาวนโหลด WiFiManager Library ทไดพฒนาใหรองรบการใชงานรวมกบ EEPROM ท github :
https://github.com/anunpanya/WiFiManager.git
2. เมอดาวนโหลดเสรจแลว ใหแตกไฟลไวทโฟลเดอรของ Arduino libraries ซงในระบบปฏบตการ
วนโดวอยท Document -> Arduino -> libraries
C:\Users\<account name>\Documents\Arduino\libraries\WiFiManager
3. หากเปดโปรแกรม Arduino IDE ไว ใหปดโปรแกรมแลวเปดใหม และเขาไป File -> Examples ->
WiFiManager -> Basic_WiFiManager_DisconnectWiFi
ตวอยางนเปนการใช WiFiManager รวมกบ Microgear Library (NETPIE) สงทแตกตางจาก
WiFiManager ปกต จะมการก าหนดต าแหนงของ EEPROM ทจะใชงาน และไมใชส าหรบ ESP.reset()
/* NETPIE ESP8266 basic sample */
/* More information visit : https://netpie.io */
#include <ESP8266WiFi.h>
#include <MicroGear.h>
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
//for LED status
#include <Ticker.h>
Ticker ticker;
#define APPID "APPID" // application id ทสรางในเวบ NETPIE
#define KEY "APPKEY" // key ทสรางในเวบ NETPIE
#define SECRET "APPSECRET" // secret ทสรางในเวบ NETPIE
#define ALIAS "esp8266dev"
WiFiClient client;
int timer = 0;
int timeoutConnection = 0; // เปนตวนบเพอสงตดการเชอมตอ wifi
MicroGear microgear(client);
void tick()
{
//toggle state
int state = digitalRead(BUILTIN_LED); // get the current state of GPIO1 pin
digitalWrite(BUILTIN_LED, !state); // set pin to the opposite state
}
//gets called when WiFiManager enters configuration mode
void configModeCallback (WiFiManager *myWiFiManager) {
Serial.println("Entered config mode");
Serial.println(WiFi.softAPIP());
//if you used auto generated SSID, print it
Serial.println(myWiFiManager->getConfigPortalSSID());
//entered config mode, make led toggle faster
ticker.attach(0.2, tick);
}
/* If a new message arrives, do this */
void onMsghandler(char *topic, uint8_t* msg, unsigned int msglen) {
Serial.print("Incoming message --> ");
msg[msglen] = '\0';
Serial.println((char *)msg);
}
void onFoundgear(char *attribute, uint8_t* msg, unsigned int msglen) {
Serial.print("Found new member --> ");
for (int i=0; i<msglen; i++)
Serial.print((char)msg[i]);
Serial.println();
}
void onLostgear(char *attribute, uint8_t* msg, unsigned int msglen) {
Serial.print("Lost member --> ");
for (int i=0; i<msglen; i++)
Serial.print((char)msg[i]);
Serial.println();
}
/* When a microgear is connected, do this */
void onConnected(char *attribute, uint8_t* msg, unsigned int msglen) {
Serial.println("Connected to NETPIE...");
/* Set the alias of this microgear ALIAS */
microgear.setAlias(ALIAS);
ticker.attach(1, tick);
timeoutConnection=0;
}
void initWiFi(){ // ฟงกชนส าหรบใชงาน wifimanager และจดการการเชอมตอ wifi
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager(192); // 192 เปนตวเลขต าแหนงเรมตน EEPROM ทไวบนทกขอมลส าหรบ wifimanager
//set timeout 180 sec.
wifiManager.setTimeout(180);
//reset settings - for testing
//wifiManager.resetSettings();
wifiManager.setAPCallback(configModeCallback);
if (!wifiManager.autoConnect()) { // ตงชอ SSID , PASSWORD ตวอยาง autoConnect("APNAME","12345678")
Serial.println("failed to connect and hit timeout");
//reset and try again, or maybe put it to deep sleep
//ESP.reset(); // ส าหรบ library ตวนจะไมใชค าสง ESP.reset()
delay(1000);
}
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
ticker.detach();
//keep LED on
digitalWrite(BUILTIN_LED, LOW);
}
void setup() {
/* Add Event listeners */
/* Call onMsghandler() when new message arraives */
microgear.on(MESSAGE,onMsghandler);
/* Call onFoundgear() when new gear appear */
microgear.on(PRESENT,onFoundgear);
/* Call onLostgear() when some gear goes offline */
microgear.on(ABSENT,onLostgear);
/* Call onConnected() when NETPIE connection is established */
microgear.on(CONNECTED,onConnected);
Serial.begin(115200);
Serial.println("Starting...");
//set led pin as output
pinMode(BUILTIN_LED, OUTPUT);
// start ticker with 0.5 because we start in AP mode and try to connect
ticker.attach(0.7, tick);
initWiFi(); // เรยกใชงานฟงกชนการจดการ wifi
/* Initial with KEY, SECRET and also set the ALIAS here */
microgear.init(KEY,SECRET,ALIAS);
/* connect to NETPIE to a specific APPID */
microgear.connect(APPID);
}
void loop() {
if(WiFi.status()!=WL_CONNECTED){ // ตรวจสอบสถานะการเชอมตอเครอขาย
Serial.println("disconnect wifi.");
initWiFi(); // เรยกใชงานฟงกชนการจดการ wifi
}else{
/* To check if the microgear is still connected */
if (microgear.connected()) {
Serial.println("connected");
/* Call this method regularly otherwise the connection may be lost */
microgear.loop();
if (timer >= 1000) {
Serial.println("Publish...");
/* Chat with the microgear named ALIAS which is myself */
microgear.chat(ALIAS,"Hello");
timer = 0;
}
else timer += 100;
}
else {
Serial.println("connection lost, reconnect...");
if (timer >= 5000) {
microgear.connect(APPID);
timeoutConnection++;
timer = 0;
}
else timer += 100;
if(timeoutConnection==6){ // ตรวจสอบ counter เพอสงตดการเชอมตอ
if(WiFi.status()==WL_CONNECTED){ // ตรวจสอบการเชอมตอ หากเชอมตออย จะท าการตดการเชอมตอ wifi
WiFi.disconnect(); // ตดการเชอมตอ wifi
}
initWiFi(); // เรยกใชงานฟงกชนการจดการ wifi
}
ticker.detach();
}
}
delay(100);
}
4. กด upload และรอจนเสรจ
สามารถปรบเปลยนโคดตามการใชงานได ขนอยกบเงอนไขทใช ไมจ าเปนตองยดตามรปแบบโคด
ตวอยางกได
Library ทมการแกไข สามารถเขาไปจดทแกไขไดท http://goo.gl/qB0zts ไฮไลทสแดง คอ โคดเดม สวน
ไฮไลทสเขยว คอ โคดทแกไขเพม