Introduction: Introducing PlatformIO for ESP8266

About: Just me!

The ESP8266 revolutionized the IoT development, specially with the Arduino IDE port!

But although the Arduino IDE is simple to use, it is not the best ti use. PlatformIO is one ofthe available options if you are looking for a good IDE.

I started to play with the ESP8266 using the Arduino IDE then I decided to use something different for my home automation project.

Here is step-by-step to get it working under Windows.

Step 1: Download & Install PlatformIO IDE

  1. Just go there or just download the IDE for Windows.
  2. Install the IDE.
  3. When asked click 'Reload Now'.
  4. Open any ESP8266 project Menu -> PlatformIO -> Project Example (WiFiScan.ino will do)
  5. Click "Enable" for Smart code Linter.
  6. When asked to install Clang (for Intelligent Code Autocompletion - a nice-to-have!) go ahead & install LLVM:
    1. Go down to "Pre-Built Binaries:"
    2. Select 32 or 64 version, depending on your Windows installation.
    3. Select “Add LLVM to the system PATH” option on the installation step.
    4. You may need to correct the 'Path' (not ('PATH'!!) environment variable for Clang to work properly with PlatformIO.
  7. Restart PlatformIO to finish the installation.

Step 2: Compiling & Uploading an Example Project

  1. Connect your ESP8266 board.
  2. Make sure that your board's COM port by going to Menu -> PlatformIO -> List Serial Ports.
  3. Press the 'Arrow' button on the right corner & you should have WiFiScan.ino compiled & uploaded to the board.
  4. Open the Serial port by going to Menu -> PlatformIO -> Serial Monitor & select the right COM port & 115200 for the Baudrate.
  5. You should get a list of the nearby Access Points with their signal strengths.

Step 3: Writing Our First Program (Blink of Course!)

  1. Create a new project by going to Menu -> PlatformIO -> Initialize or Update PlatformIO Project
  2. Right-click "src" & Add new file "Blink.ino"
  3. Add the regular blink code:
#include <Arduino.h>

void setup(/* arguments */) {
	Serial.begin(115200);
pinMode(BUILTIN_LED, OUTPUT); } int i=HIGH; void loop(){
Serial.println(i?"Hi!":"Lo!");
digitalWrite(BUILTIN_LED, i);
}

Click the "Arrow" key on the left side & the built-in led should be blinking.

(You may also have the Serial Monitor on)

Step 4: Using Libraries

We need to use the Command Line Library Manager, Menu -> PlatformIO -> Library Manager

pio lib search DHT

10 Libraries will be found & we'll install Adafruit DHT Unified which is #[18] in the list.

So we'll install it by:

pio lib -g install 18

The Library Manager will install all the dependencies needed to the global (-g) library storage folder so that it may be used by any project.

Step 5: Writing a Simple Program Using the DHT Library

We'll use the DHT library we just installed.

Normally I split my projects in multiple files for better organization of the project to make things easy during the development and easy reuse of repetitive functions.

I'll split this project into two files:

  • Main part (with setup & loop).
  • DHT routines.
  1. Create a new project call it DHTtest: Menu -> PlatformIO -> Initialize or Update PlatformIO Project
  2. In the src folder add a new file name it: main.ino with the below code:


    #include <Arduino.h>

    void setup(){
    Serial.begin(115200);
    DHTstart();
    } void loop(){
    Serial.print("Temperature = "); Serial.print(ReadT());Serial.println(" C");
    Serial.print("Humidity = "); Serial.print(ReadH());Serial.println(" %");
    }
  3. In the src folder add another file name it: DHTfunctions.ino with the below code:

    #include <DHT.h>

    #define DHTPIN D3
    #define DHTTYPE DHT21 // I'm using DHT21

    DHT dht(DHTPIN, DHTTYPE);

    void DHTstart(){
    dht.begin(); }

    float ReadT(){
    float t = dht.readTemperature();
    return t;
    }

    float ReadH(){
    float h = dht.readHumidity();
    return h;
    }
  4. Compile & upload the project &open the Serial Monitor to check the result.