First of all, this small series of blog posts, assume you have installed and are familiar with Platformio IDE to edit and upload code to your Espressif chips. We ‘ve covered in another post the installation and getting started part with this nice IoT editor. Please refer to this section of Platformio to get the basics right of the editor.
Prerequisites: Just grab any ESP32 or ESP8266 and make sure it works. Check that you can see the Serial monitor so we can debug the code.
The platformio.ini project configuration file
This is the configuration file where we can define what are the environment targets (chips) where we want to upload and run our program. For example, we can have the same code, to run in ESP8266 or ESP32 using the same source.
; File: platformio.ini [platformio] ; This is the default environment but we can also use command line ; or change here to espressif8266 to upload it to this target default_envs = lolin_d32 [env:lolin_d32] platform = https://github.com/platformio/platform-espressif32.git board = lolin_d32 framework = arduino monitor_speed = 115200 ; set frequency to 80/ 160MHz 160000000L board_build.f_cpu = 160000000L [env:espressif8266] platform = espressif8266 board = d1_mini_lite framework = arduino monitor_speed = 115200
So this will be our first program, will just include the Arduino framework, start a counter on 0. As every program on Arduino framework, will have a setup() that is executed only once when the chip is powered, and a loop() method that is precisely on a loop. It will just print what you see on setup and then a new line every second.
I also invite you to to be curious enough to use the CTRL+LEFT click over Arduino.h and to explore what it does. It includes the framework. Actually it includes the FreeRTOS that is a real-time OS for microcontrollers and also some other important definitions and includes for the Arduino ESP32 framework itself to work. And this is the way to discover any library or thing you include in your program, just CTRL+LEFT and explore the code, trying to understand what function it has.
So now let’s code our first little program:
// File: src/main.cpp #include "Arduino.h"; int counter = 0; void setup() { Serial.begin(115200); Serial.println("setup() started"); } void loop() { ++counter; Serial.printf("Counter: %d\n", counter); delay(1000); }
Very simple right? Not too many things. Just a variable in the global scope, that get’s incremented in the loop, and is getting printed using printf in a single line. So we created a very short and nice program that actually does nothing except printing some Serial output.
The variable scope is easy to grasp and is also the same for other languages as javascript. A global scope variable that is defined on top, can be incremented from each function. That means that if we would move the:
int counter = 0;
At the beginning of the loop() the loop itself would have no sense, since it will add one and go back to 0 all the time.
In the next chapter, we will learn how to use the environments we defined in platformio.ini, to add different codes keeping the same program to run in esp8266 and esp32. Just follow this blog or keep tuned to @martinfasani twitter account to read the next release.
NOTE: Up to here we could also have compiled and run the very same code in an Arduino board and it will do exactly the same. Since we are not using any of the Espressif extra goodies like WiFi, this could be run also in another boards, keep tuned to see what is coming next that will be using WiFi or Bluetooth.
Good one. I will surely try this. Thanks.