ความรู้แน่น ฟรีสำหรับชุมชน ArduinoAll ที่นี่เท่านั้น

ฟรีและดีที่สุด คอร์สอบรม Arduino + NodeMCU
ทำเพื่อแบ่งปัน ห้ามนำไปจำหน่าย หรือเก็บเงินค่าเรียน
  !!!


  


AllNewStep รับประกันคุณภาพทุกชิ้น วันจันทร์-ศุกร์แจ้งชำระสินค้าก่อน 14.00 จัดส่งทันทีวันนี้ค่ะ

กรุงเทพ /ภาคกลาง ได้พรุ่งนี้


*** สินค้าทุกชิ้น ถ้าสามารถทำรายการสั่งซื้อได้ แสดงว่ามีครบทุกรายการค่ะ *** 

พิมพ์ค้นหาบทความ หัวข้อกระทู้ และสินค้าในเว็บ AllNewStep ได้ที่นี่
QUOTE 

รบกวนสอบถามครับ ผมทำ wifimanager และทำ custom parameter แต่ไม่สามารถสืบทอดตัวแปรจาก setup ไปใช้ใน loop ได้

kohrx

จากคำสั่งใน void setup

  // put your setup code here, to run once:

  Serial.println();

 

  //clean FS, for testing

  //SPIFFS.format();

 

  //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");  

              /////////////////////////////////

              /////// custom parameter ////////

              ////////////////////////////////

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

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

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

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

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

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

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

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

          } else {

          Serial.println("failed to load json config");

        }

      }

    }

  } else {

    Serial.println("failed to mount FS");

  }

  //end read

  

  /////////////////////////////////

  /////// custom parameter ////////

  ////////////////////////////////

  Serial.println(sensor1_name);

  Serial.println(sensor2_name);

  Serial.println(sensor1_temp_low);

  Serial.println(sensor1_temp_hight);

  Serial.println(sensor2_temp_low);

  Serial.println(sensor2_temp_hight);

  Serial.println(api_key);

  Serial.println(url);

    

  /////////////////////////////////

  /////// custom parameter ////////

  ////////////////////////////////

  WiFiManagerParameter custom_sensor1_name("sensor1_name", "sensor 1 name.", sensor1_name, 50);  

  WiFiManagerParameter custom_sensor2_name("sensor2_name", "sensor 2 name.", sensor2_name, 50);  

  WiFiManagerParameter custom_sensor1_temp_low("sensor1_temp_low", "sensor 1 temp. low", sensor1_temp_low, 5);  

  WiFiManagerParameter custom_sensor1_temp_hight("sensor1_temp_hight", "sensor 1 temp. hight", sensor1_temp_hight, 5);  

  WiFiManagerParameter custom_sensor2_temp_low("sensor2_temp_low", "sensor 2 temp. low", sensor2_temp_low, 5);  

  WiFiManagerParameter custom_sensor2_temp_hight("sensor2_temp_hight", "sensor 2 temp. hight", sensor2_temp_hight, 5);  

  WiFiManagerParameter custom_api_key("api_key", "API key", api_key, 30); 

  WiFiManagerParameter custom_url("url", "URL", url, 50); 

     

  //WiFiManager

  //Local intialization. Once its business is done, there is no need to keep it around

  WiFiManager wifiManager;

      //กดปุ่มเพื่อ reset wifi ใช้ Pin0

      pinMode(ConfigWiFi_Pin,INPUT_PULLUP);

      if(digitalRead(ConfigWiFi_Pin) == LOW) // Press button

      {

      //reset saved settings

      wifiManager.resetSettings(); // go to ip 192.168.4.1 to config

      }    

 

  //set config save notify callback

  wifiManager.setSaveConfigCallback(saveConfigCallback);

 

 

  /////////////////////////////////

  /////// custom parameter ////////

  ////////////////////////////////

  wifiManager.addParameter(&custom_sensor1_name);

  wifiManager.addParameter(&custom_sensor2_name);

  wifiManager.addParameter(&custom_sensor1_temp_low);

  wifiManager.addParameter(&custom_sensor1_temp_hight);

  wifiManager.addParameter(&custom_sensor2_temp_low);

  wifiManager.addParameter(&custom_sensor2_temp_hight);

  wifiManager.addParameter(&custom_api_key);

  wifiManager.addParameter(&custom_url);

    

  //set minimu quality of signal so it ignores AP's under that quality

  //defaults to 8%

  wifiManager.setMinimumSignalQuality();

  

  //sets timeout until configuration portal gets turned off

  //useful to make it all retry or go to sleep

  //in seconds

  //wifiManager.setTimeout(120);

 

  if (!wifiManager.autoConnect("AutoConnectAP", "password")) {

    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);

  }

 

  //if you get here you have connected to the WiFi

  Serial.println("connected...yeey :)");

 

  /////////////////////////////////

  /////// custom parameter ////////

  ////////////////////////////////

 

  //read updated parameters 

  strcpy(sensor1_name, custom_sensor1_name.getValue());

  strcpy(sensor2_name, custom_sensor2_name.getValue());

  strcpy(sensor1_temp_low, custom_sensor1_temp_low.getValue());

  strcpy(sensor1_temp_hight, custom_sensor1_temp_hight.getValue());

  strcpy(sensor2_temp_low, custom_sensor2_temp_low.getValue());

  strcpy(sensor2_temp_hight, custom_sensor2_temp_hight.getValue());

  strcpy(api_key, custom_api_key.getValue());

  strcpy(url, custom_url.getValue());

      

  /////////////////////////////////

  /////// custom parameter ////////

  ////////////////////////////////

 

  //save the custom parameters to FS

  if (shouldSaveConfig) {

      Serial.println("saving config");

      DynamicJsonBuffer jsonBuffer;

      JsonObject& json = jsonBuffer.createObject();

      json["sensor1_name"] = sensor1_name;

      json["sensor2_name"] = sensor2_name;

      json["sensor1_temp_low"] = sensor1_temp_low;

      json["sensor1_temp_hight"] = sensor1_temp_hight;

      json["sensor2_temp_low"] = sensor2_temp_low;

      json["sensor2_temp_hight"] = sensor2_temp_hight;

      json["api_key"] = api_key;

      json["url"] = url;

      

    File configFile = SPIFFS.open("/config.json", "w");

    if (!configFile) {

      Serial.println("failed to open config file for writing");

    }

 

    json.prettyPrintTo(Serial);

    json.printTo(configFile);

    configFile.close();

    //end save

  }

 

 

////////////////////////////////////////////////////////////////////////////

แต่พอผมลอง Serial.println(api_key);  ใน void loop  ผลที่ได้คือว่างเปล่าครับ  แบบนี้เราจะดึง parameter นี้มาใช้ใน loop ยังไงครับ

QUOTE 
ความคิดเห็นที่ #1
kohrx

คือผมเอาตัวแปร api_key ไปใช้มันจะสามารถนำไปใช้ได้ในตอนแรกหลังจากเรา config  แต่พอ reset nodeMCU แล้วค่าพวกนี้มันก็จะว่างเพราะมันเป็นตัวแปรที่ได้จากการประกาศและมีค่าว่างในตอนแรก  ดังนั้นเราจะเอาค่าที่มัน save ไว้ใน config.json มาใช้ได้ยังไงครับ

QUOTE 
ความคิดเห็นที่ #2
เจ้าของร้าน

ลองประกาศตัวแปร api_key ไว้บนสุด นอกฟังก์ชั่น setup กับ loop

var api_key;

void setup(){}

void loop(){}

 

หรืออีกวิธีเก็บไว้ใน eeprom ครับ

https://gist.github.com/NAzT/b18ba72003590a043c3b#file-eeprom_string_esp8266-ino

แสดงความคิดเห็นที่ 1-2 จากทั้งหมด 2 ความคิดเห็น
ขาย ARDUINO
คุณภาพ อันดับ 1

ได้รับรางวัลร้านยอดเยี่ยม
ตั้งแต่ปี 2558
ขาย Arduino
วีดีโอสอน Arduino

สอน esp8266

สอน Arduino IoT
สอน Arduino แบบเร็ว

สอน NodeMCU

อุปกรณ์ Arduino

MEMBER ZONE

พูดคุย-สอบถาม