arendst / Sonoff-MQTT-OTA-Arduino

Provide ESP8266 based itead Sonoff with Web, MQTT and OTA firmware using Arduino IDE - Now EOL
618 stars 197 forks source link

Info: add #error vor MQTT_PACKET_SIZE and TTL #150

Closed hjgode closed 7 years ago

hjgode commented 7 years ago

Please add to sonoff.ino:

...

include // MQTT

if (MQTT_MAX_PACKET_SIZE < 400)

error "MQTT_MAX_PACKET_SIZE is less than 400. Edit PubSubClient.h"

endif

if (MQTT_SOCKET_TIMEOUT < 120)

error "MQTT_SOCKET_TIMEOUT is less than 120. Edit PubSubClient.h"

endif

...

To avoid ending with a non-working firmware file

~Josef

arendst commented 7 years ago

Josef,

do you really experience non-working firmware when these parameters are not set correctly? I did not encounter that problem and that's why the test and message is included in the first messages after reboot in both serial console and MQTT.

thomasf commented 7 years ago

why not just set MQTT_MAX_PACKET_SIZE before including PubSubClient.h ?

arendst commented 7 years ago

Thomas see #87 and others. Just doesn't work in Arduino IDE.

thomasf commented 7 years ago

Hmm.. by looking at the cpp file generated by the Arduino preprocessor it might work if the define is added to the top of the main sonoff.ino file because it seems to be put at the top.. Maybe arduino does more stull which would make it not working,, I don't have time to verify anything atm,.

#include <Arduino.h>
#line 1 "/home/thomasf/src/github.com/arendst/Sonoff-MQTT-OTA-Arduino/sonoff/sonoff.ino"
#line 1 "/home/thomasf/src/github.com/arendst/Sonoff-MQTT-OTA-Arduino/sonoff/sonoff.ino"
/*
 * Sonoff and ElectroDragon by Theo Arends
 *
 * ====================================================
 * Prerequisites:
 *   - Change libraries/PubSubClient/src/PubSubClient.h
 *       #define MQTT_MAX_PACKET_SIZE 400
 *
 *   - Select IDE Tools - Flash size: "1M (64K SPIFFS)"
 * ====================================================
*/

#define VERSION                0x03000900   // 3.0.9

#define SONOFF                 1            // Sonoff, Sonoff SV, Sonoff Dual, Sonoff TH 10A/16A, S20 Smart Socket, 4 Channel
#define SONOFF_POW             9            // Sonoff Pow
#define ELECTRO_DRAGON         10           // Electro Dragon Wifi IoT Relay Board Based on ESP8266

#define DHT11                  11
#define DHT21                  21
#define DHT22                  22
#define AM2301                 21
#define AM2302                 22
#define AM2321                 22

enum log_t   {LOG_LEVEL_NONE, LOG_LEVEL_ERROR, LOG_LEVEL_INFO, LOG_LEVEL_DEBUG, LOG_LEVEL_DEBUG_MORE, LOG_LEVEL_ALL};
enum week_t  {Last, First, Second, Third, Fourth};
enum dow_t   {Sun=1, Mon, Tue, Wed, Thu, Fri, Sat};
enum month_t {Jan=1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};
enum wifi_t  {WIFI_RESTART, WIFI_SMARTCONFIG, WIFI_MANAGER, WIFI_WPSCONFIG, WIFI_RETRY, MAX_WIFI_OPTION};
enum msgf_t  {LEGACY, JSON, MAX_FORMAT};
enum swtch_t {TOGGLE, FOLLOW, FOLLOW_INV, PUSHBUTTON, PUSHBUTTON_INV, MAX_SWITCH_OPTION};
enum led_t   {LED_OFF, LED_POWER, LED_MQTTSUB, LED_POWER_MQTTSUB, LED_MQTTPUB, LED_POWER_MQTTPUB, LED_MQTT, LED_POWER_MQTT, MAX_LED_OPTION};

#include "user_config.h"
#include "user_config_override.h"

/*********************************************************************************************\
 * Enable feature by removing leading // or disable feature by adding leading //
\*********************************************************************************************/

//#define USE_SPIFFS                          // Switch persistent configuration from flash to spiffs (+24k code, +0.6k mem)

/*********************************************************************************************\
 * Not released yet
\*********************************************************************************************/

