0
votes

i have a strange problem... I have a ESP-01 client that should connect to an access point and then send a request to a webserver. The access point and the webserver are hosted on a NodeMCU.

What works:

  • The ESP-01 can connect to the access point
  • My notebook can connect to the access point
  • On my notebook i can access the webserver (everything works fine)

The problem:

  • ESP-01 can't access the webserver (connection refused)

But: If i change the wifi to my local wifi on the ESP-01 and try to connect any other website, it works. So.. for me it seems to be an issue of the combination of the two ESPs.

Any ideas?

Server (NodeMCU)

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

#ifndef APSSID
#define APSSID "MyWifi"
#define APPSK  "1234567890"
#endif
const char *ssid = APSSID;
const char *password = APPSK;

const byte LED_1 = 5;
const byte LED_2 = 4;
const byte LED_3 = 13;
const byte LED_4 = 15;

const byte BTN_1 = 14;
const byte BTN_2 = 12;

const int ap_channel = 6;
const boolean ap_hidden = false;

IPAddress local_ip(192,168,4,1);
IPAddress gateway(192,168,4,1);
IPAddress netmask(255,255,255,0);

ESP8266WebServer server(80);

int led_on = 0;

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

  pinMode(LED_1, OUTPUT);
  pinMode(LED_2, OUTPUT);
  pinMode(LED_3, OUTPUT);
  pinMode(LED_4, OUTPUT);

  pinMode(BTN_1, INPUT);
  pinMode(BTN_2, INPUT);

  digitalWrite(LED_1, LOW);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_3, LOW);
  digitalWrite(LED_4, LOW);
  
  WiFi.softAPConfig(local_ip, gateway, netmask);
  
  Serial.print("IP Address: ");
  Serial.println(WiFi.softAPIP());

  Serial.println("Starting Access Point");
  WiFi.softAP(ssid, password, ap_channel, ap_hidden);
    
  server.on("/buzz", HTTP_GET, handlebuzz);
  server.begin();

}

// ##### Loop
void loop() {
  server.handleClient();
  if (digitalRead(BTN_1) == LOW){
    digitalWrite(LED_1, LOW);
    digitalWrite(LED_2, LOW);
    digitalWrite(LED_3, LOW);
    digitalWrite(LED_4, LOW);
    led_on = 0;
  }
}


void handlebuzz() {
  if( server.hasArg("id") && led_on == 0){
    server.send(200, "text/html","ok:"+server.arg("id"));
    
    if (server.arg("id") == "1"){
      digitalWrite(LED_1, HIGH);
      led_on = 1;
    } else if (server.arg("id") == "2"){
      digitalWrite(LED_2, HIGH);
      led_on = 1;
    } else if (server.arg("id") == "3"){
      digitalWrite(LED_3, HIGH);
      led_on = 1;
    } else if (server.arg("id") == "4"){
      digitalWrite(LED_4, HIGH);
      led_on = 1;
    }
    
    
  } else {
    server.send(200, "text/html","error");
  }
  return; 
}

Client (ESP-01):

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

#include <WiFiClient.h>

// ----- config -----
const char* ssid     = "MyWifi";
const char* password = "1234567890";

const byte PIN_BUZZER = 0;
const String url = "http://192.168.4.1/buzz?id=1";

// ----- config -----

int buzzing = 0;

void setup() {
  Serial.begin(115200);
  
  pinMode(PIN_BUZZER, INPUT);
 
  WiFi.begin(ssid, password);

 Serial.print("Connecting..");   
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.print("OK");
  Serial.println();
  
  
}

void loop() {   
   if (digitalRead(PIN_BUZZER)){
       Serial.println("Buzzer pressed");
       
      if (buzzing == 0) {
        Serial.println("doit");
        buzzing = 1;

        WiFiClient client;
        HTTPClient http;
        
        http.begin(client, url); 
        
        int httpCode = http.GET();    
        Serial.println(httpCode);                           
        if (httpCode > 0) {
          Serial.println(http.getString());
        }
        http.end(); 
        delay(2000);
        buzzing = 0;
        
      } else {
        Serial.println("cooldown");
      }
   }
}
1

1 Answers

0
votes

The solution was simple. I just changed the subnet from 192.168.4.0/24 to 192.168.1.0/24. I don't know why this works.