在单个ESP-12f上使用相同软件的速度比在NodeMCU上慢

时间:2019-07-05 19:48:42

标签: performance esp8266 nodemcu

我编写了一个小软件,用esp控制LED灯带。我已经编写了一个Web应用程序,该程序向esp发送请求,然后再应用颜色。到目前为止,一切正常,但是我遇到了一个奇怪的问题:

当我在NodeMCU上运行该软件时,一切都很快速且敏捷,该Web应用程序最多需要500毫秒才能加载。当我从ESP-12f (schematic)运行该软件时,加载时间更长,大约5秒钟。有时甚至根本没有加载。

另一件事很奇怪:我有两个ESP-12f,其中一个甚至不会经常加载。

我还感觉到,“正常” esps的运行温度比nodemcu高,但这可能是因为它们靠近变热的稳压器

#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");
    }
}

0 个答案:

没有答案