/* alternativer Client: libmosquitto-dev: https://mosquitto.org/api/files/mosquitto-h.html */ #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"; const char* mqtt_topic_motor1_power = "Pool/Motor1/Power"; const char* mqtt_topic_motor2_gear = "Pool/Motor2/Gear"; const char* mqtt_topic_motor2_power = "Pool/Motor2/Power"; const char* mqtt_topic_motor1_switchstate = "Pool/Motor1/SwitchState"; unsigned char nMotor1SwitchState = 255; const char* mqtt_topic_motor2_switchstate = "Pool/Motor1/SwitchState"; unsigned char nMotor2SwitchState = 255; const char* mqtt_topic_motor1_actualpowerw = "Pool/Motor1/ActualPowerW"; int iMqttMotor1ActualPowerW = -1; const char* mqtt_topic_motor2_actualpowerw = "Pool/Motor1/ActualPowerW"; int iMqttMotor2ActualPowerW = -1; const char* mqtt_broker_addr = "127.0.0.1"; const int mqtt_broker_port = 1883; struct mosquitto *mosq; /**< Libmosquito MQTT client instance. */ int iHadConnectError = 0; /// @brief callback function for incoming mqtt messages /// @param mosq /// @param userdata /// @param message void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message) { char* topic_value = (char *)malloc(message->payloadlen + 1); memcpy(topic_value, message->payload, message->payloadlen); topic_value[message->payloadlen] = '\0'; if (strcmp(message->topic, mqtt_topic_motor_gear_request) == 0) { int val = 123456789; if (sscanf(topic_value, "%d", &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_motor_power_request) == 0) { int val = 123456789; if (sscanf(topic_value, "%d", &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, 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