297 lines
		
	
	
		
			9.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			297 lines
		
	
	
		
			9.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
#include <avr/io.h>
 | 
						|
#include <avr/interrupt.h>
 | 
						|
#include <avr/wdt.h> // WatchDog
 | 
						|
#include <string.h>
 | 
						|
#include <stdlib.h>
 | 
						|
 | 
						|
#include "common.h"
 | 
						|
 | 
						|
#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 "millis.h"
 | 
						|
#include "uart.h"
 | 
						|
#include "spi.h"
 | 
						|
#include "mqtt.h"
 | 
						|
 | 
						|
#include "util/delay.h"
 | 
						|
 | 
						|
#include "kraftsensor.h"
 | 
						|
#include "taenzer.h"
 | 
						|
#include "abzug.h"
 | 
						|
 | 
						|
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 timer3_init(void);
 | 
						|
void print_network_information(void);
 | 
						|
 | 
						|
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);
 | 
						|
}
 | 
						|
 | 
						|
static void avr_init()
 | 
						|
{
 | 
						|
    // Initialize device here.
 | 
						|
    // WatchDog INIT
 | 
						|
    wdt_enable(WDTO_8S);  // set up wdt reset interval 2 second
 | 
						|
    wdt_reset(); // wdt reset ~ every <2000ms
 | 
						|
 | 
						|
    timer0_init();// Timer0 millis engine init
 | 
						|
    //timer3_init();
 | 
						|
    uart_init();
 | 
						|
 | 
						|
    sei(); //re-enable global interrupts
 | 
						|
 | 
						|
    return;
 | 
						|
}
 | 
						|
 | 
						|
void send_abzug_speed(void){
 | 
						|
    char msg[4];
 | 
						|
    sprintf(msg, "%d", abzug_speed);
 | 
						|
    mqtt_pub(&mqtt_client, "/Filamentanlage/05_Abzug/state/abzug/speed", msg, strlen(msg));
 | 
						|
 | 
						|
    if(TCCR3B & (1<<CS31))
 | 
						|
        sprintf(msg, "True");
 | 
						|
    else
 | 
						|
        sprintf(msg, "False");
 | 
						|
    mqtt_pub(&mqtt_client, "/Filamentanlage/05_Abzug/state/abzug/onoff", msg, strlen(msg));
 | 
						|
}
 | 
						|
 | 
						|