#if MODULE == SONOFF_POW
//  #define FEATURE_POWER_LIMIT
#endif
#define MAX_POWER_HOLD         10           // Time in SECONDS to allow max agreed power
#define MAX_POWER_WINDOW       30           // Time in SECONDS to disable allow max agreed power
#define SAFE_POWER_HOLD        10           // Time in SECONDS to allow max unit safe power
#define SAFE_POWER_WINDOW      30           // Time in MINUTES to disable allow max unit safe power
#define MAX_POWER_RETRY        5            // Retry count allowing agreed power limit overflow

/*********************************************************************************************\
 * No user configurable items below
\*********************************************************************************************/

#define SONOFF_DUAL            2            // (iTEAD PSB)
#define CHANNEL_3              3            // iTEAD PSB
#define CHANNEL_4              4            // iTEAD PSB
#define CHANNEL_5              5            // Future use
#define CHANNEL_6              6            // Future use
#define CHANNEL_7              7            // Future use
#define CHANNEL_8              8            // Future use

#ifndef SWITCH_MODE
#define SWITCH_MODE            TOGGLE       // TOGGLE, FOLLOW or FOLLOW_INV (the wall switch state)
#endif

#ifndef MQTT_FINGERPRINT
#define MQTT_FINGERPRINT       "A5 02 FF 13 99 9F 8B 39 8E F1 83 4F 11 23 65 0B 32 36 FC 07"
#endif

#define DEF_WIFI_HOSTNAME      "%s-%04d"    // Expands to <MQTT_TOPIC>-<last 4 decimal chars of MAC address>

#define HLW_PREF_PULSE         12530        // was 4975us = 201Hz = 1000W
#define HLW_UREF_PULSE         1950         // was 1666us = 600Hz = 220V
#define HLW_IREF_PULSE         3500         // was 1666us = 600Hz = 4.545A

#define MQTT_UNITS             0            // Default do not show value units (Hr, Sec, V, A, W etc.)
#define MQTT_SUBTOPIC          "POWER"      // Default MQTT subtopic (POWER or LIGHT)
#define MQTT_RETRY_SECS        10           // Seconds to retry MQTT connection
#define APP_POWER              0            // Default saved power state Off
#define MAX_DEVICE             1            // Max number of devices

#define STATES                 10           // loops per second
#define SYSLOG_TIMER           600          // Seconds to restore syslog_level

#define INPUT_BUFFER_SIZE      100          // Max number of characters in serial buffer
#define TOPSZ                  60           // Max number of characters in topic string
#define MESSZ                  240          // Max number of characters in JSON message string
#define LOGSZ                  128          // Max number of characters in log string
#ifdef USE_MQTT_TLS
  #define MAX_LOG_LINES        10           // Max number of lines in weblog
#else
  #define MAX_LOG_LINES        70           // Max number of lines in weblog
#endif

#define APP_BAUDRATE           115200       // Default serial baudrate

#ifdef USE_POWERMONITOR
  #define MAX_STATUS           9
#else
  #define MAX_STATUS           7
#endif

#define DOMOTICZ_RELAY_IDX3    0            // Relay 3 (4 Channel)
#define DOMOTICZ_RELAY_IDX4    0            // Relay 4 (4 Channel)
#define DOMOTICZ_KEY_IDX3      0            // Button 3 (4 Channel)
#define DOMOTICZ_KEY_IDX4      0            // Button 4 (4 Channel)

enum butt_t {PRESSED, NOT_PRESSED};

#include "support.h"                        // Global support
#include <Ticker.h>                         // RTC
#include <ESP8266WiFi.h>                    // MQTT, Ota, WifiManager
#include <ESP8266HTTPClient.h>              // MQTT, Ota
#include <ESP8266httpUpdate.h>              // Ota
#include <PubSubClient.h>                   // MQTT
#ifdef USE_WEBSERVER
  #include <ESP8266WebServer.h>             // WifiManager, Webserver
  #include <DNSServer.h>                    // WifiManager
#endif  // USE_WEBSERVER
#ifdef USE_SPIFFS
  #include <FS.h>                           // Config
#endif
#ifdef SEND_TELEMETRY_I2C
  #include <Wire.h>                         // I2C support library
