PlatformIO

Minimal STM32 HardwareTimer PlatformIO / Arduino timer interrupt blink example

This is a minimal example of using timer interrupts on PlatformIO / Arduino using HardwareTimer (which is a part of the PlatformIO STM32 Arduino installation – no need to install a library). Tested on the Olimex E407 board. It will run on almost any STM32 processor but you might need to adjust PC13 to the PIN connected to the LED. The example will blink the LED once per second.

#include <Arduino.h>

HardwareTimer timer(TIM1);
bool ledOn = false;

void OnTimer1Interrupt() {
    ledOn = !ledOn;
    digitalWrite(PC13, ledOn ? HIGH : LOW);
}

void setup() {
    pinMode(PC13, OUTPUT);
    // Configure timer
    timer.setPrescaleFactor(2564); // Set prescaler to 2564 => timer frequency = 168MHz/2564 = 65522 Hz (from prediv'd by 1 clocksource of 168 MHz)
    timer.setOverflow(32761); // Set overflow to 32761 => timer frequency = 65522 Hz / 32761 = 2 Hz
    timer.attachInterrupt(OnTimer1Interrupt);
    timer.refresh(); // Make register changes take effect
    timer.resume(); // Start
}

void loop() {
}

 

Posted by Uli Köhler in PlatformIO, STM32

How to fix PlatformIO “Start debugging” doing nothing

Problem:

When you click on Start debugging, press F5 or click on the debug start triangle in PlatformIO

the firmware builds but then nothing happens:

Linking .pio/build/olimex_e407/firmware.elf
Checking size .pio/build/olimex_e407/firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   0.7% (used 936 bytes from 131072 bytes)
Flash: [          ]   1.7% (used 18064 bytes from 1048576 bytes)
Building .pio/build/olimex_e407/firmware.bin
======================================================================== [SUCCESS] Took 2.72 seconds ========================================================================

Solution:

You need to specify a debug_tool in platformio.ini. For STM32 processors, a typical choice is

debug_tool = stlink

A list of options for debug_tool is available here.

Note that you can NOT debug many boards via the USB port. You can not debug boards attached via serial-to-USB converter (like many Arduino boards). Your board need to have a proper debugger on-board (like an stlink which is integrated on many STM32 eval boards) or you need to use an external debugger.

Posted by Uli Köhler in PlatformIO, STM32

Minimal STM32 TimerInterrupt_Generic PlatformIO / Arduino timer interrupt blink example

Note: If you need more flexibility, see Minimal STM32 HardwareTimer PlatformIO / Arduino timer interrupt blink example where we show how to use HardwareTimer instead of TimerInterrupt_Generic. Note that TimerInterrupt_Generic uses HardwareTimer internally.

This is a minimal example of using timer interrupts on PlatformIO / Arduino using the TimerInterrupt_Generic library which runs on the Olimex E407. It will run on almost any STM32 processor but you might need to adjust PC13 to the PIN connected to the LED. The example will blink the LED once per second.

#include <Arduino.h>
#include <TimerInterrupt_Generic.h>

STM32Timer tim1(TIM1);
bool ledOn = false;

void OnTimer1Interrupt() {
    ledOn = !ledOn;
    digitalWrite(PC13, ledOn ? LOW : HIGH);
}

void setup() {
    pinMode(PC13, OUTPUT);
    // Enable TIM4
    
    tim1.attachInterruptInterval(500000, OnTimer1Interrupt);
}

void loop() {
}

Now add

lib_deps =
     khoih.prog/TimerInterrupt_Generic @ ^1.7.0

to your platformio.ini. My full platformio.ini looks like this:

[env:olimex_e407]
platform = ststm32
board = olimex_e407
framework = arduino
lib_deps =
     khoih.prog/TimerInterrupt_Generic @ ^1.7.0

 

 

Posted by Uli Köhler in PlatformIO, STM32

PlatformIO Olimex E407 Arduino LED blink example

This code will make the Olimex E407 LED blink.

#include <Arduino.h>

void setup() {
    pinMode(PC13, OUTPUT);
}

void loop() {
    digitalWrite(PC13, LOW);
    delay(500);
    digitalWrite(PC13, HIGH);
    delay(500);
}

 

