├── .gitignore ├── .travis.yml ├── README.md ├── examples ├── wifi-basic-http-get │ └── wifi-basic-http-get.ino ├── wifi-basic-webserver │ ├── with-basic-angularjs │ │ └── with-basic-angularjs.ino │ ├── with-basic-html │ │ └── with-basic-html.ino │ └── with-basic-io │ │ └── with-basic-io.ino ├── wifi-basic │ └── wifi-basic.ino ├── wifi-full-options-config │ └── wifi-full-options-config.ino └── wifi-webserver-serve-static-files │ ├── data │ ├── assets │ │ └── thermometer0.png │ ├── css │ │ └── app.0.0.1.css │ ├── favicon.ico │ ├── index.html │ └── js │ │ ├── app.0.0.1.js │ │ └── app.0.0.1.js.zip │ └── wifi-webserver-serve-static-files.ino ├── extra_ci.py ├── extra_script.py ├── keywords.txt ├── lib └── readme.txt ├── library.json ├── library.properties ├── platformio.ini └── src ├── WiFiConnector.cpp └── WiFiConnector.h /.gitignore: -------------------------------------------------------------------------------- 1 | .pioenvs 2 | .clang_complete 3 | .gcc-flags.json 4 | pio_compile_here/WiFiConnector/WiFiConnector.cpp 5 | pio_compile_here/WiFiConnector/WiFiConnector.h 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - "2.7" 4 | 5 | # Cache PlatformIO packages using Travis CI container-based infrastructure 6 | sudo: false 7 | cache: 8 | directories: 9 | - "~/.platformio" 10 | env: 11 | - PLATFORMIO_CI_SRC=examples/wifi-basic 12 | - PLATFORMIO_CI_SRC=examples/wifi-full-options-config 13 | - PLATFORMIO_CI_SRC=examples/wifi-basic-http-get 14 | - PLATFORMIO_CI_SRC=examples/wifi-webserver-serve-static-files 15 | - PLATFORMIO_CI_SRC=examples/wifi-basic-webserver/with-basic-angularjs 16 | - PLATFORMIO_CI_SRC=examples/wifi-basic-webserver/with-basic-html 17 | - PLATFORMIO_CI_SRC=examples/wifi-basic-webserver/with-basic-io 18 | install: 19 | - pip install -U platformio 20 | script: 21 | - python extra_ci.py 22 | - find . 23 | - platformio ci --board=espresso_lite_v2 --lib="." 24 | # - export PLATFORMIO_BUILD_FLAGS='-DWIFI_SSID="Nat" -DWIFI_PASSPHRASE="123456789"' 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/cmmakerclub/WiFiConnector.svg?branch=master)](https://travis-ci.org/cmmakerclub/WiFiConnector) 2 | 3 | # WiFiConnector 4 | 5 | WiFi Connector เป็น Arduino Library สำหรับ ESP8266 ที่ถูกสร้างขึ้นมาให้ช่วยให้การต่อ WiFi ง่ายขึ้น และลดความซับซ้อนของโปรแกรมลงไปเพราะว่า การทำงานเป็นแบบเรียก callback เมื่อเกิด Event ต่างๆ 6 | 7 | ## Events 8 | 9 | - `on_connecting` 10 | - `on_connected` 11 | - `on_disconnected` 12 | - `on_smartconfig_done` 13 | - `on_smartconfig_waiting` 14 | - `on_smartconfig_processing` 15 | 16 | ## ตัวอย่างการใช้งาน 17 | 18 | [examples/wifi-full-options-config](https://github.com/cmmakerclub/WiFiConnector/tree/master/examples/wifi-full-options-config) 19 | 20 | ## อ่านบันเรื่องราว อัพเดต และอ่านเพิ่มเติม 21 | 22 | [![Join the chat at https://gitter.im/cmmakerclub/WiFiConnector](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/cmmakerclub/WiFiConnector?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 23 | -------------------------------------------------------------------------------- /examples/wifi-basic-http-get/wifi-basic-http-get.ino: -------------------------------------------------------------------------------- 1 | // Copyright Nat Weerawan 2015-2016 2 | // MIT License 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #ifndef WIFI_SSID 10 | #define WIFI_SSID "Nat" 11 | #define WIFI_PASSPHRASE "1234567890" 12 | #endif 13 | 14 | WiFiConnector wifi(WIFI_SSID, WIFI_PASSPHRASE); 15 | 16 | void init_hardware() 17 | { 18 | Serial.begin(115200); 19 | WiFi.disconnect(true); 20 | delay(1000); 21 | Serial.flush(); 22 | Serial.println(); 23 | Serial.println(); 24 | Serial.println("will be started in 500ms.."); 25 | } 26 | 27 | void init_wifi() { 28 | wifi.init(); 29 | wifi.on_connected([&](const void* message) 30 | { 31 | Serial.print("WIFI CONNECTED WITH IP: "); 32 | Serial.println(WiFi.localIP()); 33 | }); 34 | 35 | wifi.on_connecting([&](const void* message) 36 | { 37 | Serial.print("Connecting to "); 38 | Serial.println(wifi.get("ssid") + ", " + wifi.get("password")); 39 | delay(200); 40 | }); 41 | } 42 | 43 | 44 | void doHttpGet() { 45 | HTTPClient http; 46 | Serial.print("[HTTP] begin...\n"); 47 | // configure traged server and url 48 | //http.begin("https://192.168.1.12/test.html", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS 49 | http.begin("http://cmmc.xyz/hello.txt"); //HTTP 50 | 51 | // start connection and send HTTP header 52 | int httpCode = http.GET(); 53 | 54 | // httpCode will be negative on error 55 | if(httpCode > 0) { 56 | // HTTP header has been send and Server response header has been handled 57 | Serial.printf("[HTTP] GET... code: %d\n", httpCode); 58 | Serial.print("[CONTENT]\n"); 59 | 60 | // file found at server 61 | if(httpCode == HTTP_CODE_OK) { 62 | String payload = http.getString(); 63 | Serial.println(payload); 64 | } 65 | } else { 66 | Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); 67 | } 68 | 69 | http.end(); 70 | } 71 | 72 | void setup() 73 | { 74 | init_hardware(); 75 | init_wifi(); 76 | wifi.connect(); 77 | } 78 | 79 | void loop() 80 | { 81 | wifi.loop(); 82 | if (wifi.connected()) { 83 | doHttpGet(); 84 | delay(5000); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /examples/wifi-basic-webserver/with-basic-angularjs/with-basic-angularjs.ino: -------------------------------------------------------------------------------- 1 | // Copyright Nat Weerawan 2015-2016 2 | // MIT License 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #ifndef WIFI_SSID 9 | #define WIFI_SSID "Nat" 10 | #define WIFI_PASSPHRASE "1234567890" 11 | #endif 12 | 13 | WiFiConnector wifi(WIFI_SSID, WIFI_PASSPHRASE); 14 | ESP8266WebServer server(80); 15 | 16 | void init_hardware() 17 | { 18 | Serial.begin(115200); 19 | pinMode(LED_BUILTIN, OUTPUT); 20 | delay(10); 21 | Serial.println(); 22 | Serial.println("BEGIN"); 23 | } 24 | 25 | void init_webserver() { 26 | server.on ( "/", []() { 27 | static String responseHTML = F("" 28 | "" 29 | "" 30 | " " 31 | " " "" 32 | "" 33 | "" 52 | " " 53 | " " 54 | "
" 55 | "

