PlatformIO Community

Is ESP8266 platform outdated compare with Arduino IDE?

I’m testing secure update, I’m using this official example from repository of ESP8266 Arduino.

For reasons that I don’t know, if I upload this code from platformio, the code never finds "/certs.ar" file, but if I upload from Arduino the code runs perfectly:

/**
   httpUpdateSecure.ino
    Created on: 20.06.2018 as an adaptation of httpUpdate.ino
*/

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>

#include <time.h>

#include <FS.h>
#include <LittleFS.h>

#ifndef APSSID
#define APSSID "APSSID"
#define APPSK  "APPSK"
#endif

ESP8266WiFiMulti WiFiMulti;

// A single, global CertStore which can be used by all
// connections.  Needs to stay live the entire time any of
// the WiFiClientBearSSLs are present.
#include <CertStoreBearSSL.h>
BearSSL::CertStore certStore;

// Set time via NTP, as required for x.509 validation
void setClock() {
  configTime(0, 0, "pool.ntp.org", "time.nist.gov");  // UTC

  Serial.print(F("Waiting for NTP time sync: "));
  time_t now = time(nullptr);
  while (now < 8 * 3600 * 2) {
    yield();
    delay(500);
    Serial.print(F("."));
    now = time(nullptr);
  }

  Serial.println(F(""));
  struct tm timeinfo;
  gmtime_r(&now, &timeinfo);
  Serial.print(F("Current time: "));
  Serial.print(asctime(&timeinfo));
}

void setup() {

  Serial.begin(115200);
  // Serial.setDebugOutput(true);

  Serial.println();
  Serial.println();
  Serial.println();

  for (uint8_t t = 4; t > 0; t--) {
    Serial.printf("[SETUP] WAIT %d...\n", t);
    Serial.flush();
    delay(1000);
  }

  WiFi.mode(WIFI_STA);
  WiFiMulti.addAP(APSSID, APPSK);

  LittleFS.begin();

  int numCerts = certStore.initCertStore(LittleFS, PSTR("/certs.idx"), PSTR("/certs.ar"));
  Serial.print(F("Number of CA certs read: "));
  Serial.println(numCerts);
  if (numCerts == 0) {
    Serial.println(F("No certs found. Did you run certs-from-mozill.py and upload the LittleFS directory before running?"));
    return; // Can't connect to anything w/o certs!
  }
}

void loop() {
  // wait for WiFi connection
  if ((WiFiMulti.run() == WL_CONNECTED)) {

    setClock();

    BearSSL::WiFiClientSecure client;
    bool mfln = client.probeMaxFragmentLength("server", 443, 1024);  // server must be the same as in ESPhttpUpdate.update()
    Serial.printf("MFLN supported: %s\n", mfln ? "yes" : "no");
    if (mfln) {
      client.setBufferSizes(1024, 1024);
    }
    client.setCertStore(&certStore);

    // The line below is optional. It can be used to blink the LED on the board during flashing
    // The LED will be on during download of one buffer of data from the network. The LED will
    // be off during writing that buffer to flash
    // On a good connection the LED should flash regularly. On a bad connection the LED will be
    // on much longer than it will be off. Other pins than LED_BUILTIN may be used. The second
    // value is used to put the LED on. If the LED is on with HIGH, that value should be passed
    ESPhttpUpdate.setLedPin(LED_BUILTIN, LOW);

    t_httpUpdate_return ret = ESPhttpUpdate.update(client, "https://server/file.bin");
    // Or:
    //t_httpUpdate_return ret = ESPhttpUpdate.update(client, "server", 443, "file.bin");


    switch (ret) {
      case HTTP_UPDATE_FAILED:
        Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s\n", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
        break;

      case HTTP_UPDATE_NO_UPDATES:
        Serial.println("HTTP_UPDATE_NO_UPDATES");
        break;

      case HTTP_UPDATE_OK:
        Serial.println("HTTP_UPDATE_OK");
        break;
    }
  }
delay(5000);
}

The code is looking for a file in its LittleFS file system. You have to tell PlatformIO to upload these files as well or use what the comment tells you to use

This script (https://github.com/arduino/esp8266/blob/master/libraries/ESP8266WiFi/examples/BearSSL_CertStore/certs-from-mozilla.py) generates it. Then put it in the data/ folder of your project and upload the filesystem, as the docs tell you.

The latest espressif8266 platform already supports LittleFS, so don’t worry about the docs still saying “SPIFFS”.

1 Like

Yes, I did all of this, but still can’t find certs.ar. I not upload file system image from platformio, is from Arduino IDE. But neither work.

I did the next combination:

Firmware and FS from PlatformIO – don’t work
Frimware from PlatforIO and FS from Arduino – don’t work
Firmware from Arduino and FS from PlaformIO – don’t work
Firmware and FS from Arduino – work

Are you sure that PlatformIO can support LittleFS? can you test the code?
For me, still doesn’t work.

The LittleFS support/change Max is referring isn’t in a release build yet for some reason… only the develop branch… so you will be making a SPIFFS partition with PlatformIO, and the code is looking for a LittleFS one.

You can either switch to the development version of the platformio-espressif8266 platform using

platform = https://github.com/platformio/platform-espressif8266.git

in your platformio.ini or you can follow this guide on how to add LittleFS support via an extra_script and the mklittlefs tool. You should only need to download the two files, and add the extra_script line… python install is a given since you’re using platformio…

1 Like

Thanks, now is working, I hope this come soon to the master branch.

1 Like