Go Down

Topic: ESP8266WebServer help needed (Read 352 times) previous topic - next topic

thegasman2000

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.

code:

Code: [Select]
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include "fauxmoESP.h"
fauxmoESP fauxmo;

#define SERIAL_BAUDRATE                 115200

//Relays
#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);
      Serial.write("high");
      
    } else {
      digitalWrite(RELAY1, LOW);
      Serial.write("Low");
    }
  }

}

void setup() {

  pinMode(RELAY1, OUTPUT);

  
  digitalWrite(RELAY1, LOW);

  
    // Init serial port and clean garbage
    Serial.begin(SERIAL_BAUDRATE);



  // Fauxmo
  fauxmo.addDevice("relay1");


  fauxmo.onMessage(callback);

    Serial.begin(115200);
    delay(10);



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

    WiFi.mode(WIFI_STA);
    WiFi.begin(WLAN_SSID, WLAN_PASSWORD);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println();

    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);
        Serial.println(webString);
        server.send(200, "text/plain", webString);


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

void loop() {
    server.handleClient();
}


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

Connecting to meh
..
WiFi connected
IP address:
192.168.1.232
Relay 1
HTTP server started! Waiting for clients!

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

gdsports

The last 3 lines of code must be inside the [](){...}.

Code: [Select]
  server.on("/server", HTTP_GET, [](){
      if (relay1State == 1) {
        relay1StateString = "True";
      }
      else {
        relay1StateString = "False";
      }
      String webString = "Relay 1 " + (relay1StateString);
      Serial.println(webString);
      server.send(200, "text/plain", webString);
    });


Further simplification, optional.

Code: [Select]
  server.on("/server", HTTP_GET, [](){
      if (relay1State == 1) {
        server.send(200, "text/plain", "Relay 1 True");
      }
      else {
        server.send(200, "text/plain", "Relay 1 False");
      }
    });

Go Up