ESP8266WebServer help needed

I am making a smart Alexa powered powerbar using the NodeMCU and am running into an issue with the web server side of things.

I just want to be able to visit an ip on my network and get a readout of what relay are on and which are off. I would like to add power meters to each one later and have these values shown too. The end project is an reef aquarium monitoring system.

Currently I have got the alexa relay stuff working, then i stripped the code down to add the web stuff, which is why there is only one relay mentioned, to keep things cleaner for the time being.

So I also need to apologise for what i suspect is really, really ugly code! I bastardised some tutorial code to make a webserver which gives the state of one relay.


#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include "fauxmoESP.h"
fauxmoESP fauxmo;

#define SERIAL_BAUDRATE                 115200

#define RELAY1 9
bool relay1State = digitalRead(9);
String relay1StateString;

#define WLAN_SSID     "meh"
#define WLAN_PASSWORD "meh"

ESP8266WebServer server(80);

const long interval = 2000;

// make variables for relay state
void callback(uint8_t device_id, const char * device_name, bool state) {
  Serial.printf("[MAIN] %s state: %s\n", device_name, state ? "ON" : "OFF");

  if ( (strcmp(device_name, "relay1") == 0) ) {
    // adjust the relay immediately!
    if (state) {
      digitalWrite(RELAY1, HIGH);
    } else {
      digitalWrite(RELAY1, LOW);


void setup() {

  pinMode(RELAY1, OUTPUT);

  digitalWrite(RELAY1, LOW);

    // Init serial port and clean garbage

  // Fauxmo



    Serial.println(); Serial.println();
    Serial.print("Connecting to ");

    while (WiFi.status() != WL_CONNECTED) {

    Serial.println("WiFi connected");
    Serial.println("IP address: "); Serial.println(WiFi.localIP());

    server.on("/server", HTTP_GET, [](){
      if (relay1State == 1) {
        relay1StateString = "True"; }
        else {
          relay1StateString = "False"; }

        String webString = "Relay 1 " + (relay1StateString);
        server.send(200, "text/plain", webString);

    Serial.println("HTTP server started! Waiting for clients!");

void loop() {

Now it compiles (eventually) and loads on the node. I get this in my serial output

Connecting to meh

WiFi connected
IP address:
Relay 1
HTTP server started! Waiting for clients!

however when I got to I get nothing. The tutorial code worked fine so its not a network issue at all.

Did I do something stupid?

Did I do something stupid?


Look at what should happen if you navigate to, assuming that the Arduino's address is

server.on("/server", HTTP_GET, [](){
      if (relay1State == 1) {
        relay1StateString = "True"; }
        else {
          relay1StateString = "False"; }

That is ALL that happens. Nowhere do you do anything with relay1State or relay1StateString.

What ARE you expecting to see happen?