ESP8266 + WiFimanager Dynamic IP / Static IP + Line Notify

ฝากเก็บข้อมูลครับ

ความต้องการของ Project (เพื่อป้องกันความผิดพลาดเรื่องของ Version และตรวจสอบย้อนหลังได้)

  1. Node MCU ESP8266 V2 หรือ V3
  2. Arduino IDE Version 1.8.7
  3. WiFiManager Version 0.14.0
  4. ArduinoJson Version 5.13.3
//Node MCU ESP8266 V3 ใช้ D4
//Node MCU ESP8266 V2 ใช้ D0
//-------------------------------------------------------------------------------------------------------------------------//

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <FS.h>
#include <DNSServer.h>
#include <ArduinoJson.h>
#include <ESP8266HTTPClient.h>


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

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

String LineText1;
String string220 = " IP : ";

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

#define D7 13 // ใช้เป็นปุ่มกด เพื่อเข้า AP Config ได้ตามความต้องการของผู้ใช้
#define D6 12 // ใช้สลับ  Dynamic IP กับ  Static IP
//------------------------------------------------------------------------------------------------------------------------//

bool shouldSaveConfig1 = false;
void saveConfigCallback1 () {
  shouldSaveConfig1 = true;
  Serial.println("Should save  All Data config");
}
//-----------------------------------------------------------------------------------------------------------------------//

String IP;


//--------Static IP
char IP1A[5] = "192";
char IP2A[5] = "168";
char IP3A[5] = "1";
char IP4A[5] = "40";

String IP1B ;
String IP2B ;
String IP3B ;
String IP4B ;

int IP1;
int IP2;
int IP3;
int IP4;





//--------Static Gateway
char GW1A[5] = "192";
char GW2A[5] = "168";
char GW3A[5] = "1";
char GW4A[5] = "1";

String GW1B ;
String GW2B ;
String GW3B ;
String GW4B ;

int GW1;
int GW2;
int GW3;
int GW4;


//--------Static Subnetmark
char SN1A[5] = "255";
char SN2A[5] = "255";
char SN3A[5] = "255";
char SN4A[5] = "0";

String SN1B ;
String SN2B ;
String SN3B ;
String SN4B ;

int SN1;
int SN2;
int SN3;
int SN4;

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

void setup() {

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

  Serial.begin(115200);


  //-----------------------------------------------------------------------------------------------------------------------//
  //read configuration from FS json
  Serial.println("mounting  FS...");



  if (SPIFFS.begin()) {

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

    Serial.println("mounted Data file system");


    if (SPIFFS.exists("/config1.json")) {
      //file exists, reading and loading
      Serial.println("reading Data config file");
      File configFile1 = SPIFFS.open("/config1.json", "r");

      if (configFile1) {
        Serial.println("opened Data 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  json");

          strcpy(line_token, json["line_token"]);

          strcpy(IP1A, json["IP1A"]);
          strcpy(IP2A, json["IP2A"]);
          strcpy(IP3A, json["IP3A"]);
          strcpy(IP4A, json["IP4A"]);

          strcpy(GW1A, json["GW1A"]);
          strcpy(GW2A, json["GW2A"]);
          strcpy(GW3A, json["GW3A"]);
          strcpy(GW4A, json["GW4A"]);

          strcpy(SN1A, json["SN1A"]);
          strcpy(SN2A, json["SN2A"]);
          strcpy(SN3A, json["SN3A"]);
          strcpy(SN4A, json["SN4A"]);

        } else {
          Serial.println("failed to load Data file json config");
        }
      }
    }
    //---------------------------------------------------------------------------------//



  } else {
    Serial.println("failed to mount FS");

  }
  //end read


  delay(2000);




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

  WiFiManagerParameter custom_text0("<p> </p>");

  WiFiManagerParameter custom_text1("<label>1: ID Line กลุ่ม</label>");

  WiFiManagerParameter custom_text2("<label>2: ตั้งค่า Static IP</label>");
  WiFiManagerParameter custom_text3("<label>3: ตั้งค่า Static Gateway</label>");
  WiFiManagerParameter custom_text4("<label>4: ตั้งค่า Static Subnetmark</label>");

  WiFiManagerParameter custom_line_token("line_token", "line_token", line_token, 45);

  WiFiManagerParameter custom_IP1A("IP1A", "IP1A", IP1A, 5);
  WiFiManagerParameter custom_IP2A("IP2A", "IP2A", IP2A, 5);
  WiFiManagerParameter custom_IP3A("IP3A", "IP3A", IP3A, 5);
  WiFiManagerParameter custom_IP4A("IP4A", "IP4A", IP4A, 5);

  WiFiManagerParameter custom_GW1A("GWP1A", "GW1A", GW1A, 5);
  WiFiManagerParameter custom_GW2A("GW2A", "GW2A", GW2A, 5);
  WiFiManagerParameter custom_GW3A("GW3A", "GW3A", GW3A, 5);
  WiFiManagerParameter custom_GW4A("GW4A", "GW4A", GW4A, 5);

  WiFiManagerParameter custom_SN1A("SN1A", "SN1A", SN1A, 5);
  WiFiManagerParameter custom_SN2A("SN2A", "SN2A", SN2A, 5);
  WiFiManagerParameter custom_SN3A("SN3A", "SN3A", SN3A, 5);
  WiFiManagerParameter custom_SN4A("SN4A", "SN4A", SN4A, 5);


  //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_text0);
  wifiManager.addParameter(&custom_text1);
  wifiManager.addParameter(&custom_line_token);


  wifiManager.addParameter(&custom_text0);
  wifiManager.addParameter(&custom_text2);
  wifiManager.addParameter(&custom_IP1A);
  wifiManager.addParameter(&custom_IP2A);
  wifiManager.addParameter(&custom_IP3A);
  wifiManager.addParameter(&custom_IP4A);


  wifiManager.addParameter(&custom_text0);
  wifiManager.addParameter(&custom_text3);
  wifiManager.addParameter(&custom_GW1A);
  wifiManager.addParameter(&custom_GW2A);
  wifiManager.addParameter(&custom_GW3A);
  wifiManager.addParameter(&custom_GW4A);


  wifiManager.addParameter(&custom_text0);
  wifiManager.addParameter(&custom_text4);
  wifiManager.addParameter(&custom_SN1A);
  wifiManager.addParameter(&custom_SN2A);
  wifiManager.addParameter(&custom_SN3A);
  wifiManager.addParameter(&custom_SN4A);

  delay(2000);

  for (int i = 5; i > -1; i--) {  // นับเวลาถอยหลัง 5 วินาทีก่อนกดปุ่ม AP Config
    digitalWrite(D4, LOW);

    delay(500);
    digitalWrite(D4, HIGH);
    delay(500);
    Serial.print (String(i) + " ");
  }

  if (digitalRead(D7) == LOW) {
    digitalWrite(D4, LOW);
    Serial.println("Button Pressed");

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


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

    if (!wifiManager.autoConnect("PUY IOT")) {
      Serial.println("failed to connect and hit timeout");
      delay(3000);
      //reset and try again, or maybe put it to deep sleep
      ESP.reset();
      delay(5000);
    }
  }

  Serial.println(": Connected.......OK!)");

  strcpy(line_token, custom_line_token.getValue());

  strcpy(IP1A, custom_IP1A.getValue());
  strcpy(IP2A, custom_IP2A.getValue());
  strcpy(IP3A, custom_IP3A.getValue());
  strcpy(IP4A, custom_IP4A.getValue());


  strcpy(GW1A, custom_GW1A.getValue());
  strcpy(GW2A, custom_GW2A.getValue());
  strcpy(GW3A, custom_GW3A.getValue());
  strcpy(GW4A, custom_GW4A.getValue());

  strcpy(SN1A, custom_SN1A.getValue());
  strcpy(SN2A, custom_SN2A.getValue());
  strcpy(SN3A, custom_SN3A.getValue());
  strcpy(SN4A, custom_SN4A.getValue());

  delay(2000);

  if (shouldSaveConfig1) {
    //------------------------------------------------------------------

    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();

    //------------------------------------------------------------------
    Serial.println("saving line token config");
    json["line_token"] = line_token;
    File configFile1 = SPIFFS.open("/config1.json", "w");
    //------------------------------------------------------------------

    //-Write IP
    Serial.println("saving IP1A_ config");
    json["IP1A"] = IP1A;
    File configFile2 = SPIFFS.open("/config1.json", "w");

    Serial.println("saving IP2A_ config");
    json["IP2A"] = IP2A;
    File configFile3 = SPIFFS.open("/config1.json", "w");

    Serial.println("saving IP3A_ config");
    json["IP3A"] = IP3A;
    File configFile4 = SPIFFS.open("/config1.json", "w");

    Serial.println("saving IP4A_ config");
    json["IP4A"] = IP4A;
    File configFile5 = SPIFFS.open("/config1.json", "w");

    //------------------------------------------------------------------
    //-Write Gateway
    Serial.println("saving GW1A_ config");
    json["GW1A"] = GW1A;
    File configFile6 = SPIFFS.open("/config1.json", "w");

    Serial.println("saving GW2A_ config");
    json["GW2A"] = GW2A;
    File configFile7 = SPIFFS.open("/config1.json", "w");

    Serial.println("saving GW3A_ config");
    json["GW3A"] = GW3A;
    File configFile8 = SPIFFS.open("/config1.json", "w");

    Serial.println("saving GW4A_ config");
    json["GW4A"] = GW4A;
    File configFile9 = SPIFFS.open("/config1.json", "w");

    //------------------------------------------------------------------
    //-Write Subnetmark
    Serial.println("saving SN1A_ config");
    json["SN1A"] = SN1A;
    File configFile10 = SPIFFS.open("/config1.json", "w");

    Serial.println("saving SN2A_ config");
    json["SN2A"] = SN2A;
    File configFile11 = SPIFFS.open("/config1.json", "w");

    Serial.println("saving SN3A_ config");
    json["SN3A"] = SN3A;
    File configFile12 = SPIFFS.open("/config1.json", "w");

    Serial.println("saving SN4A_ config");
    json["SN4A"] = SN4A;
    File configFile13 = SPIFFS.open("/config1.json", "w");

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


    if (!configFile1) {
      Serial.println("failed to open  config file for writing");

    }
    json.printTo(Serial);

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

    //-------------------------------------
    //-Write Close IP
    json.printTo(configFile2);
    configFile2.close();

    json.printTo(configFile3);
    configFile3.close();

    json.printTo(configFile4);
    configFile4.close();

    json.printTo(configFile5);
    configFile5.close();

    //-------------------------------------
    //-Write Close Gateway
    json.printTo(configFile6);
    configFile6.close();

    json.printTo(configFile7);
    configFile7.close();

    json.printTo(configFile8);
    configFile8.close();

    json.printTo(configFile9);
    configFile9.close();

    //-------------------------------------
    //-Write Close Subnetmark
    json.printTo(configFile10);
    configFile10.close();

    json.printTo(configFile11);
    configFile11.close();

    json.printTo(configFile12);
    configFile12.close();

    json.printTo(configFile13);
    configFile13.close();

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


  }

  delay(2000);
  //-----------------------------------------------------------------
  Serial.println();
  Serial.println("-----------------------------------");
  Serial.println("เชคว่าค่า  IP  ที่บันทึกได้ถูกแปลงเป็น Int แล้ว");

  for (int i = 0; i < sizeof(IP1A); i++)
    IP1B  += IP1A[i];
  IP1 = IP1B .toInt();
  Serial.print("IP1    =  ");
  Serial.println(IP1);

  for (int i = 0; i < sizeof(IP2A); i++)
    IP2B  += IP2A[i];
  IP2 = IP2B .toInt();
  Serial.print("IP2    =  ");
  Serial.println(IP2);


  for (int i = 0; i < sizeof(IP3A); i++)
    IP3B  += IP3A[i];
  IP3 = IP3B .toInt();
  Serial.print("IP3    =  ");
  Serial.println(IP3);


  for (int i = 0; i < sizeof(IP4A); i++)
    IP4B  += IP4A[i];
  IP4 = IP4B .toInt();
  Serial.print("IP4    =  ");
  Serial.println(IP4);

  Serial.println("-----------------------------------");

  //-----------------------------------------------------------------
  Serial.println();
  Serial.println("-----------------------------------");
  Serial.println("เชคว่าค่า  Gateway  ที่บันทึกได้ถูกแปลงเป็น Int แล้ว");

  for (int i = 0; i < sizeof(GW1A); i++)
    GW1B  += GW1A[i];
  GW1 = GW1B .toInt();
  Serial.print("GW1    =  ");
  Serial.println(GW1);

  for (int i = 0; i < sizeof(GW2A); i++)
    GW2B  += GW2A[i];
  GW2 = GW2B .toInt();
  Serial.print("GW2    =  ");
  Serial.println(GW2);


  for (int i = 0; i < sizeof(GW3A); i++)
    GW3B  += GW3A[i];
  GW3 = GW3B .toInt();
  Serial.print("GW3    =  ");
  Serial.println(GW3);


  for (int i = 0; i < sizeof(GW4A); i++)
    GW4B  += GW4A[i];
  GW4 = GW4B .toInt();
  Serial.print("GW4    =  ");
  Serial.println(GW4);

  Serial.println("-----------------------------------");

  //-----------------------------------------------------------------
  Serial.println();
  Serial.println("-----------------------------------");
  Serial.println("เชคว่าค่า  Subnetmark  ที่บันทึกได้ถูกแปลงเป็น Int แล้ว");

  for (int i = 0; i < sizeof(SN1A); i++)
    SN1B  += SN1A[i];
  SN1 = SN1B .toInt();
  Serial.print("SN1    =  ");
  Serial.println(SN1);

  for (int i = 0; i < sizeof(SN2A); i++)
    SN2B  += SN2A[i];
  SN2 = SN2B .toInt();
  Serial.print("SN2    =  ");
  Serial.println(SN2);


  for (int i = 0; i < sizeof(SN3A); i++)
    SN3B  += SN3A[i];
  SN3 = SN3B .toInt();
  Serial.print("SN3    =  ");
  Serial.println(SN3);


  for (int i = 0; i < sizeof(SN4A); i++)
    SN4B  += SN4A[i];
  SN4 = SN4B .toInt();
  Serial.print("SN4    =  ");
  Serial.println(SN4);

  Serial.println("-----------------------------------");

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



  if (digitalRead(D6) == LOW) {//ถ้า Switch ON แสดงว่าเป็น Static IP
    IPAddress local_ip = {IP1 , IP2 , IP3 , IP4 }; //ตั้งค่า IP
    IPAddress gateway = {GW1, GW2, GW3, GW4}; //ตั้งค่า IP Gateway
    IPAddress subnet = {SN1, SN2, SN3, SN4}; //ตั้งค่า Subnet
    WiFi.config(local_ip, gateway, subnet); //setค่าไปยังโมดูล
  }







  //-----------------------------------------------------------------------------------------------------------------------//
  Serial.println("-----------------------------------");
  Serial.println();
  Serial.print("local ip   : ");
  delay(100);
  Serial.println(WiFi.localIP());
  delay(100);
  Serial.print("gateway    :");
  delay(100);
  Serial.println(WiFi.gatewayIP());
  Serial.print("subnetMask :");
  delay(100);
  Serial.println(WiFi.subnetMask());
  Serial.print("SSID       :");
  delay(100);
  Serial.println(WiFi.SSID());
  Serial.print("Password   :");
  delay(100);
  Serial.println(WiFi.psk());
  //-----------------------------------------------------------------------------------------------------------------------//
  //Print เพื่อเชคว่าค่าที่บันทึกไว้ยังอยู่หรือไม่
  Serial.println("-----------------------------------");
  Serial.println("เชคว่าค่าที่บันทึกไว้ยังอยู่หรือไม่");
  Serial.println();


  IP = WiFi.localIP().toString();
  Serial.print("IP to String = ");
  Serial.println(IP);


  digitalWrite(D4, LOW);
  
  LINE.setToken(line_token);

  LineText1 =  string220 + IP;
  LINE.notify(LineText1);



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


}

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



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


void loop(void) {

}


1 Like