NodeMCU V3 ESP8266: Difference between revisions
Jump to navigation
Jump to search
(33 intermediate revisions by the same user not shown) | |||
Line 242: | Line 242: | ||
server.send(404, "text/plain", "404: Not found"); | server.send(404, "text/plain", "404: Not found"); | ||
} | } | ||
</source> | |||
===Font Controller=== | |||
<code>FontCtrl.hpp</code> | |||
<source lang="cpp"> | |||
#ifndef __FontCtrl_hpp__ | |||
#define __FontCtrl_hpp__ | |||
#include <Arduino.h> | |||
#include <ESP8266WiFi.h> | |||
#include <ESP8266mDNS.h> | |||
#include <ESP8266WebServer.h> | |||
class FontCtrl: public RequestHandler { | |||
public: | |||
FontCtrl(const char *uri = "about"): _uri(uri) {} | |||
bool handle(ESP8266WebServer&, HTTPMethod, String) override; | |||
bool canHandle(HTTPMethod, String) override; | |||
protected: | |||
String _uri; | |||
}; | |||
#endif // __FontCtrl_hpp__ | |||
</source> | |||
<code>FontCtrl.cpp</code> | |||
<source lang="cpp"> | |||
#include "FontCtrl.hpp" | |||
bool FontCtrl::canHandle(HTTPMethod method, String uri) { | |||
if (method != HTTP_GET || uri != _uri) { | |||
return false; | |||
} | |||
return true; | |||
} | |||
bool FontCtrl::handle(ESP8266WebServer &server, HTTPMethod requestMethod, String requestUri) { | |||
if (!canHandle(requestMethod, requestUri)){ | |||
return false; | |||
} | |||
server.send(200, "text/plain", "Hello, Acadmeian!"); | |||
return true; | |||
} | |||
</source> | |||
<source lang="cpp"> | |||
#include "FontCtrl.hpp" | |||
//... | |||
void setup(void) { | |||
//... | |||
server.addHandler(new FontCtrl("/")); | |||
Serial.begin(115200); | |||
//... | |||
} | |||
</source> | |||
==Libraries== | |||
<source lang="bash> | |||
cd ~/Documents/Arduino/libraries;\ | |||
git clone https://github.com/me-no-dev/ESPAsyncTCP.git;\ | |||
git clone https://github.com/me-no-dev/ESPAsyncWebServer.git;\ | |||
git clone https://github.com/bblanchon/ArduinoJson.git;\ | |||
cd ArduinoJson; git checkout v5.13.5; cd .. | |||
</source> | |||
==Tools== | |||
===File System Uploader=== | |||
<source lang="bash> | |||
cd ~/Documents/Arduino; mkdir tools; cd tools | |||
wget https://github.com/esp8266/arduino-esp8266fs-plugin/releases/download/0.5.0/ESP8266FS-0.5.0.zip | |||
unzip ESP8266FS-0.5.0.zip;rm -rf ESP8266FS-0.5.0.zip | |||
wget https://github.com/me-no-dev/arduino-esp32fs-plugin/releases/download/1.0/ESP32FS-1.0.zip | |||
unzip ESP32FS-1.0.zip; rm -rf ESP32FS-1.0.zip | |||
</source> | |||
{| | |||
| valign="top" | | |||
<source lang="bash> | |||
# compressing the static assets for node mcu v3 | |||
gzip CKi.js | |||
gzip -c CKi.js > CKi.js.gz | |||
</source> | |||
| valign="top" | | |||
<source lang="bash> | |||
# decompressing the static assets of node mcu v3 | |||
gzip -d CKi.js.gz | |||
gunzip CKi.js.gz | |||
</source> | |||
|} | |||
== AsyncWebServer== | |||
'''Path Variable''' | |||
<source lang="bash> | |||
# windows | |||
echo 'compiler.cpp.extra_flags=-DDASYNCWEBSERVER_REGEX' \ | |||
>> ~/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.6.3/platform.local.txt | |||
# macos | |||
echo 'compiler.cpp.extra_flags=-DDASYNCWEBSERVER_REGEX' \ | |||
>> ~/Library/Arduino15/packages/esp8266/hardware/esp8266/2.6.3/platform.local.txt | |||
# linux | |||
echo 'compiler.cpp.extra_flags=-DDASYNCWEBSERVER_REGEX' \ | |||
>> ~/.arduino15/packages/esp8266/hardware/esp8266/2.6.3/platform.local.txt | |||
</source> | </source> | ||
==References== | ==References== | ||
{| | {| | ||
| valign="top" | | | valign="top" | | ||
Line 272: | Line 378: | ||
| valign="top" | | | valign="top" | | ||
* [https://github.com/esp8266/arduino-esp8266fs-plugin Arduino ESP8266 File System Uploader] | |||
* [https://github.com/me-no-dev/arduino-esp32fs-plugin Arduino ESP32 File System Uploader] | |||
* [https://stackoverflow.com/questions/1284529 Where should I need to put Enums] | * [https://stackoverflow.com/questions/1284529 Where should I need to put Enums] | ||
* [https://gist.github.com/igrr/0da0c4adc7588d9bd911 WebServer Custom Handler] | * [https://gist.github.com/igrr/0da0c4adc7588d9bd911 WebServer Custom Handler] | ||
* [https://circuits4you.com/2018/02/02/esp8266-handle-not-found-pages-and-redirect Handle Page Not Found] | * [https://circuits4you.com/2018/02/02/esp8266-handle-not-found-pages-and-redirect Handle Page Not Found] | ||
* [https://github.com/esp8266/Arduino/tree/master/cores/esp8266/spiffs SPI Flash File System] | |||
* [https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266WebServer ESP8266WebServer] | * [https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266WebServer ESP8266WebServer] | ||
* [https://www.cs.fsu.edu/~myers/c++/notes/references.html Pass By Reference] | * [https://www.cs.fsu.edu/~myers/c++/notes/references.html Pass By Reference] | ||
* [https://randomnerdtutorials.com/esp8266-web-server/ WiFi Client Server] | * [https://randomnerdtutorials.com/esp8266-web-server/ WiFi Client Server] | ||
* [https://arduino.esp8266.com/Arduino/versions/2.0.0/doc/filesystem.html File System] | |||
|} | |||
---- | |||
{| | |||
| valign="top" | | |||
* [https://arduinojson.org/v5/api/jsonarray/createnestedobject ArduinoJson Create Nested Object] | |||
* [https://github.com/marvinroger/async-mqtt-client ESPAsyncMQTTClient] | |||
* [https://github.com/me-no-dev/ESPAsyncWebServer ESPAsyncWebServer] | |||
* [https://superuser.com/questions/161706/command-to-gzip-a-folder GZip Command] | |||
* [https://stackoverflow.com/questions/9791239 Alias of a Class] | |||
* [https://github.com/me-no-dev/ESPAsyncTCP ESPAsyncTCP] | |||
* [https://github.com/baruch/esp8266_smart_home Smart Home] | |||
* [https://github.com/bblanchon/ArduinoJson ArduinoJson] | |||
* [https://arduinojson.org/v5/api/jsonobject/ JsonObject] | |||
* [https://arduinojson.org/v5/api/jsonarray/ JsonArray] | |||
| valign="top" | | |||
* [https://github.com/marvinroger/async-mqtt-client/blob/master/examples/FullyFeatured-ESP8266/FullyFeatured-ESP8266.ino AsyncMqttClient Example for ESP8266] | |||
* [https://arduino-esp8266.readthedocs.io/en/latest/filesystem.html#file-system-object-spiffs-littlefs-sd-sdfs SPIFFS: File System Object] | |||
* [https://github.com/bblanchon/ArduinoJson/releases ArduinoJson Releases] | |||
* [https://github.com/me-no-dev/ESPAsyncWebServer/issues/556 Serve Static by GZIP] | |||
* [https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WebServer/src/detail/mimetable.h Mime Table Header] | |||
|} | |} |
Latest revision as of 06:38, 6 April 2020
WiFi Modes of Operation of ESP8266: There are three modes of WiFi Operation in the ESP8266 WiFi Module. Those are:
- Station Mode (STA)
- Soft Access Point (AP)
- Soft AP + Station
Station Mode
Only Access Point
#include "ESP8266WiFi.h"
const char* ssid = "ChorkeOrg_2.4GHz";
const char* pass = "academia";
void setup(void) {
pinMode(D4, OUTPUT);
Serial.begin(115200);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("*");
}
Serial.println("");
Serial.println("WiFi connection Successful");
Serial.print("The IP Address of ESP8266 Module is: ");
Serial.print(WiFi.localIP());
}
void loop() {
digitalWrite(D4, LOW);
delay(1000);
digitalWrite(D4, HIGH);
delay(1000);
}
Multi Access Point
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
const char* ap0_ssid_50 = "ChorkeOrg";
const char* ap0_pasword = "chorke@bd/0A";
const char* ap0_ssid_24 = "ChorkeOrg_2.4GHz";
ESP8266WiFiMulti multi;
void setup(void) {
pinMode(D4, OUTPUT);
Serial.begin(115200);
delay(10);
Serial.println('\n');
multi.addAP(ap0_ssid_24, ap0_pasword);
multi.addAP(ap0_ssid_50, ap0_pasword);
Serial.print("Connecting : ");
while (multi.run() != WL_CONNECTED) {
delay(500);
Serial.print('>');
}
Serial.println('\n');
Serial.print("Connected to: ");
Serial.println(WiFi.SSID());
Serial.print("IP address : ");
Serial.println(WiFi.localIP());
}
void loop() {
digitalWrite(D4, LOW);
delay(1000);
digitalWrite(D4, HIGH);
delay(1000);
}
Soft Access Point
#include "ESP8266WiFi.h"
const char* ssid = "ChorkeIot_2.4GHz";
const char* pass = "academia";
IPAddress local_IP(10, 10, 10, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress gateway(10, 10, 10, 1);
void setup(void) {
pinMode(D4, OUTPUT);
Serial.begin(115200);
Serial.println();
Serial.print("WNC Config: ");
Serial.print(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
Serial.println("SID Config: ");
Serial.print(WiFi.softAP(ssid, pass) ? "Ready" : "Failed!");
Serial.println("WAN Config: ");
Serial.print(WiFi.softAPIP());
}
void loop() {
digitalWrite(D4, LOW);
delay(1000);
digitalWrite(D4, HIGH);
delay(1000);
}
Duel Mode
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266WiFiMulti.h>
const char* ssid = "ChorkeIot_2.4GHz";
const char* pass = "academia";
const char* ap0_ssid_50 = "ChorkeOrg";
const char* ap0_pasword = "chorke@bd/0A";
const char* ap0_ssid_24 = "ChorkeOrg_2.4GHz";
ESP8266WiFiMulti multi;
void setup(void) {
pinMode(D4, OUTPUT);
Serial.begin(115200);
delay(10);
Serial.println('\n');
multi.addAP(ap0_ssid_24, ap0_pasword);
multi.addAP(ap0_ssid_50, ap0_pasword);
Serial.print("Connecting: ");
while (multi.run() != WL_CONNECTED) {
delay(500);
Serial.print('>');
}
Serial.println('\n');
Serial.print("Connected : ");
Serial.println(WiFi.SSID());
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("DNS OK!");
}
Serial.print("WNC Config: ");
Serial.println(WiFi.softAPConfig(WiFi.localIP(), WiFi.gatewayIP(), WiFi.subnetMask()) ? "Ready" : "Failed!");
Serial.print("SID Config: ");
Serial.println(WiFi.softAP(ssid, pass) ? "Ready" : "Failed!");
Serial.print("WAN Config: ");
Serial.println(WiFi.softAPIP());
}
void loop() {
digitalWrite(D4, LOW);
delay(1000);
digitalWrite(D4, HIGH);
delay(1000);
}
Web Server
#include <WiFiClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WebServer.h>
const char* ssid = "ChorkeIot_2.4GHz";
const char* pass = "academia";
const char* ap0_ssid_50 = "ChorkeOrg";
const char* ap0_pasword = "chorke@bd/0A";
const char* ap0_ssid_24 = "ChorkeOrg_2.4GHz";
ESP8266WiFiMulti multi;
ESP8266WebServer server(80);
void handleRoot();
void handleNotFound();
void setup(void) {
pinMode(D4, OUTPUT);
Serial.begin(115200);
delay(10);
Serial.println('\n');
multi.addAP(ap0_ssid_24, ap0_pasword);
multi.addAP(ap0_ssid_50, ap0_pasword);
Serial.print("Connecting: ");
while (multi.run() != WL_CONNECTED) {
delay(500);
Serial.print('>');
}
Serial.println('\n');
Serial.print("Connected : ");
Serial.println(WiFi.SSID());
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("DNS OK!");
}
server.on("/", handleRoot);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
Serial.print("WNC Config: ");
Serial.println(WiFi.softAPConfig(WiFi.localIP(), WiFi.gatewayIP(), WiFi.subnetMask()) ? "Ready" : "Failed!");
Serial.print("SID Config: ");
Serial.println(WiFi.softAP(ssid, pass) ? "Ready" : "Failed!");
Serial.print("WAN Config: ");
Serial.println(WiFi.softAPIP());
}
void loop() {
server.handleClient();
digitalWrite(D4, LOW);
delay(1000);
digitalWrite(D4, HIGH);
delay(1000);
}
void handleRoot() {
server.send(200, "text/plain", "Hello, Acadmeian!");
}
void handleNotFound() {
server.send(404, "text/plain", "404: Not found");
}
Font Controller
FontCtrl.hpp
#ifndef __FontCtrl_hpp__
#define __FontCtrl_hpp__
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
class FontCtrl: public RequestHandler {
public:
FontCtrl(const char *uri = "about"): _uri(uri) {}
bool handle(ESP8266WebServer&, HTTPMethod, String) override;
bool canHandle(HTTPMethod, String) override;
protected:
String _uri;
};
#endif // __FontCtrl_hpp__
FontCtrl.cpp
#include "FontCtrl.hpp"
bool FontCtrl::canHandle(HTTPMethod method, String uri) {
if (method != HTTP_GET || uri != _uri) {
return false;
}
return true;
}
bool FontCtrl::handle(ESP8266WebServer &server, HTTPMethod requestMethod, String requestUri) {
if (!canHandle(requestMethod, requestUri)){
return false;
}
server.send(200, "text/plain", "Hello, Acadmeian!");
return true;
}
#include "FontCtrl.hpp"
//...
void setup(void) {
//...
server.addHandler(new FontCtrl("/"));
Serial.begin(115200);
//...
}
Libraries
cd ~/Documents/Arduino/libraries;\
git clone https://github.com/me-no-dev/ESPAsyncTCP.git;\
git clone https://github.com/me-no-dev/ESPAsyncWebServer.git;\
git clone https://github.com/bblanchon/ArduinoJson.git;\
cd ArduinoJson; git checkout v5.13.5; cd ..
Tools
File System Uploader
cd ~/Documents/Arduino; mkdir tools; cd tools
wget https://github.com/esp8266/arduino-esp8266fs-plugin/releases/download/0.5.0/ESP8266FS-0.5.0.zip
unzip ESP8266FS-0.5.0.zip;rm -rf ESP8266FS-0.5.0.zip
wget https://github.com/me-no-dev/arduino-esp32fs-plugin/releases/download/1.0/ESP32FS-1.0.zip
unzip ESP32FS-1.0.zip; rm -rf ESP32FS-1.0.zip
# compressing the static assets for node mcu v3
gzip CKi.js
gzip -c CKi.js > CKi.js.gz
|
# decompressing the static assets of node mcu v3
gzip -d CKi.js.gz
gunzip CKi.js.gz
|
AsyncWebServer
Path Variable
# windows
echo 'compiler.cpp.extra_flags=-DDASYNCWEBSERVER_REGEX' \
>> ~/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.6.3/platform.local.txt
# macos
echo 'compiler.cpp.extra_flags=-DDASYNCWEBSERVER_REGEX' \
>> ~/Library/Arduino15/packages/esp8266/hardware/esp8266/2.6.3/platform.local.txt
# linux
echo 'compiler.cpp.extra_flags=-DDASYNCWEBSERVER_REGEX' \
>> ~/.arduino15/packages/esp8266/hardware/esp8266/2.6.3/platform.local.txt