Added basic WebSocket Functionality
This commit is contained in:
@@ -12,21 +12,17 @@ String GetPayloadContent(char * data, size_t len) {
|
||||
return content;
|
||||
}
|
||||
|
||||
void ConnectToMqtt()
|
||||
{
|
||||
void ConnectToMqtt() {
|
||||
LOG_INFO("Connecting to MQTT...");
|
||||
mqttClient.connect();
|
||||
}
|
||||
|
||||
void OnMqttConnect(bool sessionPresent)
|
||||
{
|
||||
void OnMqttConnect(bool sessionPresent) {
|
||||
LOG_INFO("Connected to MQTT.");
|
||||
//LOG_INFO("Session present: %s", sessionPresent ? "Yes":"No");
|
||||
SuscribeMqtt();
|
||||
}
|
||||
|
||||
void OnMqttDisconnect(AsyncMqttClientDisconnectReason reason)
|
||||
{
|
||||
void OnMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
|
||||
LOG_WARNING("Disconnected from MQTT.");
|
||||
|
||||
if(WiFi.isConnected())
|
||||
@@ -35,51 +31,44 @@ void OnMqttDisconnect(AsyncMqttClientDisconnectReason reason)
|
||||
}
|
||||
}
|
||||
|
||||
void OnMqttSubscribe(uint16_t packetId, uint8_t qos)
|
||||
{
|
||||
void OnMqttSubscribe(uint16_t packetId, uint8_t qos) {
|
||||
LOG_INFO("Subscribe acknowledged. PacketID: %d / QoS: %d", packetId, qos);
|
||||
}
|
||||
|
||||
void OnMqttUnsubscribe(uint16_t packetId)
|
||||
{
|
||||
void OnMqttUnsubscribe(uint16_t packetId) {
|
||||
LOG_INFO("Unsubscribe Acknowledged. PacketID: %d",packetId);
|
||||
}
|
||||
|
||||
void OnMqttPublish(uint16_t packetId)
|
||||
{
|
||||
void OnMqttPublish(uint16_t packetId) {
|
||||
LOG_INFO("Publish Acknowledged. PacketID: %d", packetId);
|
||||
}
|
||||
|
||||
void ConnectWiFi_STA(bool useStaticIP = false)
|
||||
{
|
||||
Serial.println("");
|
||||
void ConnectWiFi_STA(bool useStaticIP = true) {
|
||||
WiFi.mode(WIFI_STA);
|
||||
if(useStaticIP) {
|
||||
WiFi.config(ip, gateway, subnet);
|
||||
WiFi.setHostname(hostname);
|
||||
}
|
||||
WiFi.begin(ssid, password);
|
||||
//WiFi.begin(ssid, password);
|
||||
WiFi.begin();
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(100);
|
||||
Serial.print('.');
|
||||
delay(10);
|
||||
}
|
||||
|
||||
if (LOG_LEVEL_ENABLED(LOG_LEVEL_INFO)){
|
||||
Serial.println("");
|
||||
Serial.print("Initiating STA:\t");
|
||||
Serial.print("NIGGA - Initiating STA:\t");
|
||||
Serial.println(ssid);
|
||||
Serial.print("IP address:\t");
|
||||
Serial.println(WiFi.localIP());
|
||||
}
|
||||
}
|
||||
|
||||
void ConnectWiFi_AP(bool useStaticIP = false)
|
||||
{
|
||||
void ConnectWiFi_AP(bool useStaticIP = false) {
|
||||
Serial.println("");
|
||||
WiFi.mode(WIFI_AP);
|
||||
while(!WiFi.softAP(ssid, password))
|
||||
{
|
||||
while(!WiFi.softAP(ssid, password)) {
|
||||
Serial.println(".");
|
||||
delay(100);
|
||||
}
|
||||
@@ -95,26 +84,53 @@ void ConnectWiFi_AP(bool useStaticIP = false)
|
||||
}
|
||||
}
|
||||
|
||||
void WiFiEvent(WiFiEvent_t event)
|
||||
{
|
||||
LOG_INFO("[WiFi-event] event: %d\n", event);
|
||||
void NetworkEvent(arduino_event_id_t event, arduino_event_info_t info) {
|
||||
LOG_INFO("(NET) event: %d\n", event);
|
||||
IPAddress ip = WiFi.localIP();
|
||||
switch(event)
|
||||
{
|
||||
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
|
||||
Serial.print("[INFO] - WiFi connected. IP Address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
LOG_DEBUG("WiFi connected. IP Address: %d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
|
||||
//xTimerStop(wifiReconnectTimer, 0);
|
||||
ConnectToMqtt();
|
||||
break;
|
||||
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
|
||||
LOG_WARNING("WiFi Lost Connection! :()");
|
||||
xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
|
||||
LOG_WARNING("WiFi Lost Connection! :(");
|
||||
xTimerStop(mqttReconnectTimer, 0);
|
||||
xTimerStart(wifiReconnectTimer, 0);
|
||||
break;
|
||||
|
||||
case ARDUINO_EVENT_ETH_START:
|
||||
LOG_DEBUG("ETH Started");
|
||||
ETH.setHostname(hostname);
|
||||
break;
|
||||
case ARDUINO_EVENT_ETH_CONNECTED:
|
||||
LOG_DEBUG("ETH Connected !");
|
||||
break;
|
||||
case ARDUINO_EVENT_ETH_GOT_IP:
|
||||
LOG_INFO("ETH Got IP: '%s'\n", esp_netif_get_desc(info.got_ip.esp_netif));
|
||||
WiFi.disconnect(true);
|
||||
ConnectToMqtt();
|
||||
break;
|
||||
case ARDUINO_EVENT_ETH_LOST_IP:
|
||||
LOG_WARNING("ETH Lost IP");
|
||||
break;
|
||||
case ARDUINO_EVENT_ETH_DISCONNECTED:
|
||||
LOG_WARNING("ETH Disconnected");
|
||||
xTimerStop(mqttReconnectTimer, 0);
|
||||
xTimerStart(wifiReconnectTimer, 0);
|
||||
break;
|
||||
case ARDUINO_EVENT_ETH_STOP:
|
||||
LOG_INFO("ETH Stopped");
|
||||
xTimerStop(mqttReconnectTimer, 0);
|
||||
xTimerStart(wifiReconnectTimer, 0);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void InitMqtt()
|
||||
{
|
||||
void InitMqtt() {
|
||||
mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(ConnectToMqtt));
|
||||
wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(5000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(ConnectWiFi_STA));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user