Node mcu v3 หยุดส่งค่าอุณหภูมิครับ

ปรึกษาหน่อยครับ node mcu v3 ผมตั้งเวลาให้ส่งค่าอุณภูมิทุก1ชั่วโมง เมื่อ2วันที่แล้วลงปรับโค้ดใหม่โดยใช้wifi managerมาช่วยเรื่องเชื่อมต่ดไวไฟ แต่ปัญหาคือการส่งข้อมูลพอหลังตี3ไปแล้วตัวnode mcu จะหยุดส่งค่าอุณภูมิ ต้องถอดปลั๊กแล้วเสียบใหม่ถึงจะทำงาน ทั้งที่ก่อนหน้านี้โค้ดเดิมไม่เจอปัญหานี้ครับ มาเป็นเมื่อตอนใช้wifi managerครับ เกิดจากอะไรครับ

 else if (t >= 8 && t <= 30 && p_tm->tm_hour == 0 && p_tm->tm_min == 0 && p_tm->tm_sec == 5)
    {

      LINE.notify("อุณหภูมิขณะนี้ " + String(t) + " ความชื้น " + String(h) + " %");
      LINE.notifySticker(1, 407);
      HTTPClient http;
      String url = "http://iot.icreate.com/sensor-bziot-api.php/?datetime='" + String(now_day) + "%20" + String(now_time) + "'&temperature='" + String(t) + "'&humidity='" + String(h) + "'&device='" + String(device) + "'";
      http.begin(url);                          // HTTP Initial
      int httpCode = http.GET();                // ทำการ GET Request ไปยัง Server      String response = myiot.WriteDashboard(userid, key, t, h, 0, 0, private_tweet, public_tweet);

    }
    else if (t >= 8 && t <= 30 && p_tm->tm_hour == 1 && p_tm->tm_min == 0 && p_tm->tm_sec == 5)
    {

      LINE.notify("อุณหภูมิขณะนี้ " + String(t) + " ความชื้น " + String(h) + " %");
      LINE.notifySticker(1, 407);
      HTTPClient http;
      String url = "http://iot.icreate.com/sensor-bziot-api.php/?datetime='" + String(now_day) + "%20" + String(now_time) + "'&temperature='" + String(t) + "'&humidity='" + String(h) + "'&device='" + String(device) + "'";
      http.begin(url);                          // HTTP Initial
      int httpCode = http.GET();                // ทำการ GET Request ไปยัง Server      String response = myiot.WriteDashboard(userid, key, t, h, 0, 0, private_tweet, public_tweet);

    }
    else if (t >= 8 && t <= 30 && p_tm->tm_hour == 2 && p_tm->tm_min == 0 && p_tm->tm_sec == 5)
    {

      LINE.notify("อุณหภูมิขณะนี้ " + String(t) + " ความชื้น " + String(h) + " %");
      LINE.notifySticker(1, 407);
      HTTPClient http;
      String url = "http://iot.icreate.com/sensor-bziot-api.php/?datetime='" + String(now_day) + "%20" + String(now_time) + "'&temperature='" + String(t) + "'&humidity='" + String(h) + "'&device='" + String(device) + "'";
      http.begin(url);                          // HTTP Initial
      int httpCode = http.GET();                // ทำการ GET Request ไปยัง Server      String response = myiot.WriteDashboard(userid, key, t, h, 0, 0, private_tweet, public_tweet);

    }
    else if (t >= 8 && t <= 30 && p_tm->tm_hour == 3 && p_tm->tm_min == 0 && p_tm->tm_sec == 5)
    {

      LINE.notify("อุณหภูมิขณะนี้ " + String(t) + " ความชื้น " + String(h) + " %");
      LINE.notifySticker(1, 407);
      HTTPClient http;
      String url = "http://iot.icreate.com/sensor-bziot-api.php/?datetime='" + String(now_day) + "%20" + String(now_time) + "'&temperature='" + String(t) + "'&humidity='" + String(h) + "'&device='" + String(device) + "'";
      http.begin(url);                          // HTTP Initial
      int httpCode = http.GET();                // ทำการ GET Request ไปยัง Server      String response = myiot.WriteDashboard(userid, key, t, h, 0, 0, private_tweet, public_tweet);

    }
    else if (t >= 8 && t <= 30 && p_tm->tm_hour == 4 && p_tm->tm_min == 0 && p_tm->tm_sec == 5)
    {

      LINE.notify("อุณหภูมิขณะนี้ " + String(t) + " ความชื้น " + String(h) + " %");
      LINE.notifySticker(1, 407);
      HTTPClient http;
      String url = "http://iot.icreate.com/sensor-bziot-api.php/?datetime='" + String(now_day) + "%20" + String(now_time) + "'&temperature='" + String(t) + "'&humidity='" + String(h) + "'&device='" + String(device) + "'";
      http.begin(url);                          // HTTP Initial
      int httpCode = http.GET();                // ทำการ GET Request ไปยัง Server      String response = myiot.WriteDashboard(userid, key, t, h, 0, 0, private_tweet, public_tweet);

    }
    else if (t >= 8 && t <= 30 && p_tm->tm_hour == 5 && p_tm->tm_min == 0 && p_tm->tm_sec == 5)
    {

      LINE.notify("อุณหภูมิขณะนี้ " + String(t) + " ความชื้น " + String(h) + " %");
      LINE.notifySticker(1, 407);
      HTTPClient http;
      String url = "http://iot.icreate.com/sensor-bziot-api.php/?datetime='" + String(now_day) + "%20" + String(now_time) + "'&temperature='" + String(t) + "'&humidity='" + String(h) + "'&device='" + String(device) + "'";
      http.begin(url);                          // HTTP Initial
      int httpCode = http.GET();                // ทำการ GET Request ไปยัง Server      String response = myiot.WriteDashboard(userid, key, t, h, 0, 0, private_tweet, public_tweet);

    }
    else if (t >= 8 && t <= 30 && p_tm->tm_hour == 6 && p_tm->tm_min == 0 && p_tm->tm_sec == 5)
    {

      LINE.notify("อุณหภูมิขณะนี้ " + String(t) + " ความชื้น " + String(h) + " %");
      LINE.notifySticker(1, 407);
      HTTPClient http;
      String url = "http://iot.icreate.com/sensor-bziot-api.php/?datetime='" + String(now_day) + "%20" + String(now_time) + "'&temperature='" + String(t) + "'&humidity='" + String(h) + "'&device='" + String(device) + "'";
      http.begin(url);                          // HTTP Initial
      int httpCode = http.GET();                // ทำการ GET Request ไปยัง Server      String response = myiot.WriteDashboard(userid, key, t, h, 0, 0, private_tweet, public_tweet);

    }
    else if (t >= 8 && t <= 30 && p_tm->tm_hour == 7 && p_tm->tm_min == 0 && p_tm->tm_sec == 5)
    {

      LINE.notify("อุณหภูมิขณะนี้ " + String(t) + " ความชื้น " + String(h) + " %");
      LINE.notifySticker(1, 407);
      HTTPClient http;
      String url = "http://iot.icreate.com/sensor-bziot-api.php/?datetime='" + String(now_day) + "%20" + String(now_time) + "'&temperature='" + String(t) + "'&humidity='" + String(h) + "'&device='" + String(device) + "'";
      http.begin(url);                          // HTTP Initial
      int httpCode = http.GET();                // ทำการ GET Request ไปยัง Server      String response = myiot.WriteDashboard(userid, key, t, h, 0, 0, private_tweet, public_tweet);

    }
    else if (t >= 8 && t <= 30 && p_tm->tm_hour == 8 && p_tm->tm_min == 0 && p_tm->tm_sec == 5)
    {

      LINE.notify("อุณหภูมิขณะนี้ " + String(t) + " ความชื้น " + String(h) + " %");
      LINE.notifySticker(1, 407);
      HTTPClient http;
      String url = "http://iot.icreate.com/sensor-bziot-api.php/?datetime='" + String(now_day) + "%20" + String(now_time) + "'&temperature='" + String(t) + "'&humidity='" + String(h) + "'&device='" + String(device) + "'";
      http.begin(url);                          // HTTP Initial
      int httpCode = http.GET();                // ทำการ GET Request ไปยัง Server      String response = myiot.WriteDashboard(userid, key, t, h, 0, 0, private_tweet, public_tweet);

    }
    else if (t >= 8 && t <= 30 && p_tm->tm_hour == 9 && p_tm->tm_min == 0 && p_tm->tm_sec == 5)
    {

      LINE.notify("อุณหภูมิขณะนี้ " + String(t) + " ความชื้น " + String(h) + " %");
      LINE.notifySticker(1, 407);
      HTTPClient http;
      String url = "http://iot.icreate.com/sensor-bziot-api.php/?datetime='" + String(now_day) + "%20" + String(now_time) + "'&temperature='" + String(t) + "'&humidity='" + String(h) + "'&device='" + String(device) + "'";
      http.begin(url);                          // HTTP Initial
      int httpCode = http.GET();                // ทำการ GET Request ไปยัง Server      String response = myiot.WriteDashboard(userid, key, t, h, 0, 0, private_tweet, public_tweet);

    }
    else if (t >= 8 && t <= 30 && p_tm->tm_hour == 10 && p_tm->tm_min == 0 && p_tm->tm_sec == 5)
    {

      LINE.notify("อุณหภูมิขณะนี้ " + String(t) + " ความชื้น " + String(h) + " %");
      LINE.notifySticker(1, 407);
      HTTPClient http;
      String url = "http://iot.icreate.com/sensor-bziot-api.php/?datetime='" + String(now_day) + "%20" + String(now_time) + "'&temperature='" + String(t) + "'&humidity='" + String(h) + "'&device='" + String(device) + "'";
      http.begin(url);                          // HTTP Initial
      int httpCode = http.GET();                // ทำการ GET Request ไปยัง Server      String response = myiot.WriteDashboard(userid, key, t, h, 0, 0, private_tweet, public_tweet);

    }
    else if (t >= 8 && t <= 30 && p_tm->tm_hour == 11 && p_tm->tm_min == 0 && p_tm->tm_sec == 5)
    {

      LINE.notify("อุณหภูมิขณะนี้ " + String(t) + " ความชื้น " + String(h) + " %");
      LINE.notifySticker(1, 407);
      HTTPClient http;
      String url = "http://iot.icreate.com/sensor-bziot-api.php/?datetime='" + String(now_day) + "%20" + String(now_time) + "'&temperature='" + String(t) + "'&humidity='" + String(h) + "'&device='" + String(device) + "'";
      http.begin(url);                          // HTTP Initial
      int httpCode = http.GET();                // ทำการ GET Request ไปยัง Server      String response = myiot.WriteDashboard(userid, key, t, h, 0, 0, private_tweet, public_tweet);

    }
    else if (t >= 8 && t <= 30 && p_tm->tm_hour == 12 && p_tm->tm_min == 0 && p_tm->tm_sec == 5)
    {

      LINE.notify("อุณหภูมิขณะนี้ " + String(t) + " ความชื้น " + String(h) + " %");
      LINE.notifySticker(1, 407);
      HTTPClient http;
      String url = "http://iot.icreate.com/sensor-bziot-api.php/?datetime='" + String(now_day) + "%20" + String(now_time) + "'&temperature='" + String(t) + "'&humidity='" + String(h) + "'&device='" + String(device) + "'";
      http.begin(url);                          // HTTP Initial
      int httpCode = http.GET();                // ทำการ GET Request ไปยัง Server      String response = myiot.WriteDashboard(userid, key, t, h, 0, 0, private_tweet, public_tweet);

    }
    else if (t >= 8 && t <= 30 && p_tm->tm_hour == 13 && p_tm->tm_min == 0 && p_tm->tm_sec == 5)
    {

      LINE.notify("อุณหภูมิขณะนี้ " + String(t) + " ความชื้น " + String(h) + " %");
      LINE.notifySticker(1, 407);
      HTTPClient http;
      String url = "http://iot.icreate.com/sensor-bziot-api.php/?datetime='" + String(now_day) + "%20" + String(now_time) + "'&temperature='" + String(t) + "'&humidity='" + String(h) + "'&device='" + String(device) + "'";
      http.begin(url);                          // HTTP Initial
      int httpCode = http.GET();                // ทำการ GET Request ไปยัง Server      String response = myiot.WriteDashboard(userid, key, t, h, 0, 0, private_tweet, public_tweet);

    }
    else if (t >= 8 && t <= 30 && p_tm->tm_hour == 14 && p_tm->tm_min == 0 && p_tm->tm_sec == 5)
    {

      LINE.notify("อุณหภูมิขณะนี้ " + String(t) + " ความชื้น " + String(h) + " %");
      LINE.notifySticker(1, 407);
      HTTPClient http;
      String url = "http://iot.icreate.com/sensor-bziot-api.php/?datetime='" + String(now_day) + "%20" + String(now_time) + "'&temperature='" + String(t) + "'&humidity='" + String(h) + "'&device='" + String(device) + "'";
      http.begin(url);                          // HTTP Initial
      int httpCode = http.GET();                // ทำการ GET Request ไปยัง Server      String response = myiot.WriteDashboard(userid, key, t, h, 0, 0, private_tweet, public_tweet);

    }
    else if (t >= 8 && t <= 30 && p_tm->tm_hour == 15 && p_tm->tm_min == 0 && p_tm->tm_sec == 5)
    {

      LINE.notify("อุณหภูมิขณะนี้ " + String(t) + " ความชื้น " + String(h) + " %");
      LINE.notifySticker(1, 407);
      HTTPClient http;
      String url = "http://iot.icreate.com/sensor-bziot-api.php/?datetime='" + String(now_day) + "%20" + String(now_time) + "'&temperature='" + String(t) + "'&humidity='" + String(h) + "'&device='" + String(device) + "'";
      http.begin(url);                          // HTTP Initial
      int httpCode = http.GET();                // ทำการ GET Request ไปยัง Server      String response = myiot.WriteDashboard(userid, key, t, h, 0, 0, private_tweet, public_tweet);

    }
    else if (t >= 8 && t <= 30 && p_tm->tm_hour == 16 && p_tm->tm_min == 0 && p_tm->tm_sec == 5)
    {

      LINE.notify("อุณหภูมิขณะนี้ " + String(t) + " ความชื้น " + String(h) + " %");
      LINE.notifySticker(1, 407);
      HTTPClient http;
      String url = "http://iot.icreate.com/sensor-bziot-api.php/?datetime='" + String(now_day) + "%20" + String(now_time) + "'&temperature='" + String(t) + "'&humidity='" + String(h) + "'&device='" + String(device) + "'";
      http.begin(url);                          // HTTP Initial
      int httpCode = http.GET();                // ทำการ GET Request ไปยัง Server      String response = myiot.WriteDashboard(userid, key, t, h, 0, 0, private_tweet, public_tweet);

    }
    else if (t >= 8 && t <= 30 && p_tm->tm_hour == 17 && p_tm->tm_min == 0 && p_tm->tm_sec == 5)
    {

      LINE.notify("อุณหภูมิขณะนี้ " + String(t) + " ความชื้น " + String(h) + " %");
      LINE.notifySticker(1, 407);
      HTTPClient http;
      String url = "http://iot.icreate.com/sensor-bziot-api.php/?datetime='" + String(now_day) + "%20" + String(now_time) + "'&temperature='" + String(t) + "'&humidity='" + String(h) + "'&device='" + String(device) + "'";
      http.begin(url);                          // HTTP Initial
      int httpCode = http.GET();                // ทำการ GET Request ไปยัง Server      String response = myiot.WriteDashboard(userid, key, t, h, 0, 0, private_tweet, public_tweet);

    }
    else if (t >= 8 && t <= 30 && p_tm->tm_hour == 18 && p_tm->tm_min == 0 && p_tm->tm_sec == 5)
    {

      LINE.notify("อุณหภูมิขณะนี้ " + String(t) + " ความชื้น " + String(h) + " %");
      LINE.notifySticker(1, 407);
      HTTPClient http;
      String url = "http://iot.icreate.com/sensor-bziot-api.php/?datetime='" + String(now_day) + "%20" + String(now_time) + "'&temperature='" + String(t) + "'&humidity='" + String(h) + "'&device='" + String(device) + "'";
      http.begin(url);                          // HTTP Initial
      int httpCode = http.GET();                // ทำการ GET Request ไปยัง Server      String response = myiot.WriteDashboard(userid, key, t, h, 0, 0, private_tweet, public_tweet);

    }
    else if (t >= 8 && t <= 30 && p_tm->tm_hour == 19 && p_tm->tm_min == 0 && p_tm->tm_sec == 5)
    {

      LINE.notify("อุณหภูมิขณะนี้ " + String(t) + " ความชื้น " + String(h) + " %");
      LINE.notifySticker(1, 407);
      HTTPClient http;
      String url = "http://iot.icreate.com/sensor-bziot-api.php/?datetime='" + String(now_day) + "%20" + String(now_time) + "'&temperature='" + String(t) + "'&humidity='" + String(h) + "'&device='" + String(device) + "'";
      http.begin(url);                          // HTTP Initial
      int httpCode = http.GET();                // ทำการ GET Request ไปยัง Server      String response = myiot.WriteDashboard(userid, key, t, h, 0, 0, private_tweet, public_tweet);

    }
    else if (t >= 8 && t <= 30 && p_tm->tm_hour == 20 && p_tm->tm_min == 0 && p_tm->tm_sec == 5)
    {

      LINE.notify("อุณหภูมิขณะนี้ " + String(t) + " ความชื้น " + String(h) + " %");
      LINE.notifySticker(1, 407);
      HTTPClient http;
      String url = "http://iot.icreate.com/sensor-bziot-api.php/?datetime='" + String(now_day) + "%20" + String(now_time) + "'&temperature='" + String(t) + "'&humidity='" + String(h) + "'&device='" + String(device) + "'";
      http.begin(url);                          // HTTP Initial
      int httpCode = http.GET();                // ทำการ GET Request ไปยัง Server      String response = myiot.WriteDashboard(userid, key, t, h, 0, 0, private_tweet, public_tweet);

    }
    else if (t >= 8 && t <= 30 && p_tm->tm_hour == 21 && p_tm->tm_min == 0 && p_tm->tm_sec == 5)
    {

      LINE.notify("อุณหภูมิขณะนี้ " + String(t) + " ความชื้น " + String(h) + " %");
      LINE.notifySticker(1, 407);
      HTTPClient http;
      String url = "http://iot.icreate.com/sensor-bziot-api.php/?datetime='" + String(now_day) + "%20" + String(now_time) + "'&temperature='" + String(t) + "'&humidity='" + String(h) + "'&device='" + String(device) + "'";
      http.begin(url);                          // HTTP Initial
      int httpCode = http.GET();                // ทำการ GET Request ไปยัง Server      String response = myiot.WriteDashboard(userid, key, t, h, 0, 0, private_tweet, public_tweet);

    }
    else if (t >= 8 && t <= 30 && p_tm->tm_hour == 22 && p_tm->tm_min == 0 && p_tm->tm_sec == 5)
    {

      LINE.notify("อุณหภูมิขณะนี้ " + String(t) + " ความชื้น " + String(h) + " %");
      LINE.notifySticker(1, 407);
      HTTPClient http;
      String url = "http://iot.icreate.com/sensor-bziot-api.php/?datetime='" + String(now_day) + "%20" + String(now_time) + "'&temperature='" + String(t) + "'&humidity='" + String(h) + "'&device='" + String(device) + "'";
      http.begin(url);                          // HTTP Initial
      int httpCode = http.GET();                // ทำการ GET Request ไปยัง Server      String response = myiot.WriteDashboard(userid, key, t, h, 0, 0, private_tweet, public_tweet);

    }
    else if (t >= 8 && t <= 30 && p_tm->tm_hour == 23 && p_tm->tm_min == 0 && p_tm->tm_sec == 5)
    {

      LINE.notify("อุณหภูมิขณะนี้ " + String(t) + " ความชื้น " + String(h) + " %");
      LINE.notifySticker(1, 407);
      HTTPClient http;
      String url = "http://iot.icreate.com/sensor-bziot-api.php/?datetime='" + String(now_day) + "%20" + String(now_time) + "'&temperature='" + String(t) + "'&humidity='" + String(h) + "'&device='" + String(device) + "'";
      http.begin(url);                          // HTTP Initial
      int httpCode = http.GET();                // ทำการ GET Request ไปยัง Server      String response = myiot.WriteDashboard(userid, key, t, h, 0, 0, private_tweet, public_tweet);

    }
  }
}

