Node Mcu ESP12E can't works both station mode and access point

i am new to nodemcu esp12e,Actually i want to set esp both STA+AP mode.i tried searching web but icould’nt found it.If someone knows please help me out

This is what I use:

#define DEBUG_Serial Serial

const char *APssid         = "ESP8266 Access Point";
const char *APpassword     = "thereisnospoon";

const char *ssid         = "My-WiFi";
const char *password     = "password123";

const char* WiFiHostname = "ESP8266";      // Host name of the device
IPAddress apIP(192,168,1,1);               // The IP address of the access point

boolean reset() {
  DEBUG_Serial.println("Failed.\r\nRebooting ...");
  DEBUG_Serial.flush();
  ESP.reset();
}

void startWiFi() {
  WiFi.hostname(WiFiHostname);
  WiFi.mode(WIFI_AP_STA);
  // AP
  DEBUG_Serial.println("Starting Access Point ...");
  WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
  WiFi.softAP(APssid, APpassword) || reset();             // Start the access point
  DEBUG_Serial.print("Access Point \"");
  DEBUG_Serial.print(APssid);
  DEBUG_Serial.println("\" started\r\n");
  // Station
  DEBUG_Serial.print("Connecting to ");
  DEBUG_Serial.print(ssid); DEBUG_Serial.println(" ...");
  WiFi.begin(ssid, password) || reset();            // Connect to the network
}

void printIP() {
  static boolean printed = false;
  if (WiFi.status() == WL_CONNECTED) {
    if (printed)
      return;
    DEBUG_Serial.println("Connected");
    DEBUG_Serial.print("IP address:\t");
    DEBUG_Serial.println(WiFi.localIP());
    printed = true;
  } else {
    printed = false;
  }
}

void printStations() {
  static int prevNumber = 0;
  if (WiFi.softAPgetStationNum() != prevNumber) {
    prevNumber = WiFi.softAPgetStationNum();
    DEBUG_Serial.print(prevNumber);
    DEBUG_Serial.println(" station(s) connected");
  }
}

void setup() {
  DEBUG_Serial.begin(115200);
  startWiFi();
}

void loop() {
  printIP();
  printStations();
}

You could check out my Beginner's guide to the ESP8266 to get started.

Pieter

thanks for sharing the code.. i want to control my nodemcu esp12e both online and also offline.if internet is disconnected then it has to act as offline control.How can i do that??

What do you mean ‘control’? You haven’t told us what you want to do with it.

i want to control gpio pins via both online and also offline.so now i successfully controlled both online and also offline(like webserver).i want both mode in esp.if esp is not connected to internet then it has to act as offline

There is absolutely no difference between being ‘online’ or ‘offline’, because everything happens on the local network. The only difference is the IP address. You can use mDNS for the station (online) situation, and DNS for the AP (offline) situation.

Show us your code if you want more help, read How to use this Forum first, use auto-format (CTRL+T in the IDE) and use [code] tags (</>).
Then read the article I linked to in reply #1 to get an understanding of the different networking concepts involved.

Pieter

this is the code which i used for getting json data from server

#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
const char* ssid     = "xxxxxx";
const char* password = "22395966";
char in;
char testChar[500],json[152];
String anbu,anbu1,pubString;
int start, end,j=0;
const char* host = "api.xxxxx.in";//api.devicehub.net
void setup() {
  Serial.begin(115200);
pinMode(16,OUTPUT);
pinMode(5,OUTPUT);
pinMode(4,OUTPUT);
pinMode(0,OUTPUT);
pinMode(2,OUTPUT);
pinMode(14,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
pinMode(15,OUTPUT);
pinMode(3,OUTPUT);
digitalWrite(16,LOW);
digitalWrite(5,LOW);
digitalWrite(4,LOW);
digitalWrite(0,LOW);
digitalWrite(2,LOW);
digitalWrite(14,LOW);
digitalWrite(12,LOW);
digitalWrite(13,LOW);
digitalWrite(15,LOW);
digitalWrite(3,LOW);
  delay(100);
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP()); // This is your NodeMCU IP address. Could be handy for other projects
 // GET();  // If all you need to do is push data, you don't need this
}

