esp-Mobile phone-bidirectional communication mode

Source: Internet
Author: User

1 AP accepts phone

2 STA (password fixed) connection router, AP accepts phone

3 STA (password flexible) connection Router, AP accepts phone

1 AP accepts phone
#include <esp8266wifi.h> const char *ssid = "esp8266_666"; const char *password = "12345678"; Wifiserver server (8266); void Setup () {  serial.begin (115200);   serial.println ();   Serial.print ("Setting soft-ap ... ");    ipaddress softlocal (192,168,4,1);   IPAddress softgateway (192,168,4,1); IPAddress Softsubnet (255,255,255,0);  wifi.softapconfig (softlocal, Softgateway, softsubnet);   WiFi.softAP ( SSID, password);    ipaddress MyIP = Wifi.softapip ();   serial.print ("AP IP Address:");   serial.println (MyIP);  server.begin ()  serial.printf ("Web server started, open%s in a Web browser\n", Wifi.localip (). toString (). C_STR ()),  } void Loop () { wificlient client = server.available ();  if ( Client)   {    serial.println ("\n[client connected]");     while (client.connected ())     {//print serial data to tcp  if (Serial.available ()) {size_t len = serial.available ();    uint8_t Sbuf[len];      Serial.readbytes (Sbuf, Len);    Client.write (Sbuf, Len);        Delay (1); }//Print TCP data to serial       if (Client.available ())        {        string line = Client.readstringuntil (' \ R ');         serial.print (line);             }    }    delay (1);     client.stop ();     serial.println ("[Client disonnected]");   }  } 

  

2 STA (password fixed) connection router, AP accepts phone
/* Wifitelnettoserial-example Transparent UART to Telnet Server for esp8266 Copyright (c) Hristo Gochkov.  All rights reserved.   This file was part of the Esp8266wifi Library for Arduino environment. This library was free software; Can redistribute it and/or modify it under the terms of the GNU Lesser general public License as published by the Fr EE software Foundation;  Either version 2.1 of the License, or (at your option) any later version. This library was distributed in the hope that it'll be useful, but without any WARRANTY;  Without even the implied warranty of merchantability or FITNESS for A particular PURPOSE.  See the GNU Lesser general public License for more details. You should has received a copy of the GNU Lesser general public License along with this library; If not, write to the Free Software Foundation, Inc., Wuyi Franklin St, Fifth Floor, Boston, MA 02110-1301 usa*/#include & Lt Esp8266wifi.h>//how Many clients should be able to telnet to this Esp8266#define max_srv_clients 1const char* SSID = "**********"; const char* Password = "**********"; Wifiserver Server (23);  Wificlient serverclients[max_srv_clients];void Setup () {serial1.begin (115200);  Wifi.begin (SSID, password); Serial1.print ("\nconnecting to");  SERIAL1.PRINTLN (SSID);  uint8_t i = 0;  while (Wifi.status ()! = wl_connected && i++ <) delay (500); if (i = =) {Serial1.print ("Could not connect to");    SERIAL1.PRINTLN (SSID);  while (1) delay (500);  }//start UART and the server Serial.begin (115200);  Server.begin ();    Server.setnodelay (TRUE); Serial1.print ("ready!  Use ' Telnet ');  Serial1.print (Wifi.localip ()); Serial1.println ("to connect");}  void Loop () {uint8_t i; Check if there is any new clients if (Server.hasclient ()) {for (i = 0; i < max_srv_clients; i++) {//find fre e/disconnected spot if (!serverclients[i] | |!serverclients[i].connected ()) {if (Serverclients[i]) serverclient        S[i].stop (); Serverclients[i] = Server.available (); Serial1.print ("New client:");        Serial1.print (i);      Continue    }}//no free/disconnected spot so reject wificlient serverclient = server.available ();  Serverclient.stop (); }//check clients for data for (i = 0; i < max_srv_clients; i++) {if (Serverclients[i] && serverclients[i].        Connected ()) {if (serverclients[i].available ()) {//get data from the Telnet client and push it to the UART      while (Serverclients[i].available ()) Serial.write (Serverclients[i].read ());    }}}//check UART for Data if (serial.available ()) {size_t len = serial.available ();    uint8_t Sbuf[len];    Serial.readbytes (Sbuf, Len); Push UART data to all connected Telnet clients for (i = 0; i < max_srv_clients; i++) {if (Serverclients[i] &am        p;& serverclients[i].connected ()) {Serverclients[i].write (sbuf, Len);      Delay (1); }    }  }}

  

