0

I have programmed a small software to control an LED strip using an esp. I've written a web app which sends a request to the esp which then applies the color. So far everything works, but I've got a weird problem:

When I'm running the software on the NodeMCU, everything is fast and snappy, the web app takes 500ms at max to load. When I'm running the software from an ESP-12f (schematic), it takes way longer to load, about 5 seconds. Sometimes it doesn't even load at all.

Another thing that's weird: I've got two ESP-12f, one of those won't even load very often.

I also have the feeling that the "normal" esps run hotter than the nodemcu, but that could be because they are close to the voltage regulator which gets hot

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ArduinoOTA.h>
#include <colorWheel.h>
#include <web_iro_js.h>

#define RED_LED 14
#define GREEN_LED 12
#define BLUE_LED 5

#define BUILTIN_LED 2

const char* ssid = "---";
const char* password = "---";

IPAddress ip(192,168,2,111);
IPAddress gateway(192,168,2,1);
IPAddress subnet(255,255,255,0);
ESP8266WebServer server(80);

int brightness_red      = 0;
int brightness_green    = 0;
int brightness_blue     = 0;

int presets[4][3]       = {{255,119,46},{46,0,39},{0,115,23},{0,0,0}};

boolean blinking        = false;
boolean fading          = false;
boolean jumping         = false;

int blinkDelay[]        = {50, 145, 240, 335, 430, 525, 620, 715, 810, 905};
int speedPosition       = 4;

int fadeOrder[]       = {RED_LED, GREEN_LED, BLUE_LED};
int lastFade          = 0;    //0~2, refers to fadeOrder[]
int thisFade          = 1;    //0~2, refers to fadeOrder[]
int fadeSpeed[]       = {1, 1, 1, 2, 3, 4, 5, 5, 7, 9};

boolean blinkHelper = false;

void handleRequest();
void handleGet();
void setupOTA();
void connectToWiFi();
void turnOff();
void turnOn();
void applyColor();
void applyPreset();
void blink();
void fade();
void jump();

void setup() {
    Serial.begin(115200);
    delay(10);

    analogWriteRange(255);

    pinMode(BUILTIN_LED, OUTPUT);
    digitalWrite(BUILTIN_LED, LOW);

    pinMode(RED_LED, OUTPUT);
    pinMode(GREEN_LED, OUTPUT);
    pinMode(BLUE_LED, OUTPUT);

    analogWrite(RED_LED, 0);
    analogWrite(GREEN_LED, 0);
    analogWrite(BLUE_LED, 0);

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

    WiFi.begin(ssid, password);
    WiFi.config(ip, gateway, subnet);

    while(WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println();
    Serial.println(WiFi.localIP());

    setupOTA();

    server.on("/request", handleRequest);
    server.on("/get", handleGet);

    server.on("/", HTTP_GET, []() {
        server.send(200, "text/html", GUI);
    });

    server.on("/iro.min.js", HTTP_GET, []() {
        server.send_P(200, "application/javascript", IRO_JS);
    });

    server.begin();

    Serial.println("Setup done");
    pinMode(BUILTIN_LED, OUTPUT);
    digitalWrite(BUILTIN_LED, HIGH);    
}

void loop() {
    if (WiFi.status() != WL_CONNECTED) {
        //Serial.println("no connection");
        connectToWiFi();
    }
    ArduinoOTA.handle();
    server.handleClient();

    if (blinking) {
        blink();
    }

    if (fading) {
        fade();
    }

    if (jumping) {
        jump();
    }

    if (blinking || fading || jumping) {
        delay(blinkDelay[speedPosition]);
    } else {
        delay(20);
    }
}

void handleRequest() {
    if (!server.hasArg("mode")) {
        Serial.println("no mode specified");
        server.send(400, "text/plain", "no mode specified");
        return;
    }

    String request = server.arg("mode");

    if (request.equals("color")) {
        fading = false;
        jumping = false;
        applyColor();

    } else if (request.equals("preset")) {
        fading = false;
        jumping = false;
        applyPreset();

    } else if (request.equals("blink")) {
        if (blinking) {
            blinking = false;
        } else {
            turnOff();
            fading = false;
            jumping = false;
            blinking = true;
        }
        server.send(200, "text/plain", "applied blinking");
    } else if (request.equals("fade")) {
        if (fading) {
            fading = false;
        } else {
            turnOff();
            blinking = false;
            jumping = false;
            fading = true;
        }
        server.send(200, "text/plain", "applied fading");
    } else if (request.equals("jump")) {
        if (jumping) {
            jumping = false;
        } else {
            turnOff();
            blinking = false;
            fading = false;
            jumping = true;
        }
        server.send(200, "text/plain", "applied jumping");
    } else if (request.equals("speed")) {
        if (!server.hasArg("newSpeed")) {
            server.send(400, "text/plain", "no speed specified");
            return;
        }
        int speed = server.arg("newSpeed").toInt();
        speedPosition = 9 - speed;
        server.send(200, "text/plain", "applied speed");
    } else {
        server.send(400, "text/plain", "argument not found");
    }
}

void handleGet() {
    if (!server.hasArg("arg")) {
        Serial.println("no argument specified");
        server.send(400, "text/plain", "no argument specified");
        return;
    }
    String request = server.arg("arg");
    String answer = "";

    if (request.equals("current")) {
        answer += (String(brightness_red));
        answer += (";" + String(brightness_green)); 
        answer += (";" + String(brightness_blue)); 
        answer += (";" + String(speedPosition));

    } else if (request.equals("presets")) {
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 3; j++) {
                answer += String(presets[i][j]);
                answer += ";";
            }
        }
    } else {
        server.send(400, "text/plain", "argument not found");
    }
    server.send(200, "text/plain", answer);
}

