2017-03-08 26 views
0

我不是一個編碼器,所以我問你們,如果你知道我怎麼能解決我的問題。 在arduino中,我用DS18B20傳感器編寫了第一個程序,該程序通過MQTT網絡程序在openHAB中顯示溫度。 第二個程序是同樣的東西(openhab,mqtt)只是開關燈,這意味着當我按下開關時,燈或LED通過Arduino中的回調函數打開或關閉。 另外這兩個程序工作得很好,但是當我試圖將它們連接在一起時,它不起作用,因爲我想。在我看來,問題在於循環功能。當我按下開關ON或OFF時,有時(很少)我可以打開或關閉指示燈。所以我的問題是...什麼是錯的?循環是否有問題,因爲arduino不能同時在循環中處理兩件事情?它可以解決,我真的不知道下一步該怎麼做。請幫助我,並提前致謝!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(); 

    } 

} 
+0

編輯的問題,包括你的代碼,所以我們不必猜測 – hardillb

+0

有沒有** **過程中的Arduino。 *你的意思是「arduino不能在同一時間在循環中處理兩件事情」*? Arduino按照您指定的順序依次執行每個事件**。 –

回答

0

您是否有串口輸出示例?

此外,爲了您的reconnect()功能,我認爲串口應該是您的ESP8266輸出的Serial1,因爲您在設置中使用了WiFi.init(& Serial1)。

在循環中,你有

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

它循環,看看客戶端連接,如果不是那麼它會嘗試重新連接。重新連接時會嘗試連接,如果不能連接,則每次無法連接時會等待5秒。這可能是你的問題。

你有兩個分開的程序的代碼?

+0

tnx回答奧馬爾,但正如我測試重新連接沒有任何影響。問題是無論我在循環函數中寫入「client.loop();」在呈現中,它停止工作。那麼,當client.loop在那裏時,我可能沒有任何其他的東西了? –

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(); 

} 

Picture of serial monitor for both examples joined together