我将如何优化此代码,以便我的arduino nodemcu一段时间后不会崩溃

时间:2019-03-26 11:48:34

标签: c++ arduino esp8266 nodemcu arduino-esp8266

我有这个物联网项目,它控制着变色龙的饲养容器。它具有一个连接到D5和D6引脚的继电器以控制灯光。我的Nodemcu 0.9在约20个小时后停止工作(此时灯熄灭并且无法重新打开,我总是必须重置Nodemcu)。

我一直在阅读堆栈,发现字符串使用了大量的堆内存,因此我删除了所有字符串,但是问题仍然存在。

main.cpp

AppRegistry.registerComponent('AFApplication', () => <DevMenuTrigger>{SimpleApp}</DevMenuTrigger>);

Credentials.h

#include <FirebaseArduino.h>
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiUdp.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>

#include "credentials.h"

#define DHTPIN D7
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);
double previousTemperature = 0.0;
double previousHumidity = 0.0;

boolean overRide = false;
boolean uvStatus;
boolean irStatus;

int baskingTime;
int startHour;
int startMinute;
int endTime;

int startTimeMinutes;
int endTimeMinutes;

const int uvPin = D5;
const int irPin = D6;

const char *ssid = "Waifas 2.4";
const char *password = "lsmustud";

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 7200, 60000);

void setup()
{
  pinMode(uvPin, OUTPUT);
  pinMode(irPin, OUTPUT);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
  }

  timeClient.begin();

  //initialize firebase
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);

  Firebase.setBool(OVERRIDE, false);
  baskingTime = Firebase.getInt(BASKING_TIME);
  startHour = Firebase.getInt(START_HOUR);
  startMinute = Firebase.getInt(START_MINUTE);
  uvStatus = Firebase.getBool(UV_STATUS);
  irStatus = Firebase.getBool(IR_STATUS);

  startTimeMinutes = startHour * 60 + startMinute;
  endTimeMinutes = startHour * 60 + startMinute + baskingTime * 60;

  Firebase.stream(DB_PATH);

  dht.begin();
}

void loop()
{
  timeClient.update();
  int clientHour = timeClient.getHours();
  int clientMinute = timeClient.getMinutes();
  int currentTimeMinutes = clientHour * 60 + clientMinute;

  if (Firebase.failed())
  {
    delay(1000);
    Firebase.stream(DB_PATH);
    return;
  }

  if (Firebase.available())
  {
    FirebaseObject event = Firebase.readEvent();
    String eventPath = event.getString("path");
    eventPath.toLowerCase();
    if (eventPath == "/override")
    {
      overRide = Firebase.getBool(OVERRIDE);
    }
    if (eventPath == "/uvstatus")
    {
      uvStatus = Firebase.getBool(UV_STATUS);
    }
    if (eventPath == "/irstatus")
    {
      irStatus = Firebase.getBool(IR_STATUS);
    }
    if (eventPath == "/baskingtime" || eventPath == "/starthour" || eventPath == "/startminute")
    {
      baskingTime = Firebase.getInt(BASKING_TIME);
      startHour = Firebase.getInt(START_HOUR);
      startMinute = Firebase.getInt(START_MINUTE);

      endTime = startHour + baskingTime;

      if (endTime >= 24)
      {
        endTime = endTime - 24;
      }

      startTimeMinutes = startHour * 60 + startMinute;
      endTimeMinutes = endTime * 60 + startMinute;
    }
  }

  if (!overRide)
  {
    if (startTimeMinutes <= currentTimeMinutes && currentTimeMinutes <= endTimeMinutes)
    {
      if (!uvStatus)
      {
        Firebase.setBool(UV_STATUS, true);
        uvStatus = true;
      }
      if (!irStatus)
      {
        Firebase.setBool(IR_STATUS, true);
        irStatus = true;
      }
    }
    else
    {
      if (uvStatus)
      {
        Firebase.setBool(UV_STATUS, false);
        uvStatus = false;
      }
      if (irStatus)
      {
        Firebase.setBool(IR_STATUS, false);
        irStatus = false;
      }
    }
  }

  if (uvStatus)
  {
    digitalWrite(uvPin, LOW);
  }
  else
  {
    digitalWrite(uvPin, HIGH);
  }

  if (irStatus)
  {
    digitalWrite(irPin, LOW);
  }
  else
  {
    digitalWrite(irPin, HIGH);
  }

  double t = dht.readTemperature();
  double h = dht.readHumidity();

  if (isnan(h) || isnan(t))
  {
  }
  else
  {
    if (h != previousHumidity)
    {
      previousTemperature = h;
      Firebase.setFloat(HUMIDITY, h);
    }
    if (t != previousTemperature)
    {
      previousTemperature = t;
      Firebase.setFloat(TEMPERATURE, t);
    }
  }
  delay(1000);
}

您能帮我解决这个问题吗?我不是程序员,而是业余爱好者,所以我真的很难弄清楚这一点。

0 个答案:

没有答案