void loop()
{
  GET(); // This is empty because in order for the sleep cycle to work all code is in setup()
//POST();
}
void GET(void)
{
  delay(5000);

  Serial.print("connecting to ");
  Serial.println(host);
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  Serial.print("Requesting GET: ");
  // This will send the request to the server
  client.println("GET /esp/status/?format=json HTTP/1.1");
  client.println("api.devicehub.in");

  client.println("Connection: close");
  client.println();
  delay(500);
  bool begin = false;
  // Read all the lines of the reply from server and print them to Serial
  while (client.available()) {
    if (client.find('['))
    {
      anbu = client.readStringUntil(']');
    }
  }
 anbu="["+anbu+"]";
 Serial.println(anbu);
StaticJsonBuffer<1200> jsonBuffer;
    JsonArray& root = jsonBuffer.parseArray(anbu);

    if (!root.success())
    {
      Serial.println("parseArray() failed");
      return;
    }
     int j = 0, id, ss,rr;
     for (j=0;j<11;j++)
     {
    id = root.get(j)["id"];
    ss = root.get(j)["status"];
    rr = root.get(j)["name"];
if ((id==1)&&(ss==0))
{
  digitalWrite(16,LOW);
  Serial.println("first LOW");
}
if((id==1)&&(ss==1))
{
   digitalWrite(16,HIGH);
   Serial.println("first high");
}
if ((id==2)&&(ss==0))
{
  digitalWrite(5,LOW);
  Serial.println("second LOW");
}
if((id==2)&&(ss==1))
{
   digitalWrite(5,HIGH);
   Serial.println("second high");
}
if ((id==3)&&(ss==0))
{
  digitalWrite(4,LOW);
  Serial.println("third LOW");
}
if((id==3)&&(ss==1))
{
   digitalWrite(4,HIGH);
   Serial.println("third high");
}
if ((id==4)&&(ss==0))
{
  digitalWrite(0,LOW);
  Serial.println("4th LOW");
}
if((id==4)&&(ss==1))
{
   digitalWrite(0,HIGH);
   Serial.println("4th high");
}
if ((id==5)&&(ss==0))
{
  digitalWrite(2,LOW);
  Serial.println("5th LOW");
}
if((id==5)&&(ss==1))
{
   digitalWrite(2,HIGH);
   Serial.println("5th high");
}
if ((id==6)&&(ss==0))
{
  digitalWrite(14,LOW);
  Serial.println("6th LOW");
}
if((id==6)&&(ss==1))
{
   digitalWrite(14,HIGH);
   Serial.println("6th high");
}
if ((id==7)&&(ss==0))
{
  digitalWrite(12,LOW);
  Serial.println("7th LOW");
}
if((id==7)&&(ss==1))
{
   digitalWrite(12,HIGH);
   Serial.println("7th high");
}
if ((id==8)&&(ss==0))
{
  digitalWrite(13,LOW);
  Serial.println("8th LOW");
}
if((id==8)&&(ss==1))
{
   digitalWrite(13,HIGH);
   Serial.println("8th high");
}
if ((id==9)&&(ss==0))
{
  digitalWrite(15,LOW);
  Serial.println("9th LOW");
}
if((id==9)&&(ss==1))
{
   digitalWrite(15,HIGH);
   Serial.println("9th high");
}
if ((id==10)&&(ss==0))
{
  digitalWrite(3,LOW);
  Serial.println("10th LOW");
}
if((id==10)&&(ss==1))
{
   digitalWrite(3,HIGH);
   Serial.println("10th high");
}
if ((id==11)&&(ss==0))
{
  digitalWrite(1,LOW);
  Serial.println("11th LOW");
}
if((id==11)&&(ss==1))
{
   digitalWrite(1,HIGH);
   Serial.println("11th high");
}
Serial.print("ID = ");
Serial.print(id);
Serial.print(" status = ");
Serial.print(ss);
}}

here is the code which i used for local web browser control gpio pins

#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266WebServer.h>
const char *ssid = "arduino";
const char *password = "22395966";
int state0 = LOW;
int state1 = LOW;
int state2 = LOW;
int state3 = LOW;
int state4 = LOW;
int state5 = LOW;
int state6 = LOW;
ESP8266WebServer server(80);
void handleRoot() {
   
}
void handleLedOn() {
  state0 =HIGH;
  digitalWrite(13, state0);
  
}
void handleLedOff() {
  state0 = LOW;
  digitalWrite(13, state0);
  
}
void handleLedOn1() {
  state1 =HIGH;
  digitalWrite(12, state1);
  
}
void handleLedOff1() {
  state1 = LOW;
  digitalWrite(12, state1);
  
}
void handleLedOn2(){
  state2 =HIGH;
  digitalWrite(14, state2);
  
}
void handleLedOff2() {
  state2 = LOW;
  digitalWrite(14, state2);
  
}
void handleLedOn3() {
  state3 =HIGH;
  digitalWrite(02, state3);
  
}
void handleLedOff3() {
  state3 = LOW;
  digitalWrite(02, state3);
  
}
void handleLedOn4() {
  state4 =HIGH;
  digitalWrite(0, state4);
  
}
void handleLedOff4() {
  state4 = LOW;
  digitalWrite(0, state4);
  
}
void handleLedOn5() {
  state5 =HIGH;
  digitalWrite(4, state5);
  
}
void handleLedOff5() {
  state5 = LOW;
  digitalWrite(4, state5);
  
}
void setup() {
    delay(1000);
    Serial.begin(9600);
    Serial.println();
    WiFi.softAP(ssid, password);
    IPAddress apip = WiFi.softAPIP();
    Serial.print("visit: \n");
    Serial.println(apip);
    server.on("/", handleRoot);
    server.on("/0", handleLedOn);
    server.on("/1", handleLedOff);
    server.on("/2", handleLedOn1);
    server.on("/3", handleLedOff1);
    server.on("/4", handleLedOn2);
    server.on("/5", handleLedOff2);
    server.on("/6", handleLedOn3);
    server.on("/7", handleLedOff3);
    server.on("/8", handleLedOn4);
    server.on("/9", handleLedOff4);
    server.on("/10", handleLedOn5);
    server.on("/11", handleLedOff5); 
    server.begin();
    Serial.println("HTTP server beginned");
    pinMode(13, OUTPUT);
    digitalWrite(13, state0);
    pinMode(12, OUTPUT);
    digitalWrite(12, state1);
    pinMode(14, OUTPUT);
    digitalWrite(14, state2);
    pinMode(02, OUTPUT);
    digitalWrite(02,state3);
    pinMode(0, OUTPUT);
    digitalWrite(0,state4);
    pinMode(4, OUTPUT);
    digitalWrite(4,state5);
}

void loop() {
    server.handleClient();
}

so i need to control gpio pins through online server incase internet is disconnected then i should control gpio pins via local web browser and i need to update pin status to the server whenever internet is connected to the esp

What's stopping you from combining those two programs?

I have no idea how it's works I want to know flow to work as both online and offline

Do you understand what those two pieces of code do, or did you just copy them? The first step is to understand how all core parts work, and then merge them into a bigger program.

I know how it's works. But I don't know how to write code for both conditions.That's the major problem[ /b]