CMMC Controller:

" 56 | "

millis() = {{ millis }}

" 57 | "
" 58 | " " 59 | ""); 60 | server.send (200, "text/html", responseHTML.c_str() ); 61 | }); 62 | 63 | server.on ( "/millis", []() { 64 | char buff[100]; 65 | String ms = String(millis()); 66 | sprintf(buff, "{\"millis\": %s }", ms.c_str()); 67 | server.send ( 200, "text/plain", buff ); 68 | }); 69 | } 70 | 71 | void init_wifi() { 72 | wifi.init(); 73 | wifi.on_connecting([&](const void* message) 74 | { 75 | Serial.print("Connecting to "); 76 | Serial.println(wifi.get("ssid") + ", " + wifi.get("password")); 77 | delay(200); 78 | }); 79 | 80 | wifi.on_connected([&](const void* message) 81 | { 82 | // Print the IP address 83 | Serial.print("WIFI CONNECTED => "); 84 | Serial.println(WiFi.localIP()); 85 | 86 | init_webserver(); 87 | server.begin(); 88 | Serial.println("SERVER Started."); 89 | }); 90 | 91 | wifi.on_disconnected([&](const void* message) { 92 | server.close(); 93 | }); 94 | } 95 | 96 | void setup() 97 | { 98 | init_hardware(); 99 | init_wifi(); 100 | wifi.connect(); 101 | } 102 | 103 | void loop() 104 | { 105 | wifi.loop(); 106 | server.handleClient(); 107 | } 108 | -------------------------------------------------------------------------------- /examples/wifi-basic-webserver/with-basic-html/with-basic-html.ino: -------------------------------------------------------------------------------- 1 | // Copyright Nat Weerawan 2015-2016 2 | // MIT License 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #ifndef WIFI_SSID 9 | #define WIFI_SSID "Nat" 10 | #define WIFI_PASSPHRASE "1234567890" 11 | #endif 12 | 13 | WiFiConnector wifi(WIFI_SSID, WIFI_PASSPHRASE); 14 | ESP8266WebServer server(80); 15 | 16 | void init_hardware() 17 | { 18 | Serial.begin(115200); 19 | pinMode(LED_BUILTIN, OUTPUT); 20 | delay(10); 21 | Serial.println(); 22 | Serial.println("BEGIN"); 23 | } 24 | 25 | void init_webserver() { 26 | server.on ("/", []() { 27 | static String responseHTML = F("" 28 | "" 29 | "" 30 | " " 31 | " " "" 32 | "" 33 | "" 54 | " " 55 | " " 56 | "

Hello World!

This is an esp8266 webpage example." 57 | " that you can embeded angular application inside!

" 58 | "

ESP8266 WebServer + AngularJS

" 59 | "
" 60 | " " 61 | " " 62 | "
" 63 | "

[{{millis}}] Hello, {{yourName}}

" 64 | " " 65 | "
" 66 | " " 67 | ""); 68 | server.send (200, "text/html", responseHTML.c_str() ); 69 | }); 70 | 71 | server.on ( "/millis", []() { 72 | char buff[100]; 73 | String ms = String(millis()); 74 | sprintf(buff, "{\"millis\": %s }", ms.c_str()); 75 | server.send ( 200, "text/plain", buff ); 76 | }); 77 | } 78 | 79 | void init_wifi() { 80 | wifi.init(); 81 | wifi.on_connecting([&](const void* message) 82 | { 83 | Serial.print("Connecting to "); 84 | Serial.println(wifi.get("ssid") + ", " + wifi.get("password")); 85 | delay(200); 86 | }); 87 | 88 | wifi.on_connected([&](const void* message) 89 | { 90 | // Print the IP address 91 | Serial.print("WIFI CONNECTED => "); 92 | Serial.println(WiFi.localIP()); 93 | 94 | init_webserver(); 95 | server.begin(); 96 | Serial.println("SERVER Started."); 97 | }); 98 | 99 | wifi.on_disconnected([&](const void* message) { 100 | server.close(); 101 | }); 102 | } 103 | 104 | void setup() 105 | { 106 | init_hardware(); 107 | init_wifi(); 108 | wifi.connect(); 109 | } 110 | 111 | void loop() 112 | { 113 | wifi.loop(); 114 | server.handleClient(); 115 | } 116 | -------------------------------------------------------------------------------- /examples/wifi-basic-webserver/with-basic-io/with-basic-io.ino: -------------------------------------------------------------------------------- 1 | // Copyright Nat Weerawan 2015-2016 2 | // MIT License 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #ifndef WIFI_SSID 9 | #define WIFI_SSID "Nat" 10 | #define WIFI_PASSPHRASE "1234567890" 11 | #endif 12 | 13 | WiFiConnector wifi(WIFI_SSID, WIFI_PASSPHRASE); 14 | ESP8266WebServer server(80); 15 | 16 | void init_hardware() 17 | { 18 | Serial.begin(115200); 19 | pinMode(LED_BUILTIN, OUTPUT); 20 | delay(10); 21 | Serial.println(); 22 | Serial.println("BEGIN"); 23 | } 24 | 25 | void init_webserver() { 26 | server.on ( "/", []() { 27 | bool state = digitalRead(LED_BUILTIN); 28 | digitalWrite(LED_BUILTIN, !state); 29 | char buff[100]; 30 | String ms = String(millis()); 31 | sprintf(buff, "{\"millis\": %s }", ms.c_str()); 32 | server.send ( 200, "text/plain", buff ); 33 | }); 34 | 35 | server.on ( "/millis", []() { 36 | char buff[100]; 37 | String ms = String(millis()); 38 | sprintf(buff, "{\"millis\": %s }", ms.c_str()); 39 | server.send ( 200, "text/plain", buff ); 40 | }); 41 | } 42 | 43 | void init_wifi() { 44 | wifi.init(); 45 | wifi.on_connecting([&](const void* message) 46 | { 47 | Serial.print("Connecting to "); 48 | Serial.println(wifi.get("ssid") + ", " + wifi.get("password")); 49 | delay(200); 50 | }); 51 | 52 | wifi.on_connected([&](const void* message) 53 | { 54 | // Print the IP address 55 | Serial.print("WIFI CONNECTED => "); 56 | Serial.println(WiFi.localIP()); 57 | 58 | init_webserver(); 59 | server.begin(); 60 | Serial.println("SERVER Started."); 61 | }); 62 | 63 | wifi.on_disconnected([&](const void* message) { 64 | server.close(); 65 | }); 66 | } 67 | 68 | void setup() 69 | { 70 | init_hardware(); 71 | init_wifi(); 72 | wifi.connect(); 73 | } 74 | 75 | void loop() 76 | { 77 | wifi.loop(); 78 | server.handleClient(); 79 | } 80 | -------------------------------------------------------------------------------- /examples/wifi-basic/wifi-basic.ino: -------------------------------------------------------------------------------- 1 | // Copyright Nat Weerawan 2015-2016 2 | // MIT License 3 | #include 4 | #include 5 | #include 6 | 7 | #ifndef WIFI_SSID 8 | #define WIFI_SSID "Nat" 9 | #define WIFI_PASSPHRASE "1234567890" 10 | #endif 11 | 12 | WiFiConnector wifi(WIFI_SSID, WIFI_PASSPHRASE); 13 | 14 | void init_hardware() 15 | { 16 | Serial.begin(115200); 17 | WiFi.disconnect(true); 18 | delay(1000); 19 | Serial.flush(); 20 | Serial.println(); 21 | Serial.println(); 22 | Serial.println("will be started in 500ms.."); 23 | } 24 | 25 | void init_wifi() { 26 | wifi.init(); 27 | wifi.on_connected([&](const void* message) 28 | { 29 | Serial.print("WIFI CONNECTED WITH IP: "); 30 | Serial.println(WiFi.localIP()); 31 | }); 32 | 33 | wifi.on_connecting([&](const void* message) 34 | { 35 | Serial.print("Connecting to "); 36 | Serial.println(wifi.get("ssid") + ", " + wifi.get("password")); 37 | delay(200); 38 | }); 39 | } 40 | 41 | void setup() 42 | { 43 | init_hardware(); 44 | init_wifi(); 45 | wifi.connect(); 46 | } 47 | 48 | void loop() 49 | { 50 | wifi.loop(); 51 | } 52 | -------------------------------------------------------------------------------- /examples/wifi-full-options-config/wifi-full-options-config.ino: -------------------------------------------------------------------------------- 1 | // Copyright Nat Weerawan 2015-2016 2 | // MIT License 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #ifndef WIFI_SSID 9 | #define WIFI_SSID "Nat" 10 | #define WIFI_PASSPHRASE "1234567890" 11 | #endif 12 | 13 | WiFiConnector wifi("Nat", "123456789"); 14 | 15 | void init_hardware() 16 | { 17 | Serial.begin(115200); 18 | WiFi.disconnect(true); 19 | delay(1000); 20 | Serial.flush(); 21 | Serial.println(); 22 | Serial.println(); 23 | Serial.println("will be started in 500ms.."); 24 | } 25 | 26 | void init_wifi() { 27 | wifi.init(); 28 | 29 | wifi.on_connected([&](const void* message) 30 | { 31 | Serial.print("WIFI CONNECTED WITH IP: "); 32 | Serial.println(WiFi.localIP()); 33 | }); 34 | 35 | wifi.on_connecting([&](const void* message) 36 | { 37 | Serial.print("Connecting to "); 38 | Serial.println(wifi.get("ssid") + ", " + wifi.get("password")); 39 | delay(200); 40 | }); 41 | } 42 | 43 | void setup() 44 | { 45 | init_hardware(); 46 | init_wifi(); 47 | wifi.connect(); 48 | } 49 | 50 | void loop() 51 | { 52 | wifi.loop(); 53 | } 54 | -------------------------------------------------------------------------------- /examples/wifi-webserver-serve-static-files/data/assets/thermometer0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cmmakerclub/WiFiConnector/010a55553810d5a8b18dd578685ca712c1a3b820/examples/wifi-webserver-serve-static-files/data/assets/thermometer0.png -------------------------------------------------------------------------------- /examples/wifi-webserver-serve-static-files/data/css/app.0.0.1.css: -------------------------------------------------------------------------------- 1 | /*! normalize.css v3.0.3 | MIT License | github.com/necolas/normalize.css */html{font-family:sans-serif;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%}body{margin:0}article,aside,details,figcaption,figure,footer,header,hgroup,main,menu,nav,section,summary{display:block}audio,canvas,progress,video{display:inline-block;vertical-align:baseline}audio:not([controls]){display:none;height:0}[hidden],template{display:none}a{background-color:transparent}a:active,a:hover{outline:0}abbr[title]{border-bottom:1px dotted}b,strong{font-weight:700}dfn{font-style:italic}h1{font-size:2em;margin:.67em 0}mark{background:#ff0;color:#000}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sup{top:-.5em}sub{bottom:-.25em}img{border:0}svg:not(:root){overflow:hidden}figure{margin:1em 40px}hr{box-sizing:content-box;height:0}pre{overflow:auto}code,kbd,pre,samp{font-family:monospace;font-size:1em}button,input,optgroup,select,textarea{color:inherit;font:inherit;margin:0}button{overflow:visible}button,select{text-transform:none}button,html input[type=button],input[type=reset],input[type=submit]{-webkit-appearance:button;cursor:pointer}button[disabled],html input[disabled]{cursor:default}button::-moz-focus-inner,input::-moz-focus-inner{border:0;padding:0}input{line-height:normal}input[type=checkbox],input[type=radio]{box-sizing:border-box;padding:0}input[type=number]::-webkit-inner-spin-button,input[type=number]::-webkit-outer-spin-button{height:auto}input[type=search]{-webkit-appearance:textfield;box-sizing:content-box}input[type=search]::-webkit-search-cancel-button,input[type=search]::-webkit-search-decoration{-webkit-appearance:none}fieldset{border:1px solid silver;margin:0 2px;padding:.35em .625em .75em}legend{border:0;padding:0}textarea{overflow:auto}optgroup{font-weight:700}table{border-collapse:collapse;border-spacing:0}td,th{padding:0}body,html{margin:0;min-height:100%}.H9FPl{font-size:inherit;padding:20px}._3qbDW{font-size:inherit;padding:20px}.p0mbV{border-bottom:1px solid #eee;margin:0 0 20px;padding-bottom:20px}._1tZ5c{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-box-orient:horizontal;-webkit-box-direction:normal;-webkit-flex-direction:row;-ms-flex-direction:row;flex-direction:row}._1Cvfy{height:322.6666666666667px;width:52pt}.Uxabs{-webkit-box-flex:1;-webkit-flex:1;-ms-flex:1;flex:1;background-color:#bc8f8f}.Uxabs,.k4sWH{-webkit-box-pack:center;-webkit-justify-content:center;-ms-flex-pack:center;justify-content:center;-webkit-box-align:center;-webkit-align-items:center;-ms-flex-align:center;align-items:center;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex}.k4sWH{background-color:#00d4b4;-webkit-box-flex:3;-webkit-flex:3;-ms-flex:3;flex:3}._3Gtv2{font-size:75pt;font-family:Sukhumvit Set;color:#2f4f4f}.TmtNS{background:#eee;border-radius:8px;padding:10px 10px 10px 40px;width:200px}._2BS6h{border-top:1px solid #eee;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;font-size:inherit;margin-top:20px;padding-top:20px} -------------------------------------------------------------------------------- /examples/wifi-webserver-serve-static-files/data/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cmmakerclub/WiFiConnector/010a55553810d5a8b18dd578685ca712c1a3b820/examples/wifi-webserver-serve-static-files/data/favicon.ico -------------------------------------------------------------------------------- /examples/wifi-webserver-serve-static-files/data/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | CMMC-React-App 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | -------------------------------------------------------------------------------- /examples/wifi-webserver-serve-static-files/data/js/app.0.0.1.js.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cmmakerclub/WiFiConnector/010a55553810d5a8b18dd578685ca712c1a3b820/examples/wifi-webserver-serve-static-files/data/js/app.0.0.1.js.zip -------------------------------------------------------------------------------- /examples/wifi-webserver-serve-static-files/wifi-webserver-serve-static-files.ino: -------------------------------------------------------------------------------- 1 | // Copyright Nat Weerawan 2015-2016 2 | // MIT License 3 | 4 | #include 5 | #include 6 | #include 7 | #include "FS.h" 8 | 9 | #ifndef WIFI_SSID 10 | #define WIFI_SSID "Nat" 11 | #define WIFI_PASSPHRASE "1234567890" 12 | #endif 13 | 14 | WiFiConnector wifi(WIFI_SSID, WIFI_PASSPHRASE); 15 | ESP8266WebServer server(80); 16 | 17 | void fail(const char* msg) { 18 | Serial.println(msg); 19 | while (true) { 20 | yield(); 21 | } 22 | } 23 | 24 | void init_hardware() 25 | { 26 | Serial.begin(115200); 27 | delay(10); 28 | Serial.println(); 29 | Serial.println("BEGIN"); 30 | 31 | if (!SPIFFS.begin()) { 32 | fail("SPIFFS init failed"); 33 | } 34 | 35 | Dir root = SPIFFS.openDir("/"); 36 | Serial.println("READING ROOT.."); 37 | while (root.next()) { 38 | String fileName = root.fileName(); 39 | File f = root.openFile("r"); 40 | Serial.printf("%s: %d\r\n", fileName.c_str(), f.size()); 41 | } 42 | } 43 | 44 | void init_wifi() { 45 | wifi.init(); 46 | 47 | wifi.on_connecting([&](const void* message) 48 | { 49 | Serial.print("Connecting to "); 50 | Serial.println(wifi.get("ssid") + ", " + wifi.get("password")); 51 | delay(200); 52 | }); 53 | 54 | wifi.on_connected([&](const void* message) 55 | { 56 | // Print the IP address 57 | Serial.print("WIFI CONNECTED => "); 58 | Serial.println(WiFi.localIP()); 59 | 60 | server.on ( "/test", []() { 61 | char buff[100]; 62 | String ms = String(millis()); 63 | sprintf(buff, "{\"millis\": %s }", ms.c_str()); 64 | server.send ( 200, "text/plain", buff ); 65 | }); 66 | 67 | server.on ( "/atmosphere", []() { 68 | char buff[100]; 69 | String ms = String(millis()); 70 | sprintf(buff, "{\"millis\": %s, \"temp\": %s, \"humid\": %s}", 71 | ms.c_str(), "99.9", "55.5"); 72 | server.send ( 200, "text/plain", buff ); 73 | }); 74 | 75 | server.on ( "/hello", []() { 76 | server.send ( 200, "text/plain", String("Hi, there")); 77 | }); 78 | 79 | server.serveStatic("/", SPIFFS, "/"); 80 | server.begin(); 81 | 82 | Serial.println("SERVER Started."); 83 | }); 84 | } 85 | 86 | void setup() 87 | { 88 | init_hardware(); 89 | init_wifi(); 90 | wifi.connect(); 91 | } 92 | 93 | void loop() 94 | { 95 | wifi.loop(); 96 | server.handleClient(); 97 | } 98 | -------------------------------------------------------------------------------- /extra_ci.py: -------------------------------------------------------------------------------- 1 | import glob, shutil, os 2 | 3 | # uncomment line below to see environment variables 4 | # print env.Dump() 5 | # print ARGUMENTS 6 | 7 | if not os.path.exists("pio_compile_here/WiFiConnector"): 8 | os.makedirs("pio_compile_here/WiFiConnector") 9 | 10 | for file in glob.iglob('src/*.*'): 11 | print 'Copied %s' % (file) 12 | shutil.copy2(file, "pio_compile_here/WiFiConnector") 13 | -------------------------------------------------------------------------------- /extra_script.py: -------------------------------------------------------------------------------- 1 | from SCons.Script import DefaultEnvironment 2 | import glob, shutil, os 3 | 4 | env = DefaultEnvironment() 5 | # uncomment line below to see environment variables 6 | # print env.Dump() 7 | # print ARGUMENTS 8 | 9 | # copyfile("src/WiFiConnector.h", "tmp/WiFiConnector/WiFiConnector.h") 10 | # copyfile("src/WiFiConnector.cpp", "tmp/WiFiConnector/WiFiConnector.cpp") 11 | 12 | if not os.path.exists("pio_compile_here/WiFiConnector"): 13 | os.makedirs("pio_compile_here/WiFiConnector") 14 | 15 | for file in glob.iglob('src/*.*'): 16 | print 'Copied file %s' % (file) 17 | shutil.copy2(file, "pio_compile_here/WiFiConnector") 18 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For Wire 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | ####################################### 10 | # Methods and Functions (KEYWORD2) 11 | ####################################### 12 | 13 | begin KEYWORD2 14 | connect KEYWORD2 15 | loop KEYWORD2 16 | on_connected KEYWORD2 17 | on_disconnected KEYWORD2 18 | on_smartconfig_done KEYWORD2 19 | on_smartconfig_waiting KEYWORD2 20 | on_smartconfig_processing KEYWORD2 21 | 22 | ####################################### 23 | # Instances (KEYWORD2) 24 | ####################################### 25 | 26 | WiFiConnector KEYWORD2 27 | 28 | ####################################### 29 | # Constants (LITERAL1) 30 | ####################################### 31 | 32 | -------------------------------------------------------------------------------- /lib/readme.txt: -------------------------------------------------------------------------------- 1 | 2 | This directory is intended for the project specific (private) libraries. 3 | PlatformIO will compile them to static libraries and link to executable file. 4 | 5 | The source code of each library should be placed in separate directory, like 6 | "lib/private_lib/[here are source files]". 7 | 8 | For example, see how can be organized `Foo` and `Bar` libraries: 9 | 10 | |--lib 11 | | |--Bar 12 | | | |--docs 13 | | | |--examples 14 | | | |--src 15 | | | |- Bar.c 16 | | | |- Bar.h 17 | | |--Foo 18 | | | |- Foo.c 19 | | | |- Foo.h 20 | | |- readme.txt --> THIS FILE 21 | |- platformio.ini 22 | |--src 23 | |- main.c 24 | 25 | Then in `src/main.c` you should use: 26 | 27 | #include 28 | #include 29 | 30 | // rest H/C/CPP code 31 | 32 | PlatformIO will find your libraries automatically, configure preprocessor's 33 | include paths and build them. 34 | 35 | See additional options for PlatformIO Library Dependency Finder `lib_*`: 36 | 37 | http://docs.platformio.org/en/latest/projectconf.html#lib-install 38 | 39 | -------------------------------------------------------------------------------- /library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "WiFiConnector", 3 | "keywords": "wifi, wifi manager, wifi connector", 4 | "description": "A CMMC WiFi Connect (formerly known as WiFi Connector ESP8266)", 5 | "repository": 6 | { 7 | "type": "git", 8 | "url": "https://github.com/cmmakerclub/WiFiConnector.git" 9 | }, 10 | "frameworks": "arduino", 11 | "platforms": "espressif" 12 | } 13 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=CMMC WiFi Connector 2 | version=3.0.8 3 | author=Nat Weerawan 4 | maintainer=Nat Weerawan 5 | sentence=WiFiConnector by CMMC 6 | paragraph=WiFiConnector is an event-based wifi management library. 7 | category=Communication 8 | url=https://github.com/cmmakerclub/WiFiConnector 9 | architectures=esp8266 10 | -------------------------------------------------------------------------------- /platformio.ini: -------------------------------------------------------------------------------- 1 | # 2 | # Project Configuration File 3 | # 4 | # A detailed documentation with the EXAMPLES is located here: 5 | # http://docs.platformio.org/en/latest/projectconf.html 6 | # 7 | 8 | # A sign `#` at the beginning of the line indicates a comment 9 | # Comment lines are ignored. 10 | 11 | # Simple and base environment 12 | # [env:mybaseenv] 13 | # platform = %INSTALLED_PLATFORM_NAME_HERE% 14 | # framework = 15 | # board = 16 | # 17 | # Automatic targets - enable auto-uploading 18 | # targets = upload 19 | 20 | [env:espresso_lite_v2] 21 | platform = espressif 22 | framework = arduino 23 | board = espresso_lite_v2 24 | upload_speed = 460800 25 | extra_script = extra_script.py 26 | build_flags = '-DWIFI_SSID="Nat"' '-DWIFI_PASSPHRASE="123456789"' -DDEBUG_ESP_PORT=Serial -DDEBUG_ESP_CORE -DDEBUG_ESP_SSL -DDEBUG_ESP_WIFI -DDEBUG_ESP_HTTP_CLIENT -DDEBUG_ESP_HTTP_UPDATE -DDEBUG_ESP_HTTP_SERVER -DDEBUG_ESP_UPDATER -DDEBUG_ESP_OTA -DDEBUG_TLS_MEM 27 | ; -DWIFI_CONNECTOR_DEBUG_MODE 28 | 29 | [platformio] 30 | ; src_dir = examples/wifi-basic 31 | ; src_dir = examples/wifi-full-options-config 32 | ; src_dir = examples/wifi-basic-http-get 33 | ; src_dir = examples/wifi-basic-webserver-with-html 34 | ; src_dir = examples/wifi-basic-webserver/with-basic-angularjs 35 | ; src_dir = examples/wifi-basic-webserver/with-basic-html 36 | ; src_dir = examples/wifi-basic-webserver/with-basic-io 37 | src_dir = examples/wifi-webserver-serve-static-files 38 | data_dir = examples/wifi-webserver-serve-static-files/data 39 | lib_dir = pio_compile_here 40 | -------------------------------------------------------------------------------- /src/WiFiConnector.cpp: -------------------------------------------------------------------------------- 1 | // #ifndef WIFI_CONNECTOR_H 2 | // #define WIFI_CONNECTOR_H 3 | /* 4 | 5 | Copyright Nat Weerawan 2015-2016 6 | MIT License 7 | 8 | The MIT License (MIT) 9 | 10 | Copyright (c) Nat Weerawan (cmmakerclub.com) 11 | 12 | Permission is hereby granted, free of charge, to any person obtaining a copy 13 | of this software and associated documentation files (the "Software"), to deal 14 | in the Software without restriction, including without limitation the rights 15 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16 | copies of the Software, and to permit persons to whom the Software is 17 | furnished to do so, subject to the following conditions: 18 | 19 | The above copyright notice and this permission notice shall be included in 20 | all copies or substantial portions of the Software. 21 | 22 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 28 | THE SOFTWARE. 29 | 30 | */ 31 | 32 | #include "WiFiConnector.h" 33 | 34 | // static WiFiConnector* __wifi_connector = NULL; 35 | static WiFiConnector *_wifi = NULL; 36 | 37 | WiFiConnector::WiFiConnector(String ssid, String password) { 38 | _ssid = ssid; 39 | _passphase = password; 40 | 41 | _config = new Config; 42 | _config->instance = this; 43 | _config->ssid = ssid; 44 | _config->passphase = password; 45 | 46 | }; 47 | 48 | WiFiConnector::WiFiConnector(String ssid, String password, uint8_t pin) { 49 | WiFiConnector(ssid, password); 50 | }; 51 | 52 | void WiFiConnector::setSsid(String ssid){ 53 | _ssid = ssid; 54 | } 55 | 56 | void WiFiConnector::setPasspharse(String pass){ 57 | _passphase = pass; 58 | } 59 | 60 | String WiFiConnector::get(String key) { 61 | if (key == "ssid") { 62 | return WiFi.SSID(); 63 | } 64 | // backward compatibility 65 | else if (key == "psk" || key == "password" || key == "passpharse") { 66 | return WiFi.psk(); 67 | } 68 | else { 69 | return "WRONG argument."; 70 | } 71 | } 72 | 73 | String WiFiConnector::psk() { 74 | return WiFi.psk(); 75 | } 76 | String WiFiConnector::SSID() { 77 | return WiFi.SSID(); 78 | } 79 | 80 | void WiFiConnector::init() { 81 | _wifi = WiFiConnector::instance(); 82 | _wifi->setSsid(_ssid); 83 | _wifi->setPasspharse(_passphase); 84 | _initialised = true; 85 | 86 | WiFi.setAutoConnect(true); 87 | WiFi.setAutoReconnect(true); 88 | 89 | static WiFiConnector *_this = this; 90 | 91 | WiFi.onEvent([](WiFiEvent_t event) { 92 | DEBUG_PRINT("[WiFi-event] event: "); 93 | DEBUG_PRINTLN(event); 94 | switch (event) { 95 | case WIFI_EVENT_STAMODE_CONNECTED: 96 | DEBUG_PRINTLN("DEBUG: WIFI_EVENT_STAMODE_CONNECTED"); 97 | if (_this->_user_on_connected) { 98 | // DEBUG_PRINTLN("DEBUG: STAMOD_CONNECTED."); 99 | // _this->_user_on_connected((void*)"CONNECTED"); 100 | // _this->_connected = true; 101 | } 102 | else { 103 | 104 | } 105 | break; 106 | case WIFI_EVENT_STAMODE_DISCONNECTED: 107 | // Serial.printf("%lu => WIFI_EVENT_STAMODE_DISCONNECTED\r\n", millis()); 108 | DEBUG_PRINTLN("DEBUG: WIFI_EVENT_STAMODE_DISCONNECTED"); 109 | _wifi->_connected = false; 110 | _wifi->_got_ip = false; 111 | 112 | _this->_connected = false; 113 | _this->_got_ip = false; 114 | if (_this->_user_on_disconnected) { 115 | _this->_user_on_disconnected((void*)"..DISCONNECTED"); 116 | DEBUG_PRINTLN("DEBUG: DISCONNECTED"); 117 | } 118 | break; 119 | case WIFI_EVENT_STAMODE_AUTHMODE_CHANGE: 120 | // Serial.printf("%lu => WIFI_EVENT_STAMODE_AUTHMODE_CHANGE\r\n", millis()); 121 | break; 122 | case WIFI_EVENT_STAMODE_GOT_IP: 123 | if (_this->_user_on_connected) { 124 | _this->_user_on_connected((void*)"CONNECTED"); 125 | DEBUG_PRINTLN("DEBUG: CONNECTED"); 126 | _this->_got_ip = true; 127 | _this->_connected = true; 128 | 129 | _wifi->_got_ip = true; 130 | _wifi->_connected = true; 131 | 132 | } 133 | // Serial.printf("%lu => WIFI_EVENT_STAMODE_GOT_IP: ", millis()); 134 | // Serial.println(WiFi.localIP()); 135 | break; 136 | case WIFI_EVENT_STAMODE_DHCP_TIMEOUT: 137 | // Serial.printf("%lu => WIFI_EVENT_STAMODE_DHCP_TIMEOUT: ", millis()); 138 | break; 139 | case WIFI_EVENT_SOFTAPMODE_STACONNECTED: 140 | // Serial.printf("%lu => WIFI_EVENT_SOFTAPMODE_STACONNECTED: \r\n", millis()); 141 | break; 142 | case WIFI_EVENT_SOFTAPMODE_STADISCONNECTED: 143 | // Serial.printf("%lu => WIFI_EVENT_SOFTAPMODE_STADISCONNECTED: \r\n", millis()); 144 | break; 145 | case WIFI_EVENT_SOFTAPMODE_PROBEREQRECVED: 146 | // Serial.printf("%lu => WIFI_EVENT_SOFTAPMODE_PROBEREQRECVED: \r\n", millis()); 147 | break; 148 | case WIFI_EVENT_MAX: 149 | // Serial.printf("%lu => WIFI_EVENT_MAX: \r\n", millis()); 150 | break; 151 | } 152 | }); 153 | } 154 | 155 | void WiFiConnector::loop() { 156 | if (_initialised == false) { 157 | if (millis() % 1000 == 0) { 158 | DEBUG_PRINTLN("DEBUG: not initialized."); 159 | } 160 | return; 161 | } 162 | 163 | if (_wifi->_connected == false) { 164 | if (_user_on_connecting) { 165 | _user_on_connecting((void*) "CONNECTING..."); 166 | DEBUG_PRINTLN("DEBUG: CONNECTING.."); 167 | } 168 | } 169 | } 170 | 171 | void WiFiConnector::connect() { 172 | WiFi.begin(_config->ssid.c_str(), _config->passphase.c_str()); 173 | } 174 | 175 | bool WiFiConnector::connected() { 176 | // return _wifi->_got_ip; 177 | return this->_got_ip; 178 | } 179 | 180 | void WiFiConnector::disconnect(bool wifioff) { 181 | WiFi.disconnect(wifioff); 182 | } 183 | 184 | void WiFiConnector::on_disconnected(wifi_callback_t callback) 185 | { 186 | _user_on_disconnected = callback; 187 | } 188 | 189 | void WiFiConnector::on_connected(wifi_callback_t callback) 190 | { 191 | DEBUG_PRINTLN("SET CALLBACK on_connected"); 192 | _user_on_connected = callback; 193 | } 194 | 195 | void WiFiConnector::on_connecting(wifi_callback_t callback) 196 | { 197 | _user_on_connecting = callback; 198 | } 199 | 200 | void WiFiConnector::on_smartconfig_waiting(wifi_callback_t callback) { 201 | _user_on_smartconfig_waiting = callback; 202 | } 203 | 204 | void WiFiConnector::on_smartconfig_done(wifi_callback_t callback) 205 | { 206 | _user_on_smartconfig_done = callback; 207 | } 208 | 209 | void WiFiConnector::on_smartconfig_processing(wifi_callback_t callback) 210 | { 211 | DEBUG_PRINTLN("SMARTCONFIG DONE."); 212 | _user_on_smartconfig_processing = callback; 213 | } 214 | 215 | // #endif /* WIFI_CONNECTOR_H */ 216 | -------------------------------------------------------------------------------- /src/WiFiConnector.h: -------------------------------------------------------------------------------- 1 | #ifndef WIFI_CONNECTOR_H 2 | #define WIFI_CONNECTOR_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | //#define WIFI_CONNECTOR_DEBUG_MODE 10 | using namespace std; 11 | 12 | #ifdef ESP8266 13 | extern "C" { 14 | #include "user_interface.h" 15 | } 16 | #endif 17 | 18 | #ifdef WIFI_CONNECTOR_DEBUG_MODE 19 | #define DEBUG_PRINTER Serial 20 | #define DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); } 21 | #define DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); } 22 | #else 23 | #define DEBUG_PRINT(...) { } 24 | #define DEBUG_PRINTLN(...) { } 25 | #endif 26 | 27 | // TYPES 28 | // typedef enum { 29 | // WL_NO_SHIELD = 255, // for compatibility with WiFi Shield library 30 | // WL_IDLE_STATUS = 0, 31 | // WL_NO_SSID_AVAIL = 1, 32 | // WL_SCAN_COMPLETED = 2, 33 | // WL_CONNECTED = 3, 34 | // WL_CONNECT_FAILED = 4, 35 | // WL_CONNECTION_LOST = 5, 36 | // WL_DISCONNECTED = 6 37 | // } wl_status_t; 38 | 39 | 40 | typedef std::function wifi_callback_t; 41 | 42 | 43 | class WiFiConnector; 44 | 45 | typedef struct { 46 | String ssid; 47 | String passphase; 48 | WiFiConnector *instance; 49 | } Config; 50 | 51 | class WiFiConnector { 52 | private: 53 | String _ssid; 54 | String _passphase; 55 | WiFiConnector* s_instance; 56 | bool _initialised = false; 57 | bool _connected = false; 58 | bool _got_ip = false; 59 | Config *_config; 60 | 61 | wifi_callback_t _user_on_disconnected = NULL; 62 | wifi_callback_t _user_on_connected = NULL; 63 | wifi_callback_t _user_on_connecting = NULL; 64 | 65 | wifi_callback_t _user_on_smartconfig_waiting; 66 | wifi_callback_t _user_on_smartconfig_done; 67 | wifi_callback_t _user_on_smartconfig_processing; 68 | 69 | protected: 70 | WiFiConnector() { }; 71 | public: 72 | uint32_t counter = 0; 73 | WiFiConnector(String, String); 74 | WiFiConnector(String, String, uint8_t); 75 | 76 | ~WiFiConnector() { 77 | delete s_instance; 78 | delete _config; 79 | }; 80 | 81 | String get(String); 82 | String SSID(); 83 | String psk(); 84 | 85 | void setSsid(String); 86 | void setPasspharse(String); 87 | 88 | static WiFiConnector* instance() 89 | { 90 | static WiFiConnector *s_instance = NULL; 91 | Serial.printf("addr: %x\r\n", s_instance); 92 | if (!s_instance) { 93 | s_instance = new WiFiConnector; 94 | } 95 | Serial.printf("addr: %x\r\n", s_instance); 96 | return s_instance; 97 | } 98 | 99 | void init(); 100 | void loop(); 101 | void connect(); 102 | bool connected(); 103 | void disconnect(bool wifioff = false); 104 | 105 | void on_disconnected(wifi_callback_t callback = NULL); 106 | void on_connected(wifi_callback_t callback = NULL); 107 | void on_connecting(wifi_callback_t callback = NULL); 108 | 109 | void on_smartconfig_waiting(wifi_callback_t callback = NULL); 110 | void on_smartconfig_done(wifi_callback_t callback = NULL); 111 | void on_smartconfig_processing(wifi_callback_t callback = NULL); 112 | }; 113 | 114 | // WiFiConnector* WiFiConnector::s_instance = NULL; 115 | 116 | #endif /* WIFI_CONNECTOR_H */ 117 | --------------------------------------------------------------------------------