title: Why Wemos D1 not respond after working for a while
body: <p>I’m trying to build a feeder machine on a pig farm by using the Infrared photoelectric switch Sensor E18-D80NK to control relay then send notifications to line and mqtt broker when it’s working. I have been testing for 2 days, it’s working normal, but third day, it’s not responding then I just press reset so it’s working again.
  I’m wondering it may cause by board cause it happen only one of four boards that I tasted or may be my code cause I was confused why output to line send “connected” so many times it’s should sent only one time when connected to wifi, so I’m not sure pls help.</p>
  <p><strong>Question</strong></p>
  <ol>
  <li>Why board not responding after working normal for a while.</li>
  <li>Why out output to line send &quot;connected&quot; more than one cause I put in <code>void setup()</code></li>
  </ol>
  <p><strong>Pictures</strong></p>
  <p><a href="https://i.stack.imgur.com/QiCnR.jpg" rel="nofollow noreferrer">Last line output since working</a></p>
  <p><strong>All code</strong></p>
  <p>extra library <a href="https://github.com/TridentTD/TridentTD_LineNotify" rel="nofollow noreferrer">line notification library</a></p>
  <pre><code>#include &lt;ESP8266WiFi.h&gt;
  #include &lt;PubSubClient.h&gt;
  #include &lt;TridentTD_LineNotify.h&gt;
  
  const char* ssid = &quot;Artnaseaw-2.4G&quot;;
  const char* password =  &quot;0812667120&quot;;
  const char* mqttUser = &quot;****&quot;;
  const char* mqttPassword = &quot;****&quot;;
  
  const char* mqttServer = &quot;******&quot;;
  const int mqttPort = *****;
  
  const char* linetoken = &quot;********&quot;;
  
  long lastMsg = 0;
  String DataString;
  int lastL=0;    
  
  WiFiClient espClient;
  PubSubClient client(espClient);
  
  const int ACTION = D8;
  
  const int SENSOR = D2;
  
  const char* host = &quot;maker.ifttt.com&quot;;
  const char *privateKey = &quot;******&quot;;
  const char *event = &quot;*****&quot;;
  String value1, value2;
  String postData = &quot;&quot;;
  String MachineID=&quot;A01&quot;;
  
  void setup() {
    Serial.begin(115200);
    pinMode(SENSOR, INPUT_PULLUP);
    pinMode(ACTION, OUTPUT);
  
    digitalWrite(SENSOR, LOW);
  
    WiFi.begin(ssid, password);
  
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.println(&quot;Connecting to WiFi..&quot;);
    }
  
    Serial.println(&quot;Connected to the WiFi network&quot;);
  
    client.setServer(mqttServer, mqttPort);
  
    client.setCallback(callback);
  
    while (!client.connected()) { 
      Serial.println(&quot;Connecting to MQTT...&quot;);
  
      if (client.connect(&quot;test&quot;, mqttUser, mqttPassword )) { 
  
        Serial.println(&quot;connected&quot;);
  
      } else {
  
        Serial.print(&quot;failed with state &quot;);
        Serial.print(client.state());
        delay(2000);
  
      }
    }
  
  
    client.subscribe(&quot;command&quot;);  
    LINE.setToken(linetoken);
    LINE.notify(&quot;Connected&quot;);
    Serial.println(&quot;line send connected&quot;);
  }
  
  void callback(char* topic, byte* payload, unsigned int length) {
  
    Serial.print(&quot;Message arrived in topic: &quot;);
    Serial.println(topic);
  
    Serial.print(&quot;Message:&quot;);
    for (int i = 0; i &lt; length; i++) {
      Serial.print((char)payload[i]);
    }
    Serial.println();
    Serial.println(&quot;-----------------------&quot;);
  
  }
  
  
  void loop() {
    client.loop();
  
    delay(5000);
  
    int sensorValue = digitalRead(SENSOR);
  
       long now = millis();
    if (now - lastMsg &gt; 60000) 
    {
      lastMsg = now;
  
      int sensorValue = digitalRead(SENSOR);
    if (sensorValue == LOW){
      Serial.println(&quot; === Obstacle detected&quot;);
      Serial.print(&quot; realy OFF &quot;);
      digitalWrite(ACTION,LOW);
    } else {
       LINE.notify(&quot;send data to line ---working&quot;);
       Serial.println(&quot; === All Clear&quot;);
       Serial.println(&quot; relay ON &quot;);
       digitalWrite(ACTION,HIGH);
       sendMessageToIFTTT();
       sendSensorToMQTT();
      }
    }
  }
  void sendSensorToMQTT(){
    char msg[100];
    int count = 1;
    DataString = &quot;e18,location=test detest=&quot;+String(count);
    DataString.toCharArray(msg, 100);
    Serial.print(&quot;Publish message: &quot;);
    Serial.println(msg);
    client.publish(&quot;e18&quot;, msg);   
  }
  
  void sendMessageToIFTTT(){
  
    WiFiClient client;
    const int httpPort = 80;
    if (!client.connect(host, httpPort)) {
      Serial.println(&quot;connection failed&quot;);
      return;
    }
  
    String url = &quot;/trigger/&quot;;
    url += event;
    url += &quot;/with/key/&quot;;
    url += privateKey;
  
    value1 = &quot;Worked&quot;;
    value2 = &quot;1&quot;;
  
    genJSonObject();
  
    Serial.print(&quot;Requesting URL: &quot;);
    Serial.println(url);
    Serial.println(postData);
    
    client.print(String(&quot;GET &quot;) + url + &quot; HTTP/1.1\r\n&quot; +
                 &quot;Host: &quot; + host + &quot;\r\n&quot;);
  
    client.println(&quot;Content-Type: application/json&quot;);
    client.print(&quot;Content-Length: &quot;);
    client.println(postData.length());
    client.println();
    client.println(postData);
    client.println(&quot;Connection: close\r\n\r\n&quot;);
  
    while (client.connected())
    {
      if (client.available())
      {
        String line = client.readStringUntil('\r');
        Serial.print(line);
      } else {
        // No data yet, wait a bit
        delay(50);
      };
    }
  }
  
  
  void genJSonObject()
  {
    postData = &quot;{&quot;;
  
    postData.concat(&quot;\&quot;value1\&quot;:\&quot;&quot;);
    postData.concat(value1);
  
  
    postData.concat(&quot;\&quot;,&quot;);
    postData.concat(&quot;\&quot;value2\&quot;:\&quot;&quot;);
    postData.concat(value2);
  
    postData.concat(&quot;\&quot;}&quot;);
  }
  </code></pre>