Skip to content

Commit

Permalink
Try to restore the WiFi connection after it was interrupted by the ro…
Browse files Browse the repository at this point in the history
…uter
  • Loading branch information
schreibfaul1 committed Jul 25, 2024
1 parent 0849380 commit b012e09
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 13 deletions.
2 changes: 1 addition & 1 deletion dependencies.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ dependencies:
type: idf
version: 4.4.5
manifest_hash: d37ba0ed61a018e301d65ea10c44d61a6e83204655d26b1b0c1e51e5870e3d1a
target: esp32
target: esp32s3
version: 1.0.0
82 changes: 82 additions & 0 deletions lib/BH1750/BH1750.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "BH1750.h"

//————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
hp_BH1750::hp_BH1750(TwoWire *myWire){ // constructor
_wire = myWire;
m_sensitivity = SENSITIVITY_ADJ_DEFAULT;
m_measurementTime = 120;
m_resolution = 1.0;
}
hp_BH1750::~hp_BH1750(){ // destructor
_wire->end();
}
//————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
bool hp_BH1750::setSensitivity(uint8_t sensitivity){
if(sensitivity < SENSITIVITY_ADJ_MIN) return false;
if(sensitivity > SENSITIVITY_ADJ_MAX) return false;
m_sensitivity = sensitivity;
return writeMtreg(m_sensitivity); // Set standard sensitivity
}
//————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
bool hp_BH1750::setResolutionMode(uint8_t resolutionMode){
if(resolutionMode == ONE_TIME_H_RESOLUTION_MODE) { m_resolution = 1.0; m_measurementTime = 120; m_resolutionMode = resolutionMode; return true;}
if(resolutionMode == ONE_TIME_H_RESOLUTION_MODE2){ m_resolution = 0.5; m_measurementTime = 120; m_resolutionMode = resolutionMode; return true;}
if(resolutionMode == ONE_TIME_L_RESOLUTION_MODE) { m_resolution = 4.0; m_measurementTime = 16; m_resolutionMode = resolutionMode; return true;}
return false;
}
//————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
bool hp_BH1750::begin(uint8_t address, int8_t sda, int8_t scl) {
_address = address; // Store one of the two available addresses
_wire->begin(sda, scl, 400000); // Initialisation of wire object with standard SDA/SCL lines
return writeMtreg(BH1750_MTREG_DEFAULT); // Set standard sensitivity
}
//————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
bool hp_BH1750::powerOn(){
return writeByte(0x1);
}
//————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
// For low power consuming. This mode is entered automatically if a measurement is finished
bool hp_BH1750::powerOff() {
return writeByte(0x0);
}
//————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
bool hp_BH1750::reset() {
if(powerOn() == false) return false;
return writeByte(0x7);
}
//————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
bool hp_BH1750::writeByte(uint8_t b){ // Sends command to sensor
_wire->beginTransmission(_address);
_wire->write(b);
return (_wire->endTransmission() == 0);
}
//————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
int32_t hp_BH1750::readValue() {
uint8_t buff[2];
uint16_t req = _wire->requestFrom((int)_address, (int)2); // request two bytes
if(req < 2 || _wire->available() < 2) {
return -1; // Sensor not ready
}
buff[0] = _wire->read(); // Receive one byte
buff[1] = _wire->read(); // Receive one byte
return ((buff[0] << 8) | buff[1]);
}
//————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
bool hp_BH1750::writeMtreg(uint8_t mtreg) { // Change sensitivity measurement time
uint8_t hiByte = mtreg >> 5;
hiByte |= 0b01000000;// High bit: 01000_MT[7,6,5]
writeByte(hiByte);
uint8_t loByte = mtreg & 0b00011111;
loByte |= 0b01100000;// Low bit: 011_MT[4,3,2,1,0]
return writeByte(loByte);
}
//————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
bool hp_BH1750::start(){ // Start a single shot measurement with given resolution and sensitivity
reset(); // Reset the last result in data register to zero (0)
return writeByte(m_resolutionMode);
}
//————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
uint16_t hp_BH1750::getBrightness(){
return (float)readValue() / ((float)SENSITIVITY_ADJ_DEFAULT / m_sensitivity) * m_resolution;;
}
//————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
123 changes: 123 additions & 0 deletions lib/BH1750/BH1750.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// light sensor BH1750
#include <Arduino.h>
#include <Wire.h>

#pragma once

