Compare commits

..

4 Commits

Author SHA1 Message Date
Bernhard
c4e21dca22 Änderung CYCLE_COUNTER_MAX für Testzwecke 2026-01-24 19:38:15 +01:00
Bernhard
450e93982e Im Simu-Mode Bedienung über Tastatur 2026-01-24 18:56:30 +01:00
Bernhard
4bde0cc888 MQTT-Handling optimiert 2026-01-20 21:15:35 +01:00
Bernhard
281e70d91e Settings-Datei lesen fertig 2026-01-20 20:11:36 +01:00
9 changed files with 338 additions and 210 deletions

View File

@@ -55,7 +55,7 @@ int Can_OpenInterface(int iMotorIndex, const char * ifacename)
if (settings.iCanSimu)
{
mylog(LOG_INFO, "CAN: Using simulation mode (motor %d).", iMotorIndex);
mylog(LOG_WARNING, "CAN: Using simulation mode (motor %d).", iMotorIndex);
intf_data[iMotorIndex].socket = -1;
motctrl[iMotorIndex].nSwitchState = 0xF5;
motctrl[iMotorIndex].nDriveConnected = 1;
@@ -140,7 +140,6 @@ void Can_SetMotorGear(int iMotorIndex, int iGear)
{
if (motctrl[iMotorIndex].iMotorGear != MOTOR_GEAR_FORWARD)
{
MqttClient_Publish_MotorGear(iMotorIndex, iGear);
motctrl[iMotorIndex].iMotorGear = MOTOR_GEAR_FORWARD;
// motor is switched to forward -> set min. power
Can_SetMotorPower(iMotorIndex, 1);
@@ -152,7 +151,6 @@ void Can_SetMotorGear(int iMotorIndex, int iGear)
{
if (motctrl[iMotorIndex].iMotorGear != MOTOR_GEAR_REVERSE)
{
MqttClient_Publish_MotorGear(iMotorIndex, iGear);
motctrl[iMotorIndex].iMotorGear = MOTOR_GEAR_REVERSE;
// motor is switched to reverse -> set min. power
Can_SetMotorPower(iMotorIndex, 1);
@@ -164,7 +162,6 @@ void Can_SetMotorGear(int iMotorIndex, int iGear)
{
if (motctrl[iMotorIndex].iMotorGear != MOTOR_GEAR_NEUTRAL)
{
MqttClient_Publish_MotorGear(iMotorIndex, iGear);
motctrl[iMotorIndex].iMotorGear = MOTOR_GEAR_NEUTRAL;
}
// motor is switch to neutral -> set power to 0
@@ -172,6 +169,7 @@ void Can_SetMotorGear(int iMotorIndex, int iGear)
WriteOutputPin(GPIO_LED_MOTRUN, LOW);
mylog(LOG_INFO, "CAN: Motor[%d]: Set gear neutral.", iMotorIndex);
}
MqttClient_Publish_MotorGear(iMotorIndex, iGear);
}

46
io/io.c
View File

@@ -2,17 +2,22 @@
#include "main.h"
#include "io.h"
#include <can/can_client.h>
#include <settings/settings.h>
#include <mqtt/mqtt_client.h>
struct GPIO_KEY_DATA gpioKeyStop;
struct GPIO_KEY_DATA gpioKeyPwrUp;
struct GPIO_KEY_DATA gpioKeyPwrDown;
char nInitialized = 0;
int iPowerSupplyOn = 0;
/// @brief Initialize the io pins
/// @return
int IO_Init()
{
iPowerSupplyOn = 0;
if (wiringPiSetupPinType(WPI_PIN_BCM))
{
mylog(LOG_ERR, "IO: Set up wiringPi failed!");
@@ -51,7 +56,7 @@ void SetupKeyPin(struct GPIO_KEY_DATA *pdata, int iKeyPin)
if ((pdata->iKeyPin > 0) && nInitialized)
{
//mylog(LOG_INFO, "IO: Config Pin %d as input", iOutPin);
mylog(LOG_DEBUG, "IO: Config Pin %d as input", pdata->iKeyPin);
pinMode(pdata->iKeyPin, INPUT);
pullUpDnControl(pdata->iKeyPin, PUD_UP);
}
@@ -154,7 +159,7 @@ void SetupOutputPin(int iOutPin)
{
if ((iOutPin > 0) && nInitialized)
{
//mylog(LOG_INFO, "IO: Config Pin %d as output", iOutPin);
mylog(LOG_DEBUG, "IO: Config Pin %d as output", iOutPin);
pinMode(iOutPin, OUTPUT);
digitalWrite(iOutPin, LOW);
}
@@ -179,34 +184,42 @@ void IO_DoCyclic()
ReadKey(&gpioKeyPwrUp);
ReadKey(&gpioKeyPwrDown);
if (gpioKeyStop.iKeyValue)
if (gpioKeyStop.iKeyValue || atomic_load(&abKeyStop))
{
// stop key is pressed
if (gpioKeyStop.iKeyRisingEdge)
if (gpioKeyStop.iKeyRisingEdge || atomic_load(&abKeyStop))
{
mylog(LOG_INFO, "IO: KEY-Stop: Stop motor.");
Can_SetMotorGear(0, 0);
}
}
else if (motctrl[0].nDriveReady) // plus and minus keys only when drive is ready
if (gpioKeyPwrUp.iKeyRisingEdge || atomic_load(&abKeyPlus))
{
if (gpioKeyPwrUp.iKeyRisingEdge)
if (motctrl[0].nDriveReady)
{
// plus key is pressed -> increase power
// when drive is ready to run: plus key is pressed -> increase power
if (motctrl[0].iMotorGear == MOTOR_GEAR_NEUTRAL)
{
mylog(LOG_INFO, "IO: KEY-Plus: Start motor.");
Can_SetMotorGear(0, 1);
Can_SetMotorPower(0, 1);
}
else
else if (motctrl[0].iMotorPowerSteps < settings.iMotorPwrStepCount)
{
mylog(LOG_INFO, "IO: KEY-Plus: Increase power.");
Can_SetMotorPower(0, motctrl[0].iMotorPowerSteps + 1);
}
}
else if (settings.iShellySupplyCount > 0)
{
// when drive is not ready and we have to switch the supply
MqttClient_SwitchPowerSupply(1);
iPowerSupplyOn = 1;
}
}
if (gpioKeyPwrDown.iKeyRisingEdge)
if (gpioKeyPwrDown.iKeyRisingEdge || atomic_load(&abKeyMinus))
{
// minus key is pressed -> decrease power
if (motctrl[0].iMotorPowerSteps > 1)
@@ -214,11 +227,14 @@ void IO_DoCyclic()
mylog(LOG_INFO, "IO: KEY-Minus: Decrease power.");
Can_SetMotorPower(0, motctrl[0].iMotorPowerSteps - 1);
}
else
{
mylog(LOG_INFO, "IO: KEY-Minus: Stop motor.");
Can_SetMotorGear(0, 0);
}
}
// else
// {
// mylog(LOG_INFO, "IO: KEY-Minus: Stop motor.");
// Can_SetMotorGear(0, 0);
// }
}
atomic_store(&abKeyPlus, false);
atomic_store(&abKeyMinus, false);
atomic_store(&abKeyStop, false);
}

View File

@@ -30,6 +30,8 @@ struct GPIO_KEY_DATA
int iKeyRepeatCycleCounter;
};
extern int iPowerSupplyOn;
int IO_Init();
void IO_DoCyclic();
void SetupKeyPin(struct GPIO_KEY_DATA *pdata, int iKeyPin);

80
main.c
View File

@@ -4,12 +4,17 @@
#include <can/can_client.h>
#include <io/io.h>
#include <settings/settings.h>
#include <termios.h>
// Period info of the realtime task
struct period_info pinfo;
int iThreadControl = 0; // 0: thread is running, <0: thread shall exit, >0 thread has exited
int iLogToConsole = 1;
atomic_short asThreadControl = ATOMIC_VAR_INIT(0); // 0: thread is running, <0: thread shall exit, >0 thread has exited
// values for simulation
atomic_bool abKeyPlus = ATOMIC_VAR_INIT(false);
atomic_bool abKeyMinus = ATOMIC_VAR_INIT(false);
atomic_bool abKeyStop = ATOMIC_VAR_INIT(false);
/// @brief send a log message
/// @param prio
@@ -17,7 +22,7 @@ int iLogToConsole = 1;
/// @param
void mylog(int prio, const char *format, ...)
{
if (prio >= settings.iDebugLevel)
if (prio <= settings.iDebugLevel)
{
va_list args;
@@ -150,7 +155,7 @@ void *thread_func(void *data)
}
// Connect to mqtt broker
while (MqttClient_Connect() && (iThreadControl == 0))
while (MqttClient_Connect() && atomic_load(&asThreadControl) == 0)
{
mylog(LOG_ERR, "MqttClient_Connect() failed!");
sleep(10);
@@ -163,7 +168,7 @@ void *thread_func(void *data)
WriteOutputPin(GPIO_OUT_PWRON, HIGH);
// cyclic call of do_cyclic_1ms()
while (iThreadControl == 0)
while (atomic_load(&asThreadControl) == 0)
{
pinfo.cyclecounter++;
if (pinfo.cyclecounter > CYCLE_COUNTER_MAX)
@@ -185,11 +190,22 @@ void *thread_func(void *data)
Can_CloseInterface(0);
// signal thread has finnished
iThreadControl = 1;
atomic_store(&asThreadControl, 1);
return NULL;
}
// Funktion, um das Terminal in den "Raw Mode" zu versetzen
void set_conio_terminal_mode()
{
struct termios new_termios;
tcgetattr(0, &new_termios);
new_termios.c_lflag &= ~ICANON; // Deaktiviert den zeilenweisen Modus
new_termios.c_lflag &= ~ECHO; // Verhindert, dass die Taste angezeigt wird
tcsetattr(0, TCSANOW, &new_termios);
}
/// @brief catch signals and set flag to terminate for the realtime thread
/// @param signo
void sig_handler(int signo)
@@ -197,7 +213,7 @@ void sig_handler(int signo)
if ((signo == SIGINT) || (signo == SIGTERM))
{
mylog(LOG_INFO, "Received signal %d", signo);
iThreadControl = -1; // signal realtime thread to exit
atomic_store(&asThreadControl, -1); // signal realtime thread to exit
}
}
@@ -222,6 +238,13 @@ int main(int argc, char* argv[])
// Read the settings file after opening the log
Settings_ReadConfFile();
if (settings.iCanSimu)
{
// in simulation mode switch terminal to raw mode
set_conio_terminal_mode();
}
else
{
// catch signals
if (signal(SIGTERM, sig_handler) == SIG_ERR)
{
@@ -233,6 +256,7 @@ int main(int argc, char* argv[])
mylog(LOG_ERR, "Can't catch SIGINT");
exit(-2);
}
}
/* Lock memory */
if(mlockall(MCL_CURRENT | MCL_FUTURE) == -1)
@@ -281,7 +305,7 @@ int main(int argc, char* argv[])
/* Create a pthread with specified attributes */
iThreadControl = 0;
atomic_store(&asThreadControl, 0);
ret = pthread_create(&thread, &attr, thread_func, NULL);
if (ret)
{
@@ -289,6 +313,48 @@ int main(int argc, char* argv[])
goto out;
}
if (settings.iCanSimu)
{
// in simulation mode we use keys to control the motors
mylog(LOG_INFO, "*** CanRtDriver in Simu-Mode ***");
while (1)
{
char ch = getchar();
mylog(LOG_INFO, "SIMU: Taste gedrückt: %c", ch);
if (ch == '+')
{
atomic_store(&abKeyPlus, true);
}
else if (ch == '-')
{
atomic_store(&abKeyMinus, true);
}
else if (ch == 's')
{
atomic_store(&abKeyStop, true);
}
else if (ch == 'p')
{
if (iPowerSupplyOn)
{
MqttClient_SwitchPowerSupply(0);
iPowerSupplyOn = 0;
}
else
{
MqttClient_SwitchPowerSupply(1);
iPowerSupplyOn = 1;
}
}
else if ( ch == 'q')
{
atomic_store(&asThreadControl, -1); // signal realtime thread to exit
break;
}
}
}
// join the thread and wait for it to exit
ret = pthread_join(thread, NULL);
if (ret)

10
main.h
View File

@@ -13,8 +13,12 @@
#include <signal.h>
#include <syslog.h>
#include <stdarg.h>
#include <stdatomic.h>
#include <stdbool.h>
#define CYCLE_COUNTER_MAX 86400000
//#define CYCLE_COUNTER_MAX 86400000
#define CYCLE_COUNTER_MAX 600000 // for testing only
struct period_info
{
@@ -24,6 +28,10 @@ struct period_info
float fStartTime;
};
extern atomic_bool abKeyPlus;
extern atomic_bool abKeyMinus;
extern atomic_bool abKeyStop;
extern struct period_info pinfo;
extern void mylog(int prio, const char *format, ...);

View File

@@ -8,30 +8,30 @@
#include "main.h"
#include <mqtt/mqtt_client.h>
#include <can/can_client.h>
#include <settings/settings.h>
#include <string.h>
#include <mosquitto.h>
// Topics to subscribe
const char* mqtt_topic_motor_gear_request = "Pool/Motor_Gear_Request";
const char* mqtt_topic_motor_power_request = "Pool/Motor_Power_Request";
// Topics to publish
const char* mqtt_topic_status_cyclecounter = "Pool/Status/CycleCounter";
const char* mqtt_topic_motor1_gear = "Pool/Motor1/Gear";
int iMqttMotor1Gear = 0;
const char* mqtt_topic_motor1_power = "Pool/Motor1/Power";
int iMqttMotor1Power = 0;
const char* mqtt_topic_motor2_gear = "Pool/Motor2/Gear";
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;
unsigned char nMotor1SwitchState = 255;
const char* mqtt_topic_motor2_switchstate = "Pool/Motor1/SwitchState";
unsigned char nMqttMotor2SwitchState = 0;
unsigned char nMotor2SwitchState = 255;
const char* mqtt_topic_motor1_actualpowerw = "Pool/Motor1/ActualPowerW";
int iMqttMotor1ActualPowerW = 0;
int iMqttMotor1ActualPowerW = -1;
const char* mqtt_topic_motor2_actualpowerw = "Pool/Motor1/ActualPowerW";
int iMqttMotor2ActualPowerW = 0;
int iMqttMotor2ActualPowerW = -1;
const char* mqtt_broker_addr = "127.0.0.1";
const int mqtt_broker_port = 1883;
@@ -50,60 +50,46 @@ void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mo
memcpy(topic_value, message->payload, message->payloadlen);
topic_value[message->payloadlen] = '\0';
if (strcmp(message->topic, mqtt_topic_motor1_gear) == 0)
if (strcmp(message->topic, mqtt_topic_motor_gear_request) == 0)
{
int val = 9999;
int val = 123456789;
if (sscanf(topic_value, "%d", &val))
{
mylog(LOG_INFO, "MQTT: Received value for mqtt_topic_motor1_gear: %d", val);
iMqttMotor1Gear = val;
Can_SetMotorGear(0, val);
mylog(LOG_INFO, "MQTT: Received value for mqtt_topic_motor_gear_request: %d", val);
if (val == 123456789)
{
val = 0;
}
for (int i=0; i<settings.iMotorCount; i++)
{
Can_SetMotorGear(i, val);
}
}
else
{
mylog(LOG_WARNING, "MQTT: Received mqtt_topic_motor1_gear: %s", topic_value);
mylog(LOG_WARNING, "MQTT: Received mqtt_topic_motor_gear_request: %s", topic_value);
}
}
else if (strcmp(message->topic, mqtt_topic_motor1_power) == 0)
else if (strcmp(message->topic, mqtt_topic_motor_power_request) == 0)
{
int val = 9999;
int val = 123456789;
if (sscanf(topic_value, "%d", &val))
{
mylog(LOG_INFO, "MQTT: Received value for mqtt_topic_motor1_power: %d", val);
iMqttMotor1Power = val;
Can_SetMotorPower(0, val);
mylog(LOG_INFO, "MQTT: Received value for mqtt_topic_motor_power_request: %d", val);
if (val == 123456789)
{
val = 0;
}
for (int i=0; i<settings.iMotorCount; i++)
{
Can_SetMotorPower(i, val);
}
}
else
{
mylog(LOG_WARNING, "MQTT: Received mqtt_topic_motor1_power: %s", topic_value);
}
}
else if (strcmp(message->topic, mqtt_topic_motor2_gear) == 0)
{
int val = 9999;
if (sscanf(topic_value, "%d", &val))
{
mylog(LOG_INFO, "MQTT: Received value for mqtt_topic_motor2_gear: %d", val);
iMqttMotor2Gear = val;
Can_SetMotorGear(1, val);
}
else
{
mylog(LOG_WARNING, "MQTT: Received mqtt_topic_motor2_gear: %s", topic_value);
}
}
else if (strcmp(message->topic, mqtt_topic_motor2_power) == 0)
{
int val = 9999;
if (sscanf(topic_value, "%d", &val))
{
mylog(LOG_INFO, "MQTT: Received value for mqtt_topic_motor2_power: %d", val);
iMqttMotor2Power = val;
Can_SetMotorPower(1, val);
}
else
{
mylog(LOG_WARNING, "MQTT: Received mqtt_topic_motor2_power: %s", topic_value);
mylog(LOG_WARNING, "MQTT: Received mqtt_topic_motor_gear_request: %s", topic_value);
}
}
else
@@ -114,6 +100,38 @@ void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mo
free(topic_value);
}
/// @brief callback function after connection
void my_connect_callback(struct mosquitto *mosq, void *obj, int rc)
{
if (rc == 0)
{
if (mosquitto_subscribe(mosq, NULL, mqtt_topic_motor_gear_request, 0) != MOSQ_ERR_SUCCESS)
{
mylog(LOG_ERR, "MQTT: mosquitto_subscribe(mqtt_topic_motor_gear_request) failed!");
}
if (mosquitto_subscribe(mosq, NULL, mqtt_topic_motor_power_request, 0) != MOSQ_ERR_SUCCESS)
{
mylog(LOG_ERR, "MQTT: mosquitto_subscribe(mqtt_topic_motor_power_request) failed!");
}
char message[10];
snprintf(message, sizeof(message), "0");
if (mosquitto_publish(mosq, NULL, mqtt_topic_motor_gear_request, strlen(message), &message, 0, false) != MOSQ_ERR_SUCCESS)
{
mylog(LOG_ERR, "MQTT: mosquitto_publish(mqtt_topic_motor_gear_request) failed!");
}
if (mosquitto_publish(mosq, NULL, mqtt_topic_motor_power_request, strlen(message), &message, 0, false) != MOSQ_ERR_SUCCESS)
{
mylog(LOG_ERR, "MQTT: mosquitto_publish(mqtt_topic_motor_power_request) failed!");
}
for (int i=0; i<settings.iMotorCount; i++)
{
MqttClient_Publish_MotorSwitchState(i, 0);
MqttClient_Publish_MotorActualPowerW(i, 0);
}
}
}
/// @brief connect to mqtt broker
/// @return
@@ -138,6 +156,7 @@ int MqttClient_Connect()
// Define a function which will be called by libmosquitto client every time when there is a new MQTT message
mosquitto_message_callback_set(mosq, my_message_callback);
mosquitto_connect_callback_set(mosq, my_connect_callback);
// Connect to MQTT broker
if (mosquitto_connect(mosq, mqtt_broker_addr, mqtt_broker_port, 60) != MOSQ_ERR_SUCCESS)
@@ -152,81 +171,11 @@ int MqttClient_Connect()
{
/* Wenn wir Verbindungsfehler hatten, dann befinden wir uns wohl in Boot-Prozess und der Mosquitto ist
gerade erst gestartet. Wir müssen hier etwas warten, sonst funktioniert das Subscriben nicht */
sleep(10);
sleep(500);
}
// publish all topics we want to subscribe
char message[10];
snprintf(message, sizeof(message), "0");
if (mosquitto_publish(mosq, NULL, mqtt_topic_motor1_gear, strlen(message), &message, 0, false) != MOSQ_ERR_SUCCESS)
{
mylog(LOG_ERR, "MQTT: mosquitto_publish(mqtt_topic_motor1_gear) failed!");
MqttClient_Close();
iHadConnectError++;
return 10;
}
if (mosquitto_publish(mosq, NULL, mqtt_topic_motor1_power, strlen(message), &message, 0, false) != MOSQ_ERR_SUCCESS)
{
mylog(LOG_ERR, "MQTT: mosquitto_publish(mqtt_topic_motor1_power) failed!");
MqttClient_Close();
iHadConnectError++;
return 11;
}
if (mosquitto_publish(mosq, NULL, mqtt_topic_motor2_gear, strlen(message), &message, 0, false) != MOSQ_ERR_SUCCESS)
{
mylog(LOG_ERR, "MQTT: mosquitto_publish(mqtt_topic_motor2_gear) failed!");
MqttClient_Close();
iHadConnectError++;
return 12;
MqttClient_Close();
}
if (mosquitto_publish(mosq, NULL, mqtt_topic_motor2_power, strlen(message), &message, 0, false) != MOSQ_ERR_SUCCESS)
{
mylog(LOG_ERR, "MQTT: mosquitto_publish(mqtt_topic_motor2_power) failed!");
MqttClient_Close();
iHadConnectError++;
return 13;
}
// subscribe all needed topics
if (mosquitto_subscribe(mosq, NULL, mqtt_topic_motor1_gear, 0) != MOSQ_ERR_SUCCESS)
{
mylog(LOG_ERR, "MQTT: mosquitto_subscribe(mqtt_topic_motor1_gear) failed!");
iHadConnectError++;
return 20;
}
if (mosquitto_subscribe(mosq, NULL, mqtt_topic_motor1_power, 0) != MOSQ_ERR_SUCCESS)
{
mylog(LOG_ERR, "MQTT: mosquitto_subscribe(mqtt_topic_motor1_power) failed!");
MqttClient_Close();
iHadConnectError++;
return 21;
}
if (mosquitto_subscribe(mosq, NULL, mqtt_topic_motor2_gear, 0) != MOSQ_ERR_SUCCESS)
{
mylog(LOG_ERR, "MQTT: mosquitto_subscribe(mqtt_topic_motor2_gear) failed!");
MqttClient_Close();
iHadConnectError++;
return 22;
}
if (mosquitto_subscribe(mosq, NULL, mqtt_topic_motor2_power, 0) != MOSQ_ERR_SUCCESS)
{
mylog(LOG_ERR, "MQTT: mosquitto_subscribe(mqtt_topic_motor2_power) failed!");
MqttClient_Close();
iHadConnectError++;
return 23;
}
nMqttMotor1SwitchState = 255;
MqttClient_Publish_MotorSwitchState(0, 0);
nMqttMotor2SwitchState = 255;
MqttClient_Publish_MotorSwitchState(1, 0);
iMqttMotor1ActualPowerW = -1;
MqttClient_Publish_MotorActualPowerW(0, 0);
iMqttMotor2ActualPowerW = -1;
MqttClient_Publish_MotorActualPowerW(1, 0);
mylog(LOG_INFO, "MQTT: Connected successfull!");
mylog(LOG_INFO, "MQTT: Connected successfull after %d errors!", iHadConnectError);
return 0;
}
@@ -266,25 +215,17 @@ void MqttClient_Publish_MotorGear(int iMotorIndex, int iGear)
{
if (iMotorIndex == 0)
{
if (iGear != iMqttMotor1Gear)
{
iMqttMotor1Gear = iGear;
char message[100];
snprintf(message, sizeof(message), "%d", iGear);
mosquitto_publish(mosq, NULL, mqtt_topic_motor1_gear, strlen(message), message, 0, false);
}
}
else if (iMotorIndex == 1)
{
if (iGear != iMqttMotor2Gear)
{
iMqttMotor2Gear = iGear;
char message[100];
snprintf(message, sizeof(message), "%d", iGear);
mosquitto_publish(mosq, NULL, mqtt_topic_motor2_gear, strlen(message), message, 0, false);
}
}
}
/// @brief Publish the requested motor power
@@ -294,25 +235,17 @@ void MqttClient_Publish_MotorPower(int iMotorIndex, int iPower)
{
if (iMotorIndex == 0)
{
if (iPower != iMqttMotor1Power)
{
iMqttMotor1Power = iPower;
char message[100];
snprintf(message, sizeof(message), "%d", iPower);
mosquitto_publish(mosq, NULL, mqtt_topic_motor1_power, strlen(message), message, 0, false);
}
}
else if (iMotorIndex == 1)
{
if (iPower != iMqttMotor2Power)
{
iMqttMotor2Power = iPower;
char message[100];
snprintf(message, sizeof(message), "%d", iPower);
mosquitto_publish(mosq, NULL, mqtt_topic_motor2_power, strlen(message), message, 0, false);
}
}
}
/// @brief Publish the actual switches states
@@ -322,9 +255,9 @@ void MqttClient_Publish_MotorSwitchState(int iMotorIndex, unsigned char nSwitchS
{
if (iMotorIndex == 0)
{
if (nSwitchState != nMqttMotor1SwitchState)
if (nSwitchState != nMotor1SwitchState)
{
nMqttMotor1SwitchState = nSwitchState;
nMotor1SwitchState = nSwitchState;
char message[100];
snprintf(message, sizeof(message), "%2X", nSwitchState);
mosquitto_publish(mosq, NULL, mqtt_topic_motor1_switchstate, strlen(message), message, 0, false);
@@ -332,9 +265,9 @@ void MqttClient_Publish_MotorSwitchState(int iMotorIndex, unsigned char nSwitchS
}
else if (iMotorIndex == 1)
{
if (nSwitchState != nMqttMotor2SwitchState)
if (nSwitchState != nMotor2SwitchState)
{
nMqttMotor2SwitchState = nSwitchState;
nMotor2SwitchState = nSwitchState;
char message[100];
snprintf(message, sizeof(message), "%2X", nSwitchState);
mosquitto_publish(mosq, NULL, mqtt_topic_motor2_switchstate, strlen(message), message, 0, false);
@@ -369,3 +302,16 @@ void MqttClient_Publish_MotorActualPowerW(int iMotorIndex, int iMotorPowerW)
}
}
}
void MqttClient_SwitchPowerSupply(int on)
{
char *cmd = on ? "on" : "off";
for (int i=0; i<settings.iShellySupplyCount; i++)
{
mosquitto_publish(mosq, NULL, settings.sShellySupplyTopic[i], strlen(cmd), cmd, 0, false);
}
mylog(LOG_INFO, "MQTT: Switch supply-shellies %s", cmd);
}

View File

@@ -11,5 +11,6 @@ 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);
void MqttClient_SwitchPowerSupply(int on);
#endif

114
settings/settings.c Normal file → Executable file
View File

@@ -1,7 +1,7 @@
#include <main.h>
#include <settings/settings.h>
#include <ctype.h>
struct APP_SETTINGS settings;
@@ -16,13 +16,19 @@ void Settings_InitDefaultValues()
// #define LOG_INFO 6 /* informational */
// #define LOG_DEBUG 7 /* debug-level messages */
settings.iDebugLevel = LOG_INFO;
settings.iCanSimu = 0;
settings.iCanSimu = 1;
settings.iMotorCount = 1;
settings.iMotorPwrMinRaw = 38;
settings.iMotorPwrMaxRaw = 250;
settings.iMotorPwrStepCount = 7;
settings.iShellySupplyCount = 0;
for (int i=0; i<MAX_SHELLIES_COUNT; i++)
{
settings.sShellySupplyTopic[i][0] = '\0';
}
// Get path of the executable itself
ssize_t length = readlink("/proc/self/exe", settings.sExePath, sizeof(settings.sExePath) - 1);
if (length >= 0)
@@ -38,16 +44,57 @@ void Settings_InitDefaultValues()
}
char *trim_str(const char *s)
{
// Führende Leerzeichen finden
while (isspace((unsigned char)*s))
s++;
// Falls der String leer ist
if (*s == 0)
return strdup("");
// Letztes Zeichen finden
const char *end = s + strlen(s) - 1;
while (end > s && isspace((unsigned char)*end))
end--;
// Länge des neuen Strings berechnen
size_t len = (end - s) + 1;
// Speicher reservieren (+1 für das Null-Byte)
char *new_str = malloc(len + 1);
if (new_str)
{
memcpy(new_str, s, len);
new_str[len] = '\0';
}
return new_str;
}
void Settings_ReadConfFile()
{
const char *filename = "/etc/CanRtDriver.conf";
//const char *filename = "/etc/CanRtDriver.conf";
char filename[MAX_PATH + 50];
sprintf(filename, "/etc/CanRtDriver.conf");
FILE *file = fopen(filename, "r");
if (file == NULL)
{
mylog(LOG_ERR, "Failed to open settings file %s", filename);
mylog(LOG_INFO, "SETTINGS: File %s noch found", filename);
sprintf(filename, "%s.conf", settings.sExePath);
file = fopen(filename, "r");
if (file == NULL)
{
mylog(LOG_ERR, "SETTINGS: No conf file found!");
return;
}
}
mylog(LOG_INFO, "SETTINGS: Reading %s", filename);
char line[MAX_LINE_LENGTH];
while (fgets(line, sizeof(line), file))
@@ -62,19 +109,58 @@ void Settings_ReadConfFile()
line[strcspn(line, "\r\n")] = 0;
// 3. Split line to key and value
char *key = strtok(line, "=");
char *value = strtok(NULL, "=");
char *key = trim_str(strtok(line, "="));
char *value = trim_str(strtok(NULL, "="));
if ((key != NULL) && (value != NULL))
{
mylog(LOG_DEBUG, "SETTINGS: Found key: '%s' | value: '%s'\n", key, value);
// // Beispiel: Wert verarbeiten
// if (strcmp(key, "port") == 0)
// {
// int port = atoi(value);
// printf(" [System] Port auf %d gesetzt.\n", port);
// }
if (strcmp(key, "DebugLevel") == 0)
{
// how many log messages we want to see
settings.iDebugLevel = atoi(value);
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iDebugLevel);
}
else if (strcmp(key, "CanSimu") == 0)
{
// shall we do simulation
settings.iCanSimu = atoi(value);
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iCanSimu);
}
else if (strcmp(key, "MotorPowerMinRaw") == 0)
{
// Minimum power value for the motors
settings.iMotorPwrMinRaw = atoi(value);
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iMotorPwrMinRaw);
}
else if (strcmp(key, "MotorPowerMaxRaw") == 0)
{
// Maximum power value for the motors
settings.iMotorPwrMaxRaw = atoi(value);
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iMotorPwrMaxRaw);
}
else if (strcmp(key, "MotorPowerStepCount") == 0)
{
// How many steps do we want to have switching the power
settings.iMotorPwrStepCount = atoi(value);
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iMotorPwrStepCount);
}
else if (strcmp(key, "SupplyShellyMqttTopic") == 0)
{
// MQTT topic for switching power supply
if (settings.iShellySupplyCount < MAX_SHELLIES_COUNT)
{
strcpy(settings.sShellySupplyTopic[settings.iShellySupplyCount], value);
settings.iShellySupplyCount++;
}
else
{
mylog(LOG_WARNING, "SETTINGS: Too many SupplyShellyMqttTopic!");
}
}
else
{
mylog(LOG_WARNING, "SETTING: Unknown key: %s", key);
}
}
}

7
settings/settings.h Normal file → Executable file
View File

@@ -8,17 +8,22 @@
#include <string.h>
#define MAX_LINE_LENGTH 256
#define MAX_PATH 256
#define MAX_SHELLIES_COUNT 5
struct APP_SETTINGS
{
int iDebugLevel; // Level of debug messages
char sExePath[MAX_PATH]; // Path of the executable
int iCanSimu = 0; // Simulate CAN if 1
int iCanSimu; // Simulate CAN if 1
int iMotorCount; // Number of used motors (1 or 2)
int iMotorPwrMinRaw; // Minimum power value for motor (raw value)
int iMotorPwrMaxRaw; // Maximum power value for motor (raw value)
int iMotorPwrStepCount; // Number of power steps
int iShellySupplyCount; // How many Shellies we have to switch the power supply
char sShellySupplyTopic[MAX_SHELLIES_COUNT][MAX_PATH];
};
extern struct APP_SETTINGS settings;