This simple sketch
- Flashes the onboard red LED
Lights and changes the onboard Neopixel, which I love
Reads the BME280 temperature and pressure sensor and prints out readings
Reads the LC709203F battery monitor and prints out voltage and percent charged. Terrific feature, the LC709203F.
Connects to Wifi using WiFiMulti and prints out connection info in the void loop
Again, hope this is useful to get started with this very cool ESP32-S2 Feather with BME280 and LC709203F. I wish I'd found something like this all in one place.
Code: Select all
#define SKETCH_NAME "ESP32-S2 DEMO"
#define SERIAL_SPEED 230400
// True causes builtin LED to flash, prints out assigned pin
#define USE_LED_BUILTIN true
//#define USE_LED_BUILTIN false
//True causes on-board single Neopixel to change colors
#define USE_ONBOARD_NEOPIXEL true
//#define USE_ONBOARD_NEOPIXEL false
#if USE_ONBOARD_NEOPIXEL
#include <Adafruit_NeoPixel.h>
// https://github.com/adafruit/Adafruit-Feather-ESP32-S2-PCB/blob/main/Adafruit%20Feather%20ESP32-S2%20Pinout.pdf
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 1 // Single Builtin NeoPixel on Adafruit's ESP32-S2
Adafruit_NeoPixel onBoardPixel ( NUMPIXELS, PIN_NEOPIXEL, NEO_RGBW );
#endif // Onboard Neopixel
// True causes the BME280 to be read and printed out. Not all ESP32 Feathers have this.
#define USE_BME280_SENSORS true
//#define USE_BME280_SENSORS false
#if USE_BME280_SENSORS
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10
#define SEALEVELPRESSURE_HPA (1013.25)
#define I2C_POWER_PIN 7
Adafruit_BME280 bme; // I2C
bool foundBME280 = false; // set to false until device is initialized
#endif // BME280
// True causes the integrated LC709203F to be read and printed out.
// "Standard" Adafruit ESP32 Feather does not have this unit
#define USE_ADAFRUIT_LC709203F_BATTERY_MONITOR true
//#define USE_ADAFRUIT_LC709203F_BATTERY_MONITOR false
#if USE_ADAFRUIT_LC709203F_BATTERY_MONITOR
// https://learn.adafruit.com/adafruit-lc709203f-lipo-lipoly-battery-monitor/arduino-use
#include "Adafruit_LC709203F.h" // Library for battery level meter chip on ESP32-S2 w BME280 Feather variant
// Battery level at 0x0B
#define USE_LC709203F_WITH_THERMISTOR true
//#define USE_LC709203F_WITH_THERMISTOR false
// https://learn.adafruit.com/adafruit-lc709203f-lipo-lipoly-battery-monitor/pinouts
// You want a 10K thermistor installed to read battery temerature. See doc.
Adafruit_LC709203F batteryTester;
bool foundBatteryTester = false; // set to false until initialized
#endif // LC709203F
// "Standard" Adafruit ESP32 Feather battery voltage is read from Pin A13 (set as INPUT). int value 0-4095
// True attempts to connect to WiFi only. Must set up one or more WiFi SSIDs and Passwords below
#define USE_WIFI true
//#define USE_WIFI false
#if USE_WIFI
// Using MultiFi which can cycle through multiple Wifi SSIDs to connect
// Or modify the sketch to use only 1 or two SSIDs
#define MY_WIFI_SSID1 "My favorite SSID"
#define MY_WIFI_PASSWORD1 "WiFi password to my favorite SSID"
#define MY_WIFI_SSID2 "My next favorite SSID"
#define MY_WIFI_PASSWORD2 "WiFi password to my next favorite SSID"
#define MY_WIFI_SSID3 "My third favorite SSID"
#define MY_WIFI_PASSWORD3 "WiFi password to my third favorite SSID"
#include <WiFiMulti.h>
WiFiMulti wifiMulti; // set up the WiFi object
#define WIFI_CONNECT_TIMEOUT_MS 3500 // timing for WiFiMulti testing connection in ms
#else
#include <WiFi.h> // For reading MAC address only
#endif
void setup()
{
Serial.begin ( SERIAL_SPEED );
delay ( 3500 );
Serial.println ( "\n\n=======================================" );
Serial.println ( SKETCH_NAME );
Serial.print ( "\nESP Board MAC Address: " );
Serial.println ( WiFi.macAddress() ); // Handy, but requires WiFi.h #included
Serial.println();
// Blink the onboard LED 10 times
Serial.print ( "\nLED_BUILTIN defined automatically as pin " );
Serial.println ( LED_BUILTIN );
Serial.println();
pinMode ( LED_BUILTIN, OUTPUT );
for ( int count = 0; count < 10; count++ )
{
digitalWrite ( LED_BUILTIN, HIGH );
delay ( 100 );
digitalWrite ( LED_BUILTIN, LOW );
delay ( 100 );
}
#if USE_ONBOARD_NEOPIXEL
// Initialize the single on-board Neopixel
pinMode( NEOPIXEL_POWER, OUTPUT ); // sets the Neopixel power pin to HIGH
digitalWrite ( NEOPIXEL_POWER, HIGH );
onBoardPixel.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
onBoardPixel.show(); // Turn OFF all pixels ASAP
onBoardPixel.setBrightness ( 150 ); // Set BRIGHTNESS to about 1/5 (max = 255)
Serial.println ( "\nInitialized NeoPixel" );
Serial.print ( " PIN_NEOPIXEL defined automatically as pin " );
Serial.println ( PIN_NEOPIXEL );
Serial.print ( " NEOPIXEL_POWER defined automatically as pin " );
Serial.println ( NEOPIXEL_POWER );
Serial.println( "\n" );
#endif // USE_ONBOARD_NEOPIXEL
#if USE_BME280_SENSORS
// Initialize the BME280 and I2C
pinMode ( I2C_POWER_PIN, OUTPUT );
digitalWrite ( I2C_POWER_PIN, LOW );
unsigned status = bme.begin ( 0x77 );
if ( ! status )
{
Serial.println ( "Could not find a valid BME280 sensor, check wiring, address, sensor ID!" );
Serial.print ( "SensorID was: 0x" ); Serial.println ( bme.sensorID(),16 );
Serial.println ( " ID of 0xFF probably means a bad address, a BMP 180 or BMP 085" );
Serial.println ( " ID of 0x56-0x58 represents a BMP 280," );
Serial.println ( " ID of 0x60 represents a BME 280." );
Serial.println ( " ID of 0x61 represents a BME 680." );
foundBME280 = false;
}
else // good status returned
{
Serial.print ( "Found BME280 with ID 0x" ); Serial.println ( bme.sensorID(),16 );
Serial.print ( " Returned Status = " ); Serial.println ( status );
Serial.println ();
foundBME280 = true;
}
#endif // USE_BME280_SENSORS
#if USE_ADAFRUIT_LC709203F_BATTERY_MONITOR
// Initialize the integrated LC709203F battery tester on the ESP32-S2 Feather
if ( ! batteryTester.begin() )
{
Serial.println ( "\nCouldnt find Adafruit LC709203F?\nMake sure a battery is plugged in!" );
foundBatteryTester = false;
}
else
{
Serial.println ( "\nFound Integrated LC709203F Battery Tester" );
foundBatteryTester = true;
Serial.print ( " Version: 0x" ); Serial.println ( batteryTester.getICversion(), HEX );
batteryTester.setThermistorB ( 3950 );
Serial.print ( " Thermistor B = " ); Serial.println ( batteryTester.getThermistorB() );
batteryTester.setPackSize ( LC709203F_APA_3000MAH );
/* LC709203F_APA_100MAH,
LC709203F_APA_200MAH,
LC709203F_APA_500MAH, // choose the closest one to your battery
LC709203F_APA_1000MAH,
LC709203F_APA_2000MAH,
LC709203F_APA_3000MAH,
*/
batteryTester.setAlarmVoltage ( 3.8 );
}
Serial.println();
#endif // USE_ADAFRUIT_LC709203F_BATTERY_MONITOR
#if USE_WIFI
WiFi.persistent ( false ); // Don't save anything in EEPROM or LittleFS
WiFi.mode ( WIFI_STA ); // Station mode
// Register multi WiFi networks
wifiMulti.addAP ( MY_WIFI_SSID1, MY_WIFI_PASSWORD1 );
wifiMulti.addAP ( MY_WIFI_SSID2, MY_WIFI_PASSWORD2 ); // Can delete multiple SSIDs here
wifiMulti.addAP ( MY_WIFI_SSID3, MY_WIFI_PASSWORD3 ); // Can delete multiple SSIDs here
if ( wifiMulti.run ( WIFI_CONNECT_TIMEOUT_MS ) == WL_CONNECTED )
{
Serial.print ( "\nWiFi connected just fine to ");
Serial.print ( WiFi.SSID() ); Serial.print ( " at " ); Serial.println ( WiFi.localIP() );
}
else
{
Serial.println ( "\nWiFi not connected yet." );
}
#endif // USE_WIFI
Serial.println ( "\n\nSetup done.\n\n" );
} // end setup
// Every void loop
// - change the Neopixel color randomly
// - report from the BME280 temperature and barometer (see temperature note in the doc)
// - report from the LC709203F battery monitor (only when selected and present)
// - report on WiFi connection
void loop()
{
Serial.println ( "\n\n" );
Serial.print ( SKETCH_NAME );
Serial.println ( " void loop" );
// Blink the onboard LED 3 times
Serial.println ( "Blinking the onboard LED 3 times\n" );
for ( int count = 0; count < 3; count++ )
{
digitalWrite ( LED_BUILTIN, HIGH );
delay ( 100 );
digitalWrite ( LED_BUILTIN, LOW );
delay ( 100 );
}
#if USE_ONBOARD_NEOPIXEL
// onBoardPixel.Color() change pixel ZERO color ( RGB from 0,0,0 up to 255,255,255 ) every loop
// Blink the onboard LED 3 times
Serial.println ( "New color for the onboard Neopixel\n" );
onBoardPixel.setPixelColor ( 0, // set the on-board Neopixel to a random, gamma-corrected color
onBoardPixel.gamma32 ( // always use gamma correction - looks better
onBoardPixel.Color ( random ( 0, 255 ), random ( 0, 255 ), random ( 0, 255 ) ) ) ); // random RGB color
onBoardPixel.show(); // Send the updated pixel colors to the hardware.
#endif // USE_ONBOARD_NEOPIXEL
#if USE_BME280_SENSORS
if ( foundBME280 ) printBME280values();
#endif
#if USE_ADAFRUIT_LC709203F_BATTERY_MONITOR
if ( foundBatteryTester ) printBatteryStats();
#endif
#if USE_WIFI
Serial.println ( "Wifi Reporting" );
// Note tha wifiMulti may take a while to connect - will show up in void loop
int wifiStatus = WiFi.status();
if ( wifiStatus == WL_CONNECTED )
{
Serial.print ( " connected as "); Serial.print ( WiFi.localIP() );
Serial.print ( " to " ); Serial.print ( WiFi.SSID() );
Serial.print ( " at " ); Serial.print ( WiFi.RSSI() ); Serial.print ( "dBm" );
Serial.println ();
}
else // not connected to WiFi
{
Serial.print ( " WiFi not connected. WiFi.status() = " );
Serial.println ( wifiStatus );
}
#endif // USE_WIFI
delay ( 7500 );
} // end void loop
#if USE_BME280_SENSORS
// Print out tbe BME280 temperature and barometer
void printBME280values()
{
Serial.println ( "BME280 reporting" );
Serial.print ( " Temperature = " );
float temperature = bme.readTemperature();
Serial.print ( temperature );
Serial.print ( " °C or " );
Serial.print ( temperature * 9.0/5.0 + 32.0 );
Serial.println ( " °F" );
Serial.print ( " Pressure = " );
float pressure = bme.readPressure() / 100.0;
Serial.print ( pressure );
Serial.print ( " hPa or " );
Serial.print ( pressure * 0.030 );
Serial.println ( " inHg" );
Serial.print ( " Approx. Altitude = " );
Serial.print ( bme.readAltitude ( SEALEVELPRESSURE_HPA ) );
Serial.println ( " m" );
Serial.print ( " Humidity = " );
Serial.print ( bme.readHumidity() );
Serial.println ( " %" );
Serial.println();
}
#endif // USE_BME280_SENSORS
#if USE_ADAFRUIT_LC709203F_BATTERY_MONITOR
void printBatteryStats()
{
Serial.println ( "Adafruit LC709203F Battery Monitor reporting" );
Serial.print ( " Battery Voltage: " );
Serial.print ( batteryTester.cellVoltage(), 3 );
Serial.println ( " VDC" );
Serial.print ( " Batt Percent: " );
Serial.print ( batteryTester.cellPercent(), 1 );
Serial.println ( " %" );
#if USE_LC709203F_WITH_THERMISTOR
Serial.print ( " Battery Temp: " );
Serial.print ( batteryTester.getCellTemperature(), 1 );
Serial.println ( " deg C" ); // may need converstion
#endif
Serial.println();
}
#endif