From 364e37f5badd22e508678bcf38c73fd7cdbd7dc8 Mon Sep 17 00:00:00 2001 From: luc Date: Wed, 13 May 2015 21:58:26 +0800 Subject: [PATCH] Update EEPROM content for new settings Modify EEPROM content to reflect the possible settings Add a buffer format function to query/write EEPROM instead of always using string function to save space. Update Reset/Print Configuration with new settings Code Cleaning Some settings stay default as no real add value currently to allow to change them --- esp8266/config.cpp | 63 +++++++++++++++++++++++------ esp8266/config.h | 78 +++++++++++++++++++++++------------- esp8266/esp8266.ino | 23 +++++------ esp8266/webinterface.cpp | 2 +- esp8266/wifi.cpp | 85 +++++++++++++++++++++------------------- esp8266/wifi.h | 7 ++-- 6 files changed, 161 insertions(+), 97 deletions(-) diff --git a/esp8266/config.cpp b/esp8266/config.cpp index c29b8783..3a64af32 100644 --- a/esp8266/config.cpp +++ b/esp8266/config.cpp @@ -20,8 +20,8 @@ #include "config.h" #include -//read a string or a unique byte/flag -//a flag is 1 byte, a string is multibyte + \0, this is won't work if 1 char is multibyte like chinese char +//read a string +//a string is multibyte + \0, this is won't work if 1 char is multibyte like chinese char bool CONFIG::read_string(word pos, char byte_buffer[], word size_max) { //check if parameters are acceptable @@ -43,6 +43,22 @@ bool CONFIG::read_string(word pos, char byte_buffer[], word size_max) if (b!=0)byte_buffer[i-1]=0x00; return true; } + +//read a buffer of size_buffer +bool CONFIG::read_buffer(word pos, byte byte_buffer[], word size_buffer) +{ + //check if parameters are acceptable + if (size_buffer==0 || pos+size_buffer > EEPROM_SIZE || byte_buffer== NULL)return false; + word i=0; + //read until max size is reached + while (i EEPROM_SIZE || byte_buffer== NULL)return false; + //copy the value(s) + for (word i = 0; i < size_buffer; i++) { + EEPROM.write(pos + i, byte_buffer[i]); + } + EEPROM.commit(); + return true; +} + //read a flag / byte bool CONFIG::write_byte(word pos, const byte value) { @@ -84,23 +113,35 @@ bool CONFIG::reset_config() if(!CONFIG::write_string(EP_SSID,DEFAULT_SSID,strlen(DEFAULT_SSID)))return false; if(!CONFIG::write_string(EP_PASSWORD,DEFAULT_PASSWORD,strlen(DEFAULT_PASSWORD)))return false; if(!CONFIG::write_byte(EP_IP_MODE,DEFAULT_IP_MODE))return false; - if(!CONFIG::write_string(EP_IP_VALUE,DEFAULT_IP_VALUE,strlen(DEFAULT_IP_VALUE)))return false; - if(!CONFIG::write_string(EP_MASK_VALUE,DEFAULT_MASK_VALUE,strlen(DEFAULT_MASK_VALUE)))return false; - if(!CONFIG::write_string(EP_GATEWAY_VALUE,DEFAULT_GATEWAY_VALUE,strlen(DEFAULT_GATEWAY_VALUE)))return false; - if(!CONFIG::write_string(EP_BAUD_RATE,DEFAULT_BAUD_RATE,strlen(DEFAULT_BAUD_RATE)))return false; + if(!CONFIG::write_buffer(EP_IP_VALUE,DEFAULT_IP_VALUE,IP_LENGH))return false; + if(!CONFIG::write_buffer(EP_MASK_VALUE,DEFAULT_MASK_VALUE,IP_LENGH))return false; + if(!CONFIG::write_buffer(EP_GATEWAY_VALUE,DEFAULT_GATEWAY_VALUE,IP_LENGH))return false; + if(!CONFIG::write_buffer(EP_BAUD_RATE,(const byte *)&DEFAULT_BAUD_RATE,BAUD_LENGH))return false; + if(!CONFIG::write_byte(EP_PHY_MODE,DEFAULT_PHY_MODE))return false; + if(!CONFIG::write_byte(EP_SLEEP_MODE,DEFAULT_SLEEP_MODE))return false; + if(!CONFIG::write_byte(EP_CHANNEL,DEFAULT_CHANNEL))return false; + if(!CONFIG::write_byte(EP_AUTH_TYPE,DEFAULT_AUTH_TYPE))return false; + if(!CONFIG::write_byte(EP_SSID_VISIBLE,DEFAULT_SSID_VISIBLE))return false; return true; } void CONFIG::print_config() { - char sbuf[70]; + //use bigest size for buffer + char sbuf[MAX_PASSWORD_LENGH+1]; byte bbuf=0; + int ibuf=0; if (CONFIG::read_byte(EP_WIFI_MODE, &bbuf ))Serial.println(byte(bbuf)); if (CONFIG::read_string(EP_SSID, sbuf , MAX_SSID_LENGH))Serial.println(sbuf); if (CONFIG::read_string(EP_PASSWORD, sbuf , MAX_PASSWORD_LENGH))Serial.println(sbuf); if (CONFIG::read_byte(EP_IP_MODE, &bbuf ))Serial.println(byte(bbuf)); - if (CONFIG::read_string(EP_IP_VALUE, sbuf , MAX_IP_LENGH))Serial.println(sbuf); - if (CONFIG::read_string(EP_MASK_VALUE, sbuf , MAX_IP_LENGH))Serial.println(sbuf); - if (CONFIG::read_string(EP_GATEWAY_VALUE, sbuf , MAX_IP_LENGH))Serial.println(sbuf); - if (CONFIG::read_string(EP_BAUD_RATE, sbuf , MAX_BAUD_LENGH))Serial.println(sbuf); + if (CONFIG::read_buffer(EP_IP_VALUE,(byte *)sbuf , IP_LENGH))Serial.println(wifi_config.ip2str((byte *)sbuf)); + if (CONFIG::read_buffer(EP_MASK_VALUE, (byte *)sbuf , IP_LENGH))Serial.println(wifi_config.ip2str((byte *)sbuf)); + if (CONFIG::read_buffer(EP_GATEWAY_VALUE, (byte *)sbuf , IP_LENGH))Serial.println(wifi_config.ip2str((byte *)sbuf)); + if (CONFIG::read_buffer(EP_BAUD_RATE, (byte *)&ibuf , BAUD_LENGH))Serial.println(ibuf); + if (CONFIG::read_byte(EP_PHY_MODE, &bbuf ))Serial.println(byte(bbuf)); + if (CONFIG::read_byte(EP_SLEEP_MODE, &bbuf ))Serial.println(byte(bbuf)); + if (CONFIG::read_byte(EP_CHANNEL, &bbuf ))Serial.println(byte(bbuf)); + if (CONFIG::read_byte(EP_AUTH_TYPE, &bbuf ))Serial.println(byte(bbuf)); + if (CONFIG::read_byte(EP_SSID_VISIBLE, &bbuf ))Serial.println(byte(bbuf)); } diff --git a/esp8266/config.h b/esp8266/config.h index a65434de..092ab50e 100644 --- a/esp8266/config.h +++ b/esp8266/config.h @@ -18,61 +18,83 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +//comment to disable +#define MDNS_FEATURE + #ifndef CONFIG_h #define CONFIG_h #include +#include "wifi.h" +extern "C" { +#include "user_interface.h" +} //version and sources location #define FW_VERSION "V0.1" #define REPOSITORY "https://github.com/luc-github/ESP8266" -#define MDNS_FEATURE 1 + //pin used to reset setting #define RESET_CONFIG_PIN 2 //flags -#define AP_MODE 1 -#define CLIENT_MODE 2 -#define DHCP_MODE 1 -#define STATIC_IP_MODE 2 +#define AP_MODE 1 +#define CLIENT_MODE 2 +#define DHCP_MODE 1 +#define STATIC_IP_MODE 2 //position in EEPROM //AP mode = 1; Station client mode = 2 -#define EP_WIFI_MODE 0 //1 byte = flag -#define EP_SSID 1 //33 bytes 32+1 = string ; warning does not support multibyte char like chinese -#define EP_PASSWORD 34 //65 bytes 64 +1 = string ;warning does not support multibyte char like chinese -#define EP_IP_MODE 99 //1 byte = flag -#define EP_IP_VALUE 100 //16 bytes xxx.xxx.xxx\0 = string -#define EP_MASK_VALUE 116 //16 bytes xxx.xxx.xxx\0 = string -#define EP_GATEWAY_VALUE 132 //16 bytes xxx.xxx.xxx\0 = string -#define EP_BAUD_RATE 151 //7 bytes = string (if integer value => save 4 bytes but need to create new integer function for eeprom that will take more than 4 bytes) +#define EP_WIFI_MODE 0 //1 byte = flag +#define EP_SSID 1 //33 bytes 32+1 = string ; warning does not support multibyte char like chinese +#define EP_PASSWORD 34 //65 bytes 64 +1 = string ;warning does not support multibyte char like chinese +#define EP_IP_MODE 99 //1 byte = flag +#define EP_IP_VALUE 100 //4 bytes xxx.xxx.xxx.xxx +#define EP_MASK_VALUE 104 //4 bytes xxx.xxx.xxx.xxx +#define EP_GATEWAY_VALUE 108 //4 bytes xxx.xxx.xxx.xxx +#define EP_BAUD_RATE 112 //4 bytes = int +#define EP_PHY_MODE 116 //1 byte = flag +#define EP_SLEEP_MODE 117 //1 byte = flag +#define EP_CHANNEL 118 //1 byte = flag +#define EP_AUTH_TYPE 119 //1 byte = flag +#define EP_SSID_VISIBLE 120 //1 byte = flag //default values -#define DEFAULT_WIFI_MODE AP_MODE -const char DEFAULT_SSID [] PROGMEM = "ESP8266"; -const char DEFAULT_PASSWORD [] PROGMEM = "12345678"; -#define DEFAULT_IP_MODE STATIC_IP_MODE -const char DEFAULT_IP_VALUE[] PROGMEM = "192.168.0.1"; -const char DEFAULT_MASK_VALUE[] PROGMEM = "255.255.255.0"; -#define DEFAULT_GATEWAY_VALUE DEFAULT_IP_VALUE -const char DEFAULT_BAUD_RATE[] PROGMEM = "9600"; -#if MDNS_FEATURE -const char LOCAL_NAME[] PROGMEM = "esp8266"; +#define DEFAULT_WIFI_MODE AP_MODE +const char DEFAULT_SSID [] PROGMEM = "ESP8266"; +const char DEFAULT_PASSWORD [] PROGMEM = "12345678"; +#define DEFAULT_IP_MODE STATIC_IP_MODE +const byte DEFAULT_IP_VALUE[] PROGMEM = {192,168,0,1}; +const byte DEFAULT_MASK_VALUE[] PROGMEM = {255,255,255,0}; +#define DEFAULT_GATEWAY_VALUE DEFAULT_IP_VALUE +const int DEFAULT_BAUD_RATE = 9600; +#ifdef MDNS_FEATURE +const char LOCAL_NAME[] PROGMEM = "esp8266"; #endif +#define DEFAULT_PHY_MODE PHY_MODE_11G +#define DEFAULT_SLEEP_MODE MODEM_SLEEP_T +#define DEFAULT_CHANNEL 11 +#define DEFAULT_AUTH_TYPE AUTH_WPA_PSK +#define DEFAULT_SSID_VISIBLE 1 +#define DEFAULT_MAX_CONNECTIONS 4 +#define DEFAULT_BEACON_INTERVAL 100 -#define EEPROM_SIZE 256 //max is 512 -#define MAX_SSID_LENGH 32 -#define MAX_PASSWORD_LENGH 64 -#define MAX_IP_LENGH 17 -#define MAX_BAUD_LENGH 6 +//sizes +#define EEPROM_SIZE 256 //max is 512 +#define MAX_SSID_LENGH 32 +#define MAX_PASSWORD_LENGH 64 +#define IP_LENGH 4 +#define BAUD_LENGH 4 class CONFIG { public: static bool read_string(word pos, char byte_buffer[], word size_max); + static bool read_buffer(word pos, byte byte_buffer[], word size_buffer); static bool read_byte(word pos, byte * value); static bool write_string(word pos, const char * byte_buffer, word size_buffer); + static bool write_buffer(word pos, const byte * byte_buffer, word size_buffer); static bool write_byte(word pos, const byte value); static bool reset_config(); static void print_config(); diff --git a/esp8266/esp8266.ino b/esp8266/esp8266.ino index 7e38f3ca..1e4078f8 100644 --- a/esp8266/esp8266.ino +++ b/esp8266/esp8266.ino @@ -37,7 +37,7 @@ #include #include #include -#if MDNS_FEATURE +#ifdef MDNS_FEATURE #include #endif extern "C" { @@ -53,24 +53,21 @@ void setup() { pinMode(RESET_CONFIG_PIN, INPUT); if (digitalRead(RESET_CONFIG_PIN)==0)breset_config=true;//if requested =>reset settings //default baud rate - word baud_rate; - char sbuf[7]; + int baud_rate=0; //check if EEPROM has value - if ( CONFIG::read_string(EP_BAUD_RATE, sbuf , MAX_BAUD_LENGH)) + if ( CONFIG::read_buffer(EP_BAUD_RATE, (byte *)&baud_rate , BAUD_LENGH)) { - word baud_tmp = atoi(sbuf); //check if baud value is one of allowed ones - if (baud_tmp==9600 || baud_tmp==19200 ||baud_tmp==38400 ||baud_tmp==57600 ||baud_tmp==115200 ||baud_tmp==230400) baud_rate=baud_tmp; - else breset_config=true;//baud rate is incorrect =>reset settings + if ( ! (baud_rate==9600 || baud_rate==19200 ||baud_rate==38400 ||baud_rate==57600 ||baud_rate==115200 ||baud_rate==230400) )breset_config=true;//baud rate is incorrect =>reset settings } - else breset_config=true;//cannot access to config settings=> reset settings + else breset_config=true;//cannot access to config settings=> reset settings //reset is requested if(breset_config) { //update EEPROM with default settings CONFIG::reset_config(); //use default baud rate - baud_rate=atol(DEFAULT_BAUD_RATE); + baud_rate=DEFAULT_BAUD_RATE; } //setup serial Serial.begin(baud_rate); @@ -85,14 +82,12 @@ void setup() { //main loop void loop() { -#if MDNS_FEATURE - // Check for any mDNS queries and send responses -wifi_config.mdns.update(); +#ifdef MDNS_FEATURE + // Check for any mDNS queries and send responses + wifi_config.mdns.update(); #endif //web requests web_interface.WebServer.handleClient(); //TODO use a method to handle serial also in class and call it instead of this one data_interface.WebServer.handleClient(); - - } diff --git a/esp8266/webinterface.cpp b/esp8266/webinterface.cpp index 254a78ed..c5d06bc9 100644 --- a/esp8266/webinterface.cpp +++ b/esp8266/webinterface.cpp @@ -92,7 +92,7 @@ void handle_web_interface_root() LABEL_UNITS(sbuf,F("CPU Frequency: "),system_get_cpu_freq(),F("Hz")) LABEL_UNITS(sbuf,F("Free Memory: "),String(system_get_free_heap_size()),F(" octets")) LABEL(sbuf,F("SDK Version: "),system_get_sdk_version()) - #if MDNS_FEATURE + #ifdef MDNS_FEATURE sstatus = F("http://"); sstatus+=LOCAL_NAME; LABEL_UNITS(sbuf,F("mDNS name: "),sstatus,F(".local")) diff --git a/esp8266/wifi.cpp b/esp8266/wifi.cpp index 5e277f2d..7fc83d70 100644 --- a/esp8266/wifi.cpp +++ b/esp8266/wifi.cpp @@ -22,7 +22,7 @@ #include "config.h" #include "ESP8266WiFi.h" #include "IPAddress.h" -#if MDNS_FEATURE +#ifdef MDNS_FEATURE #include #endif extern "C" { @@ -71,42 +71,54 @@ char * WIFI_CONFIG::ip2str(IPAddress Ip ) //Read configuration settings and apply them bool WIFI_CONFIG::Setup() { - byte bbuf; - char pwd[65]; - char sbuf[35]; + char pwd[MAX_PASSWORD_LENGH+1]; + char sbuf[MAX_SSID_LENGH+1]; int wstatus; - byte ip[4]={0,0,0,0}; - IPAddress currentIP; + IPAddress currentIP; + byte bflag=0; //AP or client ? - if (!CONFIG::read_byte(EP_WIFI_MODE, &bbuf ) || !CONFIG::read_string(EP_SSID, sbuf , MAX_SSID_LENGH) ||!CONFIG::read_string(EP_PASSWORD, pwd , MAX_PASSWORD_LENGH)) return false; + if (!CONFIG::read_byte(EP_WIFI_MODE, &bflag ) || !CONFIG::read_string(EP_SSID, sbuf , MAX_SSID_LENGH) ||!CONFIG::read_string(EP_PASSWORD, pwd , MAX_PASSWORD_LENGH)) return false; //disconnect if connected WiFi.disconnect(); - bbuf=AP_MODE; //this is AP mode - if (bbuf==AP_MODE) + if (bflag==AP_MODE) { + //setup Soft AP WiFi.mode(WIFI_AP); WiFi.softAP(sbuf, pwd); - - struct softap_config apconfig; + //setup PHY_MODE + if (!CONFIG::read_byte(EP_PHY_MODE, &bflag ))return false; + wifi_set_phy_mode((phy_mode)bflag); + //get current config + struct softap_config apconfig; wifi_softap_get_config(&apconfig); - apconfig.channel=11; - //apconfig.authmode=AUTH_OPEN; - apconfig.ssid_hidden=0; - apconfig.max_connection=4; - apconfig.beacon_interval=100; - wifi_set_phy_mode(PHY_MODE_11G); + //set the chanel + if (!CONFIG::read_byte(EP_CHANNEL, &bflag ))return false; + apconfig.channel=bflag; + //set Authentification type + if (!CONFIG::read_byte(EP_AUTH_TYPE, &bflag ))return false; + apconfig.authmode=(AUTH_MODE)bflag; + //set the visibility of SSID + if (!CONFIG::read_byte(EP_SSID_VISIBLE, &bflag ))return false; + apconfig.ssid_hidden=!bflag; + //no need to add these settings to configuration just use default ones + apconfig.max_connection=DEFAULT_MAX_CONNECTIONS; + apconfig.beacon_interval=DEFAULT_BEACON_INTERVAL; + //apply settings to current and to default if (!wifi_softap_set_config(&apconfig))Serial.println(F("Error Wifi AP")); if (!wifi_softap_set_config_current(&apconfig))Serial.println(F("Error Wifi AP")); wifi_softap_dhcps_start(); - wifi_set_phy_mode(PHY_MODE_11G); } else - { + {//setup station mode WiFi.mode(WIFI_STA); WiFi.begin(sbuf, pwd); + //setup PHY_MODE + if (!CONFIG::read_byte(EP_PHY_MODE, &bflag ))return false; + wifi_set_phy_mode((phy_mode)bflag); byte i=0; + //try to connect while (WiFi.status() != WL_CONNECTED && i<40) { delay(500); Serial.println(WiFi.status()); @@ -114,43 +126,36 @@ bool WIFI_CONFIG::Setup() } } //DHCP or Static IP ? - if (!CONFIG::read_byte(EP_IP_MODE, &bbuf )) return false; - if (bbuf==STATIC_IP_MODE) + if (!CONFIG::read_byte(EP_IP_MODE, &bflag )) return false; + if (bflag==STATIC_IP_MODE) { //get the IP - if (!CONFIG::read_string(EP_IP_VALUE, sbuf , MAX_IP_LENGH))return false; - //split in 4 parts - split_ip (sbuf,ip); - IPAddress local_ip (ip[0],ip[1],ip[2],ip[3]); + if (!CONFIG::read_buffer(EP_IP_VALUE,(byte *)sbuf , IP_LENGH))return false; + IPAddress local_ip (sbuf[0],sbuf[1],sbuf[2],sbuf[3]); //get the gateway - if (!CONFIG::read_string(EP_GATEWAY_VALUE, sbuf , MAX_IP_LENGH))return false; - //split in 4 parts - split_ip (sbuf,ip); - IPAddress gateway (ip[0],ip[1],ip[2],ip[3]); + if (!CONFIG::read_buffer(EP_GATEWAY_VALUE,(byte *)sbuf , IP_LENGH))return false; + IPAddress gateway (sbuf[0],sbuf[1],sbuf[2],sbuf[3]); //get the mask - if (!CONFIG::read_string(EP_MASK_VALUE, sbuf , MAX_IP_LENGH))return false; - //split in 4 parts - split_ip (sbuf,ip); - IPAddress subnet (ip[0],ip[1],ip[2],ip[3]); + if (!CONFIG::read_buffer(EP_MASK_VALUE,(byte *)sbuf , IP_LENGH))return false; + IPAddress subnet (sbuf[0],sbuf[1],sbuf[2],sbuf[3]); //apply according active wifi mode if (wifi_get_opmode()==WIFI_AP || wifi_get_opmode()==WIFI_AP_STA) WiFi.softAPConfig( local_ip, gateway, subnet); else WiFi.config( local_ip, gateway, subnet); } - #if MDNS_FEATURE + #ifdef MDNS_FEATURE //Get IP if (wifi_get_opmode()==WIFI_STA)currentIP=WiFi.localIP(); else currentIP=WiFi.softAPIP(); // Set up mDNS responder: - // - first argument is the domain name, in this example - // the fully-qualified domain name is "esp8266.local" - // - second argument is the IP address to advertise - // we send our IP address on the WiFi network - // Note: for AP mode we would use WiFi.softAPIP()! + // - first argument is the domain name, in this example + // the fully-qualified domain name is "esp8266.local" + // - second argument is the IP address to advertise + // we send our IP address on the WiFi network + // Note: for AP mode we would use WiFi.softAPIP()! if (!mdns.begin(LOCAL_NAME, currentIP)) { Serial.println(F("Error setting up MDNS responder!")); } #endif - CONFIG::print_config(); return true; } diff --git a/esp8266/wifi.h b/esp8266/wifi.h index 554a2b74..fc201115 100644 --- a/esp8266/wifi.h +++ b/esp8266/wifi.h @@ -24,7 +24,8 @@ #include "config.h" #include "IPAddress.h" #include -#if MDNS_FEATURE + +#ifdef MDNS_FEATURE #include #endif @@ -32,8 +33,8 @@ class WIFI_CONFIG { public: // multicast DNS responder feature - #if MDNS_FEATURE - MDNSResponder mdns; + #ifdef MDNS_FEATURE + MDNSResponder mdns; #endif bool Setup(); char * mac2str(uint8_t mac [WL_MAC_ADDR_LENGTH]);