Compare commits
4 Commits
874ebf51ba
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c4e21dca22 | ||
|
|
450e93982e | ||
|
|
4bde0cc888 | ||
|
|
281e70d91e |
@@ -55,7 +55,7 @@ int Can_OpenInterface(int iMotorIndex, const char * ifacename)
|
|||||||
|
|
||||||
if (settings.iCanSimu)
|
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;
|
intf_data[iMotorIndex].socket = -1;
|
||||||
motctrl[iMotorIndex].nSwitchState = 0xF5;
|
motctrl[iMotorIndex].nSwitchState = 0xF5;
|
||||||
motctrl[iMotorIndex].nDriveConnected = 1;
|
motctrl[iMotorIndex].nDriveConnected = 1;
|
||||||
@@ -140,7 +140,6 @@ void Can_SetMotorGear(int iMotorIndex, int iGear)
|
|||||||
{
|
{
|
||||||
if (motctrl[iMotorIndex].iMotorGear != MOTOR_GEAR_FORWARD)
|
if (motctrl[iMotorIndex].iMotorGear != MOTOR_GEAR_FORWARD)
|
||||||
{
|
{
|
||||||
MqttClient_Publish_MotorGear(iMotorIndex, iGear);
|
|
||||||
motctrl[iMotorIndex].iMotorGear = MOTOR_GEAR_FORWARD;
|
motctrl[iMotorIndex].iMotorGear = MOTOR_GEAR_FORWARD;
|
||||||
// motor is switched to forward -> set min. power
|
// motor is switched to forward -> set min. power
|
||||||
Can_SetMotorPower(iMotorIndex, 1);
|
Can_SetMotorPower(iMotorIndex, 1);
|
||||||
@@ -152,7 +151,6 @@ void Can_SetMotorGear(int iMotorIndex, int iGear)
|
|||||||
{
|
{
|
||||||
if (motctrl[iMotorIndex].iMotorGear != MOTOR_GEAR_REVERSE)
|
if (motctrl[iMotorIndex].iMotorGear != MOTOR_GEAR_REVERSE)
|
||||||
{
|
{
|
||||||
MqttClient_Publish_MotorGear(iMotorIndex, iGear);
|
|
||||||
motctrl[iMotorIndex].iMotorGear = MOTOR_GEAR_REVERSE;
|
motctrl[iMotorIndex].iMotorGear = MOTOR_GEAR_REVERSE;
|
||||||
// motor is switched to reverse -> set min. power
|
// motor is switched to reverse -> set min. power
|
||||||
Can_SetMotorPower(iMotorIndex, 1);
|
Can_SetMotorPower(iMotorIndex, 1);
|
||||||
@@ -164,7 +162,6 @@ void Can_SetMotorGear(int iMotorIndex, int iGear)
|
|||||||
{
|
{
|
||||||
if (motctrl[iMotorIndex].iMotorGear != MOTOR_GEAR_NEUTRAL)
|
if (motctrl[iMotorIndex].iMotorGear != MOTOR_GEAR_NEUTRAL)
|
||||||
{
|
{
|
||||||
MqttClient_Publish_MotorGear(iMotorIndex, iGear);
|
|
||||||
motctrl[iMotorIndex].iMotorGear = MOTOR_GEAR_NEUTRAL;
|
motctrl[iMotorIndex].iMotorGear = MOTOR_GEAR_NEUTRAL;
|
||||||
}
|
}
|
||||||
// motor is switch to neutral -> set power to 0
|
// 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);
|
WriteOutputPin(GPIO_LED_MOTRUN, LOW);
|
||||||
mylog(LOG_INFO, "CAN: Motor[%d]: Set gear neutral.", iMotorIndex);
|
mylog(LOG_INFO, "CAN: Motor[%d]: Set gear neutral.", iMotorIndex);
|
||||||
}
|
}
|
||||||
|
MqttClient_Publish_MotorGear(iMotorIndex, iGear);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
58
io/io.c
58
io/io.c
@@ -2,17 +2,22 @@
|
|||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "io.h"
|
#include "io.h"
|
||||||
#include <can/can_client.h>
|
#include <can/can_client.h>
|
||||||
|
#include <settings/settings.h>
|
||||||
|
#include <mqtt/mqtt_client.h>
|
||||||
|
|
||||||
struct GPIO_KEY_DATA gpioKeyStop;
|
struct GPIO_KEY_DATA gpioKeyStop;
|
||||||
struct GPIO_KEY_DATA gpioKeyPwrUp;
|
struct GPIO_KEY_DATA gpioKeyPwrUp;
|
||||||
struct GPIO_KEY_DATA gpioKeyPwrDown;
|
struct GPIO_KEY_DATA gpioKeyPwrDown;
|
||||||
|
|
||||||
char nInitialized = 0;
|
char nInitialized = 0;
|
||||||
|
int iPowerSupplyOn = 0;
|
||||||
|
|
||||||
/// @brief Initialize the io pins
|
/// @brief Initialize the io pins
|
||||||
/// @return
|
/// @return
|
||||||
int IO_Init()
|
int IO_Init()
|
||||||
{
|
{
|
||||||
|
iPowerSupplyOn = 0;
|
||||||
|
|
||||||
if (wiringPiSetupPinType(WPI_PIN_BCM))
|
if (wiringPiSetupPinType(WPI_PIN_BCM))
|
||||||
{
|
{
|
||||||
mylog(LOG_ERR, "IO: Set up wiringPi failed!");
|
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)
|
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);
|
pinMode(pdata->iKeyPin, INPUT);
|
||||||
pullUpDnControl(pdata->iKeyPin, PUD_UP);
|
pullUpDnControl(pdata->iKeyPin, PUD_UP);
|
||||||
}
|
}
|
||||||
@@ -154,7 +159,7 @@ void SetupOutputPin(int iOutPin)
|
|||||||
{
|
{
|
||||||
if ((iOutPin > 0) && nInitialized)
|
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);
|
pinMode(iOutPin, OUTPUT);
|
||||||
digitalWrite(iOutPin, LOW);
|
digitalWrite(iOutPin, LOW);
|
||||||
}
|
}
|
||||||
@@ -179,46 +184,57 @@ void IO_DoCyclic()
|
|||||||
ReadKey(&gpioKeyPwrUp);
|
ReadKey(&gpioKeyPwrUp);
|
||||||
ReadKey(&gpioKeyPwrDown);
|
ReadKey(&gpioKeyPwrDown);
|
||||||
|
|
||||||
if (gpioKeyStop.iKeyValue)
|
if (gpioKeyStop.iKeyValue || atomic_load(&abKeyStop))
|
||||||
{
|
{
|
||||||
// stop key is pressed
|
// stop key is pressed
|
||||||
if (gpioKeyStop.iKeyRisingEdge)
|
if (gpioKeyStop.iKeyRisingEdge || atomic_load(&abKeyStop))
|
||||||
{
|
{
|
||||||
mylog(LOG_INFO, "IO: KEY-Stop: Stop motor.");
|
mylog(LOG_INFO, "IO: KEY-Stop: Stop motor.");
|
||||||
Can_SetMotorGear(0, 0);
|
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)
|
if (motctrl[0].iMotorGear == MOTOR_GEAR_NEUTRAL)
|
||||||
{
|
{
|
||||||
mylog(LOG_INFO, "IO: KEY-Plus: Start motor.");
|
mylog(LOG_INFO, "IO: KEY-Plus: Start motor.");
|
||||||
Can_SetMotorGear(0, 1);
|
Can_SetMotorGear(0, 1);
|
||||||
Can_SetMotorPower(0, 1);
|
Can_SetMotorPower(0, 1);
|
||||||
}
|
}
|
||||||
else
|
else if (motctrl[0].iMotorPowerSteps < settings.iMotorPwrStepCount)
|
||||||
{
|
{
|
||||||
mylog(LOG_INFO, "IO: KEY-Plus: Increase power.");
|
mylog(LOG_INFO, "IO: KEY-Plus: Increase power.");
|
||||||
Can_SetMotorPower(0, motctrl[0].iMotorPowerSteps + 1);
|
Can_SetMotorPower(0, motctrl[0].iMotorPowerSteps + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (settings.iShellySupplyCount > 0)
|
||||||
if (gpioKeyPwrDown.iKeyRisingEdge)
|
|
||||||
{
|
{
|
||||||
// minus key is pressed -> decrease power
|
// when drive is not ready and we have to switch the supply
|
||||||
if (motctrl[0].iMotorPowerSteps > 1)
|
MqttClient_SwitchPowerSupply(1);
|
||||||
{
|
iPowerSupplyOn = 1;
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (gpioKeyPwrDown.iKeyRisingEdge || atomic_load(&abKeyMinus))
|
||||||
|
{
|
||||||
|
// minus key is pressed -> decrease power
|
||||||
|
if (motctrl[0].iMotorPowerSteps > 1)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
atomic_store(&abKeyPlus, false);
|
||||||
|
atomic_store(&abKeyMinus, false);
|
||||||
|
atomic_store(&abKeyStop, false);
|
||||||
}
|
}
|
||||||
|
|||||||
2
io/io.h
2
io/io.h
@@ -30,6 +30,8 @@ struct GPIO_KEY_DATA
|
|||||||
int iKeyRepeatCycleCounter;
|
int iKeyRepeatCycleCounter;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
extern int iPowerSupplyOn;
|
||||||
|
|
||||||
int IO_Init();
|
int IO_Init();
|
||||||
void IO_DoCyclic();
|
void IO_DoCyclic();
|
||||||
void SetupKeyPin(struct GPIO_KEY_DATA *pdata, int iKeyPin);
|
void SetupKeyPin(struct GPIO_KEY_DATA *pdata, int iKeyPin);
|
||||||
|
|||||||
94
main.c
94
main.c
@@ -4,12 +4,17 @@
|
|||||||
#include <can/can_client.h>
|
#include <can/can_client.h>
|
||||||
#include <io/io.h>
|
#include <io/io.h>
|
||||||
#include <settings/settings.h>
|
#include <settings/settings.h>
|
||||||
|
#include <termios.h>
|
||||||
|
|
||||||
// Period info of the realtime task
|
// Period info of the realtime task
|
||||||
struct period_info pinfo;
|
struct period_info pinfo;
|
||||||
int iThreadControl = 0; // 0: thread is running, <0: thread shall exit, >0 thread has exited
|
|
||||||
int iLogToConsole = 1;
|
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
|
/// @brief send a log message
|
||||||
/// @param prio
|
/// @param prio
|
||||||
@@ -17,7 +22,7 @@ int iLogToConsole = 1;
|
|||||||
/// @param
|
/// @param
|
||||||
void mylog(int prio, const char *format, ...)
|
void mylog(int prio, const char *format, ...)
|
||||||
{
|
{
|
||||||
if (prio >= settings.iDebugLevel)
|
if (prio <= settings.iDebugLevel)
|
||||||
{
|
{
|
||||||
va_list args;
|
va_list args;
|
||||||
|
|
||||||
@@ -150,7 +155,7 @@ void *thread_func(void *data)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Connect to mqtt broker
|
// Connect to mqtt broker
|
||||||
while (MqttClient_Connect() && (iThreadControl == 0))
|
while (MqttClient_Connect() && atomic_load(&asThreadControl) == 0)
|
||||||
{
|
{
|
||||||
mylog(LOG_ERR, "MqttClient_Connect() failed!");
|
mylog(LOG_ERR, "MqttClient_Connect() failed!");
|
||||||
sleep(10);
|
sleep(10);
|
||||||
@@ -163,7 +168,7 @@ void *thread_func(void *data)
|
|||||||
WriteOutputPin(GPIO_OUT_PWRON, HIGH);
|
WriteOutputPin(GPIO_OUT_PWRON, HIGH);
|
||||||
|
|
||||||
// cyclic call of do_cyclic_1ms()
|
// cyclic call of do_cyclic_1ms()
|
||||||
while (iThreadControl == 0)
|
while (atomic_load(&asThreadControl) == 0)
|
||||||
{
|
{
|
||||||
pinfo.cyclecounter++;
|
pinfo.cyclecounter++;
|
||||||
if (pinfo.cyclecounter > CYCLE_COUNTER_MAX)
|
if (pinfo.cyclecounter > CYCLE_COUNTER_MAX)
|
||||||
@@ -185,11 +190,22 @@ void *thread_func(void *data)
|
|||||||
Can_CloseInterface(0);
|
Can_CloseInterface(0);
|
||||||
|
|
||||||
// signal thread has finnished
|
// signal thread has finnished
|
||||||
iThreadControl = 1;
|
atomic_store(&asThreadControl, 1);
|
||||||
return NULL;
|
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
|
/// @brief catch signals and set flag to terminate for the realtime thread
|
||||||
/// @param signo
|
/// @param signo
|
||||||
void sig_handler(int signo)
|
void sig_handler(int signo)
|
||||||
@@ -197,7 +213,7 @@ void sig_handler(int signo)
|
|||||||
if ((signo == SIGINT) || (signo == SIGTERM))
|
if ((signo == SIGINT) || (signo == SIGTERM))
|
||||||
{
|
{
|
||||||
mylog(LOG_INFO, "Received signal %d", signo);
|
mylog(LOG_INFO, "Received signal %d", signo);
|
||||||
iThreadControl = -1; // signal realtime thread to exit
|
atomic_store(&asThreadControl, -1); // signal realtime thread to exit
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,16 +238,24 @@ int main(int argc, char* argv[])
|
|||||||
// Read the settings file after opening the log
|
// Read the settings file after opening the log
|
||||||
Settings_ReadConfFile();
|
Settings_ReadConfFile();
|
||||||
|
|
||||||
// catch signals
|
if (settings.iCanSimu)
|
||||||
if (signal(SIGTERM, sig_handler) == SIG_ERR)
|
|
||||||
{
|
{
|
||||||
mylog(LOG_ERR, "Can't catch SIGTERM");
|
// in simulation mode switch terminal to raw mode
|
||||||
exit(-1);
|
set_conio_terminal_mode();
|
||||||
}
|
}
|
||||||
if (signal(SIGINT, sig_handler) == SIG_ERR)
|
else
|
||||||
{
|
{
|
||||||
mylog(LOG_ERR, "Can't catch SIGINT");
|
// catch signals
|
||||||
exit(-2);
|
if (signal(SIGTERM, sig_handler) == SIG_ERR)
|
||||||
|
{
|
||||||
|
mylog(LOG_ERR, "Can't catch SIGTERM");
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
if (signal(SIGINT, sig_handler) == SIG_ERR)
|
||||||
|
{
|
||||||
|
mylog(LOG_ERR, "Can't catch SIGINT");
|
||||||
|
exit(-2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Lock memory */
|
/* Lock memory */
|
||||||
@@ -281,7 +305,7 @@ int main(int argc, char* argv[])
|
|||||||
|
|
||||||
|
|
||||||
/* Create a pthread with specified attributes */
|
/* Create a pthread with specified attributes */
|
||||||
iThreadControl = 0;
|
atomic_store(&asThreadControl, 0);
|
||||||
ret = pthread_create(&thread, &attr, thread_func, NULL);
|
ret = pthread_create(&thread, &attr, thread_func, NULL);
|
||||||
if (ret)
|
if (ret)
|
||||||
{
|
{
|
||||||
@@ -289,6 +313,48 @@ int main(int argc, char* argv[])
|
|||||||
goto out;
|
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
|
// join the thread and wait for it to exit
|
||||||
ret = pthread_join(thread, NULL);
|
ret = pthread_join(thread, NULL);
|
||||||
if (ret)
|
if (ret)
|
||||||
|
|||||||
10
main.h
10
main.h
@@ -13,8 +13,12 @@
|
|||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
#include <stdarg.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
|
struct period_info
|
||||||
{
|
{
|
||||||
@@ -24,6 +28,10 @@ struct period_info
|
|||||||
float fStartTime;
|
float fStartTime;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
extern atomic_bool abKeyPlus;
|
||||||
|
extern atomic_bool abKeyMinus;
|
||||||
|
extern atomic_bool abKeyStop;
|
||||||
|
|
||||||
extern struct period_info pinfo;
|
extern struct period_info pinfo;
|
||||||
extern void mylog(int prio, const char *format, ...);
|
extern void mylog(int prio, const char *format, ...);
|
||||||
|
|
||||||
|
|||||||
@@ -8,30 +8,30 @@
|
|||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include <mqtt/mqtt_client.h>
|
#include <mqtt/mqtt_client.h>
|
||||||
#include <can/can_client.h>
|
#include <can/can_client.h>
|
||||||
|
#include <settings/settings.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <mosquitto.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_status_cyclecounter = "Pool/Status/CycleCounter";
|
||||||
|
|
||||||
const char* mqtt_topic_motor1_gear = "Pool/Motor1/Gear";
|
const char* mqtt_topic_motor1_gear = "Pool/Motor1/Gear";
|
||||||
int iMqttMotor1Gear = 0;
|
|
||||||
const char* mqtt_topic_motor1_power = "Pool/Motor1/Power";
|
const char* mqtt_topic_motor1_power = "Pool/Motor1/Power";
|
||||||
int iMqttMotor1Power = 0;
|
|
||||||
const char* mqtt_topic_motor2_gear = "Pool/Motor2/Gear";
|
const char* mqtt_topic_motor2_gear = "Pool/Motor2/Gear";
|
||||||
int iMqttMotor2Gear = 0;
|
|
||||||
const char* mqtt_topic_motor2_power = "Pool/Motor2/Power";
|
const char* mqtt_topic_motor2_power = "Pool/Motor2/Power";
|
||||||
int iMqttMotor2Power = 0;
|
|
||||||
|
|
||||||
const char* mqtt_topic_motor1_switchstate = "Pool/Motor1/SwitchState";
|
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";
|
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";
|
const char* mqtt_topic_motor1_actualpowerw = "Pool/Motor1/ActualPowerW";
|
||||||
int iMqttMotor1ActualPowerW = 0;
|
int iMqttMotor1ActualPowerW = -1;
|
||||||
const char* mqtt_topic_motor2_actualpowerw = "Pool/Motor1/ActualPowerW";
|
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 char* mqtt_broker_addr = "127.0.0.1";
|
||||||
const int mqtt_broker_port = 1883;
|
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);
|
memcpy(topic_value, message->payload, message->payloadlen);
|
||||||
topic_value[message->payloadlen] = '\0';
|
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))
|
if (sscanf(topic_value, "%d", &val))
|
||||||
{
|
{
|
||||||
mylog(LOG_INFO, "MQTT: Received value for mqtt_topic_motor1_gear: %d", val);
|
mylog(LOG_INFO, "MQTT: Received value for mqtt_topic_motor_gear_request: %d", val);
|
||||||
iMqttMotor1Gear = val;
|
if (val == 123456789)
|
||||||
Can_SetMotorGear(0, val);
|
{
|
||||||
|
val = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i=0; i<settings.iMotorCount; i++)
|
||||||
|
{
|
||||||
|
Can_SetMotorGear(i, val);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
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))
|
if (sscanf(topic_value, "%d", &val))
|
||||||
{
|
{
|
||||||
mylog(LOG_INFO, "MQTT: Received value for mqtt_topic_motor1_power: %d", val);
|
mylog(LOG_INFO, "MQTT: Received value for mqtt_topic_motor_power_request: %d", val);
|
||||||
iMqttMotor1Power = val;
|
if (val == 123456789)
|
||||||
Can_SetMotorPower(0, val);
|
{
|
||||||
|
val = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i=0; i<settings.iMotorCount; i++)
|
||||||
|
{
|
||||||
|
Can_SetMotorPower(i, val);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
mylog(LOG_WARNING, "MQTT: Received mqtt_topic_motor1_power: %s", topic_value);
|
mylog(LOG_WARNING, "MQTT: Received mqtt_topic_motor_gear_request: %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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -114,6 +100,38 @@ void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mo
|
|||||||
free(topic_value);
|
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
|
/// @brief connect to mqtt broker
|
||||||
/// @return
|
/// @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
|
// 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_message_callback_set(mosq, my_message_callback);
|
||||||
|
mosquitto_connect_callback_set(mosq, my_connect_callback);
|
||||||
|
|
||||||
// Connect to MQTT broker
|
// Connect to MQTT broker
|
||||||
if (mosquitto_connect(mosq, mqtt_broker_addr, mqtt_broker_port, 60) != MOSQ_ERR_SUCCESS)
|
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
|
/* 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 */
|
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
|
mylog(LOG_INFO, "MQTT: Connected successfull after %d errors!", iHadConnectError);
|
||||||
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!");
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,23 +215,15 @@ void MqttClient_Publish_MotorGear(int iMotorIndex, int iGear)
|
|||||||
{
|
{
|
||||||
if (iMotorIndex == 0)
|
if (iMotorIndex == 0)
|
||||||
{
|
{
|
||||||
if (iGear != iMqttMotor1Gear)
|
char message[100];
|
||||||
{
|
snprintf(message, sizeof(message), "%d", iGear);
|
||||||
iMqttMotor1Gear = iGear;
|
mosquitto_publish(mosq, NULL, mqtt_topic_motor1_gear, strlen(message), message, 0, false);
|
||||||
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)
|
else if (iMotorIndex == 1)
|
||||||
{
|
{
|
||||||
if (iGear != iMqttMotor2Gear)
|
char message[100];
|
||||||
{
|
snprintf(message, sizeof(message), "%d", iGear);
|
||||||
iMqttMotor2Gear = iGear;
|
mosquitto_publish(mosq, NULL, mqtt_topic_motor2_gear, strlen(message), message, 0, false);
|
||||||
char message[100];
|
|
||||||
snprintf(message, sizeof(message), "%d", iGear);
|
|
||||||
mosquitto_publish(mosq, NULL, mqtt_topic_motor2_gear, strlen(message), message, 0, false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -294,23 +235,15 @@ void MqttClient_Publish_MotorPower(int iMotorIndex, int iPower)
|
|||||||
{
|
{
|
||||||
if (iMotorIndex == 0)
|
if (iMotorIndex == 0)
|
||||||
{
|
{
|
||||||
if (iPower != iMqttMotor1Power)
|
char message[100];
|
||||||
{
|
snprintf(message, sizeof(message), "%d", iPower);
|
||||||
iMqttMotor1Power = iPower;
|
mosquitto_publish(mosq, NULL, mqtt_topic_motor1_power, strlen(message), message, 0, false);
|
||||||
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)
|
else if (iMotorIndex == 1)
|
||||||
{
|
{
|
||||||
if (iPower != iMqttMotor2Power)
|
char message[100];
|
||||||
{
|
snprintf(message, sizeof(message), "%d", iPower);
|
||||||
iMqttMotor2Power = iPower;
|
mosquitto_publish(mosq, NULL, mqtt_topic_motor2_power, strlen(message), message, 0, false);
|
||||||
char message[100];
|
|
||||||
snprintf(message, sizeof(message), "%d", iPower);
|
|
||||||
mosquitto_publish(mosq, NULL, mqtt_topic_motor2_power, strlen(message), message, 0, false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -322,9 +255,9 @@ void MqttClient_Publish_MotorSwitchState(int iMotorIndex, unsigned char nSwitchS
|
|||||||
{
|
{
|
||||||
if (iMotorIndex == 0)
|
if (iMotorIndex == 0)
|
||||||
{
|
{
|
||||||
if (nSwitchState != nMqttMotor1SwitchState)
|
if (nSwitchState != nMotor1SwitchState)
|
||||||
{
|
{
|
||||||
nMqttMotor1SwitchState = nSwitchState;
|
nMotor1SwitchState = nSwitchState;
|
||||||
char message[100];
|
char message[100];
|
||||||
snprintf(message, sizeof(message), "%2X", nSwitchState);
|
snprintf(message, sizeof(message), "%2X", nSwitchState);
|
||||||
mosquitto_publish(mosq, NULL, mqtt_topic_motor1_switchstate, strlen(message), message, 0, false);
|
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)
|
else if (iMotorIndex == 1)
|
||||||
{
|
{
|
||||||
if (nSwitchState != nMqttMotor2SwitchState)
|
if (nSwitchState != nMotor2SwitchState)
|
||||||
{
|
{
|
||||||
nMqttMotor2SwitchState = nSwitchState;
|
nMotor2SwitchState = nSwitchState;
|
||||||
char message[100];
|
char message[100];
|
||||||
snprintf(message, sizeof(message), "%2X", nSwitchState);
|
snprintf(message, sizeof(message), "%2X", nSwitchState);
|
||||||
mosquitto_publish(mosq, NULL, mqtt_topic_motor2_switchstate, strlen(message), message, 0, false);
|
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);
|
||||||
|
}
|
||||||
@@ -11,5 +11,6 @@ void MqttClient_Publish_MotorGear(int iMotorIndex, int iGear);
|
|||||||
void MqttClient_Publish_MotorPower(int iMotorIndex, int iPower);
|
void MqttClient_Publish_MotorPower(int iMotorIndex, int iPower);
|
||||||
void MqttClient_Publish_MotorSwitchState(int iMotorIndex, unsigned char nSwitchState);
|
void MqttClient_Publish_MotorSwitchState(int iMotorIndex, unsigned char nSwitchState);
|
||||||
void MqttClient_Publish_MotorActualPowerW(int iMotorIndex, int iMotorPowerW);
|
void MqttClient_Publish_MotorActualPowerW(int iMotorIndex, int iMotorPowerW);
|
||||||
|
void MqttClient_SwitchPowerSupply(int on);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
116
settings/settings.c
Normal file → Executable file
116
settings/settings.c
Normal file → Executable file
@@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
#include <main.h>
|
#include <main.h>
|
||||||
#include <settings/settings.h>
|
#include <settings/settings.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
struct APP_SETTINGS settings;
|
struct APP_SETTINGS settings;
|
||||||
|
|
||||||
@@ -16,13 +16,19 @@ void Settings_InitDefaultValues()
|
|||||||
// #define LOG_INFO 6 /* informational */
|
// #define LOG_INFO 6 /* informational */
|
||||||
// #define LOG_DEBUG 7 /* debug-level messages */
|
// #define LOG_DEBUG 7 /* debug-level messages */
|
||||||
settings.iDebugLevel = LOG_INFO;
|
settings.iDebugLevel = LOG_INFO;
|
||||||
settings.iCanSimu = 0;
|
settings.iCanSimu = 1;
|
||||||
|
|
||||||
settings.iMotorCount = 1;
|
settings.iMotorCount = 1;
|
||||||
settings.iMotorPwrMinRaw = 38;
|
settings.iMotorPwrMinRaw = 38;
|
||||||
settings.iMotorPwrMaxRaw = 250;
|
settings.iMotorPwrMaxRaw = 250;
|
||||||
settings.iMotorPwrStepCount = 7;
|
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
|
// Get path of the executable itself
|
||||||
ssize_t length = readlink("/proc/self/exe", settings.sExePath, sizeof(settings.sExePath) - 1);
|
ssize_t length = readlink("/proc/self/exe", settings.sExePath, sizeof(settings.sExePath) - 1);
|
||||||
if (length >= 0)
|
if (length >= 0)
|
||||||
@@ -38,17 +44,58 @@ 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()
|
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");
|
FILE *file = fopen(filename, "r");
|
||||||
if (file == NULL)
|
if (file == NULL)
|
||||||
{
|
{
|
||||||
mylog(LOG_ERR, "Failed to open settings file %s", filename);
|
mylog(LOG_INFO, "SETTINGS: File %s noch found", filename);
|
||||||
return;
|
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];
|
char line[MAX_LINE_LENGTH];
|
||||||
while (fgets(line, sizeof(line), file))
|
while (fgets(line, sizeof(line), file))
|
||||||
{
|
{
|
||||||
@@ -62,19 +109,58 @@ void Settings_ReadConfFile()
|
|||||||
line[strcspn(line, "\r\n")] = 0;
|
line[strcspn(line, "\r\n")] = 0;
|
||||||
|
|
||||||
// 3. Split line to key and value
|
// 3. Split line to key and value
|
||||||
char *key = strtok(line, "=");
|
char *key = trim_str(strtok(line, "="));
|
||||||
char *value = strtok(NULL, "=");
|
char *value = trim_str(strtok(NULL, "="));
|
||||||
|
|
||||||
if ((key != NULL) && (value != NULL))
|
if ((key != NULL) && (value != NULL))
|
||||||
{
|
{
|
||||||
mylog(LOG_DEBUG, "SETTINGS: Found key: '%s' | value: '%s'\n", key, value);
|
if (strcmp(key, "DebugLevel") == 0)
|
||||||
|
{
|
||||||
// // Beispiel: Wert verarbeiten
|
// how many log messages we want to see
|
||||||
// if (strcmp(key, "port") == 0)
|
settings.iDebugLevel = atoi(value);
|
||||||
// {
|
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iDebugLevel);
|
||||||
// int port = atoi(value);
|
}
|
||||||
// printf(" [System] Port auf %d gesetzt.\n", port);
|
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
7
settings/settings.h
Normal file → Executable file
@@ -8,17 +8,22 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define MAX_LINE_LENGTH 256
|
#define MAX_LINE_LENGTH 256
|
||||||
|
#define MAX_PATH 256
|
||||||
|
#define MAX_SHELLIES_COUNT 5
|
||||||
|
|
||||||
struct APP_SETTINGS
|
struct APP_SETTINGS
|
||||||
{
|
{
|
||||||
int iDebugLevel; // Level of debug messages
|
int iDebugLevel; // Level of debug messages
|
||||||
char sExePath[MAX_PATH]; // Path of the executable
|
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 iMotorCount; // Number of used motors (1 or 2)
|
||||||
int iMotorPwrMinRaw; // Minimum power value for motor (raw value)
|
int iMotorPwrMinRaw; // Minimum power value for motor (raw value)
|
||||||
int iMotorPwrMaxRaw; // Maximum power value for motor (raw value)
|
int iMotorPwrMaxRaw; // Maximum power value for motor (raw value)
|
||||||
int iMotorPwrStepCount; // Number of power steps
|
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;
|
extern struct APP_SETTINGS settings;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user