Skip to content

Commit

Permalink
Merge pull request #3 from eoh-jsc/feature/change-wifi-gateway
Browse files Browse the repository at this point in the history
Add logic change wifi for ESP32
  • Loading branch information
bang-eoh authored Jul 16, 2024
2 parents 76bdcd2 + 1a597df commit 29a5cf6
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 0 deletions.
15 changes: 15 additions & 0 deletions linux/MQTT/ERaMqttLinux.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ class ERaMqttLinux
bool publishData(const char* topic, const char* payload,
bool retained = ERA_MQTT_PUBLISH_RETAINED,
QoST qos = (QoST)ERA_MQTT_PUBLISH_QOS) override;
bool publishChipData(const char* topic, const char* payload,
bool retained = ERA_MQTT_PUBLISH_RETAINED,
QoST qos = (QoST)ERA_MQTT_PUBLISH_QOS);
bool publishState(bool online);
bool syncConfig();

Expand Down Expand Up @@ -431,6 +434,18 @@ bool ERaMqttLinux<MQTT>::publishData(const char* topic, const char* payload,
return status;
}

template <class MQTT>
inline
bool ERaMqttLinux<MQTT>::publishChipData(const char* topic, const char* payload,
bool retained, QoST qos) {
char topicName[MAX_TOPIC_LENGTH] {0};
FormatString(topicName, this->ERaTopic);
if (topic != NULL) {
FormatString(topicName, topic);
}
return this->publishData(topicName, payload, retained, qos);
}

