Merge branch 'master' of ssh://eggertjung.de:222/Filamentstr/02_Wasserbecken
This commit is contained in:
6
Makefile
6
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)
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
76
main.c
76
main.c
@@ -1,6 +1,7 @@
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <avr/wdt.h> // WatchDog
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#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<sizeof(ADC_reading)/sizeof(ADC_reading[0]);i++){
|
||||
char tpc[64];
|
||||
sprintf(tpc, "/Filamentanlage/02_Wasserbecken/state/temp%d", i);
|
||||
send_value_fl(&mqtt_client, tpc, ADC_reading[i], 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// INIT MCU
|
||||
@@ -189,20 +201,26 @@ int main()
|
||||
while(1); //Reboot the board
|
||||
}
|
||||
|
||||
// Subscribe to all topics
|
||||
char SubString[] = "/Filamentanlage/02_Wasserbecken/#";
|
||||
//char SubString[] = "/Filamentanlage/03_Wasserbecken/#";
|
||||
mqtt_rc = MQTTSubscribe(&mqtt_client, SubString, QOS0, messageArrived);
|
||||
printf("Subscribed (%s) %ld\r\n", SubString, mqtt_rc);
|
||||
//// Subscribe to all topics
|
||||
//char SubString[] = "/Filamentanlage/02_Wasserbecken/#";
|
||||
////char SubString[] = "/Filamentanlage/03_Wasserbecken/#";
|
||||
//mqtt_rc = MQTTSubscribe(&mqtt_client, SubString, QOS0, messageArrived);
|
||||
//printf("Subscribed (%s) %ld\r\n", SubString, mqtt_rc);
|
||||
#endif
|
||||
|
||||
|
||||
ioHelperSetBit(outStatesBlinking, LED_PLC_OK, 1);
|
||||
//ioHelperSetBit(outStatesBlinking, OUT_VENTIL_ABLASS, 1);
|
||||
//ioHelperSetBit(outStatesBlinking, OUT_VENTIL_PUMPE, 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)
|
||||
{
|
||||
@@ -214,30 +232,37 @@ 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];
|
||||
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
|
||||
|
||||
|
||||
22
mqtt.c
22
mqtt.c
@@ -1,3 +1,5 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.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
|
||||
}
|
||||
|
||||
2
mqtt.h
2
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
|
||||
|
||||
24
pumpe.c
Normal file
24
pumpe.c
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
6
pumpe.h
Normal file
6
pumpe.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef _PUMPE_H_
|
||||
#define _PUMPE_H_
|
||||
|
||||
void do_pumpe(void);
|
||||
|
||||
#endif
|
||||
33
temperature.c
Normal file
33
temperature.c
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <stdint.h>
|
||||
|
||||
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
|
||||
}
|
||||
9
temperature.h
Normal file
9
temperature.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef _TEMPERATURE_H_
|
||||
#define _TEMPERATURE_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
extern volatile float ADC_reading[5];
|
||||
void initADC(void);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user