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);
}
  • 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. – romkey Oct 28 '22 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 – Juraj Oct 28 '22 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. – HileyNoteson Oct 28 '22 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. – hcheung Oct 29 '22 at 00: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);` – Juraj Oct 29 '22 at 06:03

0 Answers0