Code:
// cstringarg( char* haystack, char* vname, char* sarg )
// haystack pattern: &varname=1234abc, delimiters &, \n, \0, SPACE, EOF
cstringarg(strinput, "uname", struname); // uname
cstringarg(strinput, "upwd", strupwd); // upwd
//...
if ( (strlen(strupwd)==strlen(website_upwd) )
&& (strcmp(website_upwd, strupwd )==0)
)
{
authorized = true;
//...
}
Hier mal der komplette, abgespeckte Code:
Code:
//----------------------------------------------------------------------------
// board: ESP8266 NodeMCU 1.0 (ESP12-E module)
// Arduino IDE 1.8.5
// ESP8266 core 2.4.0
//----------------------------------------------------------------------------
// Wifi + website data
extern const char* ssid =""; // WIFI network name
extern const char* password =""; // WIFI network password
// define
char website_uname[20] ="" ; // website user name log in "MyWebsiteLoginName"
char website_upwd[20] =""; // website user pwd log in "MyWebsiteLoginPwd"
char* website_title =""; // website caption "MySiteCaption"
char* website_url =""; // website url "http:\\mysite.com"
//----------------------------------------------------------------------------
// OLED SSD1306
#include <ESP_SSD1306.h> // Modification of Adafruit_SSD1306 for ESP8266 compatibility
#include <Adafruit_GFX.h> // Needs a little change in original Adafruit library (See README.txt file)
#include <Fonts/FreeSansBold12pt7b.h> //
#include <Fonts/FreeSans9pt7b.h> //
//#include <Fonts/FreeMono12pt7b.h> //
// I2C Wire
#include <Wire.h>
#define SCL D1 // SCL
#define SDA D2 // SDA
#define D12 10 // GPIO intern
#define OLED_RESET 10 // GPIO10=D12 Pin RESET signal
ESP_SSD1306 display(OLED_RESET);
//----------------------------------------------------------------------------
// IO pins
//----------------------------------------------------------------------------
#define PIN_BTND3 D3 // Btn 0=D3 reset Alarm
// #define PIN_IN4 D4 // N/A
#define PIN_OUT1 D5 // out
#define PIN_OUT2 D7 // out
// #define PIN_OUT3 D8 // N/A
//----------------------------------------------------------------------------
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#define ORANGE 255,102,0
// WiFi Router
#define this_iph 200 // <<< local host ip
#define http_port 80
IPAddress this_ip(192, 168, 2, this_iph); // <<< Feste lokale IP dieses ESP8266-Servers
IPAddress gateway(192, 168, 2, 1); // <<< LAN Gateway IP
IPAddress subnet(255, 255, 255, 0); // <<< LAN Subnet Mask
WiFiServer wifiserver(http_port);
ESP8266WebServer lanserver(8081);
bool authorized = false;
//----------------------------------------------------------------------------
// Tools
//----------------------------------------------------------------------------
int16_t strstrpos(char * haystack, char * needle) // find 1st occurance of substr in str
{
char *p = strstr(haystack, needle);
if (p) return p - haystack;
return -1; // Not found = -1.
}
//----------------------------------------------------------------------------
const int MAXLEN = 1024;
const int TOKLEN = 64;
char * cstringarg( char* haystack, char* vname, char* sarg ) {
int i=0, pos=-1;
unsigned char ch=0xff;
char kini[3] = "&"; // start of varname: '&':
char kequ[3] = "="; // end of varname, start of argument: '='
char needle[TOKLEN]=""; // complete pattern: &varname=abc1234
//kequ[0] = '='; // customize
strcpy(sarg,"");
strcpy(needle, kini);
strcat(needle, vname);
strcat(needle, kequ);
pos = strstrpos(haystack, needle);
if(pos==-1) return sarg;
pos=pos+strlen(vname)+2; // start of value = kini+vname+kequ
while( (ch!='&')&&(ch!='\0') ) {
ch=haystack[pos+i];
if( (ch=='&')||(ch==';')||(ch==' ')||(ch=='\0') ||(ch=='\n')
||(i+pos>=strlen(haystack))||(i>TOKLEN-1) ) {
sarg[i]='\0';
return sarg;
}
if( (ch!='&') ) {
sarg[i]=ch;
i++;
}
}
return sarg;
}
//----------------------------------------------------------------------------
// OLED dashboard
//----------------------------------------------------------------------------
void dashboard(int mode) {
display.clearDisplay();
if (mode >= 0) {
display.setCursor( 0, 0);
display.print(this_ip);
display.setCursor (0, 16);
display.print(gateway);
display.display();
}
display.setFont();
}
//----------------------------------------------------------------------------
// SETUP
//----------------------------------------------------------------------------
void setup() {
int IORes;
//STR_DEGREE[0] = CHR_DEGREE; // ° symbol as ANSI C string
int progress = 0;
//----------------------------------------
Serial.begin(115200);
delay(1000);
//----------------------------------------
// i2c: init
Wire.pins(SDA, SCL); // SDA, SCL
Wire.begin();
delay(1);
//----------------------------------------
// OLED
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64)
display.setFont();
display.setTextSize(1);
display.setTextColor(WHITE);
display.clearDisplay();
display.setCursor( 0, 0); display.print("OLED TEST OK");
display.display();
delay(1);
Serial.println("OLED sensor init...");
//----------------------------------------
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.println("Connecting to Router: ");
Serial.println( WiFi.gatewayIP().toString() );
WiFi.begin(ssid, password);
WiFi.config(this_ip, gateway, subnet); // feste IP
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
display.clearDisplay();
display.setCursor( 0, 20); display.print("WiFi connecting...");
display.setCursor( 0, 40); display.print((String)progress + "%");
if (progress >= 98) {
progress = 80;
Serial.println();
}
display.display();
if (progress < 10) progress += 5;
else if (progress < 50) progress += 2;
else if (progress < 90) progress += 1;
}
display.clearDisplay();
progress = 100;
display.setCursor( 0, 20); display.print("WiFi connecting...");
display.setCursor( 0, 40); display.print((String)progress + "%");
display.display();
delay(300);
Serial.println("");
Serial.print("WiFi connected: ");
Serial.println(WiFi.gatewayIP());
//----------------------------------------
// Start the WiFi server (-> www)
wifiserver.begin();
Serial.println("WiFi Server started");
//----------------------------------------
// Start the ESP LAN server (-> ESP client)
lanserver.on("/",handleRoot) ;
lanserver.on("/client/client0/", handleClients);
delay(10);
lanserver.on("/client/client1/", handleClients);
delay(10);
lanserver.on("/client/client2/", handleClients);
delay(10);
lanserver.on("/client/client3/", handleClients);
delay(10);
lanserver.begin();
Serial.println("ESP Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.print(":");
Serial.print(http_port);
Serial.println("/");
Serial.print((String)website_url + ":" + http_port + "/");
delay(1);
//----------------------------------------
// setup done
dashboard(1);
Serial.println("setup done \n");
}
//----------------------------------------------------------------------------
// LOOP
//----------------------------------------------------------------------------
void loop() {
static double ftmp;
static unsigned long tsec=millis(), tms = millis();
static int8_t LEDmode=0;
//---------------------------------------
// Check log-in
if (!authorized) {
handleNotAuthorized();
delay(100);
}
if (authorized) {
handleWebsite();
delay(10);
}
lanserver.handleClient();
delay(10);
//---------------------------------------
// Read local + Udp data
if ( millis() - tms >= 100 ) { // refresh data rate
tms = millis();
//---------------------------------------
// display on OLED
if ( millis() - tsec >= 4000 ) {
tsec=millis();
LEDmode++;
if(LEDmode>8) LEDmode=0;
}
dashboard(LEDmode);
delay(1);
}
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void handleNotAuthorized() {
String readString="";
char strinput[MAXLEN], strupwd[TOKLEN], struname[TOKLEN], test[TOKLEN] ;
WiFiClient client = wifiserver.available();
//---------------------------------------
// debug
// authorized=true;
strcpy(strinput, "");
strcpy(strupwd, "");
strcpy(struname, "");
while ( client.connected() ) {
if (authorized) return;
readString.toCharArray(strinput, MAXLEN);
// cstringarg( char* haystack, char* vname, char* sarg )
// haystack pattern: &varname=1234abc, delimiters &, \n, \0, SPACE, EOF
cstringarg(strinput, "uname", struname); // uname
cstringarg(strinput, "upwd", strupwd); // upwd
// debug
Serial.print("strupwd >>>"); Serial.print(strupwd); Serial.println("<<<");
Serial.print("website_upwd>>>"); Serial.print(website_upwd); Serial.println("<<<");
if ( (strlen(strupwd)==strlen(website_upwd) )
&& (strcmp(website_upwd, strupwd )==0)
)
{
authorized = true;
//debug
//Serial.print("check: authorized="); Serial.println(authorized);
readString = "";
return;
}
if ( client.available() ) {
char c = client.read();
//read char by request
if (readString.length() < TOKLEN) {
//store characters to string
readString += c;
Serial.println(c);
}
//if HTTP request has ended
if (c == '\n') {
client.flush();
//now output html data header
String script = "";
script += ("HTTP/1.1 401 Log-In Required");
script += ("Content-Type: text/html \n");
script += ("\n"); // do not forget this one //????
script += ("<!DOCTYPE html> \n");
script += ("<html> \n");
script += ("<head> \n");
// utf-8 für "°" Zeichen
script += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"> \n" ;
script += "<title>" ;
script += website_title ;
script += "</title> \n" ;
script += "</head> \n" ;
script += "<body> \n" ;
script += "<h1><p style=\"color:rgb(255,0,191);\"> " + (String)website_url ;
script += (String)": <wbr> <wbr> " + "Not authorized !</p> </h1> \n" ;
script += ("<h2><p style=\"color:rgb(255,0,191);\"> log in to proceed: </p> </h2> \n");
script += ("<FORM ACTION='/' method=GET > \n");
script += ("<h2>user name: <INPUT TYPE=text NAME='uname' VALUE='' MAXLENGTH='50'> </h2> \n");
script += ("<h2>password : <INPUT TYPE=PASSWORD NAME='upwd' VALUE='' MAXLENGTH='50'> </h2> \n");
script += ("<h2><INPUT TYPE=SUBMIT></h2> \n");
script += ("</FORM> \n");
script += ("<BR> \n");
script += ("</body> \n");
script += ("</html> \n");
client.print(script);
//stopping client
client.stop();
delay(1);
//clearing string for next read
readString = "";
}
}
delay(1);
}
}
//----------------------------------------------------------------------------
void handleWebsite() {
WiFiClient client = wifiserver.available();
//---------------------------------------
// Check if a client has connected
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
//---------------------------------------
// LogOut
if (request.indexOf("/logout") != -1) {
authorized = false;
return;
}
delay(1);
//---------------------------------------
// Return the response
String script = "";
// init website
script += ("HTTP/1.1 200 OK \n");
script += ("Content-Type: text/html \n");
script += ("\n"); // do not forget this one
script += ("<!DOCTYPE html> \n");
script += ("<html> \n");
// head + title
script += ("<head> \n");
// autom. Aktualisierung alle 20 sec.
script += "<meta http-equiv=\"refresh\" content=\"20; URL=";
script += (String)website_url + ":" + (String)http_port + "\"> \n" ;
// utf-8 für "°" Zeichen
script += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"> \n" ;
script += ("<title>");
script += (website_title);
script += ("</title> \n");
script += ("</head> \n");
// body + caption
script += ("<body> \n");
script += ("<h1> <p> ");
script += ("<font style=\"color:rgb(255,0,204);\"> HELLO WORLD! ");
script += (" <wbr> <wbr> ");
script += ("<font style=\"color:rgb(0,205,102);\"> Welcome to " + (String)website_url );
script += ("! </p> </h1> "); // script+= ("! </p> </h1> \n");
delay(1);
//---------------------------------------
script += "<h2> <br> \n HEIMSERVER <br> \n </h2>";
//---------------------------------------
// remote buttons Server
// <input type="button" value="submit" style="height: 100px; width: 100px; left: 250; top: 250;">
// <button style=\"height:200px;width:200px\"> </button>
client.print(script);
script = "";
//---------------------------------------
// sensors Server
// chart table
//---------------------------------------
// text font Courier, color black
script += ("<p> <font face=\"courier\"> "); // <<< Courier
script += "<h2> ";
script += "<p style=\"color:rgb(0,0,0);\" > </p> " ;
script += ("<br> \n");
script += "</h2>";
client.print(script);
script = "";
script += ("<br> \n");
// log out
script += ("<h3>Log Out: ");
script += ("<a href=\" /logout\"\"> <button style=\"height:70px;width:140px\" > Log Out </button></a> </h3> ");
script += WiFi.localIP().toString() +" "+ (String)ssid + " <br>" ;
script += "</font> </p> \n";
script += "</body> \n";
script += "</html> \n";
client.print(script);
delay(1);
}
//----------------------------------------------------------------------------
// handle root +clients
//----------------------------------------------------------------------------
void handleRoot() {
handleClients();
}
//----------------------------------------------------------------------------
void handleClients() {
double ftmp;
String msgtok;
//------------------------------------------
//client-Werte auch bei Url-Aufruf zurückgeben
String message = "*** ";
// re CLIENT 0
/*
message += (String)"&c0t1=" + c0t1.sact + "&c0h1=" + c0h1.sact;
message += (String)"&c0t2=" + c0t2.sact + "&c0h2=" + c0h2.sact;
message += "&c0out1=" + (String)c0out1 + "&c0out2=" + (String)c0out2 + "&c0out3=" + (String)c0out3 ;
*/
message += " ###";
//Serial.println(message);
lanserver.send(200, "text/plain", message);
}
// END OF FILE
Lesezeichen