Skip to content

Commit

Permalink
Merge pull request #16 from SmallPond/dev/mos/hass_feature
Browse files Browse the repository at this point in the history
hass wifi setup optimization
  • Loading branch information
SmallPond authored Nov 11, 2023
2 parents 0ca2961 + 59abbef commit 9205e21
Show file tree
Hide file tree
Showing 5 changed files with 262 additions and 344 deletions.
15 changes: 5 additions & 10 deletions 1.Firmware/src/app/Pages/HASS/Hass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ void hass_hal_init(void)
int hass_hal_send(const char *device_name, int knob_value)
{
char topic_name[128];
String password,host,username,topic;
uint16_t port;
String password, host, username, topic;

get_mqtt_config(host,port,username,password,topic);
const char * mqtt_topic = topic.c_str();
snprintf(topic_name, sizeof(topic_name),"%s/HOME/%s", mqtt_topic, device_name);
Expand All @@ -43,7 +44,6 @@ Hass::Hass()

Hass::~Hass()
{

}

void Hass::onCustomAttrConfig()
Expand All @@ -57,8 +57,6 @@ void Hass::onViewLoad()
Model = new HassModel();
View = new HassView();

hass_hal_init();

Model->Init();
View->Create(root);

Expand Down Expand Up @@ -87,6 +85,7 @@ void Hass::onViewWillAppear()
Model->SetPlaygroundMode(app_mode);
View->SetPlaygroundMode(app_mode);

hass_hal_init();
timer = lv_timer_create(onTimerUpdate, 10, this);
}