3 STA (password flexible) connection Router, AP accepts phone
/*** Date: 2017/09/19* function: WiFi lamp 8266 End *       Add smartconfig function * MCU rookie **/#include < esp8266wifi.h> #include <EEPROM.h>  #define max_srv_clients 3  //maximum simultaneous connections, that is, the number of devices you want to access, 8266tcpserver can only access five, hey # define LED 2#define Debug//Open Debug function   #ifdef debug#define debugprintln (message)      serial.println (message) #else # define DEBUGPRINTLN (message) #endif   #ifdef debug#define debugprint ( Message)     serial.print (message) #else # define DEBUGPRINT (message) #endif    const char* ssid     = "360wifilulu";        //XXXXXX-- When using, please modify the current WiFi ssidconst char* password = "6206908you11011010";        // XXXXXX--When using, please modify the WiFi password for your current    const unsigned long baud_rate = 115200;     & nbsp;            //serial Connection speedconst unsigned long http_timeout = 5000;               //Max Respone time from Server   wifiserver server (8266);//The port number you want, change it, Range 0-65535wificlient Serverclients[max_srv_clients]; //ssid and cipher struct struct Config_type{  char ssid[32] ;   char psw[64];};  config_type config;int flag = high;//default current off light    /*** @Desc initialization operation */void Setup () {   Serial.begin (baud_rate);   pinmode (led,output);   digitalwrite (LED, high);    if (!autoconfig ()) {    smartconfig ();     debugprint ("Connecting to");// Write a few hints, haha     debugprintln (SSID);     while (Wifi.status ()! = WL_CONNECTED) {     //This function is the WiFi connection status, back to the WiFi link status        delay ($);        debugprint (".");     }&nbsp( }     delay),   digitalwrite (LED, low),   debugprintln (" IP address: ");   debugprintln (Wifi.localip ());//wifi.localip () returns 8266 IP addresses obtained   server.begin ();   server.setnodelay (TRUE); //Add to normal}   /*** @Desc   main function */void loop () {   uint8_t i;  if (Server.hasclient ()) {        for (i = 0; i < max_srv_clients; i++) {            if (!serverClients[i] | | |! Serverclients[i].connected ()) {                 if (Serverclients[i]) serverclients[i].stop ()//Not connected, release                  serverclients[i] = server.available ();//Assign New                  continue;            }         }        //8266tcpserver can only access five   exceeds the need to release          wificlient serverclient = server.available ();         if (serverclient) {           Serverclient.stop ();        }  }      for (i = 0; i < max_srv_clients; i++) {        if (ServerClients[i] && serverclients[i].connected ()) {             //processing the data sent by the client             if (ServerClients[i] . Available ()) {                 while (Serverclients[i].avAilable ())                      serial.write (Serverclients[i].read ());             }        }   }     if (serial.available () >0) {      char ch = Serial.read ();   &NBSP;&NBSP;&NBSP;&NBSP;IF (ch = = ' 1 ') {        smartconfig ();         delay (+);         digitalwrite ( LED, low);         debugprintln ("IP Address:");         debugprintln (Wifi.localip ());//wifi.localip () return 8266 IP address obtained        }   }} /*** automatically connect 20s after automatically enter Smartconfig mode */bool autoConfig () {   Wifi.mode (Wifi_ap_sta);    //Set esp8266 working mode   wifi.begin ();   delay (2000);//Just start the module, delay stability    debugprintln ("autoconfiging ...");   for (int i=0;i<20;i++) {    int Wstatus = Wifi.status ();     if (wstatus = = wl_connected) {       Debugprintln ("AutoConfig Success");       debugprint ("SSID:");       debugprintln (Wifi.ssid (). C_STR ());       debugprint ("PSW:");       debugprintln (Wifi.psk (). C_STR ());       Return True;    }else{      debugprint (".");       delay (+);       flag =!flag;       digitalwrite (LED, flag);     }   }  debugprintln ( "AutoConfig faild!");  &nbsP;return false;}  /*** Open Smartconfig function */void smartconfig () {  wifi.mode (Wifi_sta);   delay (+);   debugprintln ("Wait for Smartconfig"),   //wait for distribution   wifi.beginsmartconfig ();   while (1) {    debugprint (".");     delay,     flag =!flag;    digitalwrite (LED , flag);          if (Wifi.smartconfigdone ()) {       //smartconfig Configuration Completed       debugprintln ("SmartConfig Success");       debugprint ("SSID:");       debugprintln ( Wifi.ssid (). C_STR ());       debugprint ("PSW:");       debugprintln (Wifi.psk (). C_STR ());       strcpy (Config.ssid, WiFi.SSID (). c_ STR ());       strcpy (CONFIG.PSW, WIFI.PSK (). C_STR ());       saveconfig ();       wifi.mode ( Wifi_ap_sta);    //Set esp8266 operating mode       wifi.setautoconnect ( true); //Set auto Connect       break;    }  }}   /* save parameters to Eeprom */void Saveconfig () {  eeprom.begin (1024x768);   uint8_t *p = (uint8_t*) ( &config);   for (int i = 0; i < sizeof (config); i++)   {    eeprom.write ( I, * (P + i));   }  eeprom.commit ();}   /*    loading parameters from EEPROM */void loadconfig () {  eeprom.begin (1024x768);   uint8 _t *p = (uint8_t*) (&config);   for (int i = 0; i < sizeof (config); i++) &NBSP;&NBSP;{&NBSP;&NBSP;&NBSP;&NB sp;* (p + i) = Eeprom.read (i);   }  eeprom.commit ();}

  

esp-Mobile phone-bidirectional communication mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.