0

The code is supposed to create a soft ap, and whenever the button is pushed, it should send a '1' to the client, causing the pin to go HIGH, and whenever it is not pushed, set the pin to LOW. This behavior is not displayed, and I am not sure what is causing it.

server:

#include <ESP8266WiFi.h>
IPAddress local_IP(192,168,0,1);
IPAddress gateway(192,168,4,9);
IPAddress subnet(255,255,255,0);
WiFiServer server(80);
int laststate = 0;
void setup() {
  WiFi.softAPConfig(local_IP, gateway, subnet);
  WiFi.softAP("Button");
  server.begin();
  
}

void loop() {
  WiFiClient client = server.available();
  while (client.connected() == true) {
      if (client) {
        while (digitalRead(1) == HIGH) {
          if (laststate == 0) {
            client.println(1);
            laststate == 1;
          } 
          else {
          if (digitalRead(1) == LOW) {
            if (laststate == 1) {
              client.println(0);
            }
            laststate == 0;
          }
        }
      }
    }

    while (client.available()== true) {
      client.read();
    }
  }
}

client:

#include <ESP8266WiFi.h>
WiFiClient client;
void setup() {
  WiFi.begin("button");
  client.connect("192.168.0.1", 80);

}

void loop() {
  while (client.read() == 1) {
    digitalWrite(1, HIGH);
  }
  digitalWrite(1,LOW);
}
5
  • Do some basic debugging. Check for errors. Your code doesn't check if client.connect() succeeds. Put in some debugging messages. Right now you can't tell if you get a 1 or not. Output a message if you do. If you strip out the network code does your code successfully light up the LED or not? Find out what your code is doing by adding in debugging messages and go from there. Commented Oct 28, 2022 at 17:05
  • stop the default SoftAP on the esp which shouldn't be AP with WiFi.mode(WIFI_STA);. if it helps I will write an answer Commented Oct 28, 2022 at 18:14
  • @Juraj what do you mean by that? should i replace wifi.softap with that or what? please show me your recommended implementation using my code. Thanks. Commented Oct 28, 2022 at 23:33
  • Other than the incorrect coding (for which you should take a look at the examples that come with the library). There are also some basic networking stuff that you need to be aware of , if your gateway is 192.168.4.9, with a netmask is 255.255.255.0, it will only listen to the IPs from the subnet of 192.168.4.xxx, the data from 192.168.0.xxx will be ignored. Commented Oct 29, 2022 at 0:16
  • add the line WiFi.mode(WIFI_STA); before WiFi.begin("button");. and in the other sketch change to IPAddress local_IP(192,168,4,1); Commented Oct 29, 2022 at 6:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.