Zustünde Switch und Power publishen

This commit is contained in:
Bernhard
2025-12-08 19:36:05 +01:00
parent 48d3251247
commit b10547e070
7 changed files with 79 additions and 14 deletions

View File

@@ -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);
}

View File

@@ -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];

12
io/io.c
View File

@@ -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);

View File

@@ -5,6 +5,7 @@
#include <stdio.h>
#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

3
main.c
View File

@@ -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)
{

View File

@@ -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);
}
}
}

View File

@@ -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