Posted by Uli Köhler in PlatformIO, STM32

How to fix PlatformIO Olimex E407 LED_BUILTIN not working

Problem

You are trying to run a firmware on the Olimex E407 that blinks the builtin green status LED. You code uses LED_BUILTIN similar to this:

#include <Arduino.h>

void setup() {
    pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
    digitalWrite(LED_BUILTIN, LOW);
    delay(500);
    digitalWrite(LED_BUILTIN, HIGH);
    delay(500);
}

but when you upload the code onto the board, the LED does not blink and stays off.

Solution

Instead of LED_BUILTIN, use PC13 – the pin the LED is connected to (which you can see on the Olimex E407 schematic:

#include <Arduino.h>

void setup() {
    pinMode(PC13, OUTPUT);
}

void loop() {
    digitalWrite(PC13, LOW);
    delay(500);
    digitalWrite(PC13, HIGH);
    delay(500);
}

 

Posted by Uli Köhler in PlatformIO, STM32

How to fix PlatformIO STM32 Error: libusb_open() failed with LIBUSB_ERROR_ACCESS

Problem:

While trying to program your STM32 board using stlink and PlatformIO (most programmers integrated onto a development board are STLink programmers), you see this error message:

xPack OpenOCD, x86_64 Open On-Chip Debugger 0.11.0-00155-ge392e485e (2021-03-15-16:43)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
debug_level: 1

hla_swd
Error: libusb_open() failed with LIBUSB_ERROR_ACCESS
Error: open failed
in procedure 'program'
** OpenOCD init failed **
shutdown command invoked

Solution:

You need to setup the correct permissions for the STLink usb devices – in other words, install the correct stlink udev rules files. On Ubuntu, install stlink-tools using

sudo apt -y install stlink-tools
sudo systemctl restart udev

After that, unplug your stlink (or development board) for 5 seconds and plugin it in again. This will cause the new device permissions to take effect.

Now you can retry uploading the firmware from PlatformIO.

Posted by Uli Köhler in PlatformIO, STM32

What is the difference between BIGTREE_OCTOPUS_V1 and BIGTREE_OCTOPUS_V1_USB

When building Marlin firmware for the BigTreeTech Octopus from the official BigTreeTech GitHub repository Marlin directory, you can see two different targets in PlatformIO:

  • BIGTREE_OCTOPUS_V1
  • BIGTREE_OCTOPUS_V1_USB

It is not immediately made clear what the difference between those is, but a short description can be found in ini/stm32f4.ini:

BIGTREE_OCTOPUS_V1_USB has support for using USB flashdrives directly on the board and serial-over-USB while BIGTREE_OCTOPUS_V1 has not.

In most cases, you want to build BIGTREE_OCTOPUS_V1_USB if printing via USB (e.g. via Octoprint) because the BIGTREE_OCTOPUS_V1 configuration does not allow printing via USB.
The compiler definitions for BIGTREE_OCTOPUS_V1 in ini/stm32f4.ini are:
build_flags        = ${stm32_variant.build_flags}
                     -DSTM32F446_5VX -DUSE_USB_HS_IN_FS

whereas BIGTREE_OCTOPUS_V1_USB enabled more USB-related features:

build_flags       = ${stm_flash_drive.build_flags}
                    -DSTM32F446_5VX -DUSE_USB_HS_IN_FS
                    -DUSE_USBHOST_HS -DUSBD_IRQ_PRIO=5
                    -DUSBD_IRQ_SUBPRIO=6
                    -DUSBD_USE_CDC_MSC
Posted by Uli Köhler in 3D printing, Electronics, PlatformIO, STM32

ESP32 minimal Wifi access point example (PlatformIO / Arduino)

This minimal example shows how to create a wifi access point on the ESP32 using the Arduino framework on PlatformIO.

#include <Arduino.h>
#include <WiFi.h>

void setup() {
  WiFi.softAP("MyWifiName", "MyWifiPassword");
}

void loop() {
  // put your main code here, to run repeatedly:
}

As you can see, it’s really simple. Just call

WiFi.softAP("MyWifiName", "MyWifiPassword");

and the WiFi library will take care of the rest.

Posted by Uli Köhler in Electronics, ESP8266/ESP32, PlatformIO

Minimal ESP32 NTP client example using NTPClient_Generic and PlatformIO

Example of using NTPClient_Generic

main.cpp

#include <Arduino.h>
#include <WiFi.h>
#include <NTPClient_Generic.h>

#define TIME_ZONE_OFFSET_HRS 1 // UTC+1 for Germany, winter time
#define NTP_UPDATE_INTERVAL_MS 60000L // Update every 60s automatically
WiFiUDP ntpUDP;
NTPClient ntpClient(ntpUDP, "europe.pool.ntp.org", (3600 * TIME_ZONE_OFFSET_HRS), NTP_UPDATE_INTERVAL_MS);

void ntpUpdateTask(void* param) {
  while(true) {
    // Update NTP. This will only ACTUALLY update if
    // the NTP client has not updated for NTP_UPDATE_INTERVAL_MS
    ntpClient.update();
    vTaskDelay(1000 / portTICK_PERIOD_MS);
  }
}

void setup() {
  Serial.begin(115200);
  // Force reset Wifi to avoid failure to connect
  WiFi.disconnect(true);
  // Set hostname
  WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
  WiFi.setHostname("ESP-NTP");
  // Connect to Wifi
  WiFi.begin("MyWifiSSID", "MyWifiPassword");
  while (WiFi.status() != WL_CONNECTED) {
      delay(100);
      Serial.println("Wifi connecting...");
  }

  // Start NTP client (i.e. start listening for NTP packets)
  ntpClient.begin();
  // Create task to automatically update NTP in the background
  xTaskCreate(ntpUpdateTask, "NTP update", 2000, nullptr, 1, nullptr);
}

void loop() {
  delay(1000);
  if (ntpClient.updated()) {
    Serial.println("# Time in sync with NTP server");
  } else {
    Serial.println("# TIME NOT IN SYNC WITH NTP SERVER !");
    return; // Do not print time
  }

  Serial.println("UTC : " + ntpClient.getFormattedUTCTime());
  Serial.println("UTC : " + ntpClient.getFormattedUTCDateTime());
  Serial.println("LOC : " + ntpClient.getFormattedTime());
}

platformio.ini

[env:nodemcu-32s]
platform = espressif32
board = nodemcu-32s
framework = arduino
monitor_speed = 115200
lib_deps =
    khoih.prog/NTPClient_Generic @ ^3.2.2
    Time

Example output:

Wifi connecting...
Wifi connecting...
Wifi connecting...
# TIME NOT IN SYNC WITH NTP SERVER !
# Time in sync with NTP server
UTC : 01:22:07
UTC : 01:22:07 Sun 07 Feb 2021
LOC : 02:22:07
# Time in sync with NTP server
UTC : 01:22:08
UTC : 01:22:08 Sun 07 Feb 2021
LOC : 02:22:08
# Time in sync with NTP server
UTC : 01:22:09
UTC : 01:22:09 Sun 07 Feb 2021
LOC : 02:22:09

 

Posted by Uli Köhler in C/C++, ESP8266/ESP32, PlatformIO

How to get current gateway IP address on ESP8266/ESP32

In order to get the current gateway IP address on the ESP8266 or ESP32, use:

WiFi.gatewayIP();

In order to print the gateway IP address on the serial port, use

Serial.println("Gateway IP address: ");
Serial.println(WiFi.gatewayIP());

In order to get the gateway IP address as string, use

String gatewayIP = WiFi.gatewayIP().toString();

 

Posted by Uli Köhler in C/C++, Embedded, ESP8266/ESP32, PlatformIO

ESP32 Servo controllable via HTTP JSON API / web browser

This sketch for PlatformIO  allows you to use the ESP32 as a Servo controller (servo on pin D25) that connects to Wifi and can be controller using a simple HTTP API. The webserver is implemented using ESPAsyncWebserver. Also see ESP32 minimal JSON webserver example for PlatformIO (ESPAsyncWebserver) in case you are not familiar with that library. This example is not a finished application but a minimal starting point to build your own application.

main.cpp

#include <Arduino.h>
#include <Arduino.h>
#include <WiFi.h>
#include <ESP32Servo.h>

#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h>

AsyncWebServer server(80);

/**
 * Wait for WiFi connection, and, if not connected, reboot
 */
void waitForWiFiConnectOrReboot(bool printOnSerial=true) {
  uint32_t notConnectedCounter = 0;
  while (WiFi.status() != WL_CONNECTED) {
      delay(100);
      if(printOnSerial) {
        Serial.println("Wifi connecting...");
      }
      notConnectedCounter++;
      if(notConnectedCounter > 50) { // Reset board if not connected after 5s
          if(printOnSerial) {
            Serial.println("Resetting due to Wifi not connecting...");
          }
          ESP.restart();
      }
  }
  if(printOnSerial) {
    // Print wifi IP addess
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
  }
}

Servo servo;

void setup() {
  ESP32PWM::allocateTimer(0);
  ESP32PWM::allocateTimer(1);
  ESP32PWM::allocateTimer(2);
  ESP32PWM::allocateTimer(3);

  Serial.begin(115200);

  WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
  WiFi.setHostname("ESP-Servo");

  WiFi.begin("MyWifiSSID", "MyWifiPassword");
  waitForWiFiConnectOrReboot();

  // Attach servo to pin 25
  servo.attach(25, 1000, 2000);
  
  // Configure HTTP routes
  server.on("/api/set-servo", HTTP_GET, [](AsyncWebServerRequest *request) {
      float value = request->arg("value").toFloat();
      servo.write(value);
      // Send {status: "ok"}
      AsyncResponseStream *response = request->beginResponseStream("application/json");
      DynamicJsonDocument json(1024);
      json["status"] = "ok";
      serializeJson(json, *response);
      request->send(response);
  });
  
  // Start webserver
  server.begin();
}

void loop() {
}

platformio.ini

[env:nodemcu-32s]
platform = espressif32
board = nodemcu-32s
framework = arduino
monitor_speed = 115200
lib_deps =
    ESP Async [email protected]
    [email protected]
    ESP32Servo

How to use

Insert your Wifi credentials in the line

WiFi.begin("MyWifiSSID", "MyWifiPassword");

and upload the firmware. Now open http://[ip address of the ESP32]/api/set-servo?value=0.0. Note that in the current version of the firmware, you can not use value=10 but you must use value=10.0.

Posted by Uli Köhler in ESP8266/ESP32, PlatformIO

ESP32 minimal WebSocket example (ESPAsyncWebserver / PlatformIO)

Minimal firmware to use WebSockets on the ESP32 using ESPAsyncWebserver:

main.cpp

#include <Arduino.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h>

AsyncWebServer server(80);
AsyncWebSocket ws("/ws");

AsyncWebSocketClient* wsClient;
 
void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){
  if(type == WS_EVT_CONNECT){
    wsClient = client;
  } else if(type == WS_EVT_DISCONNECT){
    wsClient = nullptr;
  }
}

