ESP8266 Client doesn't reflect switch state.

I am using the instructables ESP8266 as a microcontroller code as a starting point to which I have added some code to get the client (Ipad) to show the state of a switch attached to the WeMos board.

The original code worked well and I was able to change the inbuilt LED on the WeMos board to on and off as well as get it to send a message that the LED was either on or off.

I added a switch attached to the board where I wanted to observe its on/off status on the client (Ipad). However, changing this switch does not effect the switch state observed on the Ipad display. I have checked that the voltage changes on Pin 6 (Digital pin 12) using the switch.

There is something obviously wrong with my approach, which is most likely my inexperience with server/client methods. Can anyone suggest where I have made an error/s?

This is a first attempt to get something working and once I get this going I will incorporate a simpleTimer to poll it.

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

//http://www.instructables.com/id/ESP8266-As-a-Microcontroller/
//Ipad on Safari to 192.168.4.1
//Note that this uses the WeMos board
const char* ssid = “ESP”;
const char* password = “123456789”;
const int DIGITAL_PIN = 12; // Digital pin to be read (is marked D6 on board)
String Vsw;
String form = “OffOn”;

ESP8266WebServer server(80);

void handle_led() {
// get the value of request argument “state” and convert it to an int
int state = server.arg(“state”).toInt();

digitalWrite(BUILTIN_LED, state);
server.send(200, “text/plain”, String("LED is now ") + ((state)?“off”:“on”) + " Switch " + (Vsw));
}

void setup(void) {
Serial.begin(115200);
pinMode(DIGITAL_PIN, INPUT_PULLUP);
pinMode(BUILTIN_LED, OUTPUT); // initialize onboard LED as output
digitalWrite(BUILTIN_LED, HIGH);//note that this sets led off
WiFi.softAP(ssid, password);

server.on("/", {
server.send(200, “text/html”, form);
});

server.on("/led", handle_led);

// Start the server
server.begin();
}

void loop(void) {
// check for incoming client connections frequently in the main loop:
server.handleClient();
digitalRead(DIGITAL_PIN);
if (DIGITAL_PIN ==LOW)
{Vsw=“Closed”;
}
else
{Vsw=“Open”;
}
delay(1000);
}``

This causes me some confusion.

const int DIGITAL_PIN = 12; // Digital pin to be read (is marked D6 on board)

Yes, it does get a bit confusing but I'm referring to GPIO12 which is marked D6 on board see:

http://www.wemos.cc/Products/d1_mini.html

I would like to be able to do a request from the client to examine the switch state but because my HTML knowledge is limited I'm unsure of how to include that in the String form.

In that way I would not need to continually poll the void loop but only do a switch state check when requested. However, I'm still mystified why the switch state doesn't show a change on the client Ipad.

The response to the server
server.send(200, "text/plain", String("LED is now ") + ((state)?"off":"on") + " Switch " + (Vsw)); is
LED is now on Switch Open

where on changes to off as requested but Open never changes to Closed despite changing switch state.

Any ideas anyone???

This is your problem.

  digitalRead(DIGITAL_PIN);
  if (DIGITAL_PIN ==LOW)
  {Vsw="Closed";
  }
  else
  {Vsw="Open";
  }

It should be like this.

  byte pinState = digitalRead(DIGITAL_PIN);
  if (pinState == LOW)
  {Vsw="Closed";
  }
  else
  {Vsw="Open";
  }

...or this:

  if (digitalRead(DIGITAL_PIN) == LOW)
  {Vsw="Closed";
  }
  else
  {Vsw="Open";
  }

Or even

Vsw = digitalRead(DIGITAL_PIN) ? "Open" : "Closed";

Thanks so much SurferTim that fixed it.

It shows that too much time spent using Basic in the MCS Bascom environment has made my C a bit wanting. I must say that Bascom still has its place particularly to do simple minimal projects such as those requiring very low power consumption - it can be applied to any of the AVG micros.

I am also very impressed with the WeMos esp8266 board particularly when I remember the frustration spent with the hairy ESP8266-01 board.

Now that this hurdle has been overcome - on to the next.

Since the above, another post has come in from Whandall, thanks for that too.