2019-06-20 10:17:44 +00:00
# include <TFT_eSPI.h>
# include <SPI.h>
# include "WiFi.h"
# include <Wire.h>
# include <Button2.h>
# include "esp_adc_cal.h"
2019-06-28 01:29:58 +00:00
# include "bmp.h"
2019-06-20 10:17:44 +00:00
2019-08-06 04:01:57 +00:00
# ifndef TFT_DISPOFF
# define TFT_DISPOFF 0x28
# endif
# ifndef TFT_SLPIN
# define TFT_SLPIN 0x10
# endif
2019-06-20 10:17:44 +00:00
# define TFT_MOSI 19
# define TFT_SCLK 18
# define TFT_CS 5
# define TFT_DC 16
# define TFT_RST 23
2020-04-14 02:52:30 +00:00
# define TFT_BL 4 // Display backlight control pin
# define ADC_EN 14 //ADC_EN is the ADC detection enable port
2019-06-28 01:29:58 +00:00
# define ADC_PIN 34
# define BUTTON_1 35
# define BUTTON_2 0
2019-06-20 10:17:44 +00:00
TFT_eSPI tft = TFT_eSPI ( 135 , 240 ) ; // Invoke custom library
2019-08-06 04:01:57 +00:00
Button2 btn1 ( BUTTON_1 ) ;
Button2 btn2 ( BUTTON_2 ) ;
2019-06-20 10:17:44 +00:00
char buff [ 512 ] ;
int vref = 1100 ;
int btnCick = false ;
2019-08-29 01:02:30 +00:00
//! Long time delay, it is recommended to use shallow sleep, which can effectively reduce the current consumption
void espDelay ( int ms )
2020-04-14 02:52:30 +00:00
{
2019-08-29 01:02:30 +00:00
esp_sleep_enable_timer_wakeup ( ms * 1000 ) ;
2020-04-14 02:52:30 +00:00
esp_sleep_pd_config ( ESP_PD_DOMAIN_RTC_PERIPH , ESP_PD_OPTION_ON ) ;
2019-08-29 01:02:30 +00:00
esp_light_sleep_start ( ) ;
}
2019-06-20 10:17:44 +00:00
void showVoltage ( )
{
static uint64_t timeStamp = 0 ;
if ( millis ( ) - timeStamp > 1000 ) {
timeStamp = millis ( ) ;
uint16_t v = analogRead ( ADC_PIN ) ;
float battery_voltage = ( ( float ) v / 4095.0 ) * 2.0 * 3.3 * ( vref / 1000.0 ) ;
String voltage = " Voltage : " + String ( battery_voltage ) + " V " ;
2019-06-28 01:29:58 +00:00
Serial . println ( voltage ) ;
2019-06-20 10:17:44 +00:00
tft . fillScreen ( TFT_BLACK ) ;
tft . setTextDatum ( MC_DATUM ) ;
tft . drawString ( voltage , tft . width ( ) / 2 , tft . height ( ) / 2 ) ;
}
}
void button_init ( )
{
2019-08-06 04:01:57 +00:00
btn1 . setLongClickHandler ( [ ] ( Button2 & b ) {
2019-06-20 10:17:44 +00:00
btnCick = false ;
int r = digitalRead ( TFT_BL ) ;
tft . fillScreen ( TFT_BLACK ) ;
tft . setTextColor ( TFT_GREEN , TFT_BLACK ) ;
tft . setTextDatum ( MC_DATUM ) ;
tft . drawString ( " Press again to wake up " , tft . width ( ) / 2 , tft . height ( ) / 2 ) ;
2019-08-29 01:02:30 +00:00
espDelay ( 6000 ) ;
2019-06-20 10:17:44 +00:00
digitalWrite ( TFT_BL , ! r ) ;
2019-08-06 04:01:57 +00:00
2019-06-28 01:29:58 +00:00
tft . writecommand ( TFT_DISPOFF ) ;
tft . writecommand ( TFT_SLPIN ) ;
2020-04-14 02:52:30 +00:00
//After using light sleep, you need to disable timer wake, because here use external IO port to wake up
esp_sleep_disable_wakeup_source ( ESP_SLEEP_WAKEUP_TIMER ) ;
// esp_sleep_enable_ext1_wakeup(GPIO_SEL_35, ESP_EXT1_WAKEUP_ALL_LOW);
esp_sleep_enable_ext0_wakeup ( GPIO_NUM_35 , 0 ) ;
delay ( 200 ) ;
2019-06-20 10:17:44 +00:00
esp_deep_sleep_start ( ) ;
} ) ;
2019-08-06 04:01:57 +00:00
btn1 . setPressedHandler ( [ ] ( Button2 & b ) {
2019-06-20 10:17:44 +00:00
Serial . println ( " Detect Voltage.. " ) ;
btnCick = true ;
} ) ;
2019-08-06 04:01:57 +00:00
btn2 . setPressedHandler ( [ ] ( Button2 & b ) {
2019-06-20 10:17:44 +00:00
btnCick = false ;
Serial . println ( " btn press wifi scan " ) ;
wifi_scan ( ) ;
} ) ;
}
void button_loop ( )
{
2019-08-06 04:01:57 +00:00
btn1 . loop ( ) ;
btn2 . loop ( ) ;
2019-06-20 10:17:44 +00:00
}
void wifi_scan ( )
{
tft . setTextColor ( TFT_GREEN , TFT_BLACK ) ;
tft . fillScreen ( TFT_BLACK ) ;
tft . setTextDatum ( MC_DATUM ) ;
tft . setTextSize ( 1 ) ;
tft . drawString ( " Scan Network " , tft . width ( ) / 2 , tft . height ( ) / 2 ) ;
WiFi . mode ( WIFI_STA ) ;
WiFi . disconnect ( ) ;
delay ( 100 ) ;
int16_t n = WiFi . scanNetworks ( ) ;
tft . fillScreen ( TFT_BLACK ) ;
if ( n = = 0 ) {
tft . drawString ( " no networks found " , tft . width ( ) / 2 , tft . height ( ) / 2 ) ;
} else {
tft . setTextDatum ( TL_DATUM ) ;
tft . setCursor ( 0 , 0 ) ;
2019-07-27 00:11:14 +00:00
Serial . printf ( " Found %d net \n " , n ) ;
2019-06-20 10:17:44 +00:00
for ( int i = 0 ; i < n ; + + i ) {
sprintf ( buff ,
" [%d]:%s(%d) " ,
i + 1 ,
WiFi . SSID ( i ) . c_str ( ) ,
WiFi . RSSI ( i ) ) ;
tft . println ( buff ) ;
}
}
2019-06-28 01:29:58 +00:00
WiFi . mode ( WIFI_OFF ) ;
2019-06-20 10:17:44 +00:00
}
void setup ( )
{
Serial . begin ( 115200 ) ;
2019-06-28 01:29:58 +00:00
Serial . println ( " Start " ) ;
2020-04-14 02:52:30 +00:00
/*
ADC_EN is the ADC detection enable port
If the USB port is used for power supply , it is turned on by default .
If it is powered by battery , it needs to be set to high level
*/
pinMode ( ADC_EN , OUTPUT ) ;
digitalWrite ( ADC_EN , HIGH ) ;
2019-06-20 10:17:44 +00:00
tft . init ( ) ;
2019-08-06 04:01:57 +00:00
tft . setRotation ( 1 ) ;
2019-06-20 10:17:44 +00:00
tft . fillScreen ( TFT_BLACK ) ;
tft . setTextSize ( 2 ) ;
2020-04-14 02:52:30 +00:00
tft . setTextColor ( TFT_GREEN ) ;
2019-06-20 10:17:44 +00:00
tft . setCursor ( 0 , 0 ) ;
tft . setTextDatum ( MC_DATUM ) ;
tft . setTextSize ( 1 ) ;
2019-09-10 23:35:08 +00:00
if ( TFT_BL > 0 ) { // TFT_BL has been set in the TFT_eSPI library in the User Setup file TTGO_T_Display.h
2020-04-14 02:52:30 +00:00
pinMode ( TFT_BL , OUTPUT ) ; // Set backlight pin to output mode
digitalWrite ( TFT_BL , TFT_BACKLIGHT_ON ) ; // Turn backlight on. TFT_BACKLIGHT_ON has been set in the TFT_eSPI library in the User Setup file TTGO_T_Display.h
2019-09-10 23:35:08 +00:00
}
2019-08-29 01:02:30 +00:00
2019-08-06 04:01:57 +00:00
tft . setSwapBytes ( true ) ;
2019-06-28 01:29:58 +00:00
tft . pushImage ( 0 , 0 , 240 , 135 , ttgo ) ;
2019-08-29 01:02:30 +00:00
espDelay ( 5000 ) ;
2019-08-06 04:01:57 +00:00
2019-06-28 01:29:58 +00:00
tft . setRotation ( 0 ) ;
2020-04-14 02:52:30 +00:00
tft . fillScreen ( TFT_RED ) ;
espDelay ( 1000 ) ;
tft . fillScreen ( TFT_BLUE ) ;
espDelay ( 1000 ) ;
tft . fillScreen ( TFT_GREEN ) ;
espDelay ( 1000 ) ;
2019-06-28 01:29:58 +00:00
2019-06-20 10:17:44 +00:00
button_init ( ) ;
esp_adc_cal_characteristics_t adc_chars ;
esp_adc_cal_value_t val_type = esp_adc_cal_characterize ( ( adc_unit_t ) ADC_UNIT_1 , ( adc_atten_t ) ADC1_CHANNEL_6 , ( adc_bits_width_t ) ADC_WIDTH_BIT_12 , 1100 , & adc_chars ) ;
//Check type of calibration value used to characterize ADC
if ( val_type = = ESP_ADC_CAL_VAL_EFUSE_VREF ) {
Serial . printf ( " eFuse Vref:%u mV " , adc_chars . vref ) ;
vref = adc_chars . vref ;
} else if ( val_type = = ESP_ADC_CAL_VAL_EFUSE_TP ) {
Serial . printf ( " Two Point --> coeff_a:%umV coeff_b:%umV \n " , adc_chars . coeff_a , adc_chars . coeff_b ) ;
} else {
Serial . println ( " Default Vref: 1100mV " ) ;
}
2020-04-14 02:52:30 +00:00
tft . fillScreen ( TFT_BLACK ) ;
tft . setTextDatum ( MC_DATUM ) ;
tft . drawString ( " LeftButton: " , tft . width ( ) / 2 , tft . height ( ) / 2 - 16 ) ;
tft . drawString ( " [WiFi Scan] " , tft . width ( ) / 2 , tft . height ( ) / 2 ) ;
tft . drawString ( " RightButton: " , tft . width ( ) / 2 , tft . height ( ) / 2 + 16 ) ;
tft . drawString ( " [Voltage Monitor] " , tft . width ( ) / 2 , tft . height ( ) / 2 + 32 ) ;
tft . drawString ( " RightButtonLongPress: " , tft . width ( ) / 2 , tft . height ( ) / 2 + 48 ) ;
tft . drawString ( " [Deep Sleep] " , tft . width ( ) / 2 , tft . height ( ) / 2 + 64 ) ;
tft . setTextDatum ( TL_DATUM ) ;
}
2019-06-20 10:17:44 +00:00
void loop ( )
{
if ( btnCick ) {
showVoltage ( ) ;
}
button_loop ( ) ;
2019-07-27 00:11:14 +00:00
}