อยากเปลี่ยนมาใช้ Custom Server ต้องแก้ไขยังไงครับ

Souliya999 (Souliya) August 13, 2020, 8:48am #1

#define BLYNK_PRINT Serial
#include <FS.h>
#include <DNSServer.h>
#include <WiFiManager.h>
#include <ESP8266WebServer.h>
#include <BlynkSimpleEsp8266.h>
#include <ArduinoJson.h>

#define SET_PIN 0

char blynk_token[40] = "";
bool shouldSaveConfig = false;

void saveConfigCallback () {
  Serial.println("Should save config");
  shouldSaveConfig = true;
}

void setup() {
  Serial.begin(115200);
  Serial.println();
  pinMode(SET_PIN, INPUT_PULLUP);
  delay(3000);

  //read configuration from FS json
  Serial.println("mounting FS...");
  if (SPIFFS.begin()) {
    Serial.println("mounted file system");
    if (SPIFFS.exists("/config.json")) {
      //file exists, reading and loading
      Serial.println("reading config file");
      File configFile = SPIFFS.open("/config.json", "r");
      if (configFile) {
        Serial.println("opened 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 json");
          strcpy(blynk_token, json["blynk_token"]);

        } else {
          Serial.println("failed to load json config");
        }
        configFile.close();
      }
    }
  } else {
    Serial.println("failed to mount FS");
  }

  WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 32);
  WiFiManager wifiManager;
  wifiManager.setSaveConfigCallback(saveConfigCallback);
  wifiManager.addParameter(&custom_blynk_token);

  if (digitalRead(SET_PIN) == LOW) {
    wifiManager.resetSettings();
  }

  if (!wifiManager.autoConnect("Kitchenroom")) {
    Serial.println("failed to connect and hit timeout");
    delay(3000);
    ESP.reset();
    delay(5000);
  }

  Serial.println("wifi connected");
  strcpy(blynk_token, custom_blynk_token.getValue());

  //save the custom parameters to FS
  if (shouldSaveConfig) {
    Serial.println("saving config");
    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();
    json["blynk_token"] = blynk_token;

    File configFile = SPIFFS.open("/config.json", "w");
    if (!configFile) {
      Serial.println("failed to open config file for writing");
    }

    json.printTo(Serial);
    json.printTo(configFile);
    configFile.close();
  }
  
  Serial.println();
  Serial.print("local ip : ");
  Serial.println(WiFi.localIP());
  Serial.print("Blynk Token : ");
  Serial.println(blynk_token);
  Blynk.config(blynk_token);

}

void loop() {
  Blynk.run();
}

Remy_Martin (Permpol Thanapunnamas) August 13, 2020, 8:49am #2

เริ่มจากตรงนี้ครับ

Souliya999 (Souliya) August 13, 2020, 9:08am #3

คือผมอยากใส่ blynk token ผ่าน wifimanager นะครับ. แต่ว่าโค็ดนี้คือสามารจใช้ได้แต่กับ blynk-cloud.com ถ้าอยากเปลี่ยนมาใช้ของ oasiskit.com ต้องแก้ไขตรงไหนครับ

Remy_Martin (Permpol Thanapunnamas) August 13, 2020, 9:11am #4

ศึกษาจากกระทู้นี้ครับ