Hello. In my code, I want to receive messages from mqtt using the callbac function. Unfortunately, I can't figure out why this feature doesn't work. No errors are displayed, everything else is working properly. I think I'm not setting the callbac function correctly in the semaphore. Help me with this please.
#include <WiFi.h>
#include <PubSubClient.h>
#include <ezButton.h>
#include <Preferences.h>
#include <GyverPortal.h>
#include "EasyNextionLibrary.h"
////////////////////////////////////////////////
//WiFi_MQTT
struct config_wifi_struct {
char ssid[30];
char pass[30];
bool DHCP;
char ip[16];
char gt[16];
char mas[16];
char dns[16];
};
struct config_mqtt_struct {
char mqtt_server[16];
int mqtt_port;
char mqtt_username[30];
char mqtt_password[30];
};
config_wifi_struct str_wifi;
config_mqtt_struct str_mqtt;
WiFiClient wifiClient;
PubSubClient MQTTclient(wifiClient);
EasyNex myNex(Serial2);
SemaphoreHandle_t sema_MQTT_KeepAlive;
/// button_relay
const int BUTTON_PIN_1 = 27;
const int BUTTON_PIN_2 = 14;
const int LED_PIN_1 = 19;
const int LED_PIN_2 = 22;
ezButton button_1(BUTTON_PIN_1);
ezButton button_2(BUTTON_PIN_2);
int ledState_1 = LOW;
int ledState_2 = LOW;
/// preferens
Preferences preferences;
/// nextion
int mqtt_delay_send = 500;
int nextion_tom_index_1 = 1;
int nextion_tom_index_2 = 0;
int nextion_tom_index_3 = 0;
////////////////////////////////////////////
void setup()
{
Serial.begin(115200);
myNex.begin(115200);
Serial.println(F("Start"));
pinMode(LED_PIN_1, OUTPUT);
pinMode(LED_PIN_2, OUTPUT);
pinMode(4, OUTPUT);
digitalWrite(4, LOW);
button_1.setDebounceTime(50);
button_2.setDebounceTime(50);
if(!button_1.getStateRaw()) {
loginPortal();
}
read_config_wifi_mqtt();
DHCP_switch();
sema_MQTT_KeepAlive = xSemaphoreCreateBinary();
xSemaphoreGive(sema_MQTT_KeepAlive);
xTaskCreatePinnedToCore(MQTTkeepalive, "MQTTkeepalive", 20000, NULL, 3, NULL, 1);
}
void loop() {
myNex.NextionListen();
button_1.loop();
button_2.loop();
if(button_1.isPressed()) {
ledState_1 = !ledState_1;
digitalWrite(LED_PIN_1, ledState_1);
char val[5];
itoa(ledState_1, val, 10);
MQTTclient.publish("esp32/led1_st", val);
}
if(button_2.isPressed()) {
ledState_2 = !ledState_2;
digitalWrite(LED_PIN_2, ledState_2);
char val[5];
itoa(ledState_2, val, 10);
MQTTclient.publish("esp32/led2_st", val);
}
}
///////////_____MQTT_WIFI_SETTINGS
void MQTTkeepalive( void *pvParameters )
{
MQTTclient.setServer(str_mqtt.mqtt_server, str_mqtt.mqtt_port);
MQTTclient.setCallback(callback);
MQTTclient.setKeepAlive(90);
for (;;)
{
if ((wifiClient.connected()) && (WiFi.status() == WL_CONNECTED))
{
xSemaphoreTake(sema_MQTT_KeepAlive, portMAX_DELAY);
MQTTclient.loop();
xSemaphoreGive( sema_MQTT_KeepAlive );
}
else {
Serial.print(F("MQTT keep alive found MQTT status: "));
Serial.println(String(wifiClient.connected()));
Serial.print(F("WiFi status: "));
Serial.println(String(WiFi.status()));
if (!(WiFi.status() == WL_CONNECTED))
{
connectToWiFi();
}
connectToMQTT();
main_Portal();
}
vTaskDelay( 250 );
}
vTaskDelete ( NULL );
}
void connectToMQTT()
{
String client_id = "esp32-client-";
client_id += String(WiFi.macAddress());
Serial.print(F("Connect to MQTT as client: "));
Serial.println(client_id);
while (!MQTTclient.connected())
{
MQTTclient.connect(client_id.c_str(), str_mqtt.mqtt_username, str_mqtt.mqtt_password);
Serial.println(F("Connecting to MQTT"));
vTaskDelay(250);
}
Serial.println(F("MQTT Connected"));
MQTTclient.publish("esp32/output", "Hi -_-");
MQTTclient.subscribe("/devices/panel_test/controls/bt_2_1_1");
MQTTclient.subscribe("/devices/panel_test/controls/bt_2_2_1");
MQTTclient.subscribe("/devices/panel_test/controls/bt_2_3_1");
}
void connectToWiFi()
{
Serial.println(F("Connect to WiFi"));
while ( WiFi.status() != WL_CONNECTED )
{
WiFi.disconnect();
Serial.println(str_wifi.ssid);
WiFi.begin(str_wifi.ssid, str_wifi.pass);
Serial.println(F("waiting on wifi connection"));
vTaskDelay( 4000 );
}
Serial.println(F("Connected to WiFi"));
Serial.print(F("ip address: "));
Serial.println(WiFi.localIP());
WiFi.onEvent(WiFiEvent);
}
void WiFiEvent(WiFiEvent_t event)
{
switch (event) {
case SYSTEM_EVENT_STA_CONNECTED:
Serial.println(F("Connected to access point"));
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println(F("Disconnected from WiFi access point"));
break;
case SYSTEM_EVENT_AP_STADISCONNECTED:
Serial.println(F("WiFi client disconnected"));
break;
default: break;
}
}
void callback(char* topic, byte* message, unsigned int length) {
String messageTemp;
for (int i = 0; i < length; i++) {
messageTemp += (char)message[i];
}
Serial.println(messageTemp);
MQTTclient.publish("esp32/output", "0");
//info_climate_device
if (String(topic) == "/devices/panel_test/controls/bt_2_1_1") {
myNex.writeNum("controll.bt2_2_1_1_st.val", messageTemp.toInt());
MQTTclient.publish("esp32/output", "1");
if(nextion_tom_index_1 == 1){
myNex.writeNum("controll.bt2_2_1.val", messageTemp.toInt());
}
}
if (String(topic) == "/devices/panel_test/controls/bt_2_2_1") {
myNex.writeNum("controll.bt2_2_2_1_st.val", messageTemp.toInt());
MQTTclient.publish("esp32/output", "2");
if(nextion_tom_index_2 == 1){
myNex.writeNum("controll.bt2_2_1.val", messageTemp.toInt());
}
}
if (String(topic) == "/devices/panel_test/controls/bt_2_3_1") {
myNex.writeNum("controll.bt2_2_3_1_st.val", messageTemp.toInt());
MQTTclient.publish("esp32/output", "3");
if(nextion_tom_index_3 == 1){
myNex.writeNum("controll.bt2_2_1.val", messageTemp.toInt());
}
}
}
///////////_____WEB_Portal
void build_login() {
BUILD_BEGIN();
GP.THEME(GP_DARK);
GP.FORM_BEGIN("/login");
GP.TEXT("lg", "Login", str_wifi.ssid);
GP.BREAK();
GP.TEXT("ps", "Password", str_wifi.pass);
GP.BREAK();
GP.LABEL("DHCP: ");
GP.BREAK();
GP.CHECK("DHCP", str_wifi.DHCP);
GP.BREAK();
GP.TEXT("ip", "ip", str_wifi.ip);
GP.BREAK();
GP.TEXT("gt", "gt", str_wifi.gt);
GP.BREAK();
GP.TEXT("mas", "mas", str_wifi.mas);
GP.BREAK();
GP.TEXT("dns", "dns", str_wifi.dns);
GP.BREAK();
GP.TEXT("mqtt_server", "mqtt_server", str_mqtt.mqtt_server);
GP.BREAK();
GP.NUMBER("mqtt_port", "mqtt_port", str_mqtt.mqtt_port);
GP.BREAK();
GP.TEXT("mqtt_username", "mqtt_username", str_mqtt.mqtt_username);
GP.BREAK();
GP.TEXT("mqtt_password", "mqtt_password", str_mqtt.mqtt_password);
GP.BREAK();
GP.SUBMIT("Submit");
GP.FORM_END();
BUILD_END();
}
void build_main() {
//const char* strLocalIp = WiFi.localIP().toString().c_str();
//const char* cstrIp = strLocalIp.c_str();
//String str_getHostname = WiFi.getHostname().toString();
//const char* cstrIp = strLocalIp.c_str();
//String str_macAddress = WiFi.macAddress().toString();
//const char* cstrIp = strLocalIp.c_str();
//String str_subnetMask = WiFi.subnetMask().toString();
//const char* cstrIp = strLocalIp.c_str();
//String str_gatewayIP = WiFi.gatewayIP().toString();
//const char* cstrIp = strLocalIp.c_str();
//String str_dnsIP = WiFi.dnsIP().toString();
//const char* cstrIp = strLocalIp.c_str();
BUILD_BEGIN();
GP.THEME(GP_DARK);
GP.FORM_BEGIN("/login");
GP.TITLE("Onyx panel");
GP.HR();
GP.LABEL("IP address: ");
//GP.LABEL(strLocalIp);
GP.BREAK();
GP.LABEL("Hostname: ");
//GP.LABEL(str_getHostname);
GP.BREAK();
GP.LABEL("Mac Address: ");
//GP.LABEL(str_macAddress);
GP.BREAK();
GP.LABEL("Subnet Mask: ");
//GP.LABEL(str_subnetMask);
GP.BREAK();
GP.LABEL("Gateway IP: ");
//GP.LABEL(str_gatewayIP);
GP.BREAK();
GP.LABEL("DNS: ");
//GP.LABEL(str_dnsIP);
GP.BREAK();
GP.HR();
GP.LABEL("MQTT IP: ");
GP.LABEL(str_mqtt.mqtt_server);
GP.BREAK();
GP.LABEL("MQTT PORT: ");
GP.LABEL(str_mqtt.mqtt_port);
GP.BREAK();
GP.FORM_END();
BUILD_END();
}
void action_login(GyverPortal& p) {
if (p.form("/login")) {
p.copyStr("lg", str_wifi.ssid);
p.copyStr("ps", str_wifi.pass);
str_wifi.DHCP = p.getCheck("DHCP");
p.copyStr("ip", str_wifi.ip);
p.copyStr("gt", str_wifi.gt);
p.copyStr("mas", str_wifi.mas);
p.copyStr("dns", str_wifi.dns);
p.copyStr("mqtt_server", str_mqtt.mqtt_server);
str_mqtt.mqtt_port = p.getInt("mqtt_port");
p.copyStr("mqtt_username", str_mqtt.mqtt_username);
p.copyStr("mqtt_password", str_mqtt.mqtt_password);
WiFi.softAPdisconnect();
load_config_wifi_mqtt();
}
}
void loginPortal() {
Serial.println(F("Portal start"));
// запускаем точку доступа
WiFi.mode(WIFI_AP);
WiFi.softAP("WiFi Config");
// запускаем портал
Serial.println(F("Запуск портала"));
GyverPortal portal;
portal.attachBuild(build_login);
Serial.println(F("Запуск build_login"));
portal.start(WIFI_AP);
Serial.println(F("Запуск WIFI_AP"));
portal.attach(action_login);
Serial.println(F("Запуск action_login"));
// работа портала
while (portal.tick());
}
void main_Portal() {
GyverPortal portal;
portal.attachBuild(build_main);
portal.start();
portal.enableOTA();
portal.log.start();
// работа портала
while (portal.tick());
}
///////////_____JSON_load_read
void load_config_wifi_mqtt(){
preferences.begin("wifi_mqtt", false);
String ssid = String(str_wifi.ssid);
String pass = String(str_wifi.pass);
String ip = String(str_wifi.ip);
String gt = String(str_wifi.gt);
String mas = String(str_wifi.mas);
String dns = String(str_wifi.dns);
String mqtt_server = String(str_mqtt.mqtt_server);
String mqtt_username = String(str_mqtt.mqtt_username);
String mqtt_password = String(str_mqtt.mqtt_password);
preferences.putString("ssid", ssid);
preferences.putString("pass", pass);
preferences.putBool("DHCP", str_wifi.DHCP);
preferences.putString("ip", ip);
preferences.putString("gt", gt);
preferences.putString("mas", mas);
preferences.putString("dns", dns);
preferences.putString("mqtt_server", mqtt_server);
preferences.putInt("mqtt_port", str_mqtt.mqtt_port);
preferences.putString("mqtt_username", mqtt_username);
preferences.putString("mqtt_password", mqtt_password);
preferences.end();
}
void read_config_wifi_mqtt(){
preferences.begin("wifi_mqtt", false);
String ssid = preferences.getString("ssid", "0");
String pass = preferences.getString("pass", "0");
str_wifi.DHCP = preferences.getBool("DHCP", true);
String ip = preferences.getString("ip", "0");
String gt = preferences.getString("gt", "0");
String mas = preferences.getString("mas", "0");
String dns = preferences.getString("dns", "0");
String mqtt_server = preferences.getString("mqtt_server", "0");
str_mqtt.mqtt_port = preferences.getInt("mqtt_port", 1883);
String mqtt_username = preferences.getString("mqtt_username", "0");
String mqtt_password = preferences.getString("mqtt_password", "0");
preferences.end();
strcpy(str_wifi.ssid, ssid.c_str());
strcpy(str_wifi.pass, pass.c_str());
strcpy(str_wifi.ip, ip.c_str());
strcpy(str_wifi.gt, gt.c_str());
strcpy(str_wifi.mas, mas.c_str());
strcpy(str_wifi.dns, dns.c_str());
strcpy(str_mqtt.mqtt_server, mqtt_server.c_str());
strcpy(str_mqtt.mqtt_username, mqtt_username.c_str());
strcpy(str_mqtt.mqtt_password, mqtt_password.c_str());
}
bool DHCP_switch (){
if(!str_wifi.DHCP){
IPAddress local_IP(ipaddr_addr(str_wifi.ip));
IPAddress gateway(ipaddr_addr(str_wifi.gt));
IPAddress subnet(ipaddr_addr(str_wifi.mas));
IPAddress primaryDNS(ipaddr_addr(str_wifi.dns));
if (WiFi.config(local_IP, gateway, subnet, primaryDNS) == false) {
Serial.println("Configuration failed.");
return false;
}
Serial.println("Configuration set.");
return true;
}
}
////nextion
void trigger0(){
nextion_tom_index_1 = myNex.readNumber("controll.bt2_1_1.val");
nextion_tom_index_2 = myNex.readNumber("controll.bt2_1_2.val");
nextion_tom_index_3 = myNex.readNumber("controll.bt2_1_3.val");
}
void trigger21(){
char mqtt_val[5];
String val = "0";
val.toCharArray(mqtt_val,5);
MQTTclient.publish("/devices/panel_test/controls/bt_2_1_1/on", mqtt_val);
val = "1";
val.toCharArray(mqtt_val,5);
delay(mqtt_delay_send);
MQTTclient.publish("/devices/panel_test/controls/bt_2_1_1/on", mqtt_val);
}
void trigger22(){
char mqtt_val[5];
String val = "0";
val.toCharArray(mqtt_val,5);
MQTTclient.publish("/devices/panel_test/controls/bt_2_1_1/on", mqtt_val);
}
void trigger23(){
char mqtt_val[5];
String val = "1";
val.toCharArray(mqtt_val,5);
MQTTclient.publish("/devices/panel_test/controls/bt_2_1_1/on", mqtt_val);
}