static const unsigned int BH1750_SATURATED = 65535;
enum BH1750Quality {
BH1750_QUALITY_HIGH = 0x20,
BH1750_QUALITY_HIGH2 = 0x21,
BH1750_QUALITY_LOW = 0x23,
};
enum BH1750CalResult {
BH1750_CAL_OK = 0,
BH1750_CAL_MTREG_CHANGED = 1,
BH1750_CAL_TOO_BRIGHT = 2,
BH1750_CAL_TOO_DARK = 3,
BH1750_CAL_COMMUNICATION_ERROR = 4,
};
struct BH1750Timing {
uint8_t mtregLow;
uint8_t mtregHigh;
unsigned int mtregLow_qualityHigh;
unsigned int mtregHigh_qualityHigh;
unsigned int mtregLow_qualityLow;
unsigned int mtregHigh_qualityLow;
};

enum BH1750MtregLimit {
BH1750_MTREG_LOW = 31, // the datashet specifies 31 as minimum value
// but you can go even lower (depending on your specific chip)
// BH1750_MTREG_LOW=5 works with my chip and enhances the range
// from 121.556,8 Lux to 753.652,5 Lux.
BH1750_MTREG_HIGH = 254,
BH1750_MTREG_DEFAULT = 69
};

enum BH1750Address { BH1750_TO_GROUND = 0x23, BH1750_TO_VCC = 0x5C };
extern TwoWire Wire; /**< Forward declaration of Wire object */

class hp_BH1750 {

public:
hp_BH1750(TwoWire* myWire);
~hp_BH1750();

public:
const uint8_t ONE_TIME_H_RESOLUTION_MODE = 0b00100000; // Start measurement at 1lx resolution. Measurement Time is typically 120ms. Power Down mode after measurement.
const uint8_t ONE_TIME_H_RESOLUTION_MODE2 = 0b00100001; // Start measurement at 0.5lx resolution. Measurement Time is typically 120ms. Power Down mode after measurement.
const uint8_t ONE_TIME_L_RESOLUTION_MODE = 0b00100011; // Start measurement at 4lx resolution. Measurement Time is typically 16ms. Power Down mode after measurement.

const uint8_t ADDR_TO_GROUND = 0x23;
const uint8_t ADDR_TO_VCC = 0x5C;

const uint8_t SENSITIVITY_ADJ_MIN = 31; // Adjust measurement result for influence of optical window. (sensor sensitivity adjusting)
const uint8_t SENSITIVITY_ADJ_DEFAULT = 69;
const uint8_t SENSITIVITY_ADJ_MAX = 254;




bool setSensitivity(uint8_t sensitivity);
bool setResolutionMode(uint8_t resolution);
bool begin(uint8_t address, int8_t sda, int8_t scl);
bool writeByte(uint8_t b);
int32_t readValue();
bool reset();
bool powerOn();
bool powerOff();
bool writeMtreg(uint8_t mtreg);



void setQuality(BH1750Quality quality);
bool start();
float getLux();
uint16_t getBrightness();


unsigned int getMtregTime() const;
// unsigned int getMtregTime(byte mtreg) const;
unsigned int getMtregTime(uint8_t mtreg, BH1750Quality quality) const;



private:
TwoWire* _wire;
uint8_t _address;
uint8_t _mtreg;
uint8_t _percent = 50;
bool _processed = false;
unsigned int _mtregTime;
unsigned long _startMillis;
unsigned long _resultMillis;
unsigned long _timeoutMillis;
unsigned long _timeout = 10;
int _offset = 0;
unsigned int _nReads;
unsigned int _time;
unsigned int _value;
float _qualFak = 0.5;
float luxCache;
BH1750Quality _quality;
BH1750Timing _timing;

uint8_t checkMtreg(uint8_t mtreg);


// unsigned int readValue();
unsigned int readChange(uint8_t mtreg, BH1750Quality quality, bool change);

uint8_t m_sensitivity = SENSITIVITY_ADJ_DEFAULT;
float m_resolution = 1.0;
uint8_t m_resolutionMode = ONE_TIME_H_RESOLUTION_MODE;
uint8_t m_measurementTime = 120;

const uint8_t m_Power_Down = 0b00000000; // No active state.
const uint8_t m_Power_On = 0b00000001; // Waiting for measurement command.
const uint8_t m_Reset = 0b00000111; // Reset Data register value. Reset command is not acceptable in Power Down mode.



};
6 changes: 3 additions & 3 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ platform_packages = framework-arduinoespressif32 @ https://github.com/espressif/
framework = arduino, espidf


