From c5b6ba804381cb40c0040fba371c3cc2730ee301 Mon Sep 17 00:00:00 2001 From: agsler Date: Wed, 9 Nov 2022 17:23:19 +0100 Subject: [PATCH 1/9] add temperature reading on adc channels --- Makefile | 3 ++- avrIOhelper/io-helper.h | 2 ++ main.c | 30 ++++++++++++++++++++++++------ temperature.c | 33 +++++++++++++++++++++++++++++++++ temperature.h | 9 +++++++++ 5 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 temperature.c create mode 100644 temperature.h diff --git a/Makefile b/Makefile index e5279be..2db348b 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,8 @@ BUILDDIR = Builds DEFINES = -I . -IInternet/MQTT -I Internet/MQTT/MQTTPacket/src -I Ethernet/W5500 -I Ethernet -DF_CPU=16000000UL -D_WIZCHIP_=W5100 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 +LDFLAGS =-mmcu=$(MCU) -Wl,--gc-sections,-u,vfprintf -lprintf_flt LDFILES = $(foreach FILE,$(FILES),$(BUILDDIR)/$(FILE).o) diff --git a/avrIOhelper/io-helper.h b/avrIOhelper/io-helper.h index 71071f1..fce607b 100644 --- a/avrIOhelper/io-helper.h +++ b/avrIOhelper/io-helper.h @@ -97,4 +97,6 @@ void ioHelperEdgeDetector(void); #define IN_NOTAUS_ANLAGE BitPinF7 #define IN_NOTAUS_SCHRANK BitPinK0 +#define IN_KLATWASSER_DRAN BitPinF6 + #endif diff --git a/main.c b/main.c index 7ca309c..7168eba 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,7 @@ #include #include #include // WatchDog +#include #include #include "Ethernet/socket.h" @@ -14,10 +15,11 @@ #include "uart.h" #include "spi.h" #include "mqtt.h" +#include "temperature.h" #include "util/delay.h" -#define PLC_MQTT_ENABLED 0 +#define PLC_MQTT_ENABLED 1 Client mqtt_client; @@ -88,6 +90,18 @@ void do_notaus(){ } } +void send_values(void){ + char msg[10]; + 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(); } @@ -217,6 +234,7 @@ static void avr_init(void) timer0_init();// Timer0 millis engine init uart_init(); + initADC(); sei(); //re-enable global interrupts diff --git a/temperature.c b/temperature.c new file mode 100644 index 0000000..173b9a8 --- /dev/null +++ b/temperature.c @@ -0,0 +1,33 @@ +#include +#include +#include + +volatile float ADC_reading[5] = {0,0,0,0,0}; + +void initADC(void) +{ + ADMUX = 1 << REFS0 | 0 << REFS1; //Select external Vref + ADCSRA = _BV(ADEN) | _BV(ADIE); // enable adc, enable interrupt + ADCSRA |= 1 << ADPS2 | 1 << ADPS1 | 1 << ADPS0; // set clock-prescaler to 128 + ADCSRA |= 1 << ADSC; // start conversion +} + +void adc_set_channel(uint8_t ch){ + ADMUX = (ADMUX & 0xE0) | (ch & 0x1F); +} + +ISR(ADC_vect) +{ + static uint8_t ch = 0; + //Reading 10bit conversion result + uint16_t tmp = ADCL; //copy the first LSB bits + tmp |= ADCH << 8; //copy remaing byte + + ADC_reading[ch] = (((float)tmp * 0.3)); + //ADC_reading[ch] = (((float)tmp * 0.03)/0.092)-2.3; + + ch = (ch + 1)%5; + adc_set_channel(ch); + + ADCSRA |= (1 << ADSC); //Start next conversion +} diff --git a/temperature.h b/temperature.h new file mode 100644 index 0000000..b2a1d7b --- /dev/null +++ b/temperature.h @@ -0,0 +1,9 @@ +#ifndef _TEMPERATURE_H_ +#define _TEMPERATURE_H_ + +#include + +extern volatile float ADC_reading[5]; +void initADC(void); + +#endif From 6c93fb80a5a82a783a54654fc847a445ccdb1787 Mon Sep 17 00:00:00 2001 From: agsler Date: Wed, 9 Nov 2022 18:14:55 +0100 Subject: [PATCH 2/9] add pump connections --- avrIOhelper/io-helper.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/avrIOhelper/io-helper.h b/avrIOhelper/io-helper.h index fce607b..b2e7874 100644 --- a/avrIOhelper/io-helper.h +++ b/avrIOhelper/io-helper.h @@ -76,6 +76,8 @@ void ioHelperEdgeDetector(void); #define LED_PLC_OK BitPH5 #define LED_BUS_OK BitPD4 +#define OUT_PUMPE_STARTSTOP BitPB5 +#define OUT_PUMPE_PWM BitPB6 //Inputs //Verknüpfen von Pin | Bit mit Bitposition (0...n) inStates[0...n/8]. From 5825769356ccd7b197974f071613553e75143fc0 Mon Sep 17 00:00:00 2001 From: agsler Date: Wed, 9 Nov 2022 18:23:05 +0100 Subject: [PATCH 3/9] gefran --- main.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/main.c b/main.c index 7168eba..daaaba8 100644 --- a/main.c +++ b/main.c @@ -16,6 +16,8 @@ #include "spi.h" #include "mqtt.h" #include "temperature.h" +#include "modbus-master.h" +#include "gefran_gtf.h" #include "util/delay.h" @@ -159,6 +161,9 @@ int main() ioHelperSetBit(outStatesBlinking, LED_PLC_OK, 1); + ioHelperSetBit(outStates, OUT_PUMPE_STARTSTOP, 1); + ioHelperSetBit(outStates, OUT_PUMPE_PWM, 1); + uint32_t timer_blink_outs = millis(); uint32_t timer_send_uptime = millis(); @@ -180,6 +185,7 @@ int main() send_values(); #endif timer_blink_outs = millis(); + printf("gefran: 0x%02X\n", gtf_firmware_version(5)); } #if PLC_MQTT_ENABLED @@ -236,6 +242,8 @@ static void avr_init(void) uart_init(); initADC(); + modbus_master_init(); + sei(); //re-enable global interrupts return; From 79feed7a92a61041619d5ddd429bf928644c6b65 Mon Sep 17 00:00:00 2001 From: agsler Date: Thu, 10 Nov 2022 16:57:48 +0100 Subject: [PATCH 4/9] add more io defines --- avrIOhelper/io-helper.h | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/avrIOhelper/io-helper.h b/avrIOhelper/io-helper.h index b2e7874..eef18cb 100644 --- a/avrIOhelper/io-helper.h +++ b/avrIOhelper/io-helper.h @@ -68,6 +68,9 @@ void ioHelperEdgeDetector(void); #define BitPD6 22 //D22 #define BitPJ4 23 //D23 +#define BitPA0 24 //R0 +#define BitPA1 25 //R1 + #define LED_GRN_NOTAUS_ANLAGE BitPE4 #define LED_ROT_NOTAUS_ANLAGE BitPE5 @@ -92,8 +95,18 @@ void ioHelperEdgeDetector(void); #define BitPinK0 8 //A8 #define BitPinK1 9 //A9 -#define BitPinD3 10 //INO -#define BitPinD2 11 //IN1 +#define BitPinK2 10 //A10 +#define BitPinK3 11 //A11 +#define BitPinK4 12 //A12 +#define BitPinK5 13 //A13 +#define BitPinK6 14 //A14 +#define BitPinK7 15 //A15 + +#define BitPinD7 16 //I16 +#define BitPinG2 17 //I17 +#define BitPinG1 18 //I18 +#define BitPinD3 19 //INO +#define BitPinD2 20 //IN1 #define IN_ANLAGE_EIN_INV BitPinF5 #define IN_NOTAUS_ANLAGE BitPinF7 From ffe7c9507b34189f1c8e9d3c11908dedcd28a615 Mon Sep 17 00:00:00 2001 From: agsler Date: Thu, 10 Nov 2022 17:06:08 +0100 Subject: [PATCH 5/9] add send_value function --- Makefile | 3 ++- main.c | 29 +++++++++++------------------ mqtt.c | 22 ++++++++++++++++++++++ mqtt.h | 2 ++ 4 files changed, 37 insertions(+), 19 deletions(-) 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 From 411f2f59cdc989524b8b6eb71df257eb09d0077a Mon Sep 17 00:00:00 2001 From: agsler Date: Thu, 10 Nov 2022 17:09:47 +0100 Subject: [PATCH 6/9] add slave connect detection --- avrIOhelper/io-helper.h | 4 +++- main.c | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/avrIOhelper/io-helper.h b/avrIOhelper/io-helper.h index eef18cb..96b15d1 100644 --- a/avrIOhelper/io-helper.h +++ b/avrIOhelper/io-helper.h @@ -78,6 +78,8 @@ void ioHelperEdgeDetector(void); #define LED_ROT_NOTAUS_SCHRANK BitPE3 #define LED_PLC_OK BitPH5 #define LED_BUS_OK BitPD4 +#define LED_KALTWASSER_DRAN_GR 23 +#define LED_KALTWASSER_DRAN_RT 22 #define OUT_PUMPE_STARTSTOP BitPB5 #define OUT_PUMPE_PWM BitPB6 @@ -112,6 +114,6 @@ void ioHelperEdgeDetector(void); #define IN_NOTAUS_ANLAGE BitPinF7 #define IN_NOTAUS_SCHRANK BitPinK0 -#define IN_KLATWASSER_DRAN BitPinF6 +#define IN_KLATWASSER_DRAN 9 #endif diff --git a/main.c b/main.c index 1a27dff..72e714e 100644 --- a/main.c +++ b/main.c @@ -184,6 +184,17 @@ int main() timer_send_temps = millis(); send_temperatures(); printf("gefran: 0x%02X\n", gtf_firmware_version(5)); + + if(read_Input(IN_KLATWASSER_DRAN, LEVEL)){ + send_value(&mqtt_client, "/Filamentanlage/03_Wasserbecken/state/connected", 1); + set_Output(LED_KALTWASSER_DRAN_GR, 1); + set_Output(LED_KALTWASSER_DRAN_RT, 0); + } + else{ + send_value(&mqtt_client, "/Filamentanlage/03_Wasserbecken/state/connected", 0); + set_Output(LED_KALTWASSER_DRAN_GR, 0); + set_Output(LED_KALTWASSER_DRAN_RT, 1); + } } // send misc info From 1bd732d81221e984eadb55a406af334e1073b24b Mon Sep 17 00:00:00 2001 From: agsler Date: Thu, 10 Nov 2022 17:15:57 +0100 Subject: [PATCH 7/9] update main --- main.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/main.c b/main.c index 72e714e..d6cf6a5 100644 --- a/main.c +++ b/main.c @@ -155,14 +155,17 @@ int main() ioHelperSetBit(outStatesBlinking, LED_PLC_OK, 1); + //ioHelperSetBit(outStatesBlinking, OUT_VENTIL_ABLASS, 1); + //ioHelperSetBit(outStatesBlinking, OUT_VENTIL_PUMPE, 1); - ioHelperSetBit(outStates, OUT_PUMPE_STARTSTOP, 1); - ioHelperSetBit(outStates, OUT_PUMPE_PWM, 1); uint32_t timer_blink_outs = millis(); uint32_t timer_send_uptime = millis(); + uint32_t timer_send_temps = millis(); printf("anlage: %x\n\r", read_Input(IN_ANLAGE_EIN_INV, LEVEL)); + send_value(&mqtt_client, "/Filamentanlage/02_Wasserbecken/connected", 1); + send_value(&mqtt_client, "/Filamentanlage/02_Wasserbecken/state/pumpe_warm", -1); while(1) { @@ -174,6 +177,7 @@ int main() // Toggle all outs which are set to blinking if(millis() - timer_blink_outs > 500){ + timer_blink_outs = millis(); outStates[0] ^= outStatesBlinking[0]; outStates[1] ^= outStatesBlinking[1]; outStates[2] ^= outStatesBlinking[2]; @@ -203,14 +207,6 @@ int main() send_value(&mqtt_client, "/Filamentanlage/02_Wasserbecken/uptime", millis()/1000); } - if(read_Input(IN_ANLAGE_EIN_INV, FALLING)){ - printf("anlage ein\n\r"); - } - // ioHelperSetBit(outStates, LED_GRN_NOTAUS_SCHRANK, 1); - //} - //else{ - // ioHelperSetBit(outStates, LED_GRN_NOTAUS_SCHRANK, 0); - //} do_notaus(); #if PLC_MQTT_ENABLED @@ -239,7 +235,7 @@ static void avr_init(void) { // Initialize device here. // WatchDog INIT - wdt_enable(WDTO_8S); // set up wdt reset interval 2 second + wdt_enable(WDTO_2S); // set up wdt reset interval 2 second wdt_reset(); // wdt reset ~ every <2000ms timer0_init();// Timer0 millis engine init From e6e0a01b6c405472a06df6e46905cf4cb3437d0f Mon Sep 17 00:00:00 2001 From: agsler Date: Thu, 10 Nov 2022 17:16:29 +0100 Subject: [PATCH 8/9] add pump control --- pumpe.c | 24 ++++++++++++++++++++++++ pumpe.h | 6 ++++++ 2 files changed, 30 insertions(+) create mode 100644 pumpe.c create mode 100644 pumpe.h diff --git a/pumpe.c b/pumpe.c new file mode 100644 index 0000000..e064c1f --- /dev/null +++ b/pumpe.c @@ -0,0 +1,24 @@ +#include "avrIOhelper/io-helper.h" + +#include "mqtt.h" +extern Client mqtt_client; + +void do_pumpe(){ + if(read_Input(BTN_BECKEN_FUELLEN, RISING)){ + set_Output(OUT_PUMPE_STARTSTOP, 1); + set_Output(OUT_PUMPE_PWM, 1); + set_Output(OUT_VENTIL_ABLASS, 0); + set_Output(OUT_VENTIL_PUMPE, 0); + set_Output(LED_PUMPE_AN, 1); + send_value(&mqtt_client, "/Filamentanlage/02_Wasserbecken/state/pumpe_warm", 1); + } + + if(read_Input(BTN_BECKEN_LEEREN, RISING)){ + set_Output(OUT_PUMPE_STARTSTOP, 0); + set_Output(OUT_PUMPE_PWM, 0); + set_Output(OUT_VENTIL_ABLASS, 1); + set_Output(OUT_VENTIL_PUMPE, 1); + set_Output(LED_PUMPE_AN, 0); + send_value(&mqtt_client, "/Filamentanlage/02_Wasserbecken/state/pumpe_warm", 0); + } +} diff --git a/pumpe.h b/pumpe.h new file mode 100644 index 0000000..f7b3518 --- /dev/null +++ b/pumpe.h @@ -0,0 +1,6 @@ +#ifndef _PUMPE_H_ +#define _PUMPE_H_ + +void do_pumpe(void); + +#endif From 246cc875a1acd011eda4186ec66ca0a35f61ec00 Mon Sep 17 00:00:00 2001 From: agsler Date: Thu, 10 Nov 2022 17:16:58 +0100 Subject: [PATCH 9/9] control pump --- avrIOhelper/io-helper.h | 8 ++++++++ main.c | 2 ++ 2 files changed, 10 insertions(+) diff --git a/avrIOhelper/io-helper.h b/avrIOhelper/io-helper.h index 96b15d1..5457c33 100644 --- a/avrIOhelper/io-helper.h +++ b/avrIOhelper/io-helper.h @@ -81,9 +81,14 @@ void ioHelperEdgeDetector(void); #define LED_KALTWASSER_DRAN_GR 23 #define LED_KALTWASSER_DRAN_RT 22 +#define LED_PUMPE_AN 11 + #define OUT_PUMPE_STARTSTOP BitPB5 #define OUT_PUMPE_PWM BitPB6 +#define OUT_VENTIL_PUMPE BitPA0 +#define OUT_VENTIL_ABLASS BitPA1 + //Inputs //Verknüpfen von Pin | Bit mit Bitposition (0...n) inStates[0...n/8]. #define BitPinF0 0 //A0 @@ -116,4 +121,7 @@ void ioHelperEdgeDetector(void); #define IN_KLATWASSER_DRAN 9 +#define BTN_BECKEN_FUELLEN 11 +#define BTN_BECKEN_LEEREN 12 + #endif diff --git a/main.c b/main.c index d6cf6a5..2f49157 100644 --- a/main.c +++ b/main.c @@ -18,6 +18,7 @@ #include "temperature.h" #include "modbus-master.h" #include "gefran_gtf.h" +#include "pumpe.h" #include "util/delay.h" @@ -207,6 +208,7 @@ int main() send_value(&mqtt_client, "/Filamentanlage/02_Wasserbecken/uptime", millis()/1000); } + do_pumpe(); do_notaus(); #if PLC_MQTT_ENABLED