Inserting the macro definition into the Arduino program

Zápisník experimentátora

To write this article I was inspired by the question on the Google+ forum. How to get a name and password (such as WiFi) into Arduino so you do not have to write it directly to the program. There is a danger that you can send your password, for example, to GitHub.

Let me show it on a practical example. Almost every example for WiFi starts with rows.

#include <WiFi101.h>

char ssid[] = "yourNetwork"; //  your network SSID (name)
char pass[] = "secretPassword";    // your network password (use for WPA, or use as key for WEP)

So you take an example, change your data, try the program, and because you want to have all the details and do the documentation, unwittingly send your creation to GitHub or post it on your blog or make a printscreen screen and publish the image somewhere. And in a few minutes you will realize that you have just published your private data on the Internet. And you have a problem and it's up to you to solve it. But it is not easy and no one wants to experience this feeling. So how to make it not happen in the future?

Extra parameters

In each compiler, it is possible to add parameter definitions that convert to macros in the program. The problem is only how to get these parameters into the Arduino IDE compiler. It's not possible to write macros right in the IDE, but the path is here. Arduino creators have added extra parameters to the Arduino Builder, and these extra parameters are very useful for resolving the problem.

The procedure is the same for any Arduino. You must always create the platform.local.txt file and write the required parameters in the syntax of the compiler. Fortunately, all C++ compilers are the same.

Arduino

For example, I have Arduino installed in the directory d:/arduino/arduino-1.8.2/. Therefore, the platform.txt file is located on d:/arduino/arduino-1.8.2/hardware/arduino/avr/platform.txt. I create a file d:/arduino/arduino-1.8.2/hardware/arduino/avr/platform.local.txt with this content.

compiler.cpp.extra_flags=-DEXTRA_PARAMETER="abc123"

After compiling arduino_extra_parameter.ino, two values are written to the serial port. If the file does not exist or I did not follow the prescribed procedure, you will see the error message EXTRA_PARAMETER not found !.

#if defined(EXTRA_PARAMETER)
  #define DEPENDENT_PARAMETER "xyz"
#else
  #error EXTRA_PARAMETER not found!
#endif

void setup() {
  Serial.begin(9600);
  Serial.println(EXTRA_PARAMETER);
  Serial.println(DEPENDENT_PARAMETER);
}

void loop() {
  // nothing
}

Arduino MKR1000

The Arduino MKR1000 can be connected to the Internet, so we can write an example with a name and password. The previous mkr1000_extra_parameter.ino will also work if you find the right directory. In my case, this is the c:\Users\Robo\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.6.15\ where you can copy the same file.

But we're focusing on transferring your WiFi name and password. We will need to set the contents of the platform.local.txt file to

compiler.cpp.extra_flags=-D_SSID="xxx" -D_PWD="xxx"

And, in your program, set your WiFi name and password as follows. The preview is truncated, the entire mkr1000_web_client.ino program can be found on GitHub.

#include <WiFi101.h>

#if defined(_SSID)
char ssid[] = _SSID; // your network SSID (name)
char pass[] = _PWD;  // your network password (use for WPA, or use as key for WEP)
#else
char ssid[] = "xxx"; // your network SSID (name)
char pass[] = "xxx"; // your network password (use for WPA, or use as key for WEP)
#endif

ESP8266

ESP8266 can be connected to the Internet so we can write an example with a name and password. The previous esp8266_extra_parameter.ino program will also work if you find the right directory. In my case this is the c:\Users\Robo\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\ where you can copy the same file.

But we're focusing on transferring your WiFi name and password. We will need to set the contents of the platform.local.txt file to

compiler.cpp.extra_flags=-D_SSID="xxx" -D_PWD="xxx"

And, in your program, set your WiFi name and password as follows. The preview is truncated, you can find the entire esp8266_web_client.ino program on GitHub.

#include <ESP8266WiFi.h>

#if defined(_SSID)
const char* ssid     = _SSID;
const char* password = _PWD;
#else
const char* ssid     = "your-ssid";
const char* password = "your-password";
#endif

Source code

You can find all the examples on GitHub.


21.06.2017


Menu