diff --git a/Makefile b/Makefile index e5279be..8908b99 100644 --- a/Makefile +++ b/Makefile @@ -10,10 +10,12 @@ 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 +#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 d556661..6a1c9e2 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 @@ -78,7 +81,16 @@ void ioHelperEdgeDetector(void); #define LED_PLC_OK BitPH5 #define LED_BUS_OK BitPD4 +#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]. @@ -106,10 +118,14 @@ void ioHelperEdgeDetector(void); #define BitPinD3 19 //INO #define BitPinD2 20 //IN1 - #define IN_ANLAGE_EIN_INV BitPinF5 #define IN_NOTAUS_ANLAGE BitPinF7 #define IN_NOTAUS_ANLAGE_KALT BitPinK1 #define IN_NOTAUS_SCHRANK BitPinK0 +#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 dc8a879..416f67b 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,7 @@ #include #include #include // WatchDog +#include #include #include "Ethernet/socket.h" @@ -14,11 +15,13 @@ #include "uart.h" #include "spi.h" #include "mqtt.h" +#include "temperature.h" +#include "modbus-master.h" +#include "gefran_gtf.h" +#include "pumpe.h" #include "util/delay.h" -#define PLC_MQTT_ENABLED 0 - Client mqtt_client; //***********Prologue for fast WDT disable & and save reason of reset/power-up: BEGIN @@ -142,6 +145,15 @@ void do_notaus(){ // } //} +void send_temperatures(void){ + for(uint8_t i=0; i 500){ + timer_blink_outs = millis(); outStates[0] ^= outStatesBlinking[0]; outStates[1] ^= outStatesBlinking[1]; - 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(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); + } } -#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"); - } - // ioHelperSetBit(outStates, LED_GRN_NOTAUS_SCHRANK, 1); - //} - //else{ - // ioHelperSetBit(outStates, LED_GRN_NOTAUS_SCHRANK, 0); - //} + do_pumpe(); do_notaus(); #if PLC_MQTT_ENABLED @@ -266,11 +291,14 @@ 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 uart_init(); + initADC(); + + modbus_master_init(); sei(); //re-enable global interrupts 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 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 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