ต้อง กดตรง serial มาดูว่ามันขึ้นว่าอะไรก่อนครับ

หรือลองเลือก Debug port เป็น Serial และ Debug Level เป็น All ดูครับ แล้วดูว่ามันบอกอะไรมาบ้าง


ของผมไม่มีคำว่าAllครับ

ตัวนี้ครับ
1


เป็นแบบนี้ครับ


แบบนี้ครับ

มันวนการ Connect WiFi ซ้ำๆ กันเหรอครับ

มันหยุดส่งค่าอุณหภูมิครับ จะเป็นตอนหลังตี3ครับ เป็นมา2วันแล้วครับหลังจากเปลี่ยนมาใช้ wifi manager เมื่อก่อนใส่ ssid , password ในโค้ดไม่เป็นครับ

image

ปกติตรงนี้จะเช็คแค่ครั้งเดียวหรือครับ

ลอง WiFI Manager แบบนี้ดูครับ แล้วเอา Code ของคุณ @BeerZaa เข้าไปแทรก ตรง Void Loop ดูครับ โดย D3
ผมต่อ SW เพื่อ ทำ AP Config

image



#include <ESP8266WiFi.h>                  //โหลดไลบารี่ ..มาใช้งาน     
#include <ESP8266WebServer.h>             //โหลดไลบารี่ ..มาใช้งาน
#include <WiFiManager.h>                  //โหลดไลบารี่ ..มาใช้งาน     
#include <FS.h>                           //โหลดไลบารี่ ..มาใช้งาน     
#include <DNSServer.h>                    //โหลดไลบารี่ ..มาใช้งาน
#include <ArduinoJson.h>                  //โหลดไลบารี่ ..มาใช้งาน     
//------------------------------------------------------------------------------------------------------------------------//
#include <BlynkSimpleEsp8266.h>           //
//------------------------------------------------------------------------------------------------------------------------//