/**
 * Wait for WiFi connection, and, if not connected, reboot
 */
void waitForWiFiConnectOrReboot(bool printOnSerial=true) {
  uint32_t notConnectedCounter = 0;
  while (WiFi.status() != WL_CONNECTED) {
      delay(100);
      if(printOnSerial) {
        Serial.println("Wifi connecting...");
      }
      notConnectedCounter++;
      if(notConnectedCounter > 50) { // Reset board if not connected after 5s
          if(printOnSerial) {
            Serial.println("Resetting due to Wifi not connecting...");
          }
          ESP.restart();
      }
  }
  if(printOnSerial) {
    // Print wifi IP addess
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
  }
}


void setup() { 
  Serial.begin(115200);
  WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
  WiFi.setHostname("ESP-Websocket-Test");

  WiFi.begin("MyWifiSSID", "MyWifiPassword");
  waitForWiFiConnectOrReboot();

  // Start webserver
  ws.onEvent(onWsEvent);
  server.addHandler(&ws);
  server.begin();
}

uint64_t counter = 0;
void loop() {
  // If client is connected ...
  if(wsClient != nullptr && wsClient->canSend()) {
    // .. send hello message :-)
    wsClient->text("Hello client");
  }

  // Wait 10 ms
  delay(10);
}

platformio.ini:

[env:nodemcu-32s]
platform = espressif32
board = nodemcu-32s
framework = arduino
monitor_speed = 115200
lib_deps =
    ESP Async [email protected]
    [email protected]

Python code for testing:

import websocket
ws = websocket.WebSocket()
ws.connect("ws://192.168.1.211/ws")
while True:
    result = ws.recv()
    print(result)

This will print Hello Client many times a second.

Posted by Uli Köhler in C/C++, ESP8266/ESP32, PlatformIO

How to fix ESP32 not connecting to the Wifi network

If you use a program like our minimal ESP32 wifi example:

#include <Arduino.h>
#include <WiFi.h>

void setup() {
  Serial.begin(115200);
  WiFi.begin("MyWifiNetworkName", "MyWifiPassword");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("Wifi connecting...");
    delay(500);
  }
  Serial.println("Wifi connected");
}
void loop() {
  // put your main code here, to run repeatedly:
}

and you just see a loop of

Wifi connecting...

messages, press the RESET button of your board (or unplug and re-plug its power to reset) and and try again. If you still see just Wifi connecting... messages after trying to reset 5 times, most likely your wifi credentials are wrong or the ESP32 can’t see your Wifi network!

Otherwise, if your ESP32 sometimes connects to the Wifi network almost immediately and sometimes it doesn’t seem to connect at all, use this code instead to automatically reset the ESP32 if it doesn’t connect within 5 seconds:

// Wait for wifi to be connected
uint32_t notConnectedCounter = 0;
while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.println("Wifi connecting...");
    notConnectedCounter++;
    if(notConnectedCounter > 150) { // Reset board if not connected after 15s
        Serial.println("Resetting due to Wifi not connecting...");
        ESP.restart();
    }
}

Full example:

#include <Arduino.h>
#include <WiFi.h>

void setup() {
  Serial.begin(115200);
  WiFi.begin("MyWifiSSID", "MyWifiPassword");

  // Wait for wifi to be connected
  uint32_t notConnectedCounter = 0;
  while (WiFi.status() != WL_CONNECTED) {
      delay(100);
      Serial.println("Wifi connecting...");
      notConnectedCounter++;
      if(notConnectedCounter > 150) { // Reset board if not connected after 5s
          Serial.println("Resetting due to Wifi not connecting...");
          ESP.restart();
      }
  }
  Serial.print("Wifi connected, IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  // put your main code here, to run repeatedly:
}

Alternatively, use this function:

/**
 * Wait for WiFi connection, and, if not connected, reboot
 */
void waitForWiFiConnectOrReboot(bool printOnSerial=true) {
  uint32_t notConnectedCounter = 0;
  while (WiFi.status() != WL_CONNECTED) {
      delay(100);
      if(printOnSerial) {
        Serial.println("Wifi connecting...");
      }
      notConnectedCounter++;
      if(notConnectedCounter > 150) { // Reset board if not connected after 5s
          if(printOnSerial) {
            Serial.println("Resetting due to Wifi not connecting...");
          }
          ESP.restart();
      }
  }
  if(printOnSerial) {
    // Print wifi IP addess
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
  }
}

 

Posted by Uli Köhler in Arduino, ESP8266/ESP32, PlatformIO

Simple way how to reboot ESP32 in Arduino or PlatformIO

Just use

ESP.restart();

This will immediately reboot the ESP32 controller

Posted by Uli Köhler in Arduino, ESP8266/ESP32, PlatformIO

ESP32 minimal WiFi client example

This example shows how to connect your ESP32 to an existing Wifi network using the Arduino Framework:

#include <Arduino.h>
#include <WiFi.h>

void setup() {
  Serial.begin(115200);
  WiFi.begin("MyWifiNetworkName", "MyWifiPassword");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("Wifi connecting...");
    delay(500);
  }
  Serial.println("Wifi connected");
}
void loop() {
  // put your main code here, to run repeatedly:
}

 

Posted by Uli Köhler in Arduino, ESP8266/ESP32, PlatformIO

ESPAsyncWebServer JSON response example for ArduinoJson 6

The ESPAsyncWebserver page features an example for generating a basic JSON response using ArduinoJson:

#include "AsyncJson.h"
#include "ArduinoJson.h"

AsyncResponseStream *response = request->beginResponseStream("application/json");
DynamicJsonBuffer jsonBuffer;
JsonObject &root = jsonBuffer.createObject();
root["heap"] = ESP.getFreeHeap();
root["ssid"] = WiFi.SSID();
root.printTo(*response);
request->send(response);

However, that example is made for ArduinoJson 5.x whereas most users want to use the updated ArduinoJson 6.x.

ArduinoJson 6.x minimal ESPAsyncWebserver example:

AsyncResponseStream *response = request->beginResponseStream("application/json");
DynamicJsonDocument json(1024);
json["status"] = "ok";
json["ssid"] = WiFi.SSID();
json["ip"] = WiFi.localIP();
serializeJson(json, *response);
request->send(response);

 

Posted by Uli Köhler in Embedded, ESP8266/ESP32, PlatformIO

How to fix ArduinoJson error: DynamicJsonBuffer is a class from ArduinoJson 5

When you see an error message like

Compiling .pio\build\d1_mini\src\main.cpp.o
src\main.cpp:22:11: error: DynamicJsonBuffer is a class from ArduinoJson 5. Please see arduinojson.org/upgrade to learn how to upgrade your program to ArduinoJson version 6
       DynamicJsonBuffer jsonBuffer;

in your PlatformIO or Arduino project using the ArduinoJson library, your code was written for an old version of ArduinoJson.

According to the official ArduinoJson 5 to ArduinoJson 6 migration guide, you need to use DynamicJsonDocument instead. Note that DynamicJsonDocument uses a slightly different API compared to DynamicJsonDocument, hence you might need to adjust more than just changing the class names. But as a first step, replace e.g.

DynamicJsonBuffer json;

by

DynamicJsonDocument json(1024);
Posted by Uli Köhler in Arduino, ESP8266/ESP32, PlatformIO

How to fix ArduinoJson error: ‘ArduinoJson::JsonObject’ has no member named ‘printTo’

Problem:

While trying to build your project using ArduinoJson, you see an error message like

src\main.cpp:26:12: error: 'ArduinoJson::JsonObject' has no member named 'printTo'
       root.printTo(*response);

Solution:

The code you’re using is for an older version of ArduinoJson: It was written for ArduinoJson version 5.x while you’re using ArduinoJson version 6.x. Use serializeJson() instead of  root.printTo(*response):

serializeJson(root, *response);

See the official ArduinoJson guide for Migrating from version 5 to 6 for further information on which calls you need to replace.

 

Posted by Uli Köhler in Arduino, Embedded, ESP8266/ESP32, PlatformIO

How to add GitHub repository to PlatformIO platformio.ini lib_deps

You can directly add a GitHub repository as a PlatformIO library dependency by specifying its repository URL in lib_deps in platformio.ini

lib_deps =
    https://github.com/Bodmer/U8g2_for_TFT_eSPI

Note that the GitHub repository must be a valid PlatformIO library i.e. it must have library.json or library.properties in its root directory. https://github.com/Bodmer/U8g2_for_TFT_eSPI is an example for a library that can be included into PlatformIO.

Posted by Uli Köhler in PlatformIO

How to fix PlatformIO ESP8266/ESP32 fatal error: SPI.h: No such file or directory

Problem:

You are trying to compile your PlatformIO application for the ESP8266 or ESP32 but you’re seeing an error message like

In file included from .pio/libdeps/d1_mini/TFT_eSPI/TFT_eSPI.cpp:17:0:
.pio/libdeps/d1_mini/TFT_eSPI/TFT_eSPI.h:32:17: fatal error: SPI.h: No such file or directory

*************************************************************
* Looking for SPI.h dependency? Check our library registry!
*
* CLI  > platformio lib search "header:SPI.h"
* Web  > https://platformio.org/lib/search?query=header:SPI.h
*
*************************************************************

 #include <SPI.h>

This problem is common using the TFT_eSPI library.

Solution:

First, ensure that your platformio.ini has

framework = arduino

If you’re using a different frameworkSPI.h won’t be available since it’s a part of the Arduino framework !

Secondly, add this line to your platformio.ini:

lib_ldf_mode = deep+

and recompile your source code. This will reconfigure the library dependency finder (ldf) to find dependencies of dependency libraries:

Dependency Graph
|-- <TFT_eSPI> 2.3.52
|   |-- <SPI> 1.0

 

Posted by Uli Köhler in ESP8266/ESP32, PlatformIO