0

I have an ESP8266 which I'm using the Arduino IDE to compile a program which uses soft access points to talk to an Android App compiled using MIT App Inventor. The app seems to work the first couple times but then my cell phone looses connection and I have to go to Settings->Wifi connections and rescan and wait for it to connect. Sometimes it says: "Denied access to network" and sometimes (most always) after I connect I will get a message that says I'm not connected to the internet, which I don't want, and then choose 'No' to stay connected to the current network. My cell phone is right next to the ESP8266 and the signal strength is 'Excellent'. I'm only powering the board via the USB cable connected to it. Do I need to run 3v3 to the pin marked '3V3'?

Here's the code:

// server running on ESP8266 to get commands from Android App
// Load Wi-Fi library
#include <ESP8266WiFi.h>

// Replace with your network credentials
const char *ssid = "PanzerD"; // The name of the Wi-Fi network that will be created
const char *password = "testasdf";   // The password required to connect to it, leave blank for an open network
// Set web server port number to 80
WiFiServer server(80);
IPAddress local_IP1(192,168,0,15);
IPAddress gateway1(192,168,0,1);
IPAddress subnet(255,255,255,0);

// Variable to store the HTTP request
String header;

// Auxiliar variables to store the current output state

// Assign output variables to GPIO pins
int tool_box_lights = 0;
int pass_side_lights = 0;
int drv_side_lights = 0;
int rear_lights = 0;
int winch = 0;

// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0; 
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;

void setup() {
  Serial.begin(115200);

  Serial.print("Setting soft-AP configuration ... ");
  Serial.println(WiFi.softAPConfig(local_IP1, gateway1, subnet) ? "Ready" : "Failed!");
  Serial.println(WiFi.softAP(ssid,password,1,4) ? "Ready" : "Failed!");

  Serial.print("Soft-AP IP address = ");
  Serial.println(WiFi.softAPIP());

  // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
/*
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
*/
  // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
//  Serial.println("IP address: ");
//  Serial.println(WiFi.localIP());
  server.begin();
}

void loop(){
  WiFiClient client = server.available();   // Listen for incoming clients

  if (client) {                             // If a new client connects,
//    Serial.print("Client: ");          // print a message out in the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    currentTime = millis();
    previousTime = currentTime;
    while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
      currentTime = millis();         
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
//        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();
            //Serial.println(header);
            // turns the GPIOs on and off
            if (header.indexOf("/tb/on") >= 0) 
            {
              Serial.println("toolbox on");
              client.println("toolbox on");
              tool_box_lights = 1;
            } else if (header.indexOf("/tb/off") >= 0) 
            {
              Serial.println("toolbox off");
              client.println("toolbox off");
              tool_box_lights = 0;
            } else if (header.indexOf("/ps/on") >= 0) 
            {
              Serial.println("pass side on");
              client.println("pass side on");
              pass_side_lights = 1;
            } else if (header.indexOf("/ps/off") >= 0) 
            {
              Serial.println("pass side off");
              client.println("pass side off");
              pass_side_lights = 0;
            } else if (header.indexOf("/ds/on") >= 0) 
            {
              Serial.println("drv side on");
              client.println("drv side on");
              drv_side_lights = 1;
            } else if (header.indexOf("/ds/off") >= 0) 
            {
              Serial.println("drv side off");
              client.println("drv side off");
              drv_side_lights = 0;
            } else if (header.indexOf("/rr/on") >= 0) 
            {
              Serial.println("rear lights on");
              client.println("rear lights on");
              rear_lights = 1;
            } else if (header.indexOf("/rr/off") >= 0) 
            {
              Serial.println("rear lights off");
              client.println("rear lights off");
              rear_lights = 0;
            } else if (header.indexOf("/wc/in") >= 0) 
            {
              Serial.println("winch in");
              client.println("winch in");
              winch = 1;
            } else if (header.indexOf("/wc/out") >= 0) 
            {
              Serial.println("winch out");
              client.println("winch out");
              winch = 2;
            } else if (header.indexOf("/wc/off") >= 0) 
            {
              Serial.println("winch off");
              client.println("winch off");
              winch = 0;
            }  
            // Display the HTML web page
            // The HTTP response ends with another blank line
            client.println();
            // Break out of the while loop
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
//    Serial.println("Client disconnected.");
//    Serial.println("");
  }
}

Here's the blocks for the MIT App Inventor: enter image description here Each one of the blocks responds to a button press and depending on what the global variable is (upper left block is for toolbox lights) it will send the URL "/tb/on" in the case for turning the toolbox lights on. [Edit:] It works fine when I use the modem as an access point but I need soft access points because I don't want to have to carry a router around in my truck.

  • Apparently I'm not formatting the html right and it's causing the connection to drop. – SpaceCowboy Feb 23 at 0:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.