Cytron ESP8266 WiFi Shield with AndroidAP

Discussion on how to get started with various type of IoT projects

Cytron ESP8266 WiFi Shield with AndroidAP

Postby poonjon » Tue Jan 09, 2018 10:34 pm

Hi, I tried connecting my Cytron ESP8266 WiFi Shield to my phone's hotspot and it fails to connect to my destination server, however it works if I use my house wifi instead.

-It shows "failed connect to server" on the serial monitor
-I have cross checked the wifi shield's ip with the IP on my laptop, both using the same hotspot
-I tried running the example code "CytronWiFiDemo" on on my arduino using both my phone's hotspot and house wifi. House wifi works but phone hotspot does not (failed connect to server).
poonjon
Freshie
 
Posts: 5
Joined: Tue Jan 09, 2018 10:23 pm

Re: Cytron ESP8266 WiFi Shield with AndroidAP

Postby bengchet » Tue Jan 09, 2018 11:22 pm

Hi,

May be same as this issue.

Try check if there is a line
CODE: SELECT_ALL_CODE
wifi.config(ip)
in your sketch. If there is just remove it and see how it goes.

Cheers ;)
bengchet
Moderator
 
Posts: 237
Joined: Tue Aug 25, 2015 8:29 am

Re: Cytron ESP8266 WiFi Shield with AndroidAP

Postby poonjon » Wed Jan 10, 2018 9:41 am

Unfortunately after I remove that line I get the message "error connecting to wifi" .

im trying to get my wifi module working as a client not server
bengchet WROTE:May be same as this issue.


Here are more information regarding my setup:
-my phone hotspot is WPA2-PSK security
-setting my phone as open network does not work as well
-my wifi module SDK is version 1.5.1
-my wifi module AT is version 0.52.0.0
-my arduino is version 1.6.9
-i have tried connecting it to 2 different hotspots from 2 different phone models, both didnt work..
poonjon
Freshie
 
Posts: 5
Joined: Tue Jan 09, 2018 10:23 pm

Re: Cytron ESP8266 WiFi Shield with AndroidAP

Postby bengchet » Thu Jan 11, 2018 10:53 am

Hi,

Can you show some screenshots on serial monitor, and also your coding? It will be easier for troubleshooting.

For your phone settings, make sure your phone hotspot SSID and password are correct (AndroidAP is too common, advise to change to a name which is more unique and easy to identify, because there might be same SSID from other android phone). Also make sure your phone hotspot set to 'allow all devices'. You can use other device or PC to connect to your phone hotspot and surf Internet.
bengchet
Moderator
 
Posts: 237
Joined: Tue Aug 25, 2015 8:29 am

Re: Cytron ESP8266 WiFi Shield with AndroidAP

Postby poonjon » Thu Jan 11, 2018 8:27 pm

Attached are 3 screen shots with descriptions. I'm using the cytron wifi example code, the only settings that i changed are SSID, password and IP.
Attachments
home wifi.jpeg
arduino wifi connection works when i connect it to my home wifi
mobile hotspot.jpeg
arduino wifi connection to server fails when i connect it to my mobile hotspot
laptop settings.jpeg
this is a reference, it shows that my mobile hotspot is in fact working when my laptop is connected to it
poonjon
Freshie
 
Posts: 5
Joined: Tue Jan 09, 2018 10:23 pm

Re: Cytron ESP8266 WiFi Shield with AndroidAP

Postby bengchet » Fri Jan 12, 2018 10:04 am

Hi,

Mind sharing your source code as well?
bengchet
Moderator
 
Posts: 237
Joined: Tue Aug 25, 2015 8:29 am

Re: Cytron ESP8266 WiFi Shield with AndroidAP

Postby poonjon » Fri Jan 12, 2018 10:42 am

I'm using the CytronWiFiDemo code... anyway here you go

CODE: SELECT_ALL_CODE
#include <CytronWiFiShield.h>
#include <CytronWiFiClient.h>
#include <CytronWiFiServer.h>
#include <SoftwareSerial.h>

const char *ssid = "ABCD";
const char *pass = "aaaaaaaa";
IPAddress ip(192, 168, 43 ,70);
ESP8266Server server(80);

const char htmlHeader[] = "HTTP/1.1 200 OK\r\n"
                        "Content-Type: text/html\r\n"
                        "Connection: close\r\n\r\n"
                        "<!DOCTYPE HTML>\r\n"
                        "<html>\r\n";
                         
void setup() {
 
  // put your setup code here, to run once:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
 
  if(!wifi.begin(2, 3))
  {
    Serial.println(F("Error talking to shield"));
    while(1);
  }
  Serial.println(wifi.firmwareVersion());
  Serial.print(F("Mode: "));Serial.println(wifi.getMode());// 1- station mode, 2- softap mode, 3- both
  Serial.println(F("Setup wifi config"));
  wifi.config(ip);
  Serial.println(F("Start wifi connection"));
  if(!wifi.connectAP(ssid, pass))
  {
    Serial.println(F("Error connecting to WiFi"));
    while(1);
  }
  Serial.print(F("Connected to "));Serial.println(wifi.SSID());
  Serial.println(F("IP address: "));
  Serial.println(wifi.localIP());
  wifi.updateStatus();
  Serial.println(wifi.status()); //2- wifi connected with ip, 3- got connection with servers or clients, 4- disconnect with clients or servers, 5- no wifi
  clientTest();
  espblink(100);
  server.begin();
}

void loop() {
  // put your main code here, to run repeatedly:
  serverTest();
}

void espblink(int time)
{
  for(int i = 0;i<12;i++)
  {
    wifi.digitalWrite(2,wifi.digitalRead(2)^1);
    delay(time);
  }
}

void serverTest()
{
  ESP8266Client client = server.available();
 
  if(client.available()>0)
  {
    String req = client.readStringUntil('\r');
    // First line of HTTP request looks like "GET /path HTTP/1.1"
    // Retrieve the "/path" part by finding the spaces
    int addr_start = req.indexOf(' ');
    int addr_end = req.indexOf(' ', addr_start + 1);
    if (addr_start == -1 || addr_end == -1) {
      Serial.print(F("Invalid request: "));
      Serial.println(req);
      return;
    }
    req = req.substring(addr_start + 1, addr_end);
    Serial.print(F("Request: "));
    Serial.println(req);
    client.flush();
   
    if(req.equals("/"))
    {
      IPAddress ip = wifi.localIP();
      String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
      client.print(htmlHeader);
      String htmlBody = "Hello from ESP8266 at ";
      htmlBody += ipStr;
      htmlBody += "</html>\r\n\r\n";
      client.print(htmlBody);
    }
   
    else if(req.equals("/analog"))
    {
      client.print(htmlHeader);
      String htmlBody="";
      for (int a = 0; a < 6; a++)
      {
        htmlBody += "A";
        htmlBody += String(a);
        htmlBody += ": ";
        htmlBody += String(analogRead(a));
        htmlBody += "<br>\r\n";
      }
      htmlBody += "\r\n</html>\r\n";
      client.print(htmlBody);
    }
   
    else if(req.equals("/gpio2"))
    {
      wifi.digitalWrite(2, wifi.digitalRead(2)^1);
      client.print(htmlHeader);
      String htmlBody="GPIO2 is now ";
      htmlBody += wifi.digitalRead(2)==HIGH?"HIGH":"LOW";
      htmlBody += "</html>\r\n";
      client.print(htmlBody);
    }
   
    else if(req.equals("/info"))
    {
      String toSend = wifi.firmwareVersion();
      toSend.replace("\r\n","<br>");
      client.print(htmlHeader);
      client.print(toSend);
      client.print("</html>\r\n");
    }

    else
      client.print("HTTP/1.1 404 Not Found\r\n\r\n");
   
    client.stop();
 
  }
}

void clientTest()
{
  const char destServer[] = "www.adafruit.com";
  ESP8266Client client;
  if (!client.connect(destServer, 80))
  {
    Serial.println(F("Failed to connect to server."));
    client.stop();
    return;
  }
 
  const char *httpRequest = "GET /testwifi/index.html HTTP/1.1\r\n"
                           "Host: www.adafruit.com\r\n"
                           "Connection: close\r\n\r\n";
  if(!client.print(httpRequest))
  {
    Serial.println(F("Sending failed"));
    client.stop();
    return;;
  }

  // set timeout approximately 5s for server reply
  int i=5000;
  while (client.available()<=0&&i--)
  {
    delay(1);
    if(i==1) {
      Serial.println(F("Timeout"));
      return;
      }
  }

  while (client.available()>0)
  {
    //char c = (char)client.read();
    //Serial.print(c);
    Serial.write(client.read());
  }
 
  client.stop();
}

poonjon
Freshie
 
Posts: 5
Joined: Tue Jan 09, 2018 10:23 pm

Re: Cytron ESP8266 WiFi Shield with AndroidAP

Postby bengchet » Fri Jan 12, 2018 12:22 pm

Hi,

Try comment these 2 lines.

CODE: SELECT_ALL_CODE
  Serial.println(wifi.firmwareVersion());
  Serial.print(F("Mode: "));Serial.println(wifi.getMode());// 1- station mode, 2- softap mode, 3- both
  // Serial.println(F("Setup wifi config"));
  // wifi.config(ip);
  Serial.println(F("Start wifi connection"));
  if(!wifi.connectAP(ssid, pass))
  {
    Serial.println(F("Error connecting to WiFi"));
    while(1);
  }
bengchet
Moderator
 
Posts: 237
Joined: Tue Aug 25, 2015 8:29 am

Re: Cytron ESP8266 WiFi Shield with AndroidAP

Postby poonjon » Fri Jan 12, 2018 10:07 pm

It works now thank you so much, may I know what is the cause of it?
poonjon
Freshie
 
Posts: 5
Joined: Tue Jan 09, 2018 10:23 pm

[Solved] Cytron ESP8266 WiFi Shield with AndroidAP

Postby bengchet » Tue Jan 16, 2018 11:23 am

Hi,

wifi.config(ip) is for setting static IP address in your network. I am not sure if mobile network will allow you to do so.
bengchet
Moderator
 
Posts: 237
Joined: Tue Aug 25, 2015 8:29 am


Return to Getting Started - IoT

Who is online

Users browsing this forum: No registered users and 5 guests

cron