//------------------------------        การกำหนดค่าในส่วนของ Blynk         ------------------------------------------------//
#define BLYNK_DEBUG
#define BLYNK_PRINT Serial        

int blynkIsDownCount = 0;       

char blynk_token[34] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";//ใส่ Blynk_token ของเราที่ Blynk ส่งมาทาง Email ตอน Create Project ใหม่


BlynkTimer timer;  




#include <TridentTD_LineNotify.h>
char line_token[45] = "XXXXXXXXXXXXXXXXXXXXXXX ";


//----------------------------------  กำหนดหมายเลขของขาของ Node MCU ESP 8266  --------------------------------------------//

#define D0 16 // ใช้ไฟ LED สีฟ้า ของบอร์ด MCU ESP8266 ให้มีสัญญาณไฟกระพริบ ตาม Code ที่เขียน
#define D3 0

//------------------------------------------------------------------------------------------------------------------------//



bool shouldSaveConfig = false;

//callback notifying us of the need to save config
void saveConfigCallback () {
  Serial.println("Should save  blynk config");
  shouldSaveConfig = true;
}



bool shouldSaveConfig1 = false;

//callback notifying us of the need to save config
void saveConfigCallback1 () {
  Serial.println("Should save line config");
  shouldSaveConfig1 = true;
}
//------------------------------------------------------------------------------------------------------------------------//


