Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can This Broker be STABLE with a “Tasmota” implemented Sonoff Devices? #27

Closed
LZHenry opened this issue Dec 16, 2018 · 8 comments
Closed

Comments

@LZHenry
Copy link

LZHenry commented Dec 16, 2018

Can This Broker be stable with a “Tasmota” implemented Sonoff Devices?

I have invested probably over 70 hours reading and attempting a solution to this... I am ready to ask questions before I give up: PLEASE.

This broker successfully connects and communicates with several “Tasmota loaded” Sonoff devices. Broker to 4 devices to existing HomeSEER operating application. However, connection to the BROKER typically REQUIRE the Sonoff devices to be active BEFORE starting the broker.

#1 Does the broker boot and send an MQTT initialize sequence that triggers the Sonoff devices to come alive and respond? If yes, then I can use a timer to force recovery should any devices power cycle during the day.

This timeout to a broker session is what the Sonoff device shows:
01:33:22 MQT: Attempting connection...
01:33:38 MQT: Connect failed to 10.0.0.8:1883, rc -4. Retry in 10 sec

Reboot the broker; the Sonoff finds success and we get:
received topic 'broker/counter' with data '100'
received topic 'tele/SONOFF_Device1/LWT' with data 'Offline'

The broker code is below, but except for my configuration it should be right from the example.

Any help will be appreciated.

`*

  • uMQTTBroker demo for Arduino (C++-style)
  • The program defines a custom broker class with callbacks,
  • starts it, subscribes locally to anything, and publishs a topic every second.
  • Try to connect from a remote client and publish something - the console will show this as well.
    */

#include <ESP8266WiFi.h>
#include "uMQTTBroker.h"

/*

  • Your WiFi config here
    */

bool WiFiAP = false; // Do yo want the ESP as AP?
unsigned int mqttPort = 1883; // the standard MQTT broker port

/*

  • Custom broker class with overwritten callback functions
    */
    class myMQTTBroker : public uMQTTBroker
    {
    public:
    virtual bool onConnect(IPAddress addr, uint16_t client_count) {
    Serial.println(addr.toString() + " connected");
    return true;
    }
    virtual bool onAuth(String username, String password) {
    Serial.println("Username/Password: " + username + "/" + password);
    return true;
    }

    virtual void onData(String topic, const char *data, uint32_t length) {
    char data_str[length + 1];
    os_memcpy(data_str, data, length);
    data_str[length] = '\0';

     Serial.println("received topic '" + topic + "' with data '" + (String)data_str + "'");
    

    }
    };

myMQTTBroker myBroker;

/*

  • WiFi init stuff
    */
    void startWiFiClient()
    {

const char* Zssid[] = "AA_HOME-xxxx"; // your network SSID (name)
const char* Zpass[] = "12345678"; // your network password

IPAddress Zip(10, 0, 0, 8);
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

**_WiFi.config(Zip, Zgateway, Zsubnet);  // Added this_**

Serial.println("Connecting to " + (String)Zssid);
WiFi.mode(WIFI_STA);
WiFi.begin(Zssid, Zpassword);

while (WiFi.status() != WL_CONNECTED) {
	delay(500);
	Serial.print(".");
}
Serial.println("");

Serial.println("WiFi connected");
Serial.println("IP address: " + WiFi.localIP().toString());

}

void startWiFiAP()
{
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, pass);
Serial.println("AP started");
Serial.println("IP address: " + WiFi.softAPIP().toString());
}
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println();

// Start WiFi
if (WiFiAP)
	startWiFiAP();
else
	startWiFiClient();

// Start the broker
Serial.println("Starting MQTT broker");
myBroker.init();

/*
 * Subscribe to anything
 */
myBroker.subscribe("#");

}

int counter = 0;

void loop()
{
/*
* Publish the counter value as String
*/
myBroker.publish("broker/counter", (String)counter++);

// wait a second
delay(1000);

}
`

@martin-ger
Copy link
Owner

An MQTT broker is waiting for incoming conections, i.e. it does nothing with the clients as long as they are not connecting - it even doesn't know about them.

I assume your problem might be the lwip - did you select "lwip Variant: 1.4 High Bandwidth" when building the broker? If not - the new lwip implementation in Arduino IDE causes problems with reconnects.

You might also try my precompiled firmware at: https://github.com/martin-ger/esp_mqtt . It runs quite robust and has a lot more options than the basic Arduino implementation.

@LZHenry
Copy link
Author

LZHenry commented Dec 16, 2018

Thank you for the kind response. I saw last night the previous conversation about the lwip. Do not yet know how to select the 1.4 variant but will investigate tonight. I do not build with the Arduino IDE; using visual studio 2017 (not VS Code) which uses all the Arduino libraries. I will be searching later how to do this selection for VS and for the IDE. If you have the patience to point me to how to select the iwip variant, that could save me a LOT of time.
Thanks for the broker implementation... this could also save a project I put on the shelf last April because of similar stay alive connection issues.

@martin-ger
Copy link
Owner

Sorry, no idea how to select this in VS - in Arduino it is a menue item. There it selects the following settings:

generic.menu.LwIPVariant.Prebuilt.build.lwip_lib=-llwip_gcc
generic.menu.LwIPVariant.Prebuilt.build.lwip_flags=-DLWIP_OPEN_SRC

@LZHenry
Copy link
Author

LZHenry commented Dec 16, 2018

Thanks again... I will be working tonight. Also did some research; this will help. Next I will try to load your binary. This will allow me to get a stable version operating. But, I still want to add custom items to my broker. LZH

@LZHenry
Copy link
Author

LZHenry commented Dec 17, 2018

Please excuse me for having to ask such BASIC questions. However, I can not discover how to use the serial commands to reset the broker parameters.

Successfully loaded the bin files:
Shown below, I worked for several hours today attempting several formats for help and set commands. Not even able to get HELP to work?
I can not determine the format to get more than a echo from the ESP.
I know it must be simple and staring at me in your description?????

uMQTT Broker V2.0.7 starting

No config found, saving default in flash
Loading default configuration
Saving configuration
Starting Console TCP Server on port 7777
Max number of TCP clients: 15
mode : sta(b4:e6:2d:14:28:32) + softAP(b6:e6:2d:14:28:32)
add if0
add if1
dhcp server start:(ip:192.168.4.1,mask:255.255.255.0,gw:192.168.4.1)
bcn 100
help:
help:
help
help
network:
network:
set network 10.0.4.1:

set network 10.0.4.1:
network:
network:

@martin-ger
Copy link
Owner

Generally looks good. Do you use correct terminal settings? Try Ctrl-M as line end.

@LZHenry
Copy link
Author

LZHenry commented Dec 18, 2018

Have your binary loaded and talking. Some additional help for others is below.

Help #1
After flashing, serial communication was not returning the “CMD>” prompt until things were properly setup in the tool.
Using Termite, MUST select “Append CR-LF”. Now the micro properly returns a “CMD>” to every request. (Visual Studio requires a similar setting change)

image

Help #2 Loading with QIO/DIO
This was confusing. FLASHING with QIO showed a good load but there is NO serial communication possible. When the load looks good, it is easy to waste a lot of time trying commends. Loading with DIO fixes all issues; serial communication is operating. Do not understand this; hoping my observation may help others.

@LZHenry
Copy link
Author

LZHenry commented Jan 26, 2019

System operating very well and stable. Next item is to enable more than the 8 standard clients.

Closing this thread. THank you very much.

@LZHenry LZHenry closed this as completed Jan 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants