Beranda > Arduino, IoT > Menggunakan SPIFFS pada ESP8266 – Membaca File Config (JSON file) dalam File System untuk Koneksi Ke WIFI

Menggunakan SPIFFS pada ESP8266 – Membaca File Config (JSON file) dalam File System untuk Koneksi Ke WIFI

19 November 2018

Image result for esp8266 v3

Pada kesempatan ini saya akan share bagaimana cara membaca data config yang di upload ke SPI Flash Filing System.

Dalam skenario ini kita akan mengkoneksika ESP8266 ke WIFI menggunakan SSID dan password yang kita simpan ke dalam SPIFFS.

Download disini.

Install ESP8266 Filesystem Uploader (ESP8266 Sketch Data Upload)

  1. Downlaod file ESP8266FS-0.3.0.zip di https://github.com/esp8266/arduino-esp8266fs-plugin/releases/tag/0.3.0.
  2. Unzip.
  3. Buka folder C:\Program Files (x86)\Arduino\tools. Buat folder ESP8266FS/tool. Copykan file esp8266fs.jar kedalamnya.
  4. Restart Arduino IDE.

 

Buat file JSON.

1. Buka https://jsoneditoronline.org untuk membuat sebuah data berbasi JSON.

2. Buat file config.json berisi hasil diatas.

{
"nama_ssid": "Flash2",
"isi_password": "qwertyui"
}

 

Sketch

1. Buka file ConfigFile.ino. Save As di tempat lain.

2. Klik Tools >> ESP8266 Sketch Data Upload. Maka akan terbuat sebuah folder data pada root folder tempat kita menyimpan sketch. Dengan pesan error karena file config.json nya belum ada.

3. Copy file config.json ke folder data.

4. Koneksikan ESP8266.

5. Sekarang klik lagi Tools >> ESP8266 Sketch Data Upload. Kalau muncul pesan berikut maka file json telah terupload ke esp8266.

error: espcomm_open failed
error: espcomm_upload_mem failed

SPIFFS Upload failed!
[SPIFFS] data   : C:\Users\creative1\Desktop\JSON SPIFFS\ConfigFile\data
[SPIFFS] size   : 1004
[SPIFFS] page   : 256
[SPIFFS] block  : 8192

/config.json

[SPIFFS] upload : C:\Users\CREATI~1\AppData\Local\Temp\arduino_build_547508/ConfigFile.spiffs.bin
[SPIFFS] address: 0x300000
[SPIFFS] reset  : nodemcu
[SPIFFS] port   : COM5
[SPIFFS] speed  : 115200

Uploading 1028096 bytes from C:\Users\CREATI~1\AppData\Local\Temp\arduino_build_547508/ConfigFile.spiffs.bin to flash at 0x00300000

...................................................................... [  7% ]
...................................................................... [ 15% ]
...................................................................... [ 23% ]
...................................................................... [ 31% ]
...................................................................... [ 39% ]
...................................................................... [ 47% ]
...................................................................... [ 55% ]
...................................................................... [ 63% ]
...................................................................... [ 71% ]
...................................................................... [ 79% ]
...................................................................... [ 87% ]
...................................................................... [ 95% ]
......................................                                [ 100% ]

6. Edit sedikit file ConfigFile.ino menjadi seperti ini:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ArduinoJson.h>
#include "FS.h"

const char *ssid;
const char *password;

ESP8266WebServer server(80);

bool loadConfig() {
File configFile = SPIFFS.open("/config.json", "r");
if (!configFile) {
Serial.println("Failed to open config file");
return false;
}

size_t size = configFile.size();
if (size > 1024) {
Serial.println("Config file size is too large");
return false;
}

std::unique_ptr<char[]> buf(new char[size]);

configFile.readBytes(buf.get(), size);

StaticJsonBuffer<200> jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());

if (!json.success()) {
Serial.println("Failed to parse config file");
return false;
}

const char* Nama_SSID = json["nama_ssid"];
const char* Isi_Password = json["isi_password"];

WiFi.disconnect();
WiFi.mode(WIFI_STA);
WiFi.begin(Nama_SSID, Isi_Password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("SSID : ");
Serial.println(Nama_SSID);
Serial.print("Password : ");
Serial.println(Isi_Password);
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

return true;
}

void setup() {
Serial.begin(115200);
Serial.println("");
delay(1000);
Serial.println("Mounting FS...");

if (!SPIFFS.begin()) {
Serial.println("Failed to mount file system");
return;
}

if (!loadConfig()) {
Serial.println("Failed to load config");
} else {
Serial.println("Config loaded");
}
}

void loop() {
}

7. Upload ConfigFile.ino ke ESP8266. Buka serial monitor.

%d blogger menyukai ini: