PlatformIO Community

PlatformIO on VS Code does not find Libraries

Hi!
I just wanted to make the switch to PlatformIO for Arduino. But I always get the same error: it does not find the includes.
I do have all of them in two different dropbox folders which I registered with c_cpp_properties.json:

“/Users/ak/Dropbox/Learn_ProgramEx/Arduino/Elegoo Uno R3/* *”, “/Users/ak/Dropbox/Learn_ProgramEx/Arduino/libraries/**”

But I always get the error that e.g. these libraries are not found with PlatformIO (cs. with the Arduino IDE):
#include <Arduino.h>
#include <LiquidCrystal.h>
#include <SD.h>

And when I try to install them via PIO it even won’t let me enter anything in the search area…

Any ideas?

Thanks!

1 Like

It sounds like you might be having a few different problems.

which I registered with c_cpp_properties.json

… completely ignoring the warning on line 4? Any changes you make will be lost when you do a build.

image

If you need to modify the include path, use the build_flags for that.

Since you’re trying to include directories with existing collections of libraries, maybe have a look at the lib_extra_dirs setting?

Since you have a new install, it’s possible any IntelliSense warnings/sqiggles about core libraries like Arduino.h are because they’re not installed yet, which will happen automatically when you do your first build, if you’ve set the platform/framework/board settings correctly in your platformio.ini (creating a new project does that for you automatically).

Regarding entering text into the search area, it’s a known bug in VSCode:

Thank you very much.

Yes, I completely overread the warning :blush:

But I found a different way to get my libraries ti platform:
I plainly copies them from the existing libraries. then I did a platform update/upgrade command and voila - (almost) all libraries are there.

Unfortunately, almost: though I even installed LiquidCystal library before the copy-paste platform/VS does not recognize it and refuses to compile.
Same goes for Arduino.h.

Btw this my platform.ini file:

[env:uno]
platform = atmelavr
board = uno
framework = arduino

If you have any further suggestions - I would be happy to try them (or get inspired by them) :wink:

So it actually doesn’t find “Arduino.h” when you try to compile… as before you compile isn’t such a problem*… If it fails to find that include file when compiling then there’s something really wrong here, as that’s provided by the arduino framework, and should be working.

Can you copy and paste the output from an attempted compile (use triple backticks - ``` [left of the 1 key on the number row] - before and after to trigger a code block) so we can see the dependency tree and compile errors. Maybe also a minimal code example that fails (or a link to one of it’s a stock example).

*I personally consider that to be more an annoying cosmetic error that doesn’t stop you from actually building your code when it happens

You are right. Something’s really wrong.
Out of curiosity I checked the builtin libs with this command: platformio lib builtin
And then I added to the top of includes the SPI library. And same here: it can’t find it :frowning:
Heres the compile output:

[Running] cd “/Users/ak/Dropbox/Learn_ProgramEx/Arduino/WaterLevel2LCD2SD/src/” && g++ main.cpp -o main && "/Users/ak/Dropbox/Learn_ProgramEx/Arduino/WaterLevel2LCD2SD/src/"main
main.cpp:1:10: fatal error: ‘SPI.h’ file not found
#include <SPI.h>
^~~~~~~
1 error generated.
[Done] exited with code=1 in 0.091 seconds

So I checked the folders libdeps and within it the folder uno and the build folder: all are empty!
Then I checked the permission and changed them to rwx for all group, did a new lib install - nothing changed. :frowning:

Heres the part of the code I want to compile (which btw doesn’t work correctly neither - the LCD shows cryptic signs instead of the water level. That’s the second problem…

Btw, I tried to use the triple backticks - ``` in the code but just got another “problem”: unexpected token.

#include <SPI.h>
#include <Arduino.h>
#include <LiquidCrystal.h>
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"

const int chipSelect = 10; //10 is default by shield, but normally on Pin 4
int interval = 20;  //Log to SD Card every 5 seconds

long timer;
String timestring;
String mvalue;

RTC_DS1307 rtc;

int adc_id = 0;
int HistoryValue = 0;
char printBuffer[128];
//void write_LCD(int x, int y, bool clearIt, String text);

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  delay(1000);
  lcd.begin(16, 2);
  Serial.println("Initializing SD card...");
  if (!SD.begin(chipSelect)) {
    Serial.println("SD Card error");
    return;
  }
  write_LCD(0,0, true, "card initialized");
  Serial.println("card initialized");
  delay(1000);
  if (! rtc.begin()) {
    Serial.println("No RTC found");
    write_LCD(0,0, true, "No RTC found");
    
  } else {
    Serial.println("RTC clock found");
    write_LCD(0,0, true, "RTC found");
  }
  if (! rtc.isrunning()) {
    Serial.println("RTC is not configured");
    write_LCD(0,0, true, "RTC not config");
  }
  // set up the LCD's number of columns and rows:
  delay(1000);
  // Print a message to the LCD.
  lcd.print("Hello, World!");
  Serial.println("Hello World");
}

void loop() {
  // put your main code here, to run repeatedly:
     int value = analogRead(adc_id); // get adc value
    if(((HistoryValue>=value) && ((HistoryValue - value) > 4)) || ((HistoryValue<value) && ((value - HistoryValue) > 10)))
    {
      sprintf(printBuffer,"ADC%d level is %d\n",adc_id, value);
      Serial.print(printBuffer);
      mvalue = String(value);
      write_LCD(0,0,true, "Feuchte");
      write_LCD(0,1,false, String(value));
      HistoryValue = value;
      get_time();
      write_data();
    }
}
 void write_LCD(int x, int y, bool clearIt, String text) {
  if (clearIt) {
    lcd.clear();
  }
  lcd.setCursor(x,y);
  lcd.print(text);
}

void get_time(){ //Read Time from RTC
  DateTime now = rtc.now();
  timestring = now.day();
  timestring += "-";
  timestring += now.month();
  timestring += "-";
  timestring += now.year();
  timestring += " ";
  timestring += now.hour();
  timestring += ":";
  timestring += now.minute();
  timestring += ":";
  timestring += now.second();
  Serial.println(timestring);
}

void write_data() { //Write to SD card
  String dataString = mvalue + "," + timestring;
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    Serial.println(dataString);
  }
  else {
    Serial.println("error writing datalog.txt");
  }
}

Can you unpack this, add the folder to your workspace (File -> Add folder to workspace in VSCode) and see if that works. It’s your source code, with a different platformio.ini and the three external libraries you’re using included in the fake_external_lib_dir. The include list in the main.cpp is in a different order, but that is just cosmetic… I just grouped the builtin libraries and then external ones to make sense of what was what. :wink:

Note: You’ll need to update the lib_extra_dirs parameter in the platformio.ini as the path won’t be correct for you! :wink:

https://transfer.sh/ZxDUq/waterLevelSensor.zip

If that doesn’t work (it compiles fine for me), we’ll need to look at your platformio install, and see what’s going on.

No, I am sorry. It seems that platformio doesn’t find any library at all: if I comment out include after include it is always the next include that gives me an error.
Some even more disturbing happens: I always (at several times) get asked to install the platformio library (platformio lib install).
Perhaps I should do a complete deinstall of VS and all plugins?

Thanks you for your help!

That sounds more like the error message you get when you try to include a library that isn’t installed, and it’s suggesting how to install it.

You shouldn’t need to remove VSCode, just platformio so that the VSCode PlatformIO extension can re-install it. Navigate to your home directory (~ on linux, %USERPROFILE% on windows, not sure about Mac), and delete the .platformio folder. When you start VSCode up again, the extension will detect that PlatformIO is missing and reinstall it.