diff --git a/Makefile b/Makefile index 2db348b..8908b99 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,8 @@ TOOL = atmelice_isp BUILDDIR = Builds -DEFINES = -I . -IInternet/MQTT -I Internet/MQTT/MQTTPacket/src -I Ethernet/W5500 -I Ethernet -DF_CPU=16000000UL -D_WIZCHIP_=W5100 +DEFINES = -I . -IInternet/MQTT -I Internet/MQTT/MQTTPacket/src -I Ethernet/W5500 -I Ethernet -DF_CPU=16000000UL -D_WIZCHIP_=W5100 -DPLC_MQTT_ENABLED=1 + CFLAGS =-mmcu=$(MCU) -O2 -Wall -Wpedantic $(DEFINES) -std=c99 -ffunction-sections -fdata-sections #LDFLAGS =-mmcu=$(MCU) -Wl,--gc-sections diff --git a/main.c b/main.c index daaaba8..1a27dff 100644 --- a/main.c +++ b/main.c @@ -21,8 +21,6 @@ #include "util/delay.h" -#define PLC_MQTT_ENABLED 1 - Client mqtt_client; //***********Prologue for fast WDT disable & and save reason of reset/power-up: BEGIN @@ -92,14 +90,11 @@ void do_notaus(){ } } -void send_values(void){ - char msg[10]; +void send_temperatures(void){ for(uint8_t i=0; i 500){ outStates[0] ^= outStatesBlinking[0]; outStates[1] ^= outStatesBlinking[1]; -#if PLC_MQTT_ENABLED - send_values(); -#endif - timer_blink_outs = millis(); + outStates[2] ^= outStatesBlinking[2]; + outStates[3] ^= outStatesBlinking[3]; + } + + if(millis() - timer_send_temps > 500){ + timer_send_temps = millis(); + send_temperatures(); printf("gefran: 0x%02X\n", gtf_firmware_version(5)); } -#if PLC_MQTT_ENABLED // send misc info if(millis() - timer_send_uptime > 5000){ - timer_send_uptime += 5000; - char msg[64]; - sprintf(msg, "%ld", millis()/1000); - mqtt_pub(&mqtt_client, "/Filamentanlage/02_Wasserbecken/uptime", msg, strlen(msg)); + timer_send_uptime = millis(); + send_value(&mqtt_client, "/Filamentanlage/02_Wasserbecken/uptime", millis()/1000); } -#endif - if(read_Input(IN_ANLAGE_EIN_INV, FALLING)){ printf("anlage ein\n\r"); diff --git a/mqtt.c b/mqtt.c index de27cf5..0a2dccf 100644 --- a/mqtt.c +++ b/mqtt.c @@ -1,3 +1,5 @@ +#include +#include #include #include "mqtt.h" @@ -64,3 +66,23 @@ void mqtt_pub(Client* mqtt_client, char * mqtt_topic, char * mqtt_msg, int mqtt_ } } } + +void send_value(Client* mqtt_client,char* topic, int16_t value){ + char msg[32]; + sprintf(msg, "%d", value); +#if PLC_MQTT_ENABLED + mqtt_pub(mqtt_client, topic, msg, strlen(msg)); +#else + printf("%s: %s\n", topic, msg); +#endif +} + +void send_value_fl(Client* mqtt_client,char* topic, float value, uint8_t decimal_points){ +#if PLC_MQTT_ENABLED + char msg[32]; + sprintf(msg, "%.1f"/*, decimal_points*/, value); + mqtt_pub(mqtt_client, topic, msg, strlen(msg)); +#else + printf("%s: %.1f\n", topic, /*decimal_points,*/ value); +#endif +} diff --git a/mqtt.h b/mqtt.h index 3e18522..3d7a477 100644 --- a/mqtt.h +++ b/mqtt.h @@ -14,5 +14,7 @@ extern uint8_t MQTT_targetIP[4]; void messageArrived(MessageData* md); void mqtt_pub(Client* mqtt_client, char * mqtt_topic, char * mqtt_msg, int mqtt_msg_len); +void send_value(Client* mqtt_client,char* topic, int16_t value); +void send_value_fl(Client* mqtt_client,char* topic, float value, uint8_t decimal_points); #endif