;platform = https://github.com/platformio/platform-espressif32.git
;platform = https://github.com/Jason2866/platform-espressif32.git#Arduino/IDF5
;platform_packages =
; platformio/framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#master
; platformio/framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#3.0.2
; platformio/framework-arduinoespressif32-libs @ https://github.com/espressif/arduino-esp32/releases/download/3.0.2/esp32-arduino-libs-3.0.2.zip
;framework = arduino, espidf
;framework = arduino

build_flags = ${common.build_flags}
monitor_speed = ${common.monitor_speed}
Expand Down
4 changes: 2 additions & 2 deletions src/common.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// created: 10.Feb.2022
// updated: 28.Jun 2024
// updated: 25.Jul 2024

#pragma once
#pragma GCC optimize("Os") // optimize for code size
Expand All @@ -21,7 +21,7 @@
#define SDMMC_FREQUENCY 80000000 // 80000000 or 40000000 MHz
#define FTP_USERNAME "esp32" // user and pw in FTP Client
#define FTP_PASSWORD "esp32"
#define CONN_TIMEOUT 1000 // unencrypted connection timeout in ms (http://...)
#define CONN_TIMEOUT 1500 // unencrypted connection timeout in ms (http://...)
#define CONN_TIMEOUT_SSL 2500 // encrypted connection timeout in ms (https://...)

//————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
Expand Down
38 changes: 31 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
MiniWebRadio -- Webradio receiver for ESP32

first release on 03/2017 */String Version ="\
Version 3.2h Jun 28/2024 ";
Version 3.2i Jul 25/2024 ";

/* 2.8" color display (320x240px) with controller ILI9341 or HX8347D (SPI) or
3.5" color display (480x320px) with controller ILI9486 or ILI9488 (SPI)
Expand Down Expand Up @@ -78,6 +78,7 @@ uint8_t _alarmMode = 1; // 0 with radio only, 1 with bell and r
uint8_t _staListPos = 0;
uint8_t _semaphore = 0;
uint8_t _reconnectCnt = 0;
uint8_t _WiFi_disconnectCnt = 0;
uint16_t _staListNr = 0;
uint8_t _fileListPos = 0;
uint8_t _radioSubmenue = 0;
Expand Down Expand Up @@ -2712,6 +2713,22 @@ void loop() {
if(_f_dlna_browseReady){ // unused
_f_dlna_browseReady = false;
}
//-------------------------------------------WIFI DISCONNECTED?-------------------------------------------------------------------------------
if((WiFi.status() != WL_CONNECTED)) {
_WiFi_disconnectCnt ++;
if(_WiFi_disconnectCnt == 15){
_WiFi_disconnectCnt = 1;
SerialPrintfln("WiFi : " ANSI_ESC_YELLOW "Reconnecting to WiFi...");
WiFi.disconnect();
WiFi.reconnect();
}
}
else{
if(_WiFi_disconnectCnt){
_WiFi_disconnectCnt = 0;
if(_state == RADIO) audioConnecttohost(_lastconnectedhost.c_str());
}
}
//------------------------------------------GET AUDIO FILE ITEMS------------------------------------------------------------------------------
if(_f_isFSConnected) {
// uint32_t t = 0;
Expand Down Expand Up @@ -2791,12 +2808,19 @@ void loop() {
void audio_info(const char* info) {
if(startsWith(info, "Request")) { SerialPrintflnCut("AUDIO_info: ", ANSI_ESC_RED, info);
if(endsWith(info, "failed!")){
audioStopSong();
WiFi.disconnect();
log_w("disconnected, wait 35s");
vTaskDelay(35000 / portTICK_PERIOD_MS);
log_w("try reconnection");
_f_reconnect = true;
if(WiFi.status() != WL_CONNECTED){
SerialPrintfln("WiFi : " ANSI_ESC_YELLOW "Reconnecting to WiFi...");
_WiFi_disconnectCnt = 1;
WiFi.disconnect();
WiFi.reconnect();
}
else{
WiFi.disconnect();
log_w("disconnected, wait 35s");
vTaskDelay(35000 / portTICK_PERIOD_MS);
log_w("try reconnection");
_f_reconnect = true;
}
}return;}
if(startsWith(info, "FLAC")) {SerialPrintflnCut("AUDIO_info: ", ANSI_ESC_GREEN, info); return;}
if(endsWith( info, "Stream lost")) {SerialPrintflnCut("AUDIO_info: ", ANSI_ESC_RED, info); return;}
Expand Down

0 comments on commit b012e09

Please sign in to comment.