void do_notaus(){
 | 
						|
    if(!read_Input(IN_NOTAUS_ANLAGE, LEVEL) || read_Input(IN_NOTAUS_SCHRANK, LEVEL) || read_Input(IN_NOTAUS_DISPLAY, LEVEL)){
 | 
						|
        /* at least on pressed */
 | 
						|
        ioHelperSetBit(outStates, AMPEL_ROT,  1);
 | 
						|
        ioHelperSetBit(outStates, AMPEL_GELB,  0);
 | 
						|
        ioHelperSetBit(outStates, AMPEL_GRUEN,  0);
 | 
						|
 | 
						|
        set_Output(LED_GRN_NOTAUS_SCHRANK,   OFF);
 | 
						|
        set_Output(LED_GRN_NOTAUS_ANLAGE,    OFF);
 | 
						|
        set_Output(LED_GRN_NOTAUS_DISPLAY,   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_DISPLAY,   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_DISPLAY,   ON);
 | 
						|
        }
 | 
						|
 | 
						|
        if(read_Input(IN_NOTAUS_DISPLAY, LEVEL)){
 | 
						|
            set_Output(LED_ROT_NOTAUS_ANLAGE,    ON);
 | 
						|
            set_Output(LED_ROT_NOTAUS_SCHRANK,   ON);
 | 
						|
            set_Output(LED_ROT_NOTAUS_DISPLAY, BLINK);
 | 
						|
        }
 | 
						|
 | 
						|
    }
 | 
						|
    else if(!read_Input(IN_ANLAGE_EIN, LEVEL)){
 | 
						|
        /* nothing pressed, but power not on */
 | 
						|
        ioHelperSetBit(outStates, AMPEL_ROT,  0);
 | 
						|
        ioHelperSetBit(outStates, AMPEL_GELB,  1);
 | 
						|
        ioHelperSetBit(outStates, AMPEL_GRUEN,  0);
 | 
						|
 | 
						|
        set_Output(LED_ROT_NOTAUS_ANLAGE,  ON);
 | 
						|
        set_Output(LED_ROT_NOTAUS_SCHRANK, ON);
 | 
						|
        set_Output(LED_ROT_NOTAUS_DISPLAY, ON);
 | 
						|
 | 
						|
        set_Output(LED_GRN_NOTAUS_SCHRANK, ON);
 | 
						|
        set_Output(LED_GRN_NOTAUS_ANLAGE,  ON);
 | 
						|
        set_Output(LED_GRN_NOTAUS_DISPLAY, ON);
 | 
						|
    }
 | 
						|
    else{
 | 
						|
        /* powered on */
 | 
						|
        ioHelperSetBit(outStates, AMPEL_ROT,  0);
 | 
						|
        ioHelperSetBit(outStates, AMPEL_GELB,  0);
 | 
						|
        ioHelperSetBit(outStates, AMPEL_GRUEN,  1);
 | 
						|
 | 
						|
        set_Output(LED_GRN_NOTAUS_SCHRANK,   ON);
 | 
						|
        set_Output(LED_GRN_NOTAUS_ANLAGE,    ON);
 | 
						|
        set_Output(LED_GRN_NOTAUS_DISPLAY,   ON);
 | 
						|
 | 
						|
        set_Output(LED_ROT_NOTAUS_ANLAGE,    OFF);
 | 
						|
        set_Output(LED_ROT_NOTAUS_SCHRANK,   OFF);
 | 
						|
        set_Output(LED_ROT_NOTAUS_DISPLAY,   OFF);
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
int main()
 | 
						|
{
 | 
						|
    // INIT MCU
 | 
						|
    avr_init();
 | 
						|
    spi_init(); //SPI Master, MODE0, 4Mhz(DIV4), CS_PB.3=HIGH - suitable for WIZNET 5x00(1/2/5)
 | 
						|
    kraftsensor_init();
 | 
						|
 | 
						|
    printf("moin!\n\r");
 | 
						|
    timer3_init();
 | 
						|
    timer4_init();
 | 
						|
 | 
						|
    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";
 | 
						|
    data.username.cstring = (char*)"Messmodul";
 | 
						|
    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
 | 
						|
    }
 | 
						|
 | 
						|
    char SubString[] = "/Filamentanlage/04_Messmodul/set/#";
 | 
						|
    mqtt_rc = MQTTSubscribe(&mqtt_client, SubString, QOS0, messageArrived);
 | 
						|
    printf("Subscribed (%s) %ld\r\n", SubString, mqtt_rc);
 | 
						|
#endif
 | 
						|
 | 
						|
    ioHelperSetBit(outStates, RELAY_INTERLOCK,  1);
 | 
						|
 | 
						|
    ioHelperSetBit(outStatesBlinking,  LED_PLC_OK,  1);
 | 
						|
 | 
						|
    uint32_t timer_blink_outs = millis();
 | 
						|
    uint32_t timer_send_uptime = millis();
 | 
						|
    uint32_t timer_modbus_poll = millis();
 | 
						|
 | 
						|
    while(1)
 | 
						|
    {
 | 
						|
        wdt_reset(); // WDT reset at least every sec
 | 
						|
 | 
						|
#if PLC_MQTT_ENABLED
 | 
						|
        if(millis() < 10)
 | 
						|
            send_abzug_speed();
 | 
						|
#endif
 | 
						|
 | 
						|
        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();
 | 
						|
        }
 | 
						|
 | 
						|
#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/04_Messmodul/state/uptime", msg, strlen(msg));
 | 
						|
        //}
 | 
						|
#endif
 | 
						|
        if(millis() - timer_modbus_poll > 200){
 | 
						|
            timer_modbus_poll += 200;
 | 
						|
            do_kraftsensor();
 | 
						|
#if PLC_MQTT_ENABLED
 | 
						|
            send_abzug_speed();
 | 
						|
            char msg[10];
 | 
						|
            ltoa(kraftsensor_value, msg, 10);
 | 
						|
            mqtt_pub(&mqtt_client, "/Filamentanlage/05_Abzug/state/kraft", msg, strlen(msg));
 | 
						|
            ltoa(taenzer_state.pos, msg, 10);
 | 
						|
            mqtt_pub(&mqtt_client, "/Filamentanlage/05_Abzug/state/speicher/pos", msg, strlen(msg));
 | 
						|
#endif
 | 
						|
        }
 | 
						|
 | 
						|
        do_notaus();
 | 
						|
        do_rolle();
 | 
						|
        do_taenzer();
 | 
						|
 | 
						|
#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();
 | 
						|
    }
 | 
						|
    return 0;
 | 
						|
}
 | 
						|
 | 
						|
void print_network_information()
 | 
						|
{
 | 
						|
 | 
						|
    uint8_t tmpstr[6] = {0,};
 | 
						|
    ctlwizchip(CW_GET_ID,(void*)tmpstr); // Get WIZCHIP name
 | 
						|
    printf("\r\n=======================================\r\n");
 | 
						|
    printf(" WIZnet chip:  %s \r\n", tmpstr);
 | 
						|
    printf("=======================================\r\n");
 | 
						|
 | 
						|
    wiz_NetInfo gWIZNETINFO;
 | 
						|
    wizchip_getnetinfo(&gWIZNETINFO);
 | 
						|
    if (gWIZNETINFO.dhcp == NETINFO_STATIC)
 | 
						|
        printf("STATIC IP\r\n");
 | 
						|
    else
 | 
						|
        printf("DHCP IP\r\n");
 | 
						|
    printf("Mac address: %02x:%02x:%02x:%02x:%02x:%02x\n\r",gWIZNETINFO.mac[0],gWIZNETINFO.mac[1],gWIZNETINFO.mac[2],gWIZNETINFO.mac[3],gWIZNETINFO.mac[4],gWIZNETINFO.mac[5]);
 | 
						|
    printf("IP address : %d.%d.%d.%d\n\r",gWIZNETINFO.ip[0],gWIZNETINFO.ip[1],gWIZNETINFO.ip[2],gWIZNETINFO.ip[3]);
 | 
						|
    printf("SM Mask	   : %d.%d.%d.%d\n\r",gWIZNETINFO.sn[0],gWIZNETINFO.sn[1],gWIZNETINFO.sn[2],gWIZNETINFO.sn[3]);
 | 
						|
    printf("Gate way   : %d.%d.%d.%d\n\r",gWIZNETINFO.gw[0],gWIZNETINFO.gw[1],gWIZNETINFO.gw[2],gWIZNETINFO.gw[3]);
 | 
						|
    printf("DNS Server : %d.%d.%d.%d\n\r",gWIZNETINFO.dns[0],gWIZNETINFO.dns[1],gWIZNETINFO.dns[2],gWIZNETINFO.dns[3]);
 | 
						|
}
 | 
						|
 | 
						|
 |