terrorsl / sMQTTBroker

Simple MQTT broker
MIT License
70 stars 19 forks source link

Issues for Me... Simple questions for You #33

Closed LZHenry closed 1 year ago

LZHenry commented 1 year ago

Obviously I thought I knew more than I actually do. You would be kind to point me to some documents/answers?

Existing working system is HomeSeer 3 --> ESP8266 Broker --> TASMOTA device Substituting the sMQTT BROKER Can not get your ESP32 BROKER to connect to either end.

HS3 is obviously sending requests to sMQTT.... I seem to get nothing at the BROKER TASMOTA device is sending requests to sMQTT... code detect nothing. I assume there are some basics that I do not understand???? Thanks for reading... LZH

CODE FOLLOWS BELOW

define IDSTRING "V 1 DEC 30 2022 CV_BROKER_ESP32TEST_Vxx "

include

const char zssid = "xxx"; // The SSID (name) of the Wi-Fi network you want to connect to const char zpassword = "xxx"; // The password of the Wi-Fi network

// This code is LOCAL BROKER talking to DEVICE/CLIENTs and HS3 IPAddress LOCAL_BROKER_IP(10, 0, 0, 111); IPAddress Zgateway(10, 0, 0, 1); // set gateway to match your network IPAddress Zsubnet(255, 255, 255, 0); // set subnet mask to match your network

const unsigned short mqttPort = 1883;

class MyBroker :public sMQTTBroker { public: bool onEvent(sMQTTEvent *event) override { Serial.println("GOT EVENT zzzz "); //

    switch (event->Type())
    {
    case NewClient_sMQTTEventType:
    {
        Serial.println("NEW CLIENT EVENT    "); //
        sMQTTNewClientEvent *e = (sMQTTNewClientEvent*)event;
        e->Login();
        e->Password();
    }
    break;
    case LostConnect_sMQTTEventType:
        Serial.println("LOST WiFI Connection  Try again....   ");   //
        WiFi.reconnect();
        break;
    case UnSubscribe_sMQTTEventType:
    case Subscribe_sMQTTEventType:
    {
        Serial.println("zz subscribe   ");  //
        sMQTTSubUnSubClientEvent *e = (sMQTTSubUnSubClientEvent*)event;
    }
    break;
    }
    return true;
}

};

MyBroker broker; unsigned long Time; unsigned long freeRam;

void setup() { Serial.begin(115200); delay(333); // wait for port to settle Serial.println(" "); // //**** Serial.println(IDSTRING); //........................... //****

WiFi.config(LOCAL_BROKER_IP, Zgateway, Zsubnet);
WiFi.begin(zssid, zpassword);

while (WiFi.status() != WL_CONNECTED) 
{ // Wait for the Wi-Fi to connect
    Serial.println("Waiting for WiFi...    "  + String (zssid));    //
    delay(1000);
}

broker.init(mqttPort);
Serial.print("WiFi Connected ...");
////Serial.print("IP address:\t");
Serial.print(WiFi.localIP());
Serial.println("  PORT is..." + String (mqttPort)); //

// all done
// your magic code
Time = millis();
freeRam = ESP.getFreeHeap();

};

// lll void loop() { broker.update(); // your magic code

if (millis() - Time > 1000)
{
    Time = millis();
    if (ESP.getFreeHeap() != freeRam)
    {
        freeRam = ESP.getFreeHeap();
        Serial.print("RAM:");
        Serial.println(freeRam);

        //zz added?? does nothing?? 
        broker.publish("Topic", "message"); // added publish here
    }
}

};

terrorsl commented 1 year ago

hello, I recommend enabling debug messages on the broker. This will allow you to understand who and how connects to the broker. It is also interesting what the device writes. As I understand it, it does not connect to the broker?

LZHenry commented 1 year ago

Thanks for the answer. Still confused on the basics. I put DEBUG print statements in the code... nothing prints.

