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"
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 ;
bool force_ec_measurement = false ;
void publishValueTimed ( String topic , float value , uint8_t decimals , mqttValueTiming & mqttvt , unsigned long loopmillis ) ;
void publishValue ( String topic , float value , uint8_t decimals ) ;
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 " ) ;
}
}
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 ;
}
void publishValueTimed ( String topic , float value , uint8_t decimals , mqttValueTiming & mqttvt , unsigned long loopmillis ) {
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-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
}
# endif