0

我不是一个编码员,所以我问你们,如果你们知道我该如何解决我的问题。在 arduino 中,我用我的 DS18B20 传感器编写了第一个程序,它通过 MQTT 协议在 openHAB 中显示温度。第二个程序是同一件事(openhab,mqtt)只是开关灯,这意味着当我按下开关时,灯或LED通过Arduino中的回调函数打开或关闭。单独两个程序都可以正常工作,但是当我尝试将它们连接在一起时,它并不能按我的意愿工作。在我看来,问题出在循环功能上。当我按下开关 ON 或 OFF 时,有时(很少)我可以打开或关闭灯。所以我的问题是……怎么了?循环有问题吗,因为 arduino 不能同时在循环中处理两件事?能不能解决,真不知道接下来该怎么办。

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <PubSubClient.h>
#include "WiFiEsp.h"

// Emulate Serial1 on pins 3/2 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(3, 2); // RX, TX
#endif

char ssid[] = "SSID;     // your network SSID (name)
char pwd[] = "password";  // your network password

float temp = 0;

byte server[] = { 192, 168, 1, 71 }; // IP Address of your MQTT Server
WiFiEspClient espClient;
PubSubClient client(espClient);


#define ONE_WIRE_BUS 4
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);



void callback(char* topic, byte* payload, unsigned int length) {
Serial.println("Callback");

Serial.println(topic);
Serial.println(length);
Serial.write(payload,length);
Serial.println();

 if (strcmp(topic,"home/luc")==0) { 
   if (payload[0] == '0') 
    {
    digitalWrite(7, LOW);
    delay(100);
    client.publish("home/luc/state","OFF");
  }  
  else if (payload[0] == '1')
  {

  digitalWrite(7, HIGH);  
  delay(100);
  client.publish("home/luc/state","ON");
    }

}
}



void temperaturni_senzor ()
{
  sensors.requestTemperatures();  
  temp = sensors.getTempCByIndex(0);
  Serial.print("Temperatura je: ");
  Serial.println(temp); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
  //Update value every 1 sec.


  client.publish("home/temperature", String(temp).c_str(),TRUE);
  delay(50);
  }
void reconnect() {
 // Loop until we're reconnected
 while (!client.connected()) {
 Serial.print("connecting to mqtt...");
 // Attempt to connect
 if (client.connect("ESP8266 client")) {
  Serial.println("connection good");
  // ... and subscribe to topic
  client.subscribe("home/luc");
 } 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()
{
  Serial1.begin(9600);
  Serial.begin(9600); //Begin serial communication
  WiFi.init(&Serial1);
  WiFi.begin(ssid, pwd);
  client.setServer(server, 1883);
  client.setCallback(callback);

 if (client.connect("arduinoClient")) 
  {
    Serial.println ("mqqt is connected");
     client.publish("outTopic","hsubscribello world");
      client.subscribe("home/luc");  // Subscribe to all messages for this device
      client.subscribe("home/temperature");
      }
    pinMode(7, OUTPUT);
    digitalWrite(7, LOW);
  sensors.begin();

}

void loop() 
{
 client.loop();

  if (!client.connected()) 
  {
  reconnect();
  }

     temperaturni_senzor ();

  }

}
4

2 回答 2

0

你有串行输出的样本吗?

同样对于您的 reconnect() 功能,我认为您的 ESP8266 输出的 Serial 应该是 Serial1,因为您在设置中使用了 WiFi.init(&Serial1)。

在循环中,您有

if (!client.connected()) { reconnect(); }

它循环查看客户端是否连接,如果没有,则尝试重新连接。在重新连接时,它会尝试连接,如果无法连接,则每次无法连接时等待 5 秒。这可能是你的问题。

你有两个分开的程序的代码吗?

于 2017-03-09T02:25:11.220 回答
0

这是 DS18B20 的代码,它可以工作

#include <OneWire.h>
    #include <DallasTemperature.h>
    #include <SPI.h>
    #include <PubSubClient.h>
    #include "WiFiEsp.h"
    #ifndef HAVE_HWSERIAL1
    #include "SoftwareSerial.h"
    SoftwareSerial Serial1(3, 2); // RX, TX
    #endif


    byte server[] = { 192, 168, 1, 71 }; // IP Address of your MQTT Server



    WiFiEspClient espClient;
    PubSubClient client(espClient);


    float temp = 0;
    char ssid[] = "SSID";     // your network SSID (name)
    char pwd[] = "password";  // your network password




    #define ONE_WIRE_BUS 4
    // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
    OneWire oneWire(ONE_WIRE_BUS);
    // Pass our oneWire reference to Dallas Temperature. 
    DallasTemperature sensors(&oneWire);


    void reconnect() {
      // Loop until we're reconnected
      while (!client.connected()) {
        Serial.print("Attempting MQTT connection...");
        // Attempt to connect
        if (client.connect("arduinoClient_temperature_sensor")) {
          Serial.println("connected");
        } 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()
    {
    Serial1.begin(9600);
    Serial.begin(9600); //Begin serial communication
    WiFi.init(&Serial1);
    WiFi.begin(ssid, pwd);
     client.setServer(server, 1883);
     sensors.begin();
    }

   void temp_senz ()
   {
    sensors.requestTemperatures();  
      temp = sensors.getTempCByIndex(0);
      Serial.print("Temperatura je: ");
      Serial.println(temp);
      client.publish("home/temperature", String(temp).c_str(),TRUE);
      delay(1000);
    } 
    void loop()
    { 
      {
      if (!client.connected()) {
        reconnect();
      }
      client.loop();
     temp_senz ();

      }
    }

这是用于灯的,它也可以工作。

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <PubSubClient.h>
#include "WiFiEsp.h"

// Emulate Serial1 on pins 7/6 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(3, 2); // RX, TX
#endif

char ssid[] = "SSID";     // your network SSID (name)
char pwd[] = "password";  // your network password


byte server[] = { 192, 168, 1, 71 }; // IP Address of your MQTT Server



WiFiEspClient espClient;
PubSubClient client(espClient);


void callback(char* topic, byte* payload, unsigned int length) {
Serial.println("Callback");

Serial.println(topic);
Serial.println(length);
Serial.write(payload,length);
Serial.println();

 if (strcmp(topic,"home/luc")==0) { 
   if (payload[0] == '0') 
    {
    digitalWrite(7, LOW);
    delay(100);
    client.publish("home/luc/state","OFF");
  }  
  else if (payload[0] == '1')
  {

  digitalWrite(7, HIGH);  
  delay(100);
  client.publish("home/luc/state","ON");
    }

}
}


void setup()
{
  Serial1.begin(9600);
  Serial.begin(9600); //Begin serial communication
  WiFi.init(&Serial1);
  WiFi.begin(ssid, pwd);
  client.setServer(server, 1883);
  client.setCallback(callback);

 if (client.connect("arduinoClient")) 
  {
    Serial.println ("mqqt je konektan");
     client.publish("outTopic","hsubscribello world");
      client.subscribe("home/luc");  // Subscribe to all messages for this device
      }

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

}



void loop() {

client.loop();

}

两个示例的串行监视器图片连接在一起

于 2017-03-09T16:11:09.180 回答