add send_value function

master
agsler 3 years ago
parent 79feed7a92
commit ffe7c9507b

@ -10,7 +10,8 @@ TOOL = atmelice_isp
BUILDDIR = Builds 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 CFLAGS =-mmcu=$(MCU) -O2 -Wall -Wpedantic $(DEFINES) -std=c99 -ffunction-sections -fdata-sections
#LDFLAGS =-mmcu=$(MCU) -Wl,--gc-sections #LDFLAGS =-mmcu=$(MCU) -Wl,--gc-sections

@ -21,8 +21,6 @@
#include "util/delay.h" #include "util/delay.h"
#define PLC_MQTT_ENABLED 1
Client mqtt_client; Client mqtt_client;
//***********Prologue for fast WDT disable & and save reason of reset/power-up: BEGIN //***********Prologue for fast WDT disable & and save reason of reset/power-up: BEGIN
@ -92,14 +90,11 @@ void do_notaus(){
} }
} }
void send_values(void){ void send_temperatures(void){
char msg[10];
for(uint8_t i=0; i<sizeof(ADC_reading)/sizeof(ADC_reading[0]);i++){ for(uint8_t i=0; i<sizeof(ADC_reading)/sizeof(ADC_reading[0]);i++){
char msg[64];
char tpc[64]; char tpc[64];
sprintf(msg, "%.1f", ADC_reading[i]);
sprintf(tpc, "/Filamentanlage/02_Wasserbecken/state/temp%d", i); sprintf(tpc, "/Filamentanlage/02_Wasserbecken/state/temp%d", i);
mqtt_pub(&mqtt_client, tpc, msg, strlen(msg)); send_value_fl(&mqtt_client, tpc, ADC_reading[i], 1);
} }
} }
@ -181,23 +176,21 @@ int main()
if(millis() - timer_blink_outs > 500){ if(millis() - timer_blink_outs > 500){
outStates[0] ^= outStatesBlinking[0]; outStates[0] ^= outStatesBlinking[0];
outStates[1] ^= outStatesBlinking[1]; outStates[1] ^= outStatesBlinking[1];
#if PLC_MQTT_ENABLED outStates[2] ^= outStatesBlinking[2];
send_values(); outStates[3] ^= outStatesBlinking[3];
#endif }
timer_blink_outs = millis();
if(millis() - timer_send_temps > 500){
timer_send_temps = millis();
send_temperatures();
printf("gefran: 0x%02X\n", gtf_firmware_version(5)); printf("gefran: 0x%02X\n", gtf_firmware_version(5));
} }
#if PLC_MQTT_ENABLED
// send misc info // send misc info
if(millis() - timer_send_uptime > 5000){ if(millis() - timer_send_uptime > 5000){
timer_send_uptime += 5000; timer_send_uptime = millis();
char msg[64]; send_value(&mqtt_client, "/Filamentanlage/02_Wasserbecken/uptime", millis()/1000);
sprintf(msg, "%ld", millis()/1000);
mqtt_pub(&mqtt_client, "/Filamentanlage/02_Wasserbecken/uptime", msg, strlen(msg));
} }
#endif
if(read_Input(IN_ANLAGE_EIN_INV, FALLING)){ if(read_Input(IN_ANLAGE_EIN_INV, FALLING)){
printf("anlage ein\n\r"); printf("anlage ein\n\r");

@ -1,3 +1,5 @@
#include <stdint.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include "mqtt.h" #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
}

@ -14,5 +14,7 @@ extern uint8_t MQTT_targetIP[4];
void messageArrived(MessageData* md); void messageArrived(MessageData* md);
void mqtt_pub(Client* mqtt_client, char * mqtt_topic, char * mqtt_msg, int mqtt_msg_len); 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 #endif