void setupOTA() {
    ArduinoOTA.setHostname("ESP8266");
    ArduinoOTA.setPassword("password");

    ArduinoOTA.onStart([]() {
        Serial.println("Start");
    });
    ArduinoOTA.onEnd([]() {
        Serial.println("\nEnd");
    });
    ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
        Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
    });
    ArduinoOTA.onError([](ota_error_t error) {
        Serial.printf("Error[%u]: ", error);
        if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
        else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
        else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
        else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
        else if (error == OTA_END_ERROR) Serial.println("End Failed");
    });
    ArduinoOTA.begin();
}

void connectToWiFi() {
  Serial.println("WiFi connection lost, reconnecting");
  WiFi.begin(ssid, password);
  WiFi.config(ip, gateway, subnet);

  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("Wifi reconnected");
}

void applyColor() {
    Serial.println("applying new color ");
    brightness_red = 0;
    if (server.hasArg("red")) {
        brightness_red = server.arg("red").toInt();
    }

    brightness_green = 0;
    if (server.hasArg("green")) {
        brightness_green = server.arg("green").toInt();
    }

    brightness_blue = 0;
    if (server.hasArg("blue")) {
        brightness_blue = server.arg("blue").toInt();
    }

    turnOn();
    server.send(200, "text/plain", "applied new color");
}

void applyPreset() {
    if (!server.hasArg("index")) {
        Serial.println("no index specified");
        server.send(400, "text/plain", "no index specified");
        return;
    }

    int index = server.arg("index").toInt();

    if (server.hasArg("save")) {
        Serial.println("saving preset");
        presets[index][0] = brightness_red;
        presets[index][1] = brightness_green;
        presets[index][2] = brightness_blue;
        server.send(200, "text/plain", "preset saved");
    } else {
        Serial.println("applying preset");
        brightness_red = presets[index][0];
        brightness_green = presets[index][1];
        brightness_blue = presets[index][2];

        turnOn();
        server.send(200, "text/plain", "preset applied");
    }
}

migrated from stackoverflow.com Jul 7 at 2:32

This question came from our site for professional and enthusiast programmers.

  • has the esp-12f a good antenna and a good reception? sometimes rotating the module with antenna towards the location of the AP makes wonder – Juraj Jul 7 at 6:09
  • @Juraj when pinging the boards they take about the same time to respond, so I think yes – matthesinator Jul 7 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.