#endif // SEND_TELEMETRY_I2C

typedef void (*rtcCallback)();

extern "C" uint32_t _SPIFFS_start;
extern "C" uint32_t _SPIFFS_end;
...
arendst commented 7 years ago

Well give it a try, test it thoroughly and if it works I'll implement but I won't spent any time on it anymore ;-)

hjgode commented 7 years ago

Josef, do you really experience non-working firmware when these parameters are not set correctly? I did not > encounter that problem and that's why the test and message is included in the first messages after reboot in both serial console and MQTT.

I thought this was the problem of 3.0.9 not running on my sonoffs. The firmware always crashed with an exception (3) and rebooted after wdt. One issue (self made? or firmware quirk?) was that my mosquitto server is set to retain and for whatever reason, cmnd/sonoff4/upgrade 1 was set with qos=1. That resulted in letting the sonoff4 (tested with other sonnoff too) do an upgrade from http, doing a reboot before the mqtt message had been acknowledged. That resulted in a boot/upgrade/crash loop. This stopped after I deleted the mosquitto.db file.

But this is not all, the 3.0.9 still does crash with exception(3) and wdt over and over. Finally I went back to 2.0.10 and the sonoffs run fine again know. Never touch a running system ;-)

thanks for all the code and patience.

For me it is OK if the MQTT_PACKET_SIZE warning reamins as is.

BTW: a) do we really need the warning about MQTT_PACKET_SIZE etc? b) putting another, same #define in the code before PubSubClient.h will result in error 'already defined' c) putting the #define after pubsubclient.h makes it useless

~josef

thomasf commented 7 years ago

Nope.. my pubsubclient.h is to be correctly written to not throw the error if already defined:

// MQTT_MAX_PACKET_SIZE : Maximum packet size
#ifndef MQTT_MAX_PACKET_SIZE
#define MQTT_MAX_PACKET_SIZE 128
#endif

// MQTT_KEEPALIVE : keepAlive interval in Seconds
#ifndef MQTT_KEEPALIVE
#define MQTT_KEEPALIVE 15
#endif

// MQTT_SOCKET_TIMEOUT: socket timeout interval in Seconds
#ifndef MQTT_SOCKET_TIMEOUT
#define MQTT_SOCKET_TIMEOUT 15
#endif
...
arendst commented 7 years ago

Josef, the change in MQTT_PACKET_SIZE is not needed for normal power on/off operation but for messages longer than the default of 128 characters it becomes a problem as not part of the message is send but the whole message is skipped if it is longer than 128 characters. The status messages would be longer messages as are the commands messages.

About the exception 3, what happens if you press and hold the button for over 4 seconds (Should reset migrated flash to user_config.h and hopefully stops running in exception)?

Thomas, have you checked it's functionality? Are you able to receive messages longer than 128 characters if you add your redefine of MQTT_MAX_PACKET_SIZE in sonoff.ino? (Answer probably NO).

hjgode commented 7 years ago

Hello Arend

during the MQTTdisaster with the retained Upgrade cmnd pressing and holding to load defaults did not help. Possibly as there was also a cmnd/sonoff/upgrade 1 in the air... The device restarted itself to fast for being accessed.

Now I am running only one sonoff with v3.0.9 and the rest (currenty 4 more) with 2.0.10. I assume 3.0.9 will run without issues except for the strange http connect (no host). I have to enter the ip several times in the browser and do Retry, before I get a connection. But this is nothing urgent.

Thanks for care

Josef

hjgode commented 7 years ago

I just realized that if compiled with platformio, the MQTT_MAX_PACKET_SIZE is already changed to 400 in platformio.ini:

build_flags = -Wl,-Tesp8266.flash.1m64.ld -DMQTT_MAX_PACKET_SIZE=400

If one compiles with Arduino IDE, the default packet size is used unless the following line is inserted in sonoff.ino BEFORE #include 'PubSubClient.h":

#define MQTT_MAX_PACKET_SIZE 400 The MQTT_MAX_PACKET_SIZE in PubSuClient.h and others were changed to be externally assignable after the discussion here: https://github.com/knolleary/pubsubclient/issues/110

I leave it up to you to include a compile #warning, #message or #error in sonoff.ino to at least inform the user before he sees the warning in the console messages (or not) of a running firmware.

~josef