Expand Down Expand Up @@ -119,11 +118,9 @@ void Hass::Update()
{
HassInfo info;
Model->GetKnobStatus(&info);
if (info.konb_direction != SUPER_DIAL_NULL)
{
if (info.konb_direction != SUPER_DIAL_NULL) {
char* name = ((HassView*)View)->GetEditedDeviceName();
if (name != NULL)
{
if (name != NULL) {
hass_hal_send(name, info.konb_direction);
}
}
Expand Down Expand Up @@ -163,7 +160,6 @@ void Hass::HassEventHandler(lv_event_t* event, lv_event_code_t code)
hass_hal_send(lv_label_get_text(label), HASS_PUSH);
}
} else if (code == LV_EVENT_LONG_PRESSED) {
printf("Hass: LV_EVENT_LONG_PRESSED\n");
if (lv_obj_has_state(obj, LV_STATE_EDITED)) {
((HassView*)View)->ClearCtrView(obj);
lv_obj_clear_state(obj, LV_STATE_EDITED);
Expand All @@ -173,7 +169,6 @@ void Hass::HassEventHandler(lv_event_t* event, lv_event_code_t code)
} else if (code == LV_EVENT_LONG_PRESSED_REPEAT) {
// return to memu
if (!lv_obj_has_state(obj, LV_STATE_EDITED)){
printf("Hass: LV_EVENT_LONG_PRESSED_REPEAT\n");
Model->ChangeMotorMode(MOTOR_UNBOUND_COARSE_DETENTS);
Manager->Pop();
}
Expand Down
1 change: 0 additions & 1 deletion 1.Firmware/src/app/Pages/HASS/HassModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ void HassModel::GetKnobStatus(HassInfo* info)
void HassModel::Init()
{
PlaygroundModel::Init();
printf("HassModel: Init start\n");
}

void HassModel::Deinit()
Expand Down
9 changes: 1 addition & 8 deletions 1.Firmware/src/app/Pages/HASS/HassView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ using namespace Page;
void HassView::SetPlaygroundMode(int16_t mode)
{
lv_obj_add_flag(ui.label_value, LV_OBJ_FLAG_HIDDEN);
// lv_label_set_text(ui.label_value, "Smart Home");
lv_meter_set_scale_ticks(ui.meter, ui.scale_pot, 73, 2, 0, lv_color_make(0xff, 0x00, 0x00));
lv_meter_set_scale_range(ui.meter, ui.scale_pot, 0, 72, 360, 270);
}
Expand Down Expand Up @@ -59,7 +58,6 @@ char* HassView::GetEditedDeviceName(void)
void HassView::ClearCtrView(lv_obj_t *obj)
{
device_t *device = device_map[obj];
printf("on_off: %d, is_set_value: %d\n",device->is_on_off, device->is_set_value);

if (device->is_set_value) {
/*
Expand Down Expand Up @@ -123,15 +121,13 @@ void HassView::device_item_create(
{

lv_obj_t *cont = lv_obj_create(par);
// lv_obj_remove_style_all(cont);
// lv_obj_set_size(cont, 42, 62);
lv_obj_clear_flag(cont, LV_OBJ_FLAG_SCROLLABLE);

lv_obj_set_flex_flow(cont, LV_FLEX_FLOW_COLUMN);

lv_obj_t *img = lv_img_create(cont);
lv_img_set_src(img, Resource.GetImage(img_src));
// lv_obj_set_size(img, 42, 42);

lv_obj_set_style_img_recolor_opa(img, LV_OPA_COVER, 0);
lv_obj_set_style_img_recolor(img, lv_color_white(), 0);

Expand All @@ -156,8 +152,6 @@ void HassView::device_item_create(
item->is_on_off = is_on_off;
item->is_set_value = is_set_value;
device_map[cont] = item;
// lv_obj_add_event_cb(item->cont, on_event, LV_EVENT_ALL, NULL);
printf("create an item: %p\n", item);
}

void on_focus(lv_group_t* g)
Expand All @@ -179,7 +173,6 @@ void HassView::group_init(void)
lv_group_add_obj(m_ui->group, (m_ui->devices[i]).cont);
}

// lv_group_focus_obj(m_ui->devices[0].cont);
}

void HassView::style_init(void)
Expand Down
77 changes: 38 additions & 39 deletions 1.Firmware/src/hal/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,33 @@

WiFiClient wifi_client;
PubSubClient mqtt_client(wifi_client);
int mqtt_last_connect_time_ = 0;
int mqtt_last_connect_time = 0;


void setup_wifi() {
int setup_wifi() {
int retry_cnt = 0;
int max_retry_cnt = 10;
String ssid,password;
get_wifi_config(ssid,password);
#if DEBUG_PRINT
printf("connecting to WiFi..: %s\n", ssid);
#endif
const char *wifi_name = ssid.c_str(); //去掉const會顯示編譯錯誤
const char *wifi_pass = password.c_str(); //去掉const會顯示編譯錯誤
const char *wifi_name = ssid.c_str();
const char *wifi_pass = password.c_str();

printf("connecting to WiFi..: %s\n", ssid);
WiFi.begin(wifi_name, wifi_pass );

while (WiFi.status() != WL_CONNECTED) {
while (WiFi.status() != WL_CONNECTED && retry_cnt < max_retry_cnt) {
Serial.print(".");
delay(500);
retry_cnt++;
delay(100);
}

#if DEBUG_PRINT
if (WiFi.status() == WL_CONNECTED) {
printf("Connected to network %s\n", ssid);
#endif
} else {
printf("WiFi connection timeout: %s\n", ssid);
return -1;
}
return 0;
}

void mqttCallback(char *topic, byte *payload, unsigned int length) {
Expand All @@ -41,39 +47,29 @@ void mqttCallback(char *topic, byte *payload, unsigned int length) {
}

void connectMQTT() {

if (WiFi.status() != WL_CONNECTED) {
setup_wifi();
}
String password,host,username,topic;
uint16_t port;
get_mqtt_config(host,port,username,password,topic);
const char *mqtt_user = username.c_str(); //去掉const會顯示編譯錯誤
const char *mqtt_pass = password.c_str(); //去掉const會顯示編譯錯誤

Serial.println("Attempting MQTT connection...");
const char *mqtt_user = username.c_str();
const char *mqtt_pass = password.c_str();
bool result;
Serial.println("Attempting MQTT connection...");

if (WiFi.status() != WL_CONNECTED) {
if (setup_wifi()) {
return;
}
}

if(password.length() > 0){
#if DEBUG_PRINT
printf("MQTT connect with password |%s| , |%s|\n" , mqtt_user , mqtt_pass );
#endif
result = mqtt_client.connect(mqtt_user, mqtt_user, mqtt_pass);
}else{
#if DEBUG_PRINT
printf("MQTT connect |%s| \n" , mqtt_user );
#endif
} else {
result = mqtt_client.connect(mqtt_user);
}
#if DEBUG_PRINT
if (result) {
printf("MQTT connected\n");
} else {

if (!result) {
printf("MQTT failed rc=%d will try again in 5 seconds\n", mqtt_client.state());
}
#endif

Serial.flush();
}


Expand All @@ -82,9 +78,9 @@ void TaskMqttUpdate(void *pvParameters) {

while(1) {
long now = millis();
if (!mqtt_client.connected() && (now - mqtt_last_connect_time_) > 5000) {
if (!mqtt_client.connected() && (now - mqtt_last_connect_time) > 5000) {
Serial.printf("Reconnecting MQTT");
mqtt_last_connect_time_ = now;
mqtt_last_connect_time = now;
connectMQTT();
}
mqtt_client.loop();
Expand Down Expand Up @@ -113,17 +109,20 @@ int HAL::mqtt_publish(const char *topic, const char *playload)
char mqtt_host[50];

void HAL::mqtt_init(void) {

setup_wifi();
String password,host,username,topic;
uint16_t port;

if (WiFi.status() != WL_CONNECTED) {
setup_wifi();
}

get_mqtt_config(host,port,username,password,topic);
sprintf(mqtt_host , "%s" , host.c_str());
sprintf(mqtt_host, "%s" , host.c_str());
#if DEBUG_PRINT
printf("MQTT connect host |%s|:|%d|\n" , mqtt_host , port );
#endif
mqtt_client.setServer(mqtt_host, port);
// connectMQTT();

mqtt_client.setCallback(mqttCallback);
xTaskCreatePinnedToCore(
TaskMqttUpdate,
Expand Down
Loading

0 comments on commit 9205e21

Please sign in to comment.