WiFi Manager · //ESP.reset(); // สาหรับ library...

14
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 ด้วย ในกรณีที่ทาไปใช้งานจริง เพื่อความปลอดภัยของตัวอุปกรณ์ และมั่นใจได้ว่าจะไม่มีใครสามารถ เข้าไปแก้ไขหรือสั่งงานอุปกรณ์ได้

Transcript of WiFi Manager · //ESP.reset(); // สาหรับ library...

Page 1: WiFi Manager · //ESP.reset(); // สาหรับ library ตัวนี้จะไม่ใชค้าสั่ง ESP.reset() delay(1000); //if you get here you have connected

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 ดวย ในกรณทท าไปใชงานจรง เพอความปลอดภยของตวอปกรณ และมนใจไดวาจะไมมใครสามารถ

เขาไปแกไขหรอสงงานอปกรณได

Page 2: WiFi Manager · //ESP.reset(); // สาหรับ library ตัวนี้จะไม่ใชค้าสั่ง ESP.reset() delay(1000); //if you get here you have connected

การตดตงและการใชงาน 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

Page 3: WiFi Manager · //ESP.reset(); // สาหรับ library ตัวนี้จะไม่ใชค้าสั่ง ESP.reset() delay(1000); //if you get here you have connected

โคดตวอยาง

#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

}

Page 4: WiFi Manager · //ESP.reset(); // สาหรับ library ตัวนี้จะไม่ใชค้าสั่ง ESP.reset() delay(1000); //if you get here you have connected

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:

}

Page 5: WiFi Manager · //ESP.reset(); // สาหรับ library ตัวนี้จะไม่ใชค้าสั่ง ESP.reset() delay(1000); //if you get here you have connected

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

เมอเขาถงอปกรณไดแลวกตงคาใหกบอปกรณไดเลย

Page 6: WiFi Manager · //ESP.reset(); // สาหรับ library ตัวนี้จะไม่ใชค้าสั่ง ESP.reset() delay(1000); //if you get here you have connected

หนา Captive Portal

หนา Configure WiFi และ Configure WiFi (No Scan)

Page 7: WiFi Manager · //ESP.reset(); // สาหรับ library ตัวนี้จะไม่ใชค้าสั่ง ESP.reset() delay(1000); //if you get here you have connected

9. ลองตงคา ssid และ password ใหอปกรณ

10. เมอกด Save ฝงอปกรณจะท าการบนทก ssid และ password แลวท าการเชอมตอ Access Point ตามท

ระบ

*WM: 3 เปนตวเลขสถานะของการเชอมตอ Access Point ส าเรจ

Page 8: WiFi Manager · //ESP.reset(); // สาหรับ library ตัวนี้จะไม่ใชค้าสั่ง ESP.reset() delay(1000); //if you get here you have connected

การตดตงและการใชงาน 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

Page 9: WiFi Manager · //ESP.reset(); // สาหรับ library ตัวนี้จะไม่ใชค้าสั่ง ESP.reset() delay(1000); //if you get here you have connected

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

Page 10: WiFi Manager · //ESP.reset(); // สาหรับ library ตัวนี้จะไม่ใชค้าสั่ง ESP.reset() delay(1000); //if you get here you have connected

#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);

}

Page 11: WiFi Manager · //ESP.reset(); // สาหรับ library ตัวนี้จะไม่ใชค้าสั่ง ESP.reset() delay(1000); //if you get here you have connected

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()

Page 12: WiFi Manager · //ESP.reset(); // สาหรับ library ตัวนี้จะไม่ใชค้าสั่ง ESP.reset() delay(1000); //if you get here you have connected

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);

Page 13: WiFi Manager · //ESP.reset(); // สาหรับ library ตัวนี้จะไม่ใชค้าสั่ง ESP.reset() delay(1000); //if you get here you have connected

/* 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

Page 14: WiFi Manager · //ESP.reset(); // สาหรับ library ตัวนี้จะไม่ใชค้าสั่ง ESP.reset() delay(1000); //if you get here you have connected

}

initWiFi(); // เรยกใชงานฟงกชนการจดการ wifi

}

ticker.detach();

}

}

delay(100);

}

4. กด upload และรอจนเสรจ

สามารถปรบเปลยนโคดตามการใชงานได ขนอยกบเงอนไขทใช ไมจ าเปนตองยดตามรปแบบโคด

ตวอยางกได

Library ทมการแกไข สามารถเขาไปจดทแกไขไดท http://goo.gl/qB0zts ไฮไลทสแดง คอ โคดเดม สวน

ไฮไลทสเขยว คอ โคดทแกไขเพม