2024-03-10 14:27:46 -04:00
# include "WiFi.h"
2024-02-12 13:57:51 -05:00
# include <PubSubClient.h>
2024-03-10 14:27:46 -04:00
# include "mqtt_hal_esp32.h"
2024-02-12 13:57:51 -05:00
# include "secrets.h"
2024-03-10 14:27:46 -04:00
# if (ENABLE_WIFI_AND_MQTT == 1)
2024-02-12 13:57:51 -05:00
WiFiClient espClient ;
PubSubClient mqttClient ( espClient ) ;
2024-03-10 14:27:46 -04:00
bool isWifiConnected = false ;
2024-03-18 15:20:59 -04:00
tAnnounceWiFiconnected_cb thisAnnounceWiFiconnected_cb = NULL ;
void set_announceWiFiconnected_cb_HAL ( tAnnounceWiFiconnected_cb pAnnounceWiFiconnected_cb ) {
thisAnnounceWiFiconnected_cb = pAnnounceWiFiconnected_cb ;
}
tAnnounceSubscribedTopics_cb thisAnnounceSubscribedTopics_cb = NULL ;
void set_announceSubscribedTopics_cb_HAL ( tAnnounceSubscribedTopics_cb pAnnounceSubscribedTopics_cb ) {
thisAnnounceSubscribedTopics_cb = pAnnounceSubscribedTopics_cb ;
2024-03-10 14:27:46 -04:00
}
bool getIsWifiConnected_HAL ( ) {
return isWifiConnected ;
}
2024-02-12 13:57:51 -05:00
// WiFi status event
void WiFiEvent ( WiFiEvent_t event ) {
//Serial.printf("[WiFi-event] event: %d\r\n", event);
if ( event = = ARDUINO_EVENT_WIFI_STA_GOT_IP ) {
// connection to MQTT server will be done in checkMQTTconnection()
// mqttClient.setServer(MQTT_SERVER, 1883); // MQTT initialization
// mqttClient.connect("OMOTE"); // Connect using a client id
}
// Set status bar icon based on WiFi status
if ( event = = ARDUINO_EVENT_WIFI_STA_GOT_IP | | event = = ARDUINO_EVENT_WIFI_STA_GOT_IP6 ) {
2024-03-10 14:27:46 -04:00
isWifiConnected = true ;
2024-03-18 15:20:59 -04:00
thisAnnounceWiFiconnected_cb ( true ) ;
2024-03-10 09:41:50 -04:00
Serial . printf ( " WiFi connected, IP address: %s \r \n " , WiFi . localIP ( ) . toString ( ) . c_str ( ) ) ;
2024-02-12 13:57:51 -05:00
} else if ( event = = ARDUINO_EVENT_WIFI_STA_DISCONNECTED ) {
2024-03-10 14:27:46 -04:00
isWifiConnected = false ;
2024-03-18 15:20:59 -04:00
thisAnnounceWiFiconnected_cb ( false ) ;
2024-02-12 13:57:51 -05:00
// automatically try to reconnect
Serial . printf ( " WiFi got disconnected. Will try to reconnect. \r \n " ) ;
WiFi . begin ( WIFI_SSID , WIFI_PASSWORD ) ;
} else {
// e.g. ARDUINO_EVENT_WIFI_STA_CONNECTED or many others
// connected is not enough, will wait for IP
2024-03-10 14:27:46 -04:00
isWifiConnected = false ;
2024-03-18 15:20:59 -04:00
thisAnnounceWiFiconnected_cb ( false ) ;
2024-02-12 13:57:51 -05:00
}
}
2024-03-10 14:27:46 -04:00
void init_mqtt_HAL ( void ) {
2024-02-12 13:57:51 -05:00
// Setup WiFi
WiFi . setHostname ( " OMOTE " ) ; //define hostname
WiFi . onEvent ( WiFiEvent ) ;
WiFi . begin ( WIFI_SSID , WIFI_PASSWORD ) ;
WiFi . setSleep ( true ) ;
}
2024-03-18 15:20:59 -04:00
std : : string subscribeTopicOMOTEtest = " OMOTE/test " ;
void callback ( char * topic , byte * payload , unsigned int length ) {
// handle message arrived
std : : string topicReceived ( topic ) ;
std : : string strPayload ( reinterpret_cast < const char * > ( payload ) , length ) ;
if ( topicReceived = = subscribeTopicOMOTEtest ) {
Serial . printf ( " MQTT: received topic %s with payload %s \r \n " , subscribeTopicOMOTEtest . c_str ( ) , strPayload . c_str ( ) ) ;
}
thisAnnounceSubscribedTopics_cb ( topicReceived , strPayload ) ;
}
void mqtt_subscribeTopics ( ) {
mqttClient . setCallback ( & callback ) ;
mqttClient . subscribe ( subscribeTopicOMOTEtest . c_str ( ) ) ;
Serial . printf ( " Successfully subscribed to MQTT topic %s \r \n " , subscribeTopicOMOTEtest . c_str ( ) ) ;
}
2024-02-12 13:57:51 -05:00
bool checkMQTTconnection ( ) {
if ( WiFi . isConnected ( ) ) {
if ( mqttClient . connected ( ) ) {
return true ;
} else {
// try to connect to mqtt server
2024-03-18 15:20:59 -04:00
mqttClient . setBufferSize ( 512 ) ; // default is 256
//mqttClient.setKeepAlive(15); // default is 15 Client will send MQTTPINGREQ to keep connection alive
//mqttClient.setSocketTimeout(15); // default is 15 This determines how long the client will wait for incoming data when it expects data to arrive - for example, whilst it is in the middle of reading an MQTT packet.
2024-02-12 13:57:51 -05:00
mqttClient . setServer ( MQTT_SERVER , MQTT_SERVER_PORT ) ; // MQTT initialization
2024-03-18 15:20:59 -04:00
std : : string mqttClientName = std : : string ( MQTT_CLIENTNAME ) + " _esp32_ " + std : : string ( WiFi . macAddress ( ) . c_str ( ) ) ;
if ( mqttClient . connect ( mqttClientName . c_str ( ) , MQTT_USER , MQTT_PASS ) ) {
2024-02-12 13:57:51 -05:00
Serial . printf ( " Successfully connected to MQTT broker \r \n " ) ;
2024-03-18 15:20:59 -04:00
mqtt_subscribeTopics ( ) ;
2024-02-12 13:57:51 -05:00
} else {
Serial . printf ( " MQTT connection failed (but WiFi is available). Will try later ... \r \n " ) ;
}
return mqttClient . connected ( ) ;
}
} else {
2024-03-18 15:20:59 -04:00
// Serial.printf(" No connection to MQTT server, because WiFi ist not connected.\r\n");
2024-02-12 13:57:51 -05:00
return false ;
}
}
2024-03-18 15:20:59 -04:00
unsigned long reconnectInterval = 100 ;
// in order to do reconnect immediately ...
unsigned long lastReconnectAttempt = millis ( ) - reconnectInterval - 1 ;
void mqtt_loop_HAL ( ) {
if ( ! mqttClient . connected ( ) ) {
unsigned long currentMillis = millis ( ) ;
if ( ( currentMillis - lastReconnectAttempt ) > reconnectInterval ) {
lastReconnectAttempt = currentMillis ;
// Attempt to reconnect
checkMQTTconnection ( ) ;
}
}
if ( mqttClient . connected ( ) ) {
mqttClient . loop ( ) ;
}
}
2024-03-10 14:27:46 -04:00
bool publishMQTTMessage_HAL ( const char * topic , const char * payload ) {
2024-02-12 13:57:51 -05:00
if ( checkMQTTconnection ( ) ) {
// Serial.printf("Sending mqtt payload to topic \"%s\": %s\r\n", topic, payload);
if ( mqttClient . publish ( topic , payload ) ) {
// Serial.printf("Publish ok\r\n");
return true ;
}
else {
Serial . printf ( " Publish failed \r \n " ) ;
}
} else {
Serial . printf ( " Cannot publish mqtt message, because checkMQTTconnection failed (WiFi or mqtt is not connected) \r \n " ) ;
}
return false ;
}
2024-03-10 14:27:46 -04:00
void wifiStop_HAL ( ) {
WiFi . disconnect ( ) ;
WiFi . mode ( WIFI_OFF ) ;
}
2024-02-12 13:57:51 -05:00
# endif