diff --git a/can/can_client.c b/can/can_client.c index 239ddca..a33b7be 100755 --- a/can/can_client.c +++ b/can/can_client.c @@ -12,8 +12,15 @@ struct CAN_INTERFACE_DATA intf_data[MOTOR_COUNT]; /// @return int Can_OpenInterface(int iMotorIndex, const char * ifacename) { + // Init control data + motctrl[iMotorIndex].iActualMotorPowerW = 0; + motctrl[iMotorIndex].iMotorGear = MOTOR_GEAR_NEUTRAL; + motctrl[iMotorIndex].iMotorPower = 0; + motctrl[iMotorIndex].iMotorPowerPct = 0; + motctrl[iMotorIndex].nSwitchState = 0; + strcpy(intf_data[iMotorIndex].iface_name, ifacename); - Can_SetMotorGear(iMotorIndex, 0); + Can_SetMotorGear(iMotorIndex, MOTOR_GEAR_NEUTRAL); Can_SetMotorPower(iMotorIndex, 0); // first we have to create a socket @@ -232,12 +239,14 @@ void Can_ReadData(int iMotorIndex) /// @param frame void Can_Read_Manu_PGN(int iMotorIndex, struct can_frame *frame) { - + motctrl[iMotorIndex].nSwitchState = frame->data[2]; + MqttClient_Publish_MotorSwitchState(iMotorIndex, motctrl[iMotorIndex].nSwitchState); } /// @brief Read PGN 65300 /// @param frame void Can_Read_Manu_PGN2(int iMotorIndex, struct can_frame *frame) { - + motctrl[iMotorIndex].iActualMotorPowerW = (frame->data[3] << 16) | (frame->data[4] << 8) | frame->data[5]; + MqttClient_Publish_MotorActualPowerW(iMotorIndex, motctrl[iMotorIndex].iActualMotorPowerW); } diff --git a/can/can_client.h b/can/can_client.h index 118cebd..5f237b3 100755 --- a/can/can_client.h +++ b/can/can_client.h @@ -30,6 +30,8 @@ struct MOTOR_CONTROL_DATA int iMotorGear; int iMotorPower; int iMotorPowerPct; + unsigned char nSwitchState; + int iActualMotorPowerW; }; extern struct MOTOR_CONTROL_DATA motctrl[MOTOR_COUNT]; diff --git a/io/io.c b/io/io.c index a2c6ef9..2ea7569 100755 --- a/io/io.c +++ b/io/io.c @@ -21,16 +21,7 @@ int IO_Init() // IO-Pins für Ausgänge konfigurieren SetupOutputPin(GPIO_LED_MOTRUN); - - // Einschaltsequenz - WriteOutputPin(GPIO_LED_MOTRUN, HIGH); - delay(500); - WriteOutputPin(GPIO_LED_MOTRUN, LOW); - delay(500); - WriteOutputPin(GPIO_LED_MOTRUN, HIGH); - delay(500); - WriteOutputPin(GPIO_LED_MOTRUN, LOW); - delay(500); + SetupOutputPin(GPIO_OUT_PWRON); printf("IO initialized successfull!\n"); @@ -164,7 +155,6 @@ void WriteOutputPin(int iOutPin, int iValue) } } - void IO_DoCyclic() { ReadKey(&gpioKeyStop); diff --git a/io/io.h b/io/io.h index 418586a..c420ab6 100755 --- a/io/io.h +++ b/io/io.h @@ -5,6 +5,7 @@ #include #define GPIO_LED_MOTRUN 17 // GPIO Pin fuer LED Motor läuft +#define GPIO_OUT_PWRON 22 // GPIO Pin für Relais "Zündschlüssel" #define GPIO_KEY_STOP 26 // GPIO Pin fuer Taster Stop #define GPIO_KEY_PWRUP 5 // GPIO Pin fuer Taster Leistung-Erhöhen diff --git a/main.c b/main.c index 483a508..705942b 100755 --- a/main.c +++ b/main.c @@ -129,6 +129,9 @@ void *thread_func(void *data) // initialize cyclic task periodic_task_init(1, &pinfo); + // "Zündung" ein + WriteOutputPin(GPIO_OUT_PWRON, 1); + // cyclic call of do_cyclic_1ms() while (iThreadControl == 0) { diff --git a/mqtt/mqtt_client.c b/mqtt/mqtt_client.c index b9b1313..30d1655 100755 --- a/mqtt/mqtt_client.c +++ b/mqtt/mqtt_client.c @@ -24,6 +24,15 @@ int iMqttMotor2Gear = 0; const char* mqtt_topic_motor2_power = "Pool/Motor2/Power"; int iMqttMotor2Power = 0; +const char* mqtt_topic_motor1_switchstate = "Pool/Motor1/SwitchState"; +unsigned char nMqttMotor1SwitchState = 0; +const char* mqtt_topic_motor2_switchstate = "Pool/Motor1/SwitchState"; +unsigned char nMqttMotor2SwitchState = 0; +const char* mqtt_topic_motor1_actualpowerw = "Pool/Motor1/ActualPowerW"; +int iMqttMotor1ActualPowerW = 0; +const char* mqtt_topic_motor2_actualpowerw = "Pool/Motor1/ActualPowerW"; +int iMqttMotor2ActualPowerW = 0; + const char* mqtt_broker_addr = "127.0.0.1"; const int mqtt_broker_port = 1883; struct mosquitto *mosq; /**< Libmosquito MQTT client instance. */ @@ -278,4 +287,53 @@ void MqttClient_Publish_MotorPower(int iMotorIndex, int iPower) mosquitto_publish(mosq, NULL, mqtt_topic_motor2_power, strlen(message), message, 0, false); } } +} + +void MqttClient_Publish_MotorSwitchState(int iMotorIndex, unsigned char nSwitchState) +{ + if (iMotorIndex == 0) + { + if (nSwitchState != nMqttMotor1SwitchState) + { + nMqttMotor1SwitchState = nSwitchState; + char message[100]; + snprintf(message, sizeof(message), "%d", nSwitchState); + mosquitto_publish(mosq, NULL, mqtt_topic_motor1_switchstate, strlen(message), message, 0, false); + } + } + else if (iMotorIndex == 1) + { + if (nSwitchState != nMqttMotor2SwitchState) + { + nMqttMotor2SwitchState = nSwitchState; + char message[100]; + snprintf(message, sizeof(message), "%d", nSwitchState); + mosquitto_publish(mosq, NULL, mqtt_topic_motor2_switchstate, strlen(message), message, 0, false); + } + } +} + + +void MqttClient_Publish_MotorActualPowerW(int iMotorIndex, int iMotorPowerW) +{ + if (iMotorIndex == 0) + { + if (iMotorPowerW != iMqttMotor1ActualPowerW) + { + iMqttMotor1ActualPowerW = iMotorPowerW; + char message[100]; + snprintf(message, sizeof(message), "%d", iMotorPowerW); + mosquitto_publish(mosq, NULL, mqtt_topic_motor1_actualpowerw, strlen(message), message, 0, false); + } + } + else if (iMotorIndex == 1) + { + if (iMotorPowerW != iMqttMotor2ActualPowerW) + { + iMqttMotor2ActualPowerW = iMotorPowerW; + char message[100]; + snprintf(message, sizeof(message), "%d", iMotorPowerW); + mosquitto_publish(mosq, NULL, mqtt_topic_motor2_actualpowerw, strlen(message), message, 0, false); + } + } } \ No newline at end of file diff --git a/mqtt/mqtt_client.h b/mqtt/mqtt_client.h index cecee53..3a35a01 100755 --- a/mqtt/mqtt_client.h +++ b/mqtt/mqtt_client.h @@ -9,5 +9,7 @@ void MqttClient_Refresher(); void MqttClient_Publisher(); void MqttClient_Publish_MotorGear(int iMotorIndex, int iGear); void MqttClient_Publish_MotorPower(int iMotorIndex, int iPower); +void MqttClient_Publish_MotorSwitchState(int iMotorIndex, unsigned char nSwitchState); +void MqttClient_Publish_MotorActualPowerW(int iMotorIndex, int iMotorPowerW); #endif