template <class MQTT>
inline
bool ERaMqttLinux<MQTT>::publishState(bool online) {
Expand Down
126 changes: 126 additions & 0 deletions src/ERa/ERaApi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ class ERaApi
void handlePinRequest(const ERaDataBuff& arrayTopic, const char* payload);
void processArduinoPinRequest(const ERaDataBuff& arrayTopic, const char* payload);
void processVirtualPinRequest(const ERaDataBuff& arrayTopic, const char* payload);
void processWifiRequest(const ERaDataBuff& arrayTopic, const char* payload);
void sendResultChangeWifi();
void initPinConfig();
void parsePinConfig(const char* str);
void storePinConfig(const char* str);
Expand Down Expand Up @@ -575,6 +577,7 @@ class ERaApi
#endif

private:
int8_t _changing_wifi_success = -1;
template <typename T>
void virtualWriteSingle(int pin, const T& value, bool json = false) {
ERaRsp_t rsp;
Expand Down Expand Up @@ -821,6 +824,129 @@ void ERaApi<Proto, Flash>::processVirtualPinRequest(const ERaDataBuff& arrayTopi
item = nullptr;
}

#if defined(ESP32)

#include <WiFi.h>

const char* scanWiFi() {
int nets = WiFi.scanNetworks(false, true, false, 150);
if (nets < 0) {
ERA_LOG("WiFi", "Failed to scan networks");
return nullptr;
}

static ERaJson json;
json.reset();
for (int i = 0; i < nets; ++i) {
json[i]["ssid"] = WiFi.SSID(i).c_str();
json[i]["rssi"] = WiFi.RSSI(i);
json[i]["bssid"] = WiFi.BSSIDstr(i).c_str();
json[i]["channel"] = WiFi.channel(i);
switch (WiFi.encryptionType(i)) {
case wifi_auth_mode_t::WIFI_AUTH_WEP:
json[i]["encryption"] = "WEP";
break;
case wifi_auth_mode_t::WIFI_AUTH_WPA_PSK:
json[i]["encryption"] = "WPA/PSK";
break;
case wifi_auth_mode_t::WIFI_AUTH_WPA2_PSK:
json[i]["encryption"] = "WPA2/PSK";
break;
case wifi_auth_mode_t::WIFI_AUTH_WPA_WPA2_PSK:
json[i]["encryption"] = "WPA/WPA2/PSK";
break;
case wifi_auth_mode_t::WIFI_AUTH_OPEN:
json[i]["encryption"] = "OPEN";
break;
default:
json[i]["encryption"] = "UNKNOWN";
break;
}
}

WiFi.scanDelete();
return json.getString();
}

bool tryConnectWifi(const char* newSsid, const char* newPassword) {
WiFi.disconnect();

if (newPassword) {
WiFi.begin(newSsid, newPassword);
} else {
WiFi.begin(newSsid);
}

for (int i = 0; i < 100; i++) { // 10s
if (WiFi.isConnected()) {
return true;
}
delay(100);
}
return false;
}

bool changeWifi(const char* newSsid, const char* newPassword) {
if (tryConnectWifi(newSsid, newPassword)) {
return true;
}
WiFi.disconnect();
return false;
}
#else
const char* scanWiFi() {
return nullptr;
}

bool changeWifi(const char* newSsid, const char* newPassword) {
return false;
}
#endif

template <class Proto, class Flash>
inline
void ERaApi<Proto, Flash>::processWifiRequest(const ERaDataBuff& arrayTopic, const char* payload) {
if (arrayTopic.size() != 3) {
return;
}
if (arrayTopic.at(2) == "ask")
{
this->thisProto().getTransp().publishChipData(
"/wifi/list",
scanWiFi()
);
}
else if (arrayTopic.at(2) == "change") {
cJSON* root = cJSON_Parse(payload);
if (!cJSON_IsObject(root)) {
cJSON_Delete(root);
root = nullptr;
return;
}
cJSON* ssidItem = cJSON_GetObjectItem(root, "ssid");
cJSON* passwordItem = cJSON_GetObjectItem(root, "password");
const char * ssid = ssidItem->valuestring;
const char * password = passwordItem->valuestring;
this->_changing_wifi_success = changeWifi(ssid, password) ? 1 : 0;
cJSON_Delete(root);
root = nullptr;
}
}

template <class Proto, class Flash>
inline
void ERaApi<Proto, Flash>::sendResultChangeWifi() {
if (this->_changing_wifi_success == -1) {
return;
}
if(this->thisProto().getTransp().publishChipData(
"/wifi/change_result",
this->_changing_wifi_success ? "{\"success\": 1}" : "{\"success\": 0}"
)) {
this->_changing_wifi_success = -1;
}
}

template <class Proto, class Flash>
inline
void ERaApi<Proto, Flash>::initPinConfig() {
Expand Down
3 changes: 3 additions & 0 deletions src/ERa/ERaProtocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,9 @@ void ERaProto<Transp, Flash>::processRequest(const char* topic, const char* payl
else if (arrayTopic.at(1) == "virtual_pin") {
Base::processVirtualPinRequest(arrayTopic, payload);
}
else if (arrayTopic.at(1) == "wifi") {
Base::processWifiRequest(arrayTopic, payload);
}
else if (arrayTopic.at(1) == "down") {
this->processDownRequest(arrayTopic, payload);
}
Expand Down
1 change: 1 addition & 0 deletions src/ERa/ERaTopic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Topic format:

/* Subscribe topic */
#define ERA_SUB_PREFIX_DOWN_TOPIC "/down"
#define ERA_SUB_PREFIX_WIFI_TOPIC "/wifi/+"
#define ERA_SUB_PREFIX_ARDUINO_TOPIC "/arduino_pin/+"
#define ERA_SUB_PREFIX_VIRTUAL_TOPIC "/virtual_pin/+"

Expand Down
20 changes: 20 additions & 0 deletions src/MQTT/ERaMqtt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ class ERaMqtt
this->config(_host, _port, _username, _password);
}

bool isConnected() {
return _connected;
}

void init(int readBufSize = ERA_MQTT_RX_BUFFER_SIZE,
int writeBufSize = ERA_MQTT_TX_BUFFER_SIZE) {
this->mqtt.init(readBufSize, writeBufSize);
Expand All @@ -142,6 +146,9 @@ class ERaMqtt
bool publishData(const char* topic, const char* payload,
bool retained = ERA_MQTT_PUBLISH_RETAINED,
QoST qos = (QoST)ERA_MQTT_PUBLISH_QOS) override;
bool publishChipData(const char* topic, const char* payload,
bool retained = ERA_MQTT_PUBLISH_RETAINED,
QoST qos = (QoST)ERA_MQTT_PUBLISH_QOS);
bool publishState(bool online);
bool syncConfig();

Expand Down Expand Up @@ -350,6 +357,7 @@ bool ERaMqtt<Client, MQTT>::connect(FunctionCallback_t fn) {
this->subscribeTopic(this->ERaTopic, ERA_SUB_PREFIX_VIRTUAL_TOPIC);
this->subscribeTopic(this->ERaTopic, ERA_SUB_PREFIX_OP_DOWN_TOPIC);
this->subscribeTopic(this->ERaTopic, ERA_SUB_PREFIX_DOWN_TOPIC);
this->subscribeTopic(this->ERaTopic, ERA_SUB_PREFIX_WIFI_TOPIC);

ERaWatchdogFeed();

Expand Down Expand Up @@ -490,6 +498,18 @@ bool ERaMqtt<Client, MQTT>::publishData(const char* topic, const char* payload,
return status;
}

template <class Client, class MQTT>
inline
bool ERaMqtt<Client, MQTT>::publishChipData(const char* topic, const char* payload,
bool retained, QoST qos) {
char topicName[MAX_TOPIC_LENGTH] {0};
FormatString(topicName, this->ERaTopic);
if (topic != NULL) {
FormatString(topicName, topic);
}
return this->publishData(topicName, payload, retained, qos);
}

template <class Client, class MQTT>
inline
bool ERaMqtt<Client, MQTT>::publishState(bool online) {
Expand Down
1 change: 1 addition & 0 deletions src/Task/ERaTaskEsp32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ void ERaApi<Proto, Flash>::runERaApiTask() {
defined(ERA_NO_RTOS)
Zigbee::run();
#endif
this->sendResultChangeWifi();
}

#endif /* INC_ERA_TASK_ESP32_HPP_ */

0 comments on commit 29a5cf6

Please sign in to comment.