2023-05-10 15:44:21 +00:00
# ifndef _WIFI_FUNCTIONS_H_
# define _WIFI_FUNCTIONS_H_
# include <WiFi.h>
# include <MQTT.h>
# include "wifi_settings.h"
2023-05-10 20:27:59 +00:00
# include "helpfunctions.h"
2023-06-20 20:30:29 +00:00
bool force_ec_measurement = false ;
2023-05-10 20:27:59 +00:00
struct mqttValueTiming {
float minchange ;
float maxchange ;
unsigned long mintime ;
unsigned long maxtime ;
float lastvalue = 0 ;
unsigned long lasttime = 0 ;
} ;
2023-05-10 15:44:21 +00:00
WiFiClient net ;
MQTTClient client ;
2023-05-10 20:27:59 +00:00
bool sendallnext_flag = false ;
bool enableTiming = true ;
2023-06-20 20:30:29 +00:00
2023-05-10 20:27:59 +00:00
2023-06-21 07:00:29 +00:00
bool publishValueTimed ( String topic , float value , uint8_t decimals , mqttValueTiming & mqttvt , unsigned long loopmillis ) ;
2023-05-10 20:27:59 +00:00
void publishValue ( String topic , float value , uint8_t decimals ) ;
2024-05-11 07:20:37 +00:00
void publishInfo ( String topic , String text ) ;
2023-05-10 20:27:59 +00:00
2023-05-10 15:44:21 +00:00
void connect ( ) {
Serial . print ( " checking wifi... " ) ;
2023-05-10 20:27:59 +00:00
uint8_t timeout = 0 ;
while ( WiFi . status ( ) ! = WL_CONNECTED & & timeout < 10 ) {
timeout + + ;
2023-05-10 15:44:21 +00:00
Serial . print ( " . " ) ;
2023-05-10 20:27:59 +00:00
delay ( 500 ) ;
2023-05-10 15:44:21 +00:00
}
2023-05-10 20:27:59 +00:00
if ( WiFi . status ( ) = = WL_CONNECTED )
{
Serial . print ( " \n connecting... " ) ;
timeout = 0 ;
# define CONNECT_TIMEOUT 5
while ( ! client . connect ( client_id , " public " , " public " ) & & timeout < CONNECT_TIMEOUT ) {
Serial . print ( " . " ) ;
delay ( 1000 ) ;
timeout + + ;
}
if ( timeout > = CONNECT_TIMEOUT ) {
Serial . println ( " \n connection failed! " ) ;
} else {
Serial . println ( " \n connected! " ) ;
client . subscribe ( ( String ) client_id + " /sendall " ) ;
client . subscribe ( ( String ) client_id + " /ec/trigger " ) ;
// client.unsubscribe("/hello");
}
2023-05-10 15:44:21 +00:00
2023-05-10 20:27:59 +00:00
}
2023-05-10 15:44:21 +00:00
}
void messageReceived ( String & topic , String & payload ) {
Serial . println ( " incoming: " + topic + " - " + payload ) ;
2023-05-10 20:27:59 +00:00
if ( topic = = ( ( String ) client_id + " /sendall " ) & & payload = = " true " ) {
sendallnext_flag = true ;
Serial . println ( " Send all values next time " ) ;
}
if ( topic = = ( ( String ) client_id + " /ec/trigger " ) & & payload = = " true " ) {
force_ec_measurement = true ;
Serial . println ( " Forced EC Measurement " ) ;
}
2024-05-12 07:23:41 +00:00
if ( topic = = ( ( String ) client_id + " /errorack " ) & & payload = = " true " ) { //error acknowledge
valueError = false ;
Serial . println ( " Reset value error flag " ) ;
}
2024-05-12 07:37:24 +00:00
if ( topic = = ( ( String ) client_id + " /reboot " ) & & payload = = " true " ) { //error acknowledge
Serial . println ( " Reboot by mqtt " ) ;
delay ( 100 ) ;
ESP . restart ( ) ;
}
2023-05-10 20:27:59 +00:00
}
bool mqtt_loop ( unsigned long loopmillis ) {
static unsigned long last_client_loop = 0 ;
# define CLIENT_LOOP_INTERVAL 10 //ms. fixes some wifi issues. from example https://registry.platformio.org/libraries/256dpi/MQTT/examples/ESP32DevelopmentBoard/ESP32DevelopmentBoard.ino
if ( loopmillis > = last_client_loop + CLIENT_LOOP_INTERVAL ) {
last_client_loop = loopmillis ;
client . loop ( ) ;
static unsigned long last_connection_try = 0 ;
# define RETRY_CONNECTION 60000
if ( ! client . connected ( ) ) {
if ( loopmillis - last_connection_try > RETRY_CONNECTION ) {
connect ( ) ;
}
} else {
return true ;
}
}
return false ;
}
2024-05-11 07:20:37 +00:00
bool publishValueTimedOverride ( String topic , float value , uint8_t decimals , mqttValueTiming & mqttvt , unsigned long loopmillis ) {
mqttvt . lasttime = loopmillis ;
mqttvt . lastvalue = value ;
publishValue ( topic , value , decimals ) ;
return true ;
}
2023-06-21 07:00:29 +00:00
bool publishValueTimed ( String topic , float value , uint8_t decimals , mqttValueTiming & mqttvt , unsigned long loopmillis ) {
2023-05-10 20:27:59 +00:00
unsigned long timediff = loopmillis - mqttvt . lasttime ;
float valuediff = abs ( value - mqttvt . lastvalue ) ;
valuediff = constrain ( valuediff , mqttvt . minchange , mqttvt . maxchange ) ;
unsigned long sendafter = mapf ( valuediff , mqttvt . minchange , mqttvt . maxchange , mqttvt . maxtime , mqttvt . mintime ) ; //map valuediff to time when to send
/*Serial.println();
Serial . print ( " timediff= " ) ; Serial . print ( timediff ) ;
Serial . print ( " valuediff= " ) ; Serial . print ( valuediff ) ;
Serial . print ( " sendafter= " ) ; Serial . println ( sendafter ) ; */
if ( timediff > = sendafter | | ! enableTiming ) {
mqttvt . lasttime = loopmillis ;
mqttvt . lastvalue = value ;
publishValue ( topic , value , decimals ) ;
2023-06-21 07:00:29 +00:00
return true ;
2023-05-10 20:27:59 +00:00
}
2023-06-21 07:00:29 +00:00
return false ;
2023-05-10 20:27:59 +00:00
}
2023-05-10 15:44:21 +00:00
2023-05-10 20:27:59 +00:00
void publishValue ( String topic , float value , uint8_t decimals ) {
char buffer [ 16 ] ;
dtostrf ( value , 1 , decimals , buffer ) ;
client . publish ( ( String ) client_id + " / " + topic , buffer ) ;
Serial . print ( " Publish Topic= " ) ; Serial . print ( ( String ) client_id + " / " + topic ) ; Serial . print ( " Message= " ) ; Serial . println ( buffer ) ;
2023-05-10 15:44:21 +00:00
}
2024-05-11 07:20:37 +00:00
void publishInfo ( String topic , String text ) {
client . publish ( ( String ) client_id + " / " + topic , text ) ;
Serial . print ( " Publish Topic= " ) ; Serial . print ( ( String ) client_id + " / " + topic ) ; Serial . print ( " Message= " ) ; Serial . println ( text ) ;
}
2023-05-10 15:44:21 +00:00
# endif