RPi + ESP8266稳定性问题

时间:2017-10-23 14:54:07

标签: raspberry-pi3 esp8266 home-automation

我最近正在研究一个家庭自动化项目,该项目终于结束了,我正在考虑将它安装在我的家中。首先,我想向您介绍基本架构。

我使用Raspberry Pi 3作为运行Node-Red及其Mosca调色板的中央控制器节点。目前,该项目共有5个ESP-01。其中四个用继电器接线,剩下的ESP用DHT11温度传感器接线。几乎一切都运行良好,但我面临一些稳定性问题,例如,当我回收电源时,ESP-01不运行程序。串行监视器保持空白。但是,当我断开GPIO2与继电器的连接然后打开ESP电源时。该计划开始。所以,我必须拔出GPIO2然后给ESP加电,然后在每次电源循环时将GPIO2与继电器连接起来。我面临的另一个问题是ESP有时会自动崩溃。它有时会打印致命异常(0)或软wdt重置,即使我已经添加了一个看门狗定时器。

以下是其中一个客户端ESP的代码:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "........";
const char* password = ".........";
const int led = 13;

const char* mqtt_server = "192.168.1.8";

WiFiClient espClient;
PubSubClient client(espClient);

const int ledGPIO2 = 2;

void setup_wifi() {
  int i;
  delay(10);

  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("WIFI BEGUN");
  while (WiFi.status() != WL_CONNECTED) {
    ESP.wdtFeed();
    delay(500);
    i++;
    if ((i&0x01)==0){
      digitalWrite(led, 0);
    } else {
      digitalWrite(led, 1);     // led should start blinking at .5 seconds
    }
    Serial.print(".");
    if (i>1000) break;     // get out after 50 seconds

    if (i==1000){

    }
    Serial.print(".");

    Serial.println("");
    Serial.print("WiFi connected - ESP IP address: ");
    Serial.println(WiFi.localIP());
  }
}

void callback(String topic, byte* message, unsigned int length) {
  Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  String messageTemp;

  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  Serial.println();

  if(topic=="Lamp1"){
      Serial.print("Changing GPIO 2 to ");
      if(messageTemp == "on"){
        digitalWrite(ledGPIO2, HIGH);
        Serial.print("On");
      }
  else if(messageTemp == "off"){
    digitalWrite(ledGPIO2, LOW);
    Serial.print("Off");
  }
  }
  Serial.println();
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    if (client.connect("ESP8266Client")) {
      Serial.println("connected");  

      client.subscribe("Lamp1");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
  // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  pinMode(ledGPIO2, OUTPUT);
  digitalWrite(ledGPIO2, true);
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);    
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  if(!client.loop())
    client.connect("ESP8266Client");
}

另外,我一直在考虑为ESP提供高效的电源。电池长期不可靠并且通过适配器加电是不可行的,因为模块将安装在墙壁上。此外,交流到直流转换器似乎是一种适合供电的方式。

Vcc - 3.3V
CH_PD - 3.3V
Tx - Tx(Arduino)
Rx - Rx(Arduino)
GPIO0 - GND(上传草图时)/ 3.3V
GND - GND

我没有使用电容器或电阻器。我从Arduino获得5V电源,使用LD33V稳压器调节到3.3V。

任何建议将不胜感激。谢谢!!

0 个答案:

没有答案
相关问题