3

Just starting with this coming from a JS background.

I am looking into IoT development and wanted to set up my own repo without uploading the SSID and password of my personal WiFi.

Platform IO offers this platformio.ini as I understand to set build_flags.

  build_flags = 
     -D SSID="MySSID"

I don't know how to access them from my CPP file though. I want to inject the value from the build flag SSID into my *.cpp file.

  #define SSID

  void loop()
  {
    Serial.println(SSID);
  }

The above doesn't write anything to the serial monitor.

What am I doing wrong? The code does compile.

5
  • Isn't it a .cpp file? You wrote .ccp file. Commented Sep 30, 2018 at 18:21
  • Yes, it is. I wrote the correct one in the title though. Commented Sep 30, 2018 at 18:27
  • From the documentation and this example, the -D directives in build_flags are translated in the code. So, you just need to define those in platformio.ini and then you can use them directly in your code. You don't need to #define them in the code separately. Commented Sep 30, 2018 at 19:01
  • Yeah, that is what I understood too. However then Visual Code throws a compile error: `unable to find numeric literal operator 'operator"" FritzBox' Commented Sep 30, 2018 at 19:25
  • Right. I believe then it is some kind of configuration problem on part of VS and arduino compiler. You might want to write a different question focused on that problem. Commented Oct 1, 2018 at 4:51

2 Answers 2

3

I know it's been two years, and you've probably moved on from this question. However, for anyone else - like myself - who happens across this from a Google search, here is the answer as I've found it:

According to PlatformIO's Dynamic Variables page, you correctly define the build variable so that it exists to C++ (as a macro); i.e., -DSSID="MySSID". What you missed, however, is that the values need to be quoted (and potentially escaped): -DSSID='"MySSID"' so that when you access the macro in C++ it is a const char * string and not an unknown symbol.

See the Warning at the bottom of the page, and note the quotes around the string:

Be careful with special characters in system environment variables on Unix systems, especially when they are used as the value for preprocessor directives. Symbols like $, &, ~, etc must be explicitly escaped, for example:

export WIFI_PASS='\"my\~p\&a\\\$\$\$\$word\"'

It wasn't initially obvious to me either, but it makes sense because the preprocessor will replace SSID with whatever you've defined and makes no assumptions about it or its type at all.

Sign up to request clarification or add additional context in comments.

1 Comment

I stumbled across this, thank you. In looking at the PlatformIO documentation it looks like you actually need to encase the entire build flag in single quotes and only escape any double quotes within the value. So '-DSSID="MySSID"'
2

As @Azeem mentioned, you're redefining the SSID to an empty value. Using preprocessor like this, you must first check if the value exist and if not, assign it a default value.

Here is a simple C++ example:

#include <iostream>

#ifndef SSID
#define SSID "(SSID not defined)"
#endif

int main() 
{
    std::cout << "SSID value: " << SSID << std::endl;
    return 0;

}

You can compile and run the code with:

g++ main.cpp -o main && ./main

As you see it prints (SSID not defined).

Now, compiling and running with the following:

g++ main.cpp -o main -DSSID='"Hello, World!"' && ./main

will output: SSID value: Hello, World!

If you want to learn more about preprocessor directives, cplusplus.com has very nice tutorial

Also, don't forget to start your Serial in void setup().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.