diff --git a/can/can_client.c b/can/can_client.c index 5ac8bbc..09ec94e 100755 --- a/can/can_client.c +++ b/can/can_client.c @@ -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); } diff --git a/mqtt/mqtt_client.c b/mqtt/mqtt_client.c index 0493af8..4b6163b 100755 --- a/mqtt/mqtt_client.c +++ b/mqtt/mqtt_client.c @@ -8,30 +8,30 @@ #include "main.h" #include #include +#include #include #include +// 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_DEBUG, "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; itopic, 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_DEBUG, "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; itopic, mqtt_topic_motor2_gear) == 0) - { - int val = 9999; - if (sscanf(topic_value, "%d", &val)) - { - mylog(LOG_DEBUG, "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_DEBUG, "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