I expected the demo code to see the TASMOTA transmit for connection... nothing prints. I expect the same from a HomeSEER request... but nothing.

are there more examples of how to trigger a print when a message is received???

Thanks

terrorsl commented 1 year ago

Enable debug in arduino ide -> https://www.mischianti.org/wp-content/uploads/2020/09/ArduinoIDE-esp32-log-levels.jpg PlatformIO -> https://github.com/terrorsl/sMQTTBroker/issues/12 if you want to see publish message:

class MyBroker:public sMQTTBroker
{
public:
    bool onEvent(sMQTTEvent *event) override
    {
        switch(event->Type())
        {
        case NewClient_sMQTTEventType:
            {
                sMQTTNewClientEvent *e=(sMQTTNewClientEvent*)event;
                e->Login();
                e->Password();
            }
            break;
        case LostConnect_sMQTTEventType:
            WiFi.reconnect();
            break;
        case Public_sMQTTEventType:
               sMQTTPublicClientEvent *e;
               break;
        case UnSubscribe_sMQTTEventType:
        case Subscribe_sMQTTEventType:
            {
                sMQTTSubUnSubClientEvent *e=(sMQTTNewClientEvent*)event;
            }
            break;
        }
        return true;
    }
};
LZHenry commented 1 year ago

Ah! that debug... Thanks... working on it with my Visual Studio.

LZHenry commented 1 year ago

Very STUPID user error... as typical. Fixed. Connected. On to normal testing. Will still need a lot of learning now. Thank you. But while I have you, how many MQTT connections are possible? allowed? LZH

LZHenry commented 1 year ago

YES Broker is talking to device and HomeSEER. Thank you, but still a bit lost. If it is easy for you, I want to print to serial window.... IP Address of device attaching Topic arriving Number of Clients Attached

Thanks... LZH

terrorsl commented 1 year ago

Very STUPID user error... as typical. Fixed. Connected. On to normal testing. Will still need a lot of learning now. Thank you.

But while I have you, how many MQTT connections are possible? allowed? LZH

smqttbroker doesn't limit, this limit hardware and framework.

terrorsl commented 1 year ago

YES Broker is talking to device and HomeSEER. Thank you, but still a bit lost. If it is easy for you, I want to print to serial window....

IP Address of device attaching

Topic arriving

Number of Clients Attached

Thanks... LZH

On connection event you may know all about client. Also broker know number attached client, see sMqttBroker.h

LZHenry commented 1 year ago

OK.. the LAST time this year I will bother you. Not needed from you, but I have invested hours and this should be EASY for you to explain... Please.

Again, the BROKER works, I just want to better understand the operation. I am old and the class structures do not fit well into my head.
Here are the calls in the code that do not work; can you advise/fix???? Thanks.

    **case NewClient_sMQTTEventType:**
    {
        sMQTTNewClientEvent *e = (sMQTTNewClientEvent*)event;

        //! receive topic name by index
        // This prints garbage
        std::string zTopicName = getRetaiedTopicName(1);  // BAD HARDCODED this is wrong #!!
        Serial.printf(" .....................>>>>>>>Topic is     %s\n\n", (zTopicName));

        // This prints garbage
        std::string  dddf;
        dddf = e->Login();
        Serial.printf("  login %s ",dddf );
        dddf = e->Password();
        Serial.printf(" Password %s \n", dddf);

        // YES this prints possibly a count
        Serial.printf("...........   zTopicCount =  %5d  \n", (getRetainedTopicCount()));   // YES OK
terrorsl commented 1 year ago

std::string have function c_str() return char's ending 0. You need send it to printf.

LZHenry commented 1 year ago

Thank you thank you thank you.. NOW can see all the messages arriving.

Found everything I need to see. Will work some more and possibly send you an updated DEMO file.

Thanks... LZH

github-actions[bot] commented 1 year ago

Stale issue message