#ifndef LWIP_POSIX_SOCKETS_IO_NAMES
#define LWIP_POSIX_SOCKETS_IO_NAMES 0
#endif
extern "C"
{
#include "esp_common.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
}
#include "fdvserial.h"
#include "fdvsync.h"
#include "fdvflash.h"
#include "fdvtask.h"
#include "fdvnetwork.h"
#include "user_config.h"
typedef struct {
char id[32];
int16_t mode;
char ssid[32];
char pwd[64];
char ip[32];
char nm[32];
char gw[32];
char host[32];
int16_t port;
uint32_t valid;
} user_config_t;
static user_config_t user_config;
const char ID[] = "ESP07";
const char SSID[] = "Test";
const char PASSWD[] = "test";
const char IP[] = "192.168.0.12";
const char NM[] = "255.255.255.0";
const char GW[] = "192.168.0.1";
const char HOST[] = "192.168.0.9";
const int PORT = 2323;
const char httpform[] = "<!DOCTYPE html><html><head>\
<meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\"></head>\
<title>ESP8266EX SETUP</title><body><h1>RFID reader setup</h1><form method=\"post\" action=\"\"><table>\
<tr><td></td><td><input type=\"hidden\" name=\"__begin\" value=\"begin\"></td></tr>\
<tr><td>Device ID:</td><td><input name=\"__devid\" maxlength=\"32\" SIZE=\"22\" value=\"1234\"></td></tr>\
<tr><td>Wi-fi mode:</td><td><select name=\"__mode\">\
<option value=\"0\">station</option><option value=\"1\">client</option></select></td></tr>\
<tr><td>SSID:</td><td><input name=\"__ssid\" maxlength=\"32\" SIZE=\"22\" value=\"ESP8266\"></td></tr>\
<tr><td>Password:</td><td><input name=\"__pwd\" maxlength=\"32\" SIZE=\"22\" value=\"00000000\"></td></tr>\
<tr><td>ip addr:</td><td><input name=\"__ip\" maxlength=\"32\" SIZE=\"22\" value=\"192.168.0.1\"></td></tr>\
<tr><td>netmask:</td><td><input name=\"__nm\" maxlength=\"32\" SIZE=\"22\" value=\"255.255.255.0\"></td></tr>\
<tr><td>gateway:</td><td><input name=\"__gw\" maxlength=\"32\" SIZE=\"22\" value=\"192.168.0.1\"></td></tr>\
<tr><td>srv ip addr:</td><td><input name=\"__host\" SIZE=\"10\" maxlength=\"32\" value=\"192.168.0.10\">\
port:<input name=\"__port\" SIZE=\"1\" maxlength=\"5\" value=\"2323\"></td></tr>\
<tr><td></td><td><input type=\"hidden\" name=\"__end\" value=\"end\"></td></tr>\
</table><input type=submit value=\"apply\"></form></body></html>";
//__id=&__mode=0&__ssid=dlink&__pwd=pass&ip=192.168.1.1&port=2323
//save form data to file system
//fdv::FlashFileSystem::save("def1", &dev_config, sizeof(MyConfig));
//load data
//fdv::FlashFileSystem::load("def1", &dev_config);
int ParseURLtoStr(char const *in, char *out, const char *searchstr, char delim, int maxLength)
{
//int32_t len = read(str1, bufsize);
char *data;
data = strstr( in, searchstr );
if(!data) return 0;
data += strlen(searchstr);
int i = 0;
while( (data[i] != delim) && (i < maxLength ))i++;
strncpy( out, data, i );
printf("%s => %s\n", searchstr, out);
return i;
}
int ParseURLtoInt( char const *in, int16_t out, const char *searchstr, char delim, int maxLength)
{
//int32_t len = read(str1, bufsize);
char *data;
data = strstr( in, searchstr );
if(!data) return 0;
data += strlen(searchstr);
int i = 0;
char pbuf[maxLength];
while( (data[i] != delim) && (i < maxLength))i++;
strncpy( pbuf, data, i );
out = atoi(pbuf);
printf("%s => %d\n", searchstr, out);
return i;
}
struct HttpHandler : public fdv::TCPConnectionHandler
{
void write_http(const char *http)
{
char httplen[32];
const char http200[] = "HTTP/1.1 200 OK\n";
const char httptype[]= "Content-Type: text/html\n\n";
sprintf(httplen, "%s%d%s", "Content-length: ", strlen(http), "\n");
write( const_cast<char*>(http200) );
write( const_cast<char*>(httplen));
write( const_cast<char*>(httptype ));
write( const_cast<char*>(http), strlen(http));
}
void ICACHE_FLASH_ATTR connectionHandler()
{
int len;
char *data = new char[128];
int i, j, m, n, x, y, z, s, w;
memset((void*)&user_config, 0, sizeof(user_config_t));
while (isConnected())
{
write_http(httpform);
len = read(data, 128);
i = ParseURLtoStr(data, user_config.id, "__devid=", '&', 16 );
j = ParseURLtoInt(data, user_config.mode, "__mode=", '&', 1 );
m = ParseURLtoStr(data, user_config.ssid, "__ssid=", '&', 32 );
n = ParseURLtoStr(data, user_config.pwd, "__pwd=", '&', 64 );
x = ParseURLtoStr(data, user_config.ip, "__ip=", '&', 16 );
y = ParseURLtoStr(data, user_config.nm, "__nm=", '&', 16 );
z = ParseURLtoStr(data, user_config.gw, "__gw=", '&', 16);
s = ParseURLtoStr(data, user_config.host, "__host=", '&', 16);
w = ParseURLtoInt(data, user_config.port, "__port=", '&', 4);
}
if(i && j && m && n && x && y && z && s && w)
fdv::FlashFileSystem::save("conf", &user_config, sizeof(user_config_t));
delete[] data;
//printf("disconnected\n\r");
}
};
struct Task1 : fdv::Task
{
Task1(fdv::Serial* serial)
: fdv::Task(400), m_serial(serial)
{
}
fdv::Serial* m_serial;
void ICACHE_FLASH_ATTR exec()
{
if ( fdv::FlashFileSystem::load("conf", &user_config))
{
fdv::WiFi::setMode(static_cast<fdv::WiFi::Mode>(user_config.mode));
if(user_config.mode == fdv::WiFi::Client)
{
fdv::WiFi::configureClient(user_config.ssid, user_config.pwd);
fdv::IP::configureStatic(fdv::IP::ClientNetwork, user_config.ip, user_config.nm, user_config.gw);
}
else
{
fdv::WiFi::configureAccessPoint(user_config.ssid, user_config.pwd, 9);
fdv::IP::configureStatic(fdv::IP::AccessPointNetwork, user_config.ip, user_config.nm, user_config.ip);
}
}
else
{
fdv::WiFi::setMode(fdv::WiFi::Client);
fdv::WiFi::configureClient(SSID, PASSWD);
fdv::IP::configureStatic(fdv::IP::ClientNetwork, IP, NM, GW);
}
//new fdv::TCPClient<MyTCPConnectionHandler>(2323);
new fdv::TCPServer<HttpHandler>(80);
while (1)
{
}
}
};
struct MainTask : fdv::Task
{
void ICACHE_FLASH_ATTR exec()
{
//fdv::DisableStdOut();
fdv::DisableWatchDog();
fdv::HardwareSerial serial(9600, 128);
Task1 task1(&serial);
suspend();
}
};
extern "C" void /*ICACHE_FLASH_ATTR*/ user_init(void)
{
new MainTask; // never destroy!
}