title: arduino loop don't work with two processes
body: <p>I am not much of a coder, so I am asking you guys, if you know how could I solve my problem.
  In arduino I wrote a first program  with my DS18B20 sensor, which is displaying temperature in openHAB over MQTT procotol.
  Second program is the same thing (openhab, mqtt) just switch lamp, meaning that when I press the switch, the lamp or LED turns on or off over callback function in Arduino. 
  Separately both programs works just fine, but when I am trying to join them together it doesn't work as I would like. Seems to me, that the problem is with loop function. When I press switch ON or OFF, sometimes (rarely) I can turn lamp on or off. So my question is...what is wrong? Is it problem with loop, because arduino can't procede two things in the loop in the same time? Can it be solved, I really don't know what to do next. Please help me and thanks in advance!</p>
  
  <pre><code>#include &lt;OneWire.h&gt;
  #include &lt;DallasTemperature.h&gt;
  #include &lt;SPI.h&gt;
  #include &lt;PubSubClient.h&gt;
  #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(&amp;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(&amp;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 ();
  
    }
  
  }
  </code></pre>