0

I am newbie in Arduino. I'm trying to send data from an ESP-12E client module to a mobile via an ESP router module (I am using Arduino IDE for programming the ESP-12E).

For this purpose I successfully connect the client ESP-12E module with the router ESP module. Here is my code:

Server side code

#include <ESP8266WiFi.h>

WiFiServer server(80); //Initialize the server on Port 80

void setup() {
  WiFi.mode(WIFI_AP); //Our ESP8266-12E is an AccessPoint
  WiFi.softAP("11111111", "12345678");
  // Provide the (SSID, password);
  server.begin(); // Start the HTTP Server
  Serial.begin(115200);
  //Start communication between the ESP8266-12E and the monitor window
  IPAddress HTTPS_ServerIP= WiFi.softAPIP();
  // Obtain the IP of the Server
  Serial.print("Server IP is: ");
  // Print the IP to the monitor window
  Serial.println(HTTPS_ServerIP);
}

void loop() {
  WiFiClient client = server.available();
  if (!client) {
    return; 
  } 
}

client side code

#include <ESP8266WiFi.h>

const char *ssid = "11111111";
const char *password = "12345678";
const char *host = "192.168.4.1";
const int httpPort = 80;

void setup() {
  Serial.begin(115200);
  delay(10);
  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());
}

void loop() {
  delay(8000);
  Serial.print("connecting to ");
  Serial.println(host);
  WiFiClient client;
  client.connect(host,httpPort);
  if (!client.connect(host,httpPort)) {
    Serial.println("connection failed");
    return;
  }
  Serial.println("connection Ok");
  client.print("Temperature");
  client.println( " C " );
  client.print(", ");
  client.print("HI data received");
}

Now I want to receive data on my mobile. So I connect my mobile with the router ESP-12E module. And then I send data from the Client ESP-12E but I got nothing on my mobile phone. Can anyone tell me how to get the data on my phone?

Now I am using the client side code below.

client side code

#include <ESP8266WiFi.h>

#define MAX_SRV_CLIENTS 2
//how many clients should be able to telnet to this ESP8266
const char* ssid = "11111111";
const char* password = "12345678";
WiFiServer server(80);
WiFiClient serverClients[MAX_SRV_CLIENTS];

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.print("\nConnecting to ");
  Serial.println(ssid);
  uint8_t i = 0;
  while (WiFi.status() != WL_CONNECTED && i++ < 20) delay(500);
  if(i == 21) {
    Serial.print("Could not connect to"); 
    Serial.println(ssid);
    while(1) delay(500);
  }
  //start UART and the server
  Serial.begin(115200);
  server.begin();
  server.setNoDelay(true);
  Serial.print("Ready! Use 'telnet ");
  Serial.print(WiFi.localIP());
  Serial.println(" 23' to connect");
}

void loop() {
  uint8_t i;
  //check if there are any new clients
  if (server.hasClient()) {
    for(i = 0; i < MAX_SRV_CLIENTS; i++) {
      //find free/disconnected spot
      if (!serverClients[i] || !serverClients[i].connected()) {
        if(serverClients[i]) serverClients[i].stop();
        serverClients[i] = server.available();
        Serial.print("New client: ");
        Serial.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] && serverClients[i].connected()) {
        serverClients[i].write(sbuf, len);
        WiFiClient client;
        client.print("a");
        delay(1);
      }
    }
  }
}

I connect my mobile phone with the router ESP module and send data from my client ESP module to my mobile and when type the IP address on mobile phone for receiving data. Then I get this output on the client ESP 12E module on the serial port and I get nothing on my mobile phone.

 New client: 0GET / HTTP/1.1
.Host: 192.168.4.2
.Connection: keep-alive
.Cache-Control: max-age=0
.Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
.User-Agent: Mozilla/5.0 (Linux; Android 4.4.4; AO5510 Build/KTU84Q) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.135 Mobile Safari/537.36
.Accept-Encoding: gzip,deflate,sdch
.Accept-Language: en-US,en;q=0.8

Can anyone help me? What am I doing wrong?

  • There is nothing at all in your "router" code that deals with transferring of any data. All you have done is create an access point, accepted connections to a port, and then ignored anything that arrives. You need to actively read from each connected client and send that data out to all other connected clients. – Majenko Jul 1 '16 at 10:50
  • Try looking at the WiFiTelnetToSerial example, but remove the serial aspect of it. – Majenko Jul 1 '16 at 10:55
  • thanx for reply sir but this example is for read data through Serial but i want to read data from Client Esp Module so what will change in this code? this code used at either router side or client side please help me – Amit Upadhyay Jul 1 '16 at 11:13
  • Like I said - you need to read from every connection and send out to every other connection. Just like that example does, but changing the reading from serial to reading from every connection. The key point is the maintaining of all the client connections so you can transfer data between them, which that example shows you how. – Majenko Jul 1 '16 at 12:01
  • i get this message on serial of Client ESP when i am trying to received data on mobile via ip after connected to router New client: 0GET / HTTP/1.1 .Host: 192.168.4.2 .Connection: keep-alive .Cache-Control: max-age=0 .Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8 .User-Agent: Mozilla/5.0 (Linux; Android 4.4.4; AO5510 Build/KTU84Q) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.135 Mobile Safari/537.36 .Accept-Encoding: gzip,deflate,sdch .Accept-Language: en-US,en;q=0.8 – Amit Upadhyay Jul 2 '16 at 9:23
0

You can not do what you want to do with the code you have.

At the moment all you have done is write an access point, this just lets WIFI devices connect to the 'router'. You need to write some code that reads what your client sends. Take a look at the examples there are a couple of web server examples that you can study.

Forget about the client for now, just you a browser to request your pages, get the server working then get your client working.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.