//-------------สำหรับ Server local ที่แจกให้ เพิ่ม **** แค่ 2 บรรทัดนี้--------------   (ถ้าเป็น Blynk Server ปกติไม่ต้องใส่)  ----------//
char server[] = "oasiskit.com";
int port = 8080;
//------------------------------------------------------------------------------------------------------------------------//





//------------------------------------------------------------------------------------------------------------------------//
//*********************************************       void setup        **************************************************//
//------------------------------------------------------------------------------------------------------------------------//

void setup() {

  //-------IO NODE MCU Esp8266-------//
  pinMode(D0, OUTPUT);      //กำหนดโหมดใช้งานให้กับขา D0 เป็นขา สัญญาณไฟ ในสภาวะต่างๆ
  pinMode(D3, INPUT_PULLUP);//กำหนดโหมดใช้งานให้กับขา D3 เป็นขา กดปุ่ม ค้าง เพื่อตั้งค่า AP config


  // ให้ LED ทั้งหมดดับก่อน
  digitalWrite(D0, LOW);//ให้หลอด LED สีฟ้าดับก่อน

  //-------------------------------//


  Serial.begin(115200);
  //-------------------------------//






  //read configuration from FS json
  Serial.println("mounting blynk FS...");//แสดงข้อความใน Serial Monitor

  if (SPIFFS.begin()) {
    Serial.println("mounted blynk file system");//แสดงข้อความใน Serial Monitor

    if (SPIFFS.exists("/config.json")) {
      //file exists, reading and loading
      Serial.println("reading blynk config file");//แสดงข้อความใน Serial Monitor
      File configFile = SPIFFS.open("/config.json", "r");

      if (configFile) {
        Serial.println("opened blynk config file");
        size_t size = configFile.size();
        // Allocate a buffer to store contents of the file.
        std::unique_ptr<char[]> buf(new char[size]);

        configFile.readBytes(buf.get(), size);
        DynamicJsonBuffer jsonBuffer;
        JsonObject& json = jsonBuffer.parseObject(buf.get());
        json.printTo(Serial);
        if (json.success()) {
          Serial.println("\nparsed blynk json");//แสดงข้อความใน Serial Monitor
          strcpy(blynk_token, json["blynk_token"]);

        } else {
          Serial.println("failed to load blynk json config");//แสดงข้อความใน Serial Monitor
        }
      }
    }
  } else {
    Serial.println("failed blynk to mount FS");//แสดงข้อความใน Serial Monitor
  }
  //end read



  delay(500);



  //read configuration from FS json
  Serial.println("mounting line FS...");//แสดงข้อความใน Serial Monitor

  if (SPIFFS.begin()) {
    Serial.println("mounted line file system");//แสดงข้อความใน Serial Monitor

    if (SPIFFS.exists("/config1.json")) {
      //file exists, reading and loading
      Serial.println("reading line config file");//แสดงข้อความใน Serial Monitor
      File configFile1 = SPIFFS.open("/config1.json", "r");

      if (configFile1) {
        Serial.println("opened line config file");
        size_t size = configFile1.size();
        // Allocate a buffer to store contents of the file.
        std::unique_ptr<char[]> buf1(new char[size]);

        configFile1.readBytes(buf1.get(), size);
        DynamicJsonBuffer jsonBuffer;
        JsonObject& json = jsonBuffer.parseObject(buf1.get());
        json.printTo(Serial);
        if (json.success()) {
          Serial.println("\nparsed line json");//แสดงข้อความใน Serial Monitor
          strcpy(line_token, json["line_token"]);
        } else {
          Serial.println("failed to load line json config");//แสดงข้อความใน Serial Monitor
        }
      }
    }
  } else {
    Serial.println("failed line to mount FS");//แสดงข้อความใน Serial Monitor
  }
  //end read

  delay(500);








  //**************************        AP AUTO CONNECT   ********************************************//

  WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 34);
  WiFiManagerParameter custom_line_token("LINE", "line_token", line_token, 45);
  //WiFiManager
  //Local intialization. Once its business is done, there is no need to keep it around
  WiFiManager wifiManager;

  //set config save notify callback
  wifiManager.setSaveConfigCallback(saveConfigCallback);
  wifiManager.addParameter(&custom_blynk_token);
  delay(500);
  wifiManager.setSaveConfigCallback(saveConfigCallback1);
  wifiManager.addParameter(&custom_line_token);
  delay(500);

  for (int i = 5; i > -1; i--) {  // นับเวลาถอยหลัง 5 วินาทีก่อนกดปุ่ม AP Config
    digitalWrite(D0, HIGH);
    delay(500);
    digitalWrite(D0, LOW);
    delay(500);
    Serial.print (String(i) + " ");//แสดงข้อความใน Serial Monitor
  }


  if (digitalRead(D3) == LOW) {
    digitalWrite(D0, HIGH);
    Serial.println("Button Pressed");//แสดงข้อความใน Serial Monitor

    wifiManager.resetSettings();//ให้ล้างค่า SSID และ Password ที่เคยบันทึกไว้


    // wifiManager.autoConnect(); ใช้สร้างชื่อแอคเซสพอยต์อันโนมัติจาก ESP + ChipID

    if (!wifiManager.autoConnect("BeerZaa AP CONFIG")) {
      Serial.println("failed to connect and hit timeout");//แสดงข้อความใน Serial Monitor
      delay(3000);
      //reset and try again, or maybe put it to deep sleep
      ESP.reset();
      delay(5000);

    }
  }



  Serial.println("Connected.......OK!)");//แสดงข้อความใน Serial Monitor
  strcpy(blynk_token, custom_blynk_token.getValue());
  delay(500);
  strcpy(line_token, custom_line_token.getValue());
  delay(500);

  //save the custom parameters to FS
  if (shouldSaveConfig) {
    Serial.println("saving blynk config");//แสดงข้อความใน Serial Monitor
    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();
    json["blynk_token"] = blynk_token;
    json["line_token"] = line_token;
    File configFile = SPIFFS.open("/config.json", "w");

    if (!configFile) {
      Serial.println("failed to open blynk config file for writing");//แสดงข้อความใน Serial Monitor

    }
    json.printTo(Serial);
    json.printTo(configFile);
    configFile.close();
    //end save
  }
  delay(500);

  if (shouldSaveConfig1) {
    Serial.println("saving line config");//แสดงข้อความใน Serial Monitor
    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();
    json["line_token"] = line_token;
    File configFile1 = SPIFFS.open("/config1.json", "w");

    if (!configFile1) {
      Serial.println("failed to open line config file for writing");//แสดงข้อความใน Serial Monitor

    }
    json.printTo(Serial);
    json.printTo(configFile1);
    configFile1.close();

  }

  delay(500);








  //**************************    จบ    AP AUTO CONNECT   *****************************************//




  Serial.println("local ip"); //แสดงข้อความใน Serial Monitor
  delay(100);
  Serial.println(WiFi.localIP());//แสดงข้อความใน Serial Monitor
  Serial.println("gateway");
  delay(100);
  Serial.println(WiFi.gatewayIP());
  Serial.println("subnetMask");
  delay(100);
  Serial.println(WiFi.subnetMask());
  Serial.println("SSID");
  delay(100);
  Serial.println(WiFi.SSID());
  Serial.println("Password");
  delay(100);
  Serial.println(WiFi.psk());


  // Blynk.config(blynk_token);////เริ่มการเชื่อมต่อ Blynk Server แบบปกติ
  Blynk.config(blynk_token, server, port);
  ////เริ่มการเชื่อมต่อ Blynk Server*********สำหรับ Server local ที่แจกให้ เพิ่ม, server, port  แค่นี้จบ



  timer.setInterval(30000L, reconnecting);  //Function reconnect

  //ตั้งเวลาส่งข้อมูลให้ Blynk Server ทุกๆ 30 วินาที
  LINE.setToken(line_token);
  LINE.notify("line token for wifi manager OK");
}


