2019-01-04

HELLO (esp32) WORLD - a re-creation of the code running on my ESP32 Board when I got it

Wasted a number of hours this evening in re-creating the WiFi scanning code that was running on the board when it arrived (and which I immediately blew away with my first upload). Mostly this was a learning experience in the U8g2 driver but there's a bit of Arduino IDE learning involved too....

Here's it running:


(It's not identical to the original, in particular the font I've used is almost certainly different. And it doesn't display the logo on boot, mainly because I've got no idea where that logo is defined and I've got no inclination to find or create a new one).

Note that I'm explicitly not claiming ownership of the code, I've noted where I've heavily borrowed from. Mostly this is for my own later use if I have to build another dev environment, if I'm honest.

The original WiFi scanner is a built-in sample of the ESP32 board samples (File -> Examples -> (ESP32) WiFi -> WiFi Scan). This version started from the example written by Robot Zero One available here, but I modified it to use the page-buffer because I wanted to have control over font sizes. I'm still not 100% sure I'm Doing It Right because pushing stuff to the display seems to be over-complicated, but it does have the virtue of, you know, working.....



/*
 * Modified from example at https://robotzero.one/heltec-wifi-kit-32/
 * Graphics library documented at https://github.com/olikraus/u8g2/wiki
 * Representative (but not definitive) board pin-out found at: http://www.heltec.cn/download/WIFI_Kit_32-Diagram.pdf
 * (see also: https://github.com/Heltec-Aaron-Lee/WiFi_Kit_series/blob/master/PinoutDiagram/WIFI%20Kit%2032.pdf )
 * Representative (but not definitive) Schematic diagram found at: https://github.com/Heltec-Aaron-Lee/WiFi_Kit_series/blob/master/SchematicDiagram/WIFI_Kit_32_Schematic_diagram.PDF
 * 2019-01-04 MCE
 * Target Board: Heltec_WIFI_Kit_32 (with onboard OLED)
 * 
 * 1) Install Arduino IDE
 * 2) In Preferences, add https://dl.espressif.com/dl/package_esp32_index.json to "Additional Boards URL"
 * 3) Tools -> Board -> Board Manager. Search for "ESP", and install it
 * 4) Tools -> Manage Libraries. Search for "U8G2", and install it.
 * 5) If on OSX, install the driver from https://www.silabs.com/documents/public/software/Mac_OSX_VCP_Driver.zip
 *    (note: requires security permissions setting in Sys Prefs to load 3rd party KEXTS. Verify loaded with:
 *       "kextstat | grep -v com.apple" and in the output should be the extension:
 *                       com.silabs.driver.CP210xVCPDriver (5.0.7)
 * 6) Plug in the ESP32. Set Port to "/dev/cu.SLAB_USBtoUART". 
 *
 * NB: Uses U8g2 ver of library to get better font-size control.
 */


#include "WiFi.h"
#include <U8g2lib.h>

//#ifdef U8X8_HAVE_HW_SPI
//#include <SPI.h>
//#endif
//#ifdef U8X8_HAVE_HW_I2C
//#include <Wire.h>
//#endif

// the OLED used on the board maps to....
U8G2_SSD1306_128X64_NONAME_1_SW_I2C  u8g2(U8G2_R0, /* clock=*/ 15, /* data=*/ 4, /* reset=*/ 16);

// According to schematic, LED_BUILTIN should be on 25
// (Power light is constantly flashing... not sure why, no documentation)

//Helpers for specific font chosen setting cursor position:
//Font u8g2_font_haxrcorp4089_tr is 8 wide, 10 high
//Font u8g2_font_baby_tf is 10 x 10
const int FW = 10;
const int FH = 10;

void setup()
{
  // Set WiFi to station mode and disconnect from an AP if it was previously connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

  u8g2.begin();
  
  //Setup onboard LED for output:
  pinMode(LED_BUILTIN, OUTPUT);
}


static void doSomeWork()
{

  digitalWrite(LED_BUILTIN, HIGH); //LED ON
  u8g2.firstPage();
  do {
    u8g2.setFont(u8g2_font_baby_tf);  
    u8g2.drawStr(4 * FW, 4 * FH, "...Scanning...");
  } while (u8g2.nextPage() );

  int n = WiFi.scanNetworks();

  u8g2.clear();
  if (n == 0) {
    u8g2.firstPage();
    do {
      u8g2.setFont(u8g2_font_baby_tf);
      u8g2.drawStr(4 * FW, 4 * FH, "NO NETWORKS FOUND");
    } while(u8g2.nextPage());
   } else {
    u8g2.firstPage();
    do {
      u8g2.setFont(u8g2_font_baby_tf);
      u8g2.setCursor(FW,1*FH); 
      char outputLine[32];
      sprintf(outputLine,"%-2d  networks found:",n);
      u8g2.print(outputLine);
      //Top 5 results only
      n == (n < 5) ? n : 5;
      
      for (int i = 0; i < n; ++i) {
        // Print SSID for each network found
        // Ordered by strongest to weakest, as it comes out of the scanner
        sprintf(outputLine,"%1i:  (%-4i)  ",(i+1),WiFi.RSSI(i));
        u8g2.setCursor(0, (i + 2) * FH);  u8g2.print(outputLine); u8g2.print(WiFi.SSID(i));
       }
    } while(u8g2.nextPage());
    
  }

  // Wait a bit before scanning again
  digitalWrite(LED_BUILTIN, LOW); // LED OFF
  delay(15000);
}


void loop()
{
  doSomeWork();
}

No comments:

Post a Comment