Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Luís Magalhães
#27384 Hey guys,

I've been getting example codes from all around the forums and I did my own code, but now my buttons aren't working as intended. My code is the following:

Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <Adafruit_NeoPixel.h>

#define PIN 14

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(5, PIN, NEO_GRB + NEO_KHZ800);
 
const char* ssid     = "TheInventorsCore";

const char* html = "<html>\
  <head>\
    <meta http-equiv='refresh' content='5'/>\
    <title>The Inventors Core Demo</title>\
    <style>\
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
    </style>\
  </head>\
  <body>\
    <h1 align='center'>The Inventors Core</h1>\
    <div style='width:100%;height:100%;position:absolute;vertical-align:middle;text-align:center;'>\
    <form action='/red'>\
      <button type='submit' align='center' style='font-size:50px;width:50%; height:20%'>Red LEDs</button><br><br>\
    </form>\
    <form action='/green'>\
     <button type='submit' align='center' style='font-size:50px;width:50%; height:20%'>Green LEDs</button><br><br>\
    </form>\
    <form action='/off'>\
      <button type='submit' align='center' style='font-size:50px;width:50%; height:20%'>Turn Off</button>\
    </form>\
    </div>\
  </body>\
</html>";

const byte DNS_PORT = 53;
IPAddress apIP(192, 168, 1, 1);
IPAddress netMsk(255, 255, 255, 0);
DNSServer dnsServer;
ESP8266WebServer server(80);
String HTTP_req;

void setup() {
  pinMode(0, OUTPUT);
  pinMode(2, OUTPUT);
  Serial.begin(115200);
  Serial.setDebugOutput(true);
  WiFi.mode(WIFI_AP);
  WiFi.softAPConfig(apIP, apIP, netMsk);
  WiFi.softAP(ssid);
  Serial.print("SSID: ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.softAPIP());
  dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
  dnsServer.start(DNS_PORT, "*", apIP);
  Serial.println("USP Server started");
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  strip.setBrightness(20);
  server.on("/", handle_root);
  server.on("/generate_204", handle_root);  //Android captive portal
  server.on ( "/red", drawRed);
  server.on ( "/green", drawGreen);
  server.on ( "/off", turnOff);
  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("HTTP server started");

}

void loop() {
  dnsServer.processNextRequest();
  server.handleClient();
  if(HTTP_req.indexOf("draw_green") > -1)
  {
    drawGreen();
  }
}

void handleNotFound() {
  Serial.print("\t\t\t\t URI Not Found: ");
  Serial.println(server.uri());
  server.send ( 200, "text/plain", "URI Not Found" );
}

void handle_root() {
  Serial.println("Page served");
  String toSend = html;
  server.send(200, "text/html", toSend);
  delay(100);
}

void drawRed(){
  for(int i=0;i<5;i++)
  {
    strip.setPixelColor(i,strip.Color(255, 0, 0));
  }
  strip.show();
  handle_root();
}

void drawGreen(){
  for(int i=0;i<5;i++)
  {
    strip.setPixelColor(i,strip.Color(0, 255, 0));
  }
  strip.show();
  handle_root();
}

void turnOff(){
  for(int i=0;i<5;i++)
  {
    strip.setPixelColor(i,strip.Color(0, 0, 0));
  }
  strip.show();
  handle_root();
}



My buttons are changing the URL of the page, which makes it impossible for two users to be using the same webapp, because once they change the URL, even if they don't do anything, that action will reload from time to time, reforcing the action. I've tried to change the buttons and I've been able to change one of them by doing it like this:

Code: Select all...

    <script>\
       function drawGreen()\
       {\
          var request = new XMLHttpRequest();\
          request.open('GET', 'draw_green', true);\
          request.send(null);\
        }\
    </script>\
...
<form>\
     <button type='button' onclick='drawGreen()' align='center' style='font-size:50px;width:50%; height:20%'>Green LEDs</button><br><br>\
    </form>\
...
server.on ( "/draw_green", drawGreen);
...


And this works. But if I try to do the same for the other buttons, the code won't compile. It will just spit out alot of Java errors and won't let me compile the code :s Does anyone know how I can get around this problem? Thanks
User avatar
By Luís Magalhães
#27467 But that's the problem, I get no concrete error. It's like there's a line in one of the libs that just goes haywire and it spits out a ton of java errors. I can't scroll up far enough to see the initial error, so I have no idea what is making the code break. But I do know that if I comment out the html variable the error goes away and the IDE lets me compile again