#include #include #include // WatchDog #include #include #include #include "Ethernet/socket.h" #include "Ethernet/wizchip_conf.h" #include "Internet/MQTT/mqtt_interface.h" #include "Internet/MQTT/MQTTClient.h" #include "avrIOhelper/io-helper.h" #include "kraftsensor.h" #include "millis.h" #include "uart.h" #include "spi.h" #include "mqtt.h" #include "modbus.h" #include "modbus-master.h" #include "modbus_io_slave.h" #include "gefran_gtf.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 uint8_t mcucsr_mirror __attribute__ ((section (".noinit"))); // This is for fast WDT disable & and save reason of reset/power-up void get_mcusr(void) \ __attribute__((naked)) \ __attribute__((section(".init3"))); void get_mcusr(void) { mcucsr_mirror = MCUSR; MCUSR = 0; wdt_disable(); } //***********Prologue for fast WDT disable & and save reason of reset/power-up: END //FUNC headers static void avr_init(void); void timer0_init(void); void print_network_information(void); volatile uint16_t ADC_reading = 0; void IO_LIBRARY_Init(void) { uint8_t bufSize[] = {2, 2, 2, 2, 2, 2, 2, 2}; reg_wizchip_cs_cbfunc(spi_select, spi_deselect); reg_wizchip_spi_cbfunc(spi_read, spi_write); //reg_wizchip_spiburst_cbfunc(spi_rb_burst, spi_wb_burst); wizchip_init(bufSize, bufSize); wizchip_setnetinfo(&netInfo); //wizchip_setinterruptmask(IK_SOCK_0); } void do_notaus(){ if(read_Input(IN_NOTAUS_ANLAGE, LEVEL) || read_Input(IN_NOTAUS_SCHRANK, LEVEL) || read_Input(IN_NOTAUS_ANLAGE_R, LEVEL)){ /* at least one pressed */ //notaus_state = POWER_OFF; ioHelperSetBit(outStates, LED_AMPEL_ROT, 1); ioHelperSetBit(outStates, LED_AMPEL_GELB, 0); ioHelperSetBit(outStates, LED_AMPEL_GRUEN, 0); set_Output(LED_GRN_NOTAUS_SCHRANK, OFF); set_Output(LED_GRN_NOTAUS_ANLAGE, OFF); set_Output(LED_GRN_NOTAUS_ANLAGE_R, OFF); if(read_Input(IN_NOTAUS_ANLAGE, LEVEL)){ set_Output(LED_ROT_NOTAUS_ANLAGE, BLINK); set_Output(LED_ROT_NOTAUS_SCHRANK, ON); set_Output(LED_ROT_NOTAUS_ANLAGE_R, ON); } if(read_Input(IN_NOTAUS_SCHRANK, LEVEL)){ set_Output(LED_ROT_NOTAUS_ANLAGE, ON); set_Output(LED_ROT_NOTAUS_SCHRANK, BLINK); set_Output(LED_ROT_NOTAUS_ANLAGE_R, ON); } if(read_Input(IN_NOTAUS_ANLAGE_R, LEVEL)){ set_Output(LED_ROT_NOTAUS_ANLAGE, ON); set_Output(LED_ROT_NOTAUS_SCHRANK, ON); set_Output(LED_ROT_NOTAUS_ANLAGE_R, BLINK); } } else if(!read_Input(IN_ANLAGE_EIN_INV, LEVEL)){ /* nothing pressed, but power not on */ //notaus_state = ARMED; ioHelperSetBit(outStates, LED_AMPEL_ROT, 0); ioHelperSetBit(outStates, LED_AMPEL_GELB, 1); ioHelperSetBit(outStates, LED_AMPEL_GRUEN, 0); set_Output(LED_ROT_NOTAUS_ANLAGE, ON); set_Output(LED_ROT_NOTAUS_SCHRANK, ON); set_Output(LED_ROT_NOTAUS_ANLAGE_R, ON); set_Output(LED_GRN_NOTAUS_SCHRANK, ON); set_Output(LED_GRN_NOTAUS_ANLAGE, ON); set_Output(LED_GRN_NOTAUS_ANLAGE_R, ON); } else{ /* powered on */ //notaus_state = POWER_ON; ioHelperSetBit(outStates, LED_AMPEL_ROT, 0); ioHelperSetBit(outStates, LED_AMPEL_GELB, 0); ioHelperSetBit(outStates, LED_AMPEL_GRUEN, 1); set_Output(LED_GRN_NOTAUS_SCHRANK, ON); set_Output(LED_GRN_NOTAUS_ANLAGE, ON); set_Output(LED_GRN_NOTAUS_ANLAGE_R, ON); set_Output(LED_ROT_NOTAUS_ANLAGE, OFF); set_Output(LED_ROT_NOTAUS_SCHRANK, OFF); set_Output(LED_ROT_NOTAUS_ANLAGE_R, OFF); } } void send_values(void){ char msg[10]; sprintf(msg, "%d", ADC_reading); mqtt_pub(&mqtt_client, "/Filamentanlage/01_Extruder/state/temp", msg, strlen(msg)); } int main(){ // INIT MCU avr_init(); spi_init(); //SPI Master, MODE0, 4Mhz(DIV4), CS_PB.3=HIGH - suitable for WIZNET 5x00(1/2/5) ioHelperInitBuffer(); ioHelperIoConf(); //Wizchip WIZ5500 Ethernet initialize IO_LIBRARY_Init(); //After that ping must working print_network_information(); #if PLC_MQTT_ENABLED //****************MQTT client initialize //Find MQTT broker and connect with it uint8_t mqtt_buf[100]; int32_t mqtt_rc = 0; Network mqtt_network; mqtt_network.my_socket = SOCK_MQTT; printf(">>Trying connect to MQTT broker: %d.%d.%d.%d ..\r\n", MQTT_targetIP[0], MQTT_targetIP[1], MQTT_targetIP[2], MQTT_targetIP[3]); NewNetwork(&mqtt_network); ConnectNetwork(&mqtt_network, MQTT_targetIP, 1883); MQTTClient(&mqtt_client, &mqtt_network, 1000, mqtt_buf, 100, mqtt_readBuffer, MQTT_BUFFER_SIZE); //Connection to MQTT broker MQTTPacket_connectData data = MQTTPacket_connectData_initializer; data.willFlag = 0; data.MQTTVersion = 4;//3; data.clientID.cstring = (char*)"controllino_wasserbecken"; data.username.cstring = (char*)"Wasserbecken"; data.password.cstring = (char*)"\0"; data.keepAliveInterval = 10; data.cleansession = 1; mqtt_rc = MQTTConnect(&mqtt_client, &data); if (mqtt_rc == SUCCESSS) { printf("++MQTT Connected SUCCESS: %ld\r\n", mqtt_rc); } else { printf("--MQTT Connected ERROR: %ld\r\n", mqtt_rc); while(1); //Reboot the board } // Subscribe to all topics char SubString[] = "/Filamentanlage/01_Extruder/set/#"; //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(outStates, BitPG5, 1); uint32_t timer_blink_outs = millis(); uint32_t timer_read_slave = millis(); #ifdef PLC_MQTT_ENABLED uint32_t timer_send_info = millis(); uint32_t timer_send_uptime = millis(); #endif //printf("anlage: %x\n\r", read_Input(IN_ANLAGE_EIN_INV, LEVEL)); gtf_write_register(5, 54, 1); gtf_write_register(5, 55, _BV(4)); gtf_write_register(5, 56, 100); while(1) { wdt_reset(); // WDT reset at least every sec ioHelperReadPins(); ioHelperDebounce(); ioHelperEdgeDetector(); // Toggle all outs which are set to blinking if(millis() - timer_blink_outs > 500){ outStates[0] ^= outStatesBlinking[0]; outStates[1] ^= outStatesBlinking[1]; outStates[2] ^= outStatesBlinking[2]; outStates[3] ^= outStatesBlinking[3]; timer_blink_outs = millis(); do_kraftsensor(); #if PLC_MQTT_ENABLED char msg[64]; sprintf(msg, "%ld", kraftsensor_value); mqtt_pub(&mqtt_client, "/Filamentanlage/01_Extruder/kraft", msg, strlen(msg)); #endif printf("gefran 54: 0x%02X\n", gtf_read_register(5, 54)); printf("gefran 55: 0x%02X\n", gtf_read_register(5, 55)); printf("gefran 56: 0x%02X\n", gtf_read_register(5, 56)); printf("gefran pv: 0x%02X\n", gtf_read_register(5, GTF_PV)); printf("gefran oup: 0x%02X\n", gtf_read_register(5, GTF_OuP)); printf("\n"); } if(millis() - timer_read_slave > 200){ timer_read_slave = millis(); modbus_io_read(); if(modbus_io_read_Input(16, RISING)) modbus_io_set_Output(4, 3, ON); if(modbus_io_read_Input(17, RISING)) modbus_io_set_Output(4, 3, OFF); } if(read_Input(BTN_HEIZEN_AN, RISING)){ modbus_io_set_Output(4, MOD_OUT_HEIZEN_LED, 1); } if(read_Input(BTN_HEIZEN_AUS, RISING)){ modbus_io_set_Output(4, MOD_OUT_HEIZEN_LED, 0); } if(read_Input(BTN_LIFT_UP, LEVEL)){ set_Output(LIFT_MOTOR_DIR, 1); TCCR3B |= _BV(CS30); } else if(read_Input(BTN_LIFT_DOWN, LEVEL)){ set_Output(LIFT_MOTOR_DIR, 0); TCCR3B |= _BV(CS30); } else TCCR3B &= ~_BV(CS30); if(read_Input(BTN_EXTR_ON, RISING)){ TCCR1B |= _BV(CS11); modbus_io_set_Output(4, MOD_OUT_EXTR_LED, 1); } if(read_Input(BTN_EXTR_OFF, RISING)){ TCCR1B &= ~_BV(CS11); modbus_io_set_Output(4, MOD_OUT_EXTR_LED, 0); } #if PLC_MQTT_ENABLED // send misc info if(millis() - timer_send_uptime > 5000){ timer_send_uptime = millis(); char msg[64]; //sprintf(msg, "%ld", millis()/1000); mqtt_pub(&mqtt_client, "/Filamentanlage/01_Extruder/uptime", msg, strlen(msg)); } #endif #if PLC_MQTT_ENABLED // send misc info if(millis() - timer_send_info > 200){ timer_send_info += 200; send_values(); // 10ms //send_info(); // 27ms every 200ms } #endif do_notaus(); #if PLC_MQTT_ENABLED ioHelperSetBit(outStates, LED_BUS_OK, 1); ioHelperSetOuts(); MQTTYield(&mqtt_client, 10); //blocking call ioHelperSetBit(outStates, LED_BUS_OK, 0); #endif ioHelperSetOuts(); } } // Timer0 // 1ms IRQ // Used for millis() timing void timer0_init(void) { TCCR0A = (1<250kHz:250-=>1kHz) TIMSK0 |= 1<