boolean ESPHB::save_ip(String _ip,String *_return){ // Hàm check và lưu IP vào EEPROM (_ip: ip cần setup, *_return kết quả chuỗi IP trả về sau lưu) String _curent_ip=""; // khởi tạo ip tạm *_return=""; // trả về = "" IPAddress _ip_tosave; // khởi tạo struct IPAddress để lưu int _start=0; // vị trí xuất phát String IP int _dot=_ip.indexOf('.'); // vị trí dấu . đầu tiên int _ends=_ip.length(); // tổng chiều dài String IP for(char k=0;k<4;k++){ String ipk=_ip.substring(_start,_dot); // octet 1: từ vị trí xuất phát đến dấu . đầu tiên _ip_tosave[k]=ipk.toInt(); // convert String to Int _start=_dot+1; // dấu . đầu thành điểm xuất phát if(k<3){ _dot=_ip.indexOf('.',_start);} else{ // tại octet 4(octet cuối) điểm kết thúc là chiều dài String IP(ends) _dot=_ends;}; } WiFi.config(_ip_tosave,gateway,mask); // thiết lập lại địa chỉ IP IPtoString(WiFi.localIP(),&_curent_ip); // lấy địa chỉ IP sau khi thiết lập if(_curent_ip==_ip){ // nếu địa chỉ IP nhận được == địa chỉ IP setup save_ip(_ip_tosave,__IP_adrr); // lưu ip mới vào EEPROM for (unsigned char i = 0; i < 4; i++) { // Đọc IP vừa lưu từ EEPROM _ip_tosave[i] = read1byte_EEPROM(i + __IP_adrr + 1); } IPtoString(_ip_tosave,_return); // trả về String ip sau khi được lưu return true; // return true hàm }else{ *_return=_ip; // trả về String ip ban đầu đem setup return false; } }
/*------------------------------------------------------------------- TCP Server Task -------------------------------------------------------------------*/ void TcpServerTask(void * pd) { int ListenPort = (int) pd; // Set up the listening TCP socket int fdListen = listen(INADDR_ANY, ListenPort, 5); if (fdListen > 0) { IPADDR client_addr; WORD port; while(1) { // The accept() function will block until a TCP client requests // a connection. Once a client connection is accepting, the // file descriptor fdnet is used to read/write to it. iprintf( "Wainting for connection on port %d...\n", ListenPort ); int fdnet = accept(fdListen, &client_addr, &port, 0); iprintf("Connected to: "); ShowIP(client_addr); iprintf(":%d\n", port); writestring(fdnet, "Welcome to the NetBurner TCP Server\r\n"); char s[20]; IPtoString(EthernetIP, s); siprintf(RXBuffer, "You are connected to IP Address %s, port %d\r\n", s, TCP_LISTEN_PORT); writestring(fdnet, RXBuffer); while (fdnet > 0) { /* Loop while connection is valid. The read() function will return 0 or a negative number if the client closes the connection, so we test the return value in the loop. Note: you can also use ReadWithTimout() in place of read to enable the connection to terminate after a period of inactivity. */ int n = 0; do { n = read( fdnet, RXBuffer, RX_BUFSIZE ); RXBuffer[n] = '\0'; iprintf( "Read %d bytes: %s\n", n, RXBuffer ); } while ( n > 0 ); // Don't foreget to close ! iprintf("Closing client connection: "); ShowIP(client_addr); iprintf(":%d\n", port); close(fdnet); fdnet = 0; } } // while(1) } // while listen }
boolean ESPHB::wifi_connect(){ if(ssid!=""){ unsigned char _timeout=0; // khởi tạo biến timeout debug("Connecting to "); char _sta_ssid[(ssid).length()+1]; // chuyển đổi String ssid, password sang char array char _sta_password[(password).length()+1]; ssid.toCharArray(_sta_ssid, ssid.length()+1); password.toCharArray(_sta_password, password.length()+1); debug(_sta_ssid); debug(_sta_password); WiFi.begin(_sta_ssid, _sta_password); // kết nối tới mạng wifi while (WiFi.status() != WL_CONNECTED) { // nếu chưa kết nối được delay(500); if(isdebug){Serial.print(".");} // xuất ký tự . mỗi 0.5s _timeout++; // tăng timeout apmode=false; if(_timeout>__timeout_STA){ // nếu timeout > thời gian cho phép timeout_STA apmode=true; // bật cờ apmode break; } } if((!ipstatic)&&(!apmode)){ String _tmp=""; save_ip(WiFi.localIP(),__IP_adrr); save_ip(WiFi.gatewayIP(),__gateway_adrr); save_ip(WiFi.subnetMask(),__mask_adrr); IPtoString(WiFi.localIP(),&_tmp);debug("New IP: "+_tmp); IPtoString(WiFi.gatewayIP(),&_tmp);debug("New gateway: "+_tmp); IPtoString(WiFi.subnetMask(),&_tmp);debug("New submask: "+_tmp); update1byte_EEPROM(__ip_static,1); ipstatic=true; } else if(ipstatic&&(!apmode)){ WiFi.config(ip,gateway,mask); }; if(!apmode){ // nếu cờ apmode chưa bật( đã kết nối tới wifi) debug(""); debug("WiFi connected to:"); debug(_sta_ssid); } } return apmode; // trả về true nếu kết nối và false chưa kết nối };
void ESPHB::wifi_APconnect(defaults *_df){ String _tmp; char _ap_ssid[(_df->apssid).length()+1]; char _ap_password[(_df->appassword).length()+1]; (_df->apssid).toCharArray(_ap_ssid, (_df->apssid).length()+1); (_df->appassword).toCharArray(_ap_password, (_df->appassword).length()+1); apmode=true; WiFi.mode(WIFI_AP_STA); WiFi.softAP(_ap_ssid, _ap_password); debug("Cannot connected, you can connect to:"); IPtoString(WiFi.softAPIP(),&_tmp); debug(_tmp); }
boolean ESPHB::wifi_connect(String _ssid, String _password, String *_ip,String * _result){ unsigned char _timeout=0; String _tmp=""; boolean _receiveip=true; debug("Connecting to "); // convert String of ssid, password to array (as update_EEPROM) char _sta_ssid[(_ssid).length()+1]; char _sta_password[(_password).length()+1]; _ssid.toCharArray(_sta_ssid, _ssid.length()+1); _password.toCharArray(_sta_password, _password.length()+1); debug(_sta_ssid); debug(_sta_password); WiFi.begin(_sta_ssid, _sta_password); while (WiFi.status() != WL_CONNECTED) { delay(500); if(isdebug){Serial.print(".");} _timeout++; if(_timeout>__timeout_STA){ _receiveip=false; *_result="failed"; *_ip=""; break; } } if(_receiveip){ debug(""); debug("WiFi connected to:"); debug(_sta_ssid); IPtoString(WiFi.localIP(),&_tmp); save_ip(WiFi.localIP(),__IP_adrr); save_ip(WiFi.gatewayIP(),__gateway_adrr); save_ip(WiFi.subnetMask(),__mask_adrr); update1byte_EEPROM(__ip_static,1); debug("IP: "+_tmp); *_ip=_tmp; *_result="connected"; save(__ssid,_ssid); save(__password,_password); } return !_receiveip; }
/*------------------------------------------------------------------- TCP Server Task -------------------------------------------------------------------*/ void TcpServerTask(void * pd) { int ListenPort = (int) pd; // Set up the listening TCP socket int fdListen = listen(INADDR_ANY, ListenPort, 5); if (fdListen > 0) { IPADDR client_addr; WORD port; while(1) { // The accept() function will block until a TCP client requests // a connection. Once a client connection is accepting, the // file descriptor fdnet is used to read/write to it. iprintf( "Wainting for connection on port %d...\n", ListenPort ); int fdnet = accept(fdListen, &client_addr, &port, 0); iprintf("Connected to: "); ShowIP(client_addr); iprintf(":%d\n", port); writestring(fdnet, "Welcome to the NetBurner TCP Server\r\n"); char s[20]; IPtoString(EthernetIP, s); siprintf(RXBuffer, "You are connected to IP Address %s, port %d\r\n", s, TCP_LISTEN_PORT); writestring(fdnet, RXBuffer); int tcp_timeout = 0; while (fdnet > 0) { // declare file descriptor sets fd_set read_fds; fd_set error_fds; // Clear all bits in fd sets FD_ZERO(&read_fds); FD_ZERO(&error_fds); // Set bits in fd sets for our fd, fdnet FD_SET(fdnet, &read_fds); FD_SET(fdnet, &error_fds); // Select will block until an event occurs and a fd set bit is // set, or a timeout occurs. A timeout is ok in this situation, // we just use it to illustrate the task is still running. if (select(FD_SETSIZE, &read_fds,(fd_set *)0, &error_fds, 30 * TICKS_PER_SECOND)) { if (FD_ISSET(fdnet,&read_fds)) // Network data available to be read { int n = read(fdnet, RXBuffer, RX_BUFSIZE); RXBuffer[n] = '\0'; iprintf("Read %d bytes: %s\n", n, RXBuffer); //writestring(fdnet, RXBuffer); } if (FD_ISSET(fdnet, &error_fds) && !FD_ISSET(fdnet, &read_fds)) // Network error condition { iprintf("Network Error, closing socket\n"); close(fdnet); fdnet = 0; } } else { // Select timed out. If timeout exceeds 10 timeout // periods, the connection will be closed. iprintf("select() timeout\n"); tcp_timeout++; if (tcp_timeout > 10) { iprintf("Connection timed out, closing socket\n"); close(fdnet); fdnet = 0; } } // Select } // While fdnet is valid } // while(1) } // while listen }