So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By dancingapples
#75535 I'm trying to create a code that will eventually be able to send several different commands to carry out a certain procedure on the NodeMCU.

I've adapted a code I found online to suit this need but I'm having problems with the new settings I've added.

The two new settings equal and set 1 change in unison and not independently as I require e.g. I activate set 1 on the webpage and equal also changes and vice versa. As well as this everytime the page refreshes it resets the variable.

Any advice my code is below I'm probably missing something basic.

set 1 and equal do not need to activate anything at this time but I am trying to code it so that if equal is pressed set 1 goes false. It works like this with the two leds but not with my variables.

code below:
Code: Select all#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

//IPAddress    apIP(42, 42, 42, 42);  // Defining a static IP address: local & gateway
                                    // Default IP in AP mode is 192.168.4.1

/* This are the WiFi access point settings. Update them to your likin */
const char *ssid = "ESP8266";
const char *password = "ESP8266Test";

// Define a web server at port 80 for HTTP
ESP8266WebServer server(80);

const int ledPin1 = D1; // an LED is connected to NodeMCU pin D1 (ESP8266 GPIO5) via a 1K Ohm resistor
const int ledPin2 = D2; // an LED is connected to NodeMCU pin D1 (ESP8266 GPIO5) via a 1K Ohm resistor
bool ledState1 = false;
bool ledState2 = false;
bool equalState;
bool set1State = false;
bool set2State = false;
bool pelicanState = false;
bool maintenanceState = false;

void handleRoot() {
  digitalWrite (LED_BUILTIN, 0); //turn the built in LED on pin DO of NodeMCU on
  digitalWrite (ledPin1, server.arg("led1").toInt());
  digitalWrite (ledPin2, server.arg("led2").toInt());
 

 /* Dynamically generate the LED toggle link, based on its current state (on or off)*/
  char ledText1[80];
  char ledText2[80];
  char equalText[80];
  char set1Text[80];
  char set2Text[80];
  char pelicanText[80];
  char EqualText[80];

  if(equalState){
    strcpy(equalText, "priority is equal. <a href=\"/?equal=0\">Deactivate</a>");
    equalState = false;
  }
  else {
    strcpy(equalText, "priority is not equal. <a href=\"/?equal=1\">Activate</a>");
    equalState = true;
  }

  if(set1State){
    strcpy(set1Text, "priority is set1. <a href=\"/?set1=0\">Deactivate</a>");
    set1State = false;
  }
  else {
    strcpy(set1Text, "priority is not set1. <a href=\"/?set1=1\">Activate</a>");
    set1State = true;
  }
 
 
  if (ledState1) {
    strcpy(ledText1, "LED 1 is on. <a href=\"/?led1=0\">Turn it OFF!</a>");
  }

  else {
    strcpy(ledText1, "LED 1 is OFF. <a href=\"/?led1=1\">Turn it ON!</a>");
  }
 
  ledState1 = digitalRead(ledPin1);
 
  if (ledState2) {
    strcpy(ledText2, "LED 2 is on. <a href=\"/?led2=0\">Turn it OFF!</a>");
  }

  else {
    strcpy(ledText2, "LED 2 is OFF. <a href=\"/?led2=1\">Turn it ON!</a>");
  }
 
  ledState2 = digitalRead(ledPin2);
 

  char html[1000];
  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;

  int brightness = analogRead(A0);
  brightness = (int)(brightness + 5) / 10; //converting the 0-1024 value to a (approximately) percentage value

// Build an HTML page to display on the web-server root address
  snprintf ( html, 1000,

"<html>\
  <head>\
    <meta http-equiv='refresh' content='10'/>\
    <title>ESP8266 WiFi Network</title>\
    <style>\
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; font-size: 1.5em; Color: #000000; }\
      h1 { Color: #AA0000; }\
    </style>\
  </head>\
  <body>\
    <h1>ESP8266 Wi-Fi Access Point and Web Server Demo</h1>\
    <p>Uptime: %02d:%02d:%02d</p>\
    <p>Brightness: %d%</p>\
    <p>%s<p>\
    <p>%s<p>\
    <p>%s<p>\
    <p>%s<p>\
    <p>This page refreshes every 10 seconds. Click <a href=\"javascript:window.location.reload();\">here</a> to refresh the page now.</p>\
  </body>\
</html>",

    hr, min % 60, sec % 60,
    brightness,
    ledText1,
    ledText2,
    equalText,
    set1Text
  );
  server.send ( 200, "text/html", html );
  digitalWrite ( LED_BUILTIN, 1 );
}

void handleNotFound() {
  digitalWrite ( LED_BUILTIN, 0 );
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += ( server.method() == HTTP_GET ) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";

  for ( uint8_t i = 0; i < server.args(); i++ ) {
    message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n";
  }

  server.send ( 404, "text/plain", message );
  digitalWrite ( LED_BUILTIN, 1 ); //turn the built in LED on pin DO of NodeMCU off
}

void setup() {
  pinMode ( ledPin1, OUTPUT );
  digitalWrite ( ledPin1, 0 );
  pinMode ( ledPin2, OUTPUT );
  digitalWrite ( ledPin2, 0 );
  delay(1000);
  Serial.begin(115200);
  Serial.println();
  Serial.println("Configuring access point...");

  //set-up the custom IP address
  //WiFi.mode(WIFI_AP_STA);
  //WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));   // subnet FF FF FF 00 
 
  /* You can remove the password parameter if you want the AP to be open. */
  WiFi.softAP(ssid, password);

  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
 
  server.on ( "/", handleRoot );
  server.on ( "/led1=1", handleRoot);
  server.on ( "/led1=0", handleRoot);
  server.on ( "/led2=1", handleRoot);
  server.on ( "/led2=0", handleRoot);
  server.on ( "/equal=1", handleRoot);
  server.on ( "/equal=0", handleRoot);
  server.on ( "/set1=1", handleRoot);
  server.on ( "/set1=0", handleRoot);
  server.on ( "/inline", []() {
    server.send ( 200, "text/plain", "this works as well" );
  } );
  server.onNotFound ( handleNotFound );
 
  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  server.handleClient();
  // checking states work
  if (equalState == true){
    Serial.println("Equal");
  }
  else{
    Serial.println("not equal");
    }
  if(set1State == true){
    Serial.println("set 1");
  }
  else{
    Serial.println("not set 1");
    }
    delay(1000);
}