0
votes

I have a setup with two ESP8266 chips. One is AP and listening for alarms from other ESP8266 in WiFi mode. AP is an access point accepting get requests from "alarm" chips.

My problem is that when the first GET request has been handled (succesfully) by the AP, it is no longer possible to make more requests unless the AP chip is reset. Connection to the AP WiFI goes fine apparently but the actual get request only works on the first attempt after reset, the next ones all return error code -1 to the client.

Code from the AP chip:

void setupAP()
{
  WiFi.mode(WIFI_AP);
  String AP_NameString = "KEEP SAFE " + deviceID;

  char AP_NameChar[AP_NameString.length() + 1];
  memset(AP_NameChar, 0, AP_NameString.length() + 1);

  for (int i = 0; i < AP_NameString.length(); i++)
    AP_NameChar[i] = AP_NameString.charAt(i);

  WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));   // subnet FF FF FF 00
  WiFi.softAP(AP_NameChar, WiFiAPPSK);

  yield();
  activeMenu = mainMenu;
  server.on("/setup", handleWebRequest_Setup); //Associate the handler function to the path
  server.on("/confirm", handleWebRequest_Confirm); //Associate the handler function to the path
  server.on("/alarm", HTTP_GET, handleWebRequest_Alarm); 
  server.begin();                                       //Start the server
  Serial.println("Server listening");
}


void handleWebRequest_Alarm(){
    Serial.println("Client connected");
    sensorAlarmPostback(server);

  String s = "";
  s += "<!DOCTYPE HTML>\r\n<html>\r\n";
  s += "OK";
  s += "</html>\n";
  // Send the response to the client
  Serial.println("Send OK to client");
  server.send(200, "text/html", s);    
  Serial.println("OK Has been sent to the client");
}

 void sensorAlarmPostback(ESP8266WebServer serv) {
  Serial.println(String("Alaram recieved from : " + serv.arg("id")));
    for (int i = 0; i < maxSensors; i++)
    {
      if (sensors[i].ID == serv.arg("id"))
        sensors[i].alarm = true;
      yield();
    }
    Serial.println("Sensor alarm flag has been set");
  }

Code from the "sender" (alarm) chip:

void loop() {
  initWifi();
  publishAlarm();
  WiFi.disconnect();
  delay(30 * 1000); // Wait 30 seconds before next alarm
}



void initWifi() {
  WiFi.disconnect();
  WiFi.mode(WIFI_STA); // added in V 3.1a to disable AP_SSID publication in Client mode - default was WIFI_AP_STA
  WiFi.begin(ssid.c_str(), password.c_str());
  int retryCount = 0;
  while (WiFi.status() != WL_CONNECTED && retryCount < 10) {
    delay(2000);
    retryCount++;
  }
  if (WiFi.status() != WL_CONNECTED)
    Serial.println("WiFi ERROR");
  else
    Serial.println("WiFi Connected");
  delay(500);
}

void publishAlarm(){
  HTTPClient http;
  String postStr = "?id=" + AlarmID;
  postStr.replace("\r", "");
  postStr.replace("\n", "");



  Serial.println("String is");
  Serial.println(postStr);
  Serial.println("Connecting to Controller...");
  String PostURL = "http://10.10.10.1/alarm" + postStr;
  Serial.println("URL is: " + PostURL);
  http.begin(PostURL);
  int httpCode = http.GET();
  if (httpCode > 0)
  {
    Serial.println("httpCode > 0");
    yield();
    delay(100);
    http.end();
  }
  else
  {
    Serial.println("Error in upload "  + String(httpCode));
    yield();
    delay(100);
    http.end();
  }
}

I realize that there is a lot of unneeded stuff in the code, but I have been trying almost everything. I just don't get it, and I have the same problem in other projects as well. The chips I use are Wemos D1 Minis and various NodeMCU dev. boards. Does not seem to be related to a specific board.

1

1 Answers

0
votes

Update - SOLVED I decided to wipe my entire development environment and re-install. It wasn't that big of a deal. And after a recompile and deployment to the chip (The AP Chip) it works as it alwas should have. Oh well ....

Hope this helps someone if they end up in the same situation.

Now to cleaning up the code