Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ESP32 and core libraries #50

Merged
merged 10 commits into from
Feb 3, 2024
21 changes: 12 additions & 9 deletions EleksTubeHAX/Backlights.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "Backlights.h"
#include <math.h>

void Backlights::begin(StoredConfig::Config::Backlights *config_) {
config=config_;
Expand All @@ -21,17 +20,17 @@ void Backlights::begin(StoredConfig::Config::Backlights *config_) {


// These feel like they should be generalizable into a helper function.
// https://stackoverflow.com/questions/11720656/modulo-operation-with-negative-numbers
void Backlights::setNextPattern(int8_t i) {
int8_t next_pattern = (config->pattern + i) % num_patterns;
// https://stackoverflow.com/questions/11720656/modulo-operation-with-negative-numbers
while (next_pattern < 0) {
next_pattern += num_patterns;
}
setPattern(patterns(next_pattern));
}

void Backlights::adjustColorPhase(int16_t adj) {
int16_t new_phase = (int16_t(config->color_phase%max_phase) + adj) % max_phase;
int16_t new_phase = (int16_t(config->color_phase % max_phase) + adj) % max_phase;
while (new_phase < 0) {
new_phase += max_phase;
}
Expand All @@ -53,6 +52,9 @@ void Backlights::setIntensity(uint8_t intensity) {
}

void Backlights::loop() {
#ifdef DEBUG_OUTPUT_EXTENDED
Serial.println("In Backlights::loop()");
#endif
// enum patterns { dark, test, constant, rainbow, pulse, breath, num_patterns };
if (off || config->pattern == dark) {
if (pattern_needs_init) {
Expand All @@ -65,14 +67,15 @@ void Backlights::loop() {
}
else if (config->pattern == constant) {
if (pattern_needs_init) {
if (dimming) {
setBrightness(0xFF >> max_intensity - (BACKLIGHT_DIMMED_INTENSITY) - 1);
} else {
setBrightness(0xFF >> max_intensity - config->intensity - 1);
}
fill(phaseToColor(config->color_phase));
show();
}
if (dimming) {
setBrightness(0xFF >> max_intensity - BACKLIGHT_DIMMED_INTENSITY - 1);
} else {
setBrightness(0xFF >> max_intensity - config->intensity - 1);
}

show();
}
else if (config->pattern == rainbow) {
rainbowPattern();
Expand Down
1 change: 1 addition & 0 deletions EleksTubeHAX/Backlights.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* Otherwise, class Backlights behaves exactly as Adafruit_NeoPixel does.
*/
#include <stdint.h>
#include <math.h>
#include "StoredConfig.h"
#include <Adafruit_NeoPixel.h>

Expand Down
2 changes: 1 addition & 1 deletion EleksTubeHAX/Clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#else
// For the DS3231 RTC
#include <DS1307RTC.h>
uint32_t RtcBegin() {}
void RtcBegin() {}
uint32_t RtcGet() {
return RTC.get();
}
Expand Down
26 changes: 25 additions & 1 deletion EleksTubeHAX/EleksTubeHAX.ino
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,14 @@ void setup() {
void loop() {
uint32_t millis_at_top = millis();
// Do all the maintenance work
#ifdef DEBUG_OUTPUT_EXTENDED
Serial.println("WifiReconnect()");
#endif
WifiReconnect(); // if not connected attempt to reconnect

#ifdef DEBUG_OUTPUT_EXTENDED
Serial.println("Mqtt stuff");
#endif
MqttStatusPower = tfts.isEnabled();
MqttStatusState = (uclock.getActiveGraphicIdx()+1) * 5; // 10
MqttLoop();
Expand Down Expand Up @@ -145,20 +151,38 @@ void loop() {
*/
}

#ifdef DEBUG_OUTPUT_EXTENDED
Serial.println("buttons.loop()");
#endif
buttons.loop();

// Power button: If in menu, exit menu. Else turn off displays and backlight.
if (buttons.power.isDownEdge() && (menu.getState() == Menu::idle)) {
tfts.toggleAllDisplays();
backlights.togglePower();
}


#ifdef DEBUG_OUTPUT_EXTENDED
Serial.println("menu.loop()");
#endif
menu.loop(buttons); // Must be called after buttons.loop()
#ifdef DEBUG_OUTPUT_EXTENDED
Serial.println("backlights.loop()");
#endif
backlights.loop();
#ifdef DEBUG_OUTPUT_EXTENDED
Serial.println("uclock.loop()");
#endif
uclock.loop();

#ifdef DEBUG_OUTPUT_EXTENDED
Serial.println("EveryFullHour()");
#endif
EveryFullHour(); // night or daytime

#ifdef DEBUG_OUTPUT_EXTENDED
Serial.println("updateClockDisplay()");
#endif
// Update the clock.
updateClockDisplay();

Expand Down
51 changes: 27 additions & 24 deletions EleksTubeHAX/USER_DEFINES.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,60 @@
* User defines are located in "USER_DEFINES.h"
*/


#ifndef USER_DEFINES_H_
#define USER_DEFINES_H_


//#define DEBUG_OUTPUT

//#define DEBUG_OUTPUT_EXTENDED

// ************* Type of clock *************
//#define HARDWARE_Elekstube_CLOCK // uncomment for the original Elekstube clock
#define HARDWARE_Elekstube_CLOCK // uncomment for the original Elekstube clock
//#define HARDWARE_SI_HAI_CLOCK // uncomment for the SI HAI copy of the clock
#define HARDWARE_NovelLife_SE_CLOCK // uncomment for the NovelLife SE version; non-SE not tested
//#define HARDWARE_NovelLife_SE_CLOCK // uncomment for the NovelLife SE version; non-SE not tested


// ************* Version Infomation *************
#define DEVICE_NAME "IPS-clock"
#define FIRMWARE_VERSION "aly-fly IPS clock v0.3"
#define FIRMWARE_VERSION "IPS clock v0.4"
#define USE_CLK_FILES // select between .CLK and .BMP images
#define ESP_MANUFACTURER "ESPRESSIF"
#define ESP_MODEL_NUMBER "ESP32"
#define ESP_MODEL_NAME "IPS clock"

// ************* Display Dimming *************
#define NIGHT_TIME 22 // dim displays at 10 pm
#define DAY_TIME 7 // full brightness after 7 am
#define BACKLIGHT_DIMMED_INTENSITY 1 // 0..7
#define TFT_DIMMED_INTENSITY 20 // 0..255
#define NIGHT_TIME 22 // dim displays at 10 pm
#define DAY_TIME 7 // full brightness after 7 am
#define BACKLIGHT_DIMMED_INTENSITY 0 // 0..7
#define TFT_DIMMED_INTENSITY 48 // 0..255

// ************* WiFi config *************
#define WIFI_CONNECT_TIMEOUT_SEC 20
#define WIFI_RETRY_CONNECTION_SEC 15
#define ESP_WPS_MODE WPS_TYPE_PBC // push-button; uncomment to use WPS
#define WIFI_USE_WPS //uncomment to use WPS
//#define SECRET_WIFI_SSID //not needed for WPS
//#define SECRET_WIFI_PASSWD //not needed for WPS. Caution - Hard coded password stored clear text in BIN file
// WPS Mode (use EITHER this or SSID and Password)
#define ESP_WPS_MODE WPS_TYPE_PBC
#define WIFI_USE_WPS
// SSID and Password Mode
// CAUTION - Hard coded password stored clear text in BIN file
//#define SECRET_WIFI_SSID "YOUR-SSID"
//#define SECRET_WIFI_PASSWD "yourPassword"


// ************* Geolocation *************
// Get your API Key on https://www.abstractapi.com/ (login) --> https://app.abstractapi.com/api/ip-geolocation/tester (key) *************
//#define GEOLOCATION_API_KEY "key"
// Get your API Key on https://www.abstractapi.com/ (login)
// --> https://app.abstractapi.com/api/ip-geolocation/tester (key)
#define GEOLOCATION_API_KEY "keykeykey"


// ************* MQTT config *************
//#define MQTT_ENABLED // enable after creating an account, setting up the device on smartnest.cz and pilling in all the data below:
//#define MQTT_BROKER "smartnest.cz"
//#define MQTT_CLIENT "deviceid"
//#define MQTT_USERNAME "Username"
//#define MQTT_PASSWORD "MQTT_PASSWORD"
#define MQTT_PORT 1883 // Broker port
#define MQTT_RECONNECT_WAIT_SEC 30 // how long to wait between retries to connect to broker
#define MQTT_REPORT_STATUS_EVERY_SEC 71 // How often report status to MQTT Broker
// Easy mode: Setup an account at smartnest.cz and make a new thermostat device
//#define MQTT_ENABLED
//#define MQTT_BROKER "smartnest.cz"
//#define MQTT_CLIENT "clientid"
//#define MQTT_USERNAME "username"
//#define MQTT_PASSWORD "apikey"
#define MQTT_PORT 1883 // Broker port
#define MQTT_RECONNECT_WAIT_SEC 33 // how long to wait between retries to connect to broker
#define MQTT_REPORT_STATUS_EVERY_SEC 317 // How often to report status to MQTT Broker


#endif
103 changes: 21 additions & 82 deletions EleksTubeHAX/WiFi_WPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ double GeoLocTZoffset = 0;


#ifdef WIFI_USE_WPS //// WPS code

//https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/examples/WPS/WPS.ino
static esp_wps_config_t wps_config;
void wpsInitConfig(){
wps_config.crypto_funcs = &g_wifi_default_wps_crypto_funcs;
//wps_config.crypto_funcs = &g_wifi_default_wps_crypto_funcs;
wps_config.wps_type = ESP_WPS_MODE;
strcpy(wps_config.factory_info.manufacturer, ESP_MANUFACTURER);
strcpy(wps_config.factory_info.model_number, ESP_MODEL_NUMBER);
Expand All @@ -30,103 +30,42 @@ void wpsInitConfig(){
}
#endif

void WiFiEvent(WiFiEvent_t event, system_event_info_t info){
void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info){
switch(event){
case SYSTEM_EVENT_STA_START:
case ARDUINO_EVENT_WIFI_STA_START:
WifiState = disconnected;
Serial.println("Station Mode Started");
break;
case SYSTEM_EVENT_STA_CONNECTED:
// WifiState = connected; // IP not yet assigned
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
Serial.println("Connected to AP: " + String(WiFi.SSID()));

/* https://github.com/espressif/arduino-esp32/blob/04963009eedfbc1e0ea2e1378ae69e7cebda6fd6/tools/sdk/include/esp32/esp_event_legacy.h#L80
typedef struct {
uint8_t ssid[32]; // < SSID of connected AP
uint8_t ssid_len; // < SSID length of connected AP
uint8_t bssid[6]; // < BSSID of connected AP
uint8_t channel; // < channel of connected AP
wifi_auth_mode_t authmode;
} system_event_sta_connected_t;
*/

/*
// Serial.println(String(info.connected.ssid));
// Serial.println(String(info.connected.bssid));
// Serial.println(String(info.connected.channel));

memset(dest, '\0', sizeof(dest));
strcpy(src, "This is tutorialspoint.com");
strcpy(dest, src);
*/
break;
case SYSTEM_EVENT_STA_GOT_IP:
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
Serial.print("Got IP: ");
// IPAddress ip = IPAddress(WiFi.localIP());
// Serial.println(ip);
Serial.println(WiFi.localIP());

/*
if (ip[0] == 0) {
WifiState = disconnected; // invalid IP
} else { */
WifiState = connected;
// }
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
WifiState = disconnected;
Serial.print("WiFi lost connection. Reason: ");
Serial.println(info.disconnected.reason);
Serial.println(info.wifi_sta_disconnected.reason);
WifiReconnect();
break;
case SYSTEM_EVENT_STA_WPS_ER_SUCCESS:
WifiState = wps_success;
/*
* https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/network/esp_wifi.html#_CPPv431wifi_event_sta_wps_er_success_t
structwifi_event_sta_wps_er_success_t
Argument structure for WIFI_EVENT_STA_WPS_ER_SUCCESS event

Public Members

uint8_t ap_cred_cnt
Number of AP credentials received

uint8_t ssid[MAX_SSID_LEN]
SSID of AP

uint8_t passphrase[MAX_PASSPHRASE_LEN]
Passphrase for the AP

structwifi_event_sta_wps_er_success_t::[anonymous]ap_cred[MAX_WPS_AP_CRED]
All AP credentials received from WPS handshake


sprintf(stored_config.config.wifi.ssid, info.ssid);
sprintf(stored_config.config.wifi.password, info.passphrase);



memcpy(someBuffer, evt->ap_cred[0].ssid, sizeof(*evt->ap_cred[0].ssid));
That is, assuming cred[0].ssid is defined as an array... you may be better off using strncpy() if it's not.
*/


#ifdef WIFI_USE_WPS //// WPS code
case ARDUINO_EVENT_WPS_ER_SUCCESS:
WifiState = wps_success;
Serial.println("WPS Successful, stopping WPS and connecting to: " + String(WiFi.SSID()));
esp_wifi_wps_disable();
delay(10);
// https://stackoverflow.com/questions/48024780/esp32-wps-reconnect-on-power-on
delay(10);
WiFi.begin();
break;

#ifdef WIFI_USE_WPS //// WPS code
case SYSTEM_EVENT_STA_WPS_ER_FAILED:
break;
case ARDUINO_EVENT_WPS_ER_FAILED:
WifiState = wps_failed;
Serial.println("WPS Failed, retrying");
esp_wifi_wps_disable();
esp_wifi_wps_enable(&wps_config);
esp_wifi_wps_start(0);
break;
case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT:
case ARDUINO_EVENT_WPS_ER_TIMEOUT:
// WifiState = wps_failed;
Serial.println("WPS Timeout, retrying");
tfts.setTextColor(TFT_RED, TFT_BLACK);
Expand All @@ -137,9 +76,7 @@ That is, assuming cred[0].ssid is defined as an array... you may be better off u
esp_wifi_wps_start(0);
WifiState = wps_active;
break;
#endif
default:
break;
#endif
}
}

Expand All @@ -149,7 +86,6 @@ void WifiBegin() {
WifiState = disconnected;

WiFi.mode(WIFI_STA);
WiFi.onEvent(WiFiEvent);
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
WiFi.setHostname(DEVICE_NAME);

Expand Down Expand Up @@ -187,6 +123,7 @@ void WifiBegin() {
}
#else ////NO WPS -- Hard coded credentials

WiFi.onEvent(WiFiEvent);
WiFi.begin(SECRET_WIFI_SSID, SECRET_WIFI_PASSWD);
unsigned long StartTime = millis();
while ((WiFi.status() != WL_CONNECTED)) {
Expand Down Expand Up @@ -224,11 +161,11 @@ void WifiReconnect() {
Serial.println("Attempting WiFi reconnection...");
WiFi.reconnect();
TimeOfWifiReconnectAttempt = millis();
}
}
}

#ifdef WIFI_USE_WPS //// WPS code
bool WiFiStartWps() {
void WiFiStartWps() {
// erase settings
sprintf(stored_config.config.wifi.ssid, "");
sprintf(stored_config.config.wifi.password, "");
Expand All @@ -242,6 +179,8 @@ bool WiFiStartWps() {
tfts.setTextColor(TFT_RED, TFT_BLACK);
tfts.println("PRESS WPS BUTTON ON THE ROUTER");

//disconnect from wifi first if we were connected
WiFi.disconnect(true, true);

WifiState = wps_active;
WiFi.onEvent(WiFiEvent);
Expand Down
Loading