Skip to content

Trying to get a Json from URL, else (if there is no connection) get it from SPIFFS #1710

@ArtalMQS

Description

@ArtalMQS

Hello to everyone, i'm trying to get a .json file to get some data to my project.
The purpose is to get the file from URL and override it to my .json file stored on SPIFFS.
Else, if there is no connection found i would take the json file directly from SPIFFS.

I tryed to get it from the URL and the test get well, but I can't reach to get it from SPIFFS.
I apologize for my English and any help would be appreciate.

Environment
Here is the environment that I'm using':

  • Microconroller: NodeMCU 0.9 (ESP-12 Module)
  • Core/runtime: ESP8266 core NodeMCU
  • IDE: Arduino IDE 1.8.16

MY CODE

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <FS.h>

const char* ssid = "myssid"; /i use it right
const char* password = "mypsw"; /use my right one too

#include <WiFiClient.h>

WiFiClient wifiClient;

void setup()
{
Serial.begin(115200);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting...");
}

if (WiFi.status() == WL_CONNECTED)
{
WiFiClient client; // or WiFiClientSecure for HTTPS
HTTPClient http;

// Send request
http.useHTTP10(true);
http.begin(client, "http://acicerone.altervista.org/Questions_Answers.json");
http.GET();


StaticJsonDocument<384> doc;

DeserializationError error = deserializeJson(doc, http.getStream());

if (error) {
  Serial.print(F("deserializeJson() failed: "));
  Serial.println(error.f_str());
  return;
}

JsonArray question = doc["question"];
const char* question_0 = question[0]; // "D3AC5F16"
const char* question_1 = question[1]; // "83707A16"
const char* question_2 = question[2]; // "23995E16"
const char* question_3 = question[3]; // "E3EB5B16"

JsonArray answer = doc["answer"];
const char* answer_0 = answer[0]; // "33757816"
const char* answer_1 = answer[1]; // "E3F36B16"
const char* answer_2 = answer[2]; // "03577B16"
const char* answer_3 = answer[3]; // "F3806916"

Serial.println("....");
Serial.println(question_0);
Serial.println("....");
Serial.println(answer_2);



// Disconnect
http.end();

}
else {
SPIFFS.begin();

File config = SPIFFS.open("config.json", "r");


StaticJsonDocument<384> doc;

DeserializationError error = deserializeJson(doc, config);

if (error) {
  Serial.print(F("deserializeJson() failed: "));
  Serial.println(error.f_str());
  return;
}


JsonArray question = doc["question"];
const char* question_0 = question[0]; // "D3AC5F16"
const char* question_1 = question[1]; // "83707A16"
const char* question_2 = question[2]; // "23995E16"
const char* question_3 = question[3]; // "E3EB5B16"

JsonArray answer = doc["answer"];
const char* answer_0 = answer[0]; // "33757816"
const char* answer_1 = answer[1]; // "E3F36B16"
const char* answer_2 = answer[2]; // "03577B16"
const char* answer_3 = answer[3]; // "F3806916"

Serial.println("__");
Serial.println(question_2);
Serial.println("__");

delay(60000);

}
}
void loop()
{
}
END CODE

the .json file saved to my data SPIFFS is the same at this URL: http://acicerone.altervista.org/Questions_Answers.json

Thanks to everyone

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions