การ Config ค่า SSID และ Password รวมถึงการใส่ Token ต่างๆลงใน esp32 อีกรูปแบบหนึ่ง

หลังจากที่ผมพยายามหา library ในการคอนฟิกค่า SSID และ Password รวมถึงการใส่ Token ต่างๆ ก็มาเจอ library ที่ชื่อว่า IotWebConf หลังจากที่ลองใช้แล้วปรากฏว่า มันสามารถใช้ได้ผลดี ดังนั้นจึงเอามาแชร์ในที่นี้กันครับ

หลังจากที่เราโหลดไฟล์ Library มาใช้งานแล้ว ผมจะใช้ตัวอย่างใน example ที่ชื่อว่า IotWebConf03CustomParameters.ino และทำการปรับปรุงแก้ไขเพื่อให้ได้ ตามที่ผมต้องการ โดยถ้าใครอยากจะลองก็ให้ลองไฟล์ชื่อนี้ก่อนนะครับแล้วจากนั้นมาลองไฟล์ที่ผมปรับแต่งดูก็จะเห็นความแตกต่างว่ามันเป็นยังไงบ้าง สังเกตว่าเราไม่ต้องใช้ปุ่มอะไรเลยนะครับ มีแต่ใช้ปุ่ม En ที่อยู่ใน esp32 on board มาเป็นตัว Reset เมื่อระบบทำงานมันจะมีเวลาให้เราตั้งค่าคอนฟิกตามเวลาที่กำหนดเอาไว้คือ 30 วินาทีถ้าภายใน 30 วินาทีไม่มีการตั้งค่า ก็จะทำการนำค่าเดิมที่เราเคยตั้งค่าเอาไว้มาทำการเชื่อมต่อ WiFi แต่ถ้าเดิมไม่มีการตั้งค่าหรือเป็นการตั้งค่าครั้งแรกก็จะมีชื่อ WiFi และ Password ตามโค้ด ที่เขาให้มาเป็นตัว Config ค่าเข้าไปก่อนจากนั้นเมื่อเข้าไปถึงหน้าการตั้งค่าเราก็จะสามารถที่จะปรับชื่อของตัว Config ให้เป็นของเราจากนั้นก็ทำการตั้งค่าได้ปกติครับ

ภาพ

ภาพ

อันนี้จะเป็นไฟล์ที่ผมปรับแต่งค่าเอาไว้ให้ส่ง LINE นะครับมันจะส่งแบบรัวๆหน่อยนะครับก็ไปลองปรับได้กันดู

#include <IotWebConf.h>

// -- Initial name of the Thing. Used e.g. as SSID of the own Access Point.
const char thingName[] = "PUY IOT FIRST";

// -- Initial password to connect to the Thing, when it creates an own Access Point.
const char wifiInitialApPassword[] = "111111111";
#include <TridentTD_LineNotify.h>
#define STRING_LEN 45
//#define NUMBER_LEN 32

// -- Configuration specific key. The value should be modified if config structure was changed.
#define CONFIG_VERSION "dem2"

// -- When CONFIG_PIN is pulled to ground on startup, the Thing will use the initial
//      password to buld an AP. (E.g. in case of lost password)
#define CONFIG_PIN D2

// -- Status indicator pin.
//      First it will light up (kept LOW), on Wifi connection it will blink,
//      when connected to the Wifi it will turn off (kept HIGH).
#define STATUS_PIN 2

// -- Callback method declarations.
void configSaved();
boolean formValidator();

DNSServer dnsServer;
WebServer server(80);

char stringParamValue[STRING_LEN];
//char intParamValue[NUMBER_LEN];
//char floatParamValue[NUMBER_LEN];

IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword, CONFIG_VERSION);
IotWebConfParameter stringParam = IotWebConfParameter("Line_token", "Line_token", stringParamValue, STRING_LEN);
//IotWebConfSeparator separator1 = IotWebConfSeparator();
//IotWebConfParameter intParam = IotWebConfParameter("Int param", "intParam", intParamValue, NUMBER_LEN, "number", "1..100", NULL, "min='1' max='100' step='1'");
// -- We can add a legend to the separator
//IotWebConfSeparator separator2 = IotWebConfSeparator("Calibration factor");
//IotWebConfParameter floatParam = IotWebConfParameter("Float param", "floatParam", floatParamValue, NUMBER_LEN, "number", "e.g. 23.4", NULL, "step='0.1'");