//------------------------------------------------------------------------------------------------------------------------//
//*********************************************   จบ  void setup        **************************************************//
//------------------------------------------------------------------------------------------------------------------------//









//------------------------------------------------------------------------------------------------------------------------//
//*********************************************       void Loop        ***************************************************//
//------------------------------------------------------------------------------------------------------------------------//


void loop() {


  //-----------------------------------------------------------------------------------------------------------//
  if (Blynk.connected())
  {
    Blynk.run();
    digitalWrite(D0, HIGH);

  } else {

    digitalWrite(D0, LOW);
    delay(200);
    digitalWrite(D0, HIGH);
    delay(100);

  }
  //-----------------------------------------------------------------------------------------------------------//

  timer.run();//ให้เวลาของ Blynk ทำงาน

  delay(100);


}

//------------------------------------------------------------------------------------------------------------------------//
//*********************************************      จบ void Loop       **************************************************//
//------------------------------------------------------------------------------------------------------------------------//









//------------------------------------------------------------------------------------------------------------------------//
void reconnecting()
{
  if (!Blynk.connected())
  {
    blynkIsDownCount++;
    BLYNK_LOG("blynk server is down! %d  times", blynkIsDownCount);
    Blynk.connect(5000);
  }
}


BLYNK_CONNECTED()
{

  Blynk.syncAll();//ให้ซิงค์ข้อมูลทั้งหมดล่าสุดจาก Blynk Server

}







1 Likes

@BeerZaa ใช่อินเตอร์ของอะไรครับ

Code ที่ให้ไป มี Line Token ที่สามารถใส่ได้ใน Web Portal เวลาเรา กดปุ่ม D3 เพื่อตั้งค่า WiFi นะครับ แถวบนเป็นของ Blynk Token แถวล่างเป็น Line Token ยังไงลองดูจุดนี้ให้ได้ก่อน แล้วค่อยเอา Code ของเราเข้าไปแทรก

ครับผม ขอบคุณครับ

ทีโอทีครับ

ขอบคุณมากนะครับ เดี๋ยวผมลองดูครับ

ถ้าผมไม่ได้ใช้Blynkผมตัดโค้ดส่วนนี้ออกไปได้เลยหรือเปล่าครับ จะerrorไหมครับ

ได้ครับ Code ตามนี้


// ----Add---Node MCU Board---------http://arduino.esp8266.com/stable/package_esp8266com_index.json  -----------------------//
//--------------------------------------------------------------------------------------------------------------------------//
#include <ESP8266WiFi.h>                  //โหลดไลบารี่ ..มาใช้งาน     //https://github.com/esp8266/Arduino
#include <ESP8266WebServer.h>             //โหลดไลบารี่ ..มาใช้งาน
#include <WiFiManager.h>                  //โหลดไลบารี่ ..มาใช้งาน     //https://github.com/tzapu/WiFiManager
#include <FS.h>                           //โหลดไลบารี่ ..มาใช้งาน     //this needs to be first, or it all crashes and burns...
#include <DNSServer.h>                    //โหลดไลบารี่ ..มาใช้งาน
#include <ArduinoJson.h>                  //โหลดไลบารี่ ..มาใช้งาน     //https://github.com/bblanchon/ArduinoJson
//------------------------------------------------------------------------------------------------------------------------//



#include <TridentTD_LineNotify.h>
char line_token[45] = "xxxxxxxxxxxxxxxxxxxxxxx ";


//----------------------------------  กำหนดหมายเลขของขาของ Node MCU ESP 8266  --------------------------------------------//

#define D0 16 // ใช้ไฟ LED สีฟ้า ของบอร์ด MCU ESP8266 ให้มีสัญญาณไฟกระพริบ ตาม Code ที่เขียน
#define D3 0

//------------------------------------------------------------------------------------------------------------------------//



bool shouldSaveConfig1 = false;

//callback notifying us of the need to save config
void saveConfigCallback1 () {
  Serial.println("Should save line config");
  shouldSaveConfig1 = true;
}
//------------------------------------------------------------------------------------------------------------------------//








//------------------------------------------------------------------------------------------------------------------------//
//*********************************************       void setup        **************************************************//
//------------------------------------------------------------------------------------------------------------------------//

void setup() {

  //-------IO NODE MCU Esp8266-------//
  pinMode(D0, OUTPUT);      //กำหนดโหมดใช้งานให้กับขา D0 เป็นขา สัญญาณไฟ ในสภาวะต่างๆ
  pinMode(D3, INPUT_PULLUP);//กำหนดโหมดใช้งานให้กับขา D3 เป็นขา กดปุ่ม ค้าง เพื่อตั้งค่า AP config


  // ให้ LED ทั้งหมดดับก่อน
  digitalWrite(D0, LOW);//ให้หลอด LED สีฟ้าดับก่อน

  //-------------------------------//


  Serial.begin(115200);
  //-------------------------------//




  //read configuration from FS json
  Serial.println("mounting line FS...");//แสดงข้อความใน Serial Monitor

  if (SPIFFS.begin()) {
    Serial.println("mounted line file system");//แสดงข้อความใน Serial Monitor

    if (SPIFFS.exists("/config1.json")) {
      //file exists, reading and loading
      Serial.println("reading line config file");//แสดงข้อความใน Serial Monitor
      File configFile1 = SPIFFS.open("/config1.json", "r");

      if (configFile1) {
        Serial.println("opened line config file");
        size_t size = configFile1.size();
        // Allocate a buffer to store contents of the file.
        std::unique_ptr<char[]> buf1(new char[size]);

        configFile1.readBytes(buf1.get(), size);
        DynamicJsonBuffer jsonBuffer;
        JsonObject& json = jsonBuffer.parseObject(buf1.get());
        json.printTo(Serial);
        if (json.success()) {
          Serial.println("\nparsed line json");//แสดงข้อความใน Serial Monitor
          strcpy(line_token, json["line_token"]);
        } else {
          Serial.println("failed to load line json config");//แสดงข้อความใน Serial Monitor
        }
      }
    }
  } else {
    Serial.println("failed line to mount FS");//แสดงข้อความใน Serial Monitor
  }
  //end read

  delay(500);



  //**************************        AP AUTO CONNECT   ********************************************//

 
  WiFiManagerParameter custom_line_token("LINE", "line_token", line_token, 45);
  //WiFiManager
  //Local intialization. Once its business is done, there is no need to keep it around
  WiFiManager wifiManager;

  //set config save notify callback
  wifiManager.setSaveConfigCallback(saveConfigCallback1);
  wifiManager.addParameter(&custom_line_token);
  delay(500);

  for (int i = 5; i > -1; i--) {  // นับเวลาถอยหลัง 5 วินาทีก่อนกดปุ่ม AP Config
    digitalWrite(D0, HIGH);
    delay(500);
    digitalWrite(D0, LOW);
    delay(500);
    Serial.print (String(i) + " ");//แสดงข้อความใน Serial Monitor
  }


  if (digitalRead(D3) == LOW) {
    digitalWrite(D0, HIGH);
    Serial.println("Button Pressed");//แสดงข้อความใน Serial Monitor

    wifiManager.resetSettings();//ให้ล้างค่า SSID และ Password ที่เคยบันทึกไว้


    // wifiManager.autoConnect(); ใช้สร้างชื่อแอคเซสพอยต์อันโนมัติจาก ESP + ChipID

    if (!wifiManager.autoConnect("PUYIOT AP CONFIG")) {
      Serial.println("failed to connect and hit timeout");//แสดงข้อความใน Serial Monitor
      delay(3000);
      //reset and try again, or maybe put it to deep sleep
      ESP.reset();
      delay(5000);

    }
  }



  Serial.println("Connected.......OK!)");//แสดงข้อความใน Serial Monitor
  strcpy(line_token, custom_line_token.getValue());
  delay(500);

  //save the custom parameters to FS
 

  if (shouldSaveConfig1) {
    Serial.println("saving line config");//แสดงข้อความใน Serial Monitor
    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();
    json["line_token"] = line_token;
    File configFile1 = SPIFFS.open("/config1.json", "w");

    if (!configFile1) {
      Serial.println("failed to open line config file for writing");//แสดงข้อความใน Serial Monitor

    }
    json.printTo(Serial);
    json.printTo(configFile1);
    configFile1.close();

  }

  delay(500);








  //**************************    จบ    AP AUTO CONNECT   *****************************************//




  Serial.println("local ip"); //แสดงข้อความใน Serial Monitor
  delay(100);
  Serial.println(WiFi.localIP());//แสดงข้อความใน Serial Monitor
  Serial.println("gateway");
  delay(100);
  Serial.println(WiFi.gatewayIP());
  Serial.println("subnetMask");
  delay(100);
  Serial.println(WiFi.subnetMask());
  Serial.println("SSID");
  delay(100);
  Serial.println(WiFi.SSID());
  Serial.println("Password");
  delay(100);
  Serial.println(WiFi.psk());


  LINE.setToken(line_token);
  LINE.notify("line token for wifi manager OK");
}


//------------------------------------------------------------------------------------------------------------------------//
//*********************************************   จบ  void setup        **************************************************//
//------------------------------------------------------------------------------------------------------------------------//









//------------------------------------------------------------------------------------------------------------------------//
//*********************************************       void Loop        ***************************************************//
//------------------------------------------------------------------------------------------------------------------------//


void loop() {

}

//------------------------------------------------------------------------------------------------------------------------//
//*********************************************      จบ void Loop       **************************************************//
//------------------------------------------------------------------------------------------------------------------------//



ผลลัพธ์ครับ
กดปุ่ม PB (D3) ค้างไว้ ตอน Esp8266 เริ่มทำงาน (ภายใน 5 วินาที) จน LED ที่กระพริบบน Esp8266 ดับ จากนั้นก็ เชื่อมต่อ WiFi ตามรูปเลยครับ

1

2

ช่องล่างสุดใส่ Line Token (ของคุณนะครับ) เมื่อมันเชื่อมต่อ WiFi internet ได้แล้ว ก็ส่งข้อความกลับมาใน Line ในLine กลุ่มของเรา

3

ขอให้งานสำเร็จนะครับ

2

1 Likes

ขอบคุณมากๆเลยนะครับ อีกนิดครับ ถ้าผมไม่ได้ต่อ PB (D3) ผมจะconnect wifi managerแบบปกติได้ไหมครับ

ไม่ได้ครับ เพราะใน ArduinoJson ยังไม่ได้บันทึกSSID Password Line Token ของเราเลย โดยครังแรกที่ Upload code เข้าไป จะต้องทำครับ