In a previous blog post I introduced to you the D-duino ESP8266 boards. In this blog post, I’ll show you a small Arduino code which is providing the basic functionality for my next projects.
What’s in the box
I am using the awesome WifiManager library. I will handle the configuration and connection to a WiFi network. If  the ESP is unconfigured or the AP is not reachable, it will start an access point by its own. You can connect to this AP and a caption portal will open. Scan for networks, select one and enter the password. The configuration will be saved in the EEPROM. No need to set the credentials inside of your code.
Over-the-Air update is also implemented. This makes it very easy to update the code without connecting the MCU via USB to the host. It’s also faster than serial uploads.
Last part is of course the OLED display. I am using the ESP8266_SSD1306 library. It has some very nice features like progress bars, several graph commands (lines, shapes etc.), pictures and of text.
The Code
I am using platform.io for all my MCU projects. It has a full IDE based on the Atom editor and comes with things like code completion, a library/dependency manager and many other features like an integrated serial terminal.
Start by creating a new project. Select “Generic ESP8266 Module” as board.
After the project is created, copy the following contents to your platformio.ini. It’ll make sure that you can easily upload code via USB without using the flash button on the back (autoflash & reset). It will also configure the appropriate flash layout for the ESP-12F chip.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
; PlatformIO Project Configuration File ; ; Build options: build flags, source filter ; Upload options: custom upload port, speed and extra flags ; Library options: dependencies, extra library storages ; Advanced options: extra scripting ; ; Please visit documentation for the other options and examples ; http://docs.platformio.org/page/projectconf.html [env:esp-12F] platform = espressif8266 board = esp8285 framework = arduino upload_resetmethod = nodemcu build_flags = -Wl,-Tesp8266.flash.4m1m.ld lib_deps = ESP8266_SSD1306 WifiManager ;upload_port=192.168.1.119 |
Now the main course. Create a new file “main.ino” and copy the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
#include <Wire.h> #include <ESP8266WiFi.h> #include <ESP8266mDNS.h> #include <ArduinoOTA.h> #include <DNSServer.h> #include <ESP8266WebServer.h> #include <WiFiManager.h> #include "SH1106.h" #define D1 5 #define D2 4 SH1106 display(0x3c, D1, D2); // This is called if the WifiManager is in config mode (AP open) void configModeCallback (WiFiManager *myWiFiManager) { display.clear(); display.drawString(DISPLAY_WIDTH/2, 10, "Config Mode"); // display the AP name display.drawString(DISPLAY_WIDTH/2, 30, myWiFiManager->getConfigPortalSSID()); display.display(); } void setup() { Serial.begin(115200); Serial.println("Boot"); // Setup display display.init(); display.flipScreenVertically(); display.setFont(ArialMT_Plain_10); display.setTextAlignment(TEXT_ALIGN_CENTER_BOTH); // Clear display buffer display.clear(); // Write some text display.drawString(DISPLAY_WIDTH/2, 20, "Boot..."); display.drawString(DISPLAY_WIDTH/2, 32, "Connecting to WiFi"); // Send display buffer to display display.display(); // Setup WifiManager WiFiManager wifiManager; //wifiManager.resetSettings(); wifiManager.setAPCallback(configModeCallback); // The programm will stop here until the WiFi is configured wifiManager.autoConnect(); // Wifi connected display.clear(); display.drawString(DISPLAY_WIDTH/2, 20, "Setup done"); display.drawString(DISPLAY_WIDTH/2, 32, "IP:" + WiFi.localIP().toString()); display.display(); // Setup ArduinoOTA ArduinoOTA.begin(); ArduinoOTA.onStart([]() { display.clear(); display.setTextAlignment(TEXT_ALIGN_CENTER_BOTH); display.drawString(DISPLAY_WIDTH/2, DISPLAY_HEIGHT/2 - 10, "OTA Update"); display.display(); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { // Draw nice progress bar display.drawProgressBar(4, 32, 120, 8, progress / (total / 100) ); display.display(); }); ArduinoOTA.onEnd([]() { display.clear(); display.setTextAlignment(TEXT_ALIGN_CENTER_BOTH); display.drawString(DISPLAY_WIDTH/2, DISPLAY_HEIGHT/2, "Restart"); display.display(); }); } void loop() { // Keep OTA running ArduinoOTA.handle(); } |
Now connect your D-duino and build/upload the code. After the restart, the OLED display should come to life.
The WifiManager will now start an access point. The AP name is displayed. Connect to the AP and wait for the portal to come up. If the portal is not shown, open a web browser and go to 192.168.4.1. Select “Configure WiFi”, select your network and set the password. The D-duino will now try to connect.
You can now set the “upload_port” parameter in the platformio.ini to the shown IP to use OTA 🙂