void setup()
{
  Serial.begin(115200);


  Serial.println();
  Serial.println("Starting up...");

  iotWebConf.setStatusPin(STATUS_PIN);
  //iotWebConf.setConfigPin(CONFIG_PIN);
  iotWebConf.addParameter(&stringParam);
  //  iotWebConf.addParameter(&separator1);
  //  iotWebConf.addParameter(&intParam);
  //  iotWebConf.addParameter(&separator2);
  //  iotWebConf.addParameter(&floatParam);
  iotWebConf.setConfigSavedCallback(&configSaved);
  iotWebConf.setFormValidator(&formValidator);
  iotWebConf.getApTimeoutParameter()->visible = true;

  // -- Initializing the configuration.
  iotWebConf.init();

  // -- Set up required URL handlers on the web server.
  server.on("/", handleRoot);
  server.on("/config", [] { iotWebConf.handleConfig(); });
  server.onNotFound([]() {
    iotWebConf.handleNotFound();
  });

  Serial.println("Ready.");



}












void loop()
{

    LINE.setToken(stringParamValue);
   LINE.notify("ESP32 CAM เริ่มทำงาน");

  // -- doLoop should be called as frequently as possible.
  iotWebConf.doLoop();
}

























/**
   Handle web requests to "/" path.
*/
void handleRoot()
{
  // -- Let IotWebConf test and handle captive portal requests.
  if (iotWebConf.handleCaptivePortal())
  {
    // -- Captive portal request were already served.
    return;
  }
  String s = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/>";
  s += "<title>IotWebConf 03 Custom Parameters</title></head><body>Hello world!";
  s += "<ul>";
  s += "<li>String param value: ";
  s += stringParamValue;
  // s += "<li>Int param value: ";
  //s += atoi(intParamValue);
  //s += "<li>Float param value: ";
  // s += atof(floatParamValue);
  s += "</ul>";
  s += "Go to <a href='config'>configure page</a> to change values.";
  s += "</body></html>\n";

  server.send(200, "text/html", s);
}

void configSaved()
{
  Serial.println("Configuration was updated.");

}

boolean formValidator()
{
  Serial.println("Validating form.");
  boolean valid = true;

  int l = server.arg(stringParam.getId()).length();
  if (l < 3)
  {
    stringParam.errorMessage = "Please provide at least 3 characters for this test!";
    valid = false;
  }



  return valid;
}

https://drive.google.com/file/d/1JppRI7JMWsYDbY-Brz5q5rP9u2DJAhL8/view?usp=sharing

Code ESP32_WEBconfig_Line_RSSI_Internet_DMY_PB_Blynk

สำหรับโค้ดชุดนี้ เป็นการ Config WiFi ด้วย WEBconfig โดยมีการแยก core 1 และ 0 อย่างชัดเจน เพื่อแยกค่าการทำงานของ RSSI DMY PB Blynk Linenotify

ESP32_WEBconfig_Line_RSSI_Internet_DMY_PB_Blynk.rar (5.0 KB)

72936

ESP32_WEBconfig_Line_RSSI_Internet_DMY_PB_Blynk_SSID_PWS_IP_SUB.rar (5.3 KB)

สำหรับ Code ชุดนี้ก็ตามชื่อไฟล์นะครับจะประกอบด้วยการคอนฟิกค่าด้วย webconfig มี LINE INTERNET วันเดือนปี Blynk Push button switch และการแสดงค่า ssid Password IP Address subnet Mask และเกตเวย์

8:12:2563
IotWebConf.rar (66.2 KB)

ใช้ ESP32 1 ตัว : Config WiFi - LCD1602 - PB10 - Relay10- Line notify-Blynk

ขอบคุณมากครับ ทึ่แชร์ความรู้ใหม่ๆที่คิดได้มาแชร์ให้ดูนะครับ มีประโยชน์มากครับ ผมมือใหม่เพิ่งเริ่มศึกษายังไม่ค่อยเข้าใจต้องศึกษาอีกเยอะ ถ้ามีปํญหาขอคำปรึกษาด้วยนะครับ