Skip to content

Commit

Permalink
v7.0 - Major Update - Plug Mini and ESP32 Meshing (#91)
Browse files Browse the repository at this point in the history
v7.0 - Major Update

- Mesh multiple ESP32s together. Set one ESP32 as the primary and the others as secondary. View Readme and Examples folder for configurations
- Support for plug mini (with power monitoring)
- Curtain position will move as the curtain is moving
- Active Scanning VS Passive Scanning
- Better memory task management between BLE and WIFI/MQTT
  • Loading branch information
devWaves authored Aug 9, 2022
1 parent 32964fc commit 84ecc50
Show file tree
Hide file tree
Showing 5 changed files with 371 additions and 0 deletions.
100 changes: 100 additions & 0 deletions Examples/Config-Example1/Mesh-PrimaryESP32-Config-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@

/*
*
* THIS EXAMPLE IS FOR THE PRIMARY ESP32 IN A MESH
*
* Choose a balance of settings between active scanning and passive scanning. Active scanning uses more battery of switchbot devices but it required for bot, curtain meter
*
*
* THIS IS NOT THE FULL CODE. These are the settings that apply to a mesh setup
*/


/* Wifi Settings */
static const char* host = "esp32"; // Unique name for ESP32. The name detected by your router and MQTT. If you are using more then 1 ESPs to control different switchbots be sure to use unique hostnames. Host is the MQTT Client name and is used in MQTT topics

/* Mesh Settings */
/* Ignore if only one ESP32 is used */
static const bool enableMesh = true; // Ignore if only one ESP32 is used. Set to false
static const char* meshHost = ""; // Ignore if only one ESP32 is used. Ignore if you don't have either meter/contact/motion. Enter the host value of the primary ESP32 if you are using multiple esp32s and you want to mesh them together for better contact/motion
static const bool meshMeters = true; // Mesh meters together if meshHost is set. The meter values will use the meshHost MQTT topics
static const bool meshContactSensors = true; // Mesh contact sensors together if meshHost is set. The contact values will use the meshHost MQTT topics.
static const bool meshMotionSensors = true; // Mesh motion sensors together if meshHost is set. The motion values will use the meshHost MQTT topics
static const bool onlyAllowRootESPToPublishContact = true; // All meshed messages for contact and motions sensors will pass through the root mesh host ESP32. Only the root host will send contact and motion messages
static const bool onlyAllowRootESPToPublishMotion = false; // All meshed messages for motion will pass through the root mesh host ESP32. Only the root host will send motion messages
static const bool onlyAllowRootESPToPublishLight = false; // All meshed messages for illuminance will pass through the root mesh host ESP32. Only the root host will send illuminance messages
static const bool countContactToAvoidDuplicates = true; // count the number of open/close/timeout over all esp32s so that none are duplicated
static const bool countMotionToAvoidDuplicates = false; // count the number of motion/no motion over all esp32s so that none are duplicated
static const bool countLightToAvoidDuplicates = false; // count the number of dark/bright over all esp32s so that none are duplicated
static const int timeToIgnoreDuplicates = 30; // if a duplicate is determined, ignore it within X seconds



/* Switchbot Bot Settings */
static std::map<std::string, std::string> allBots = {
{ "switchbotone", "xX:xX:xX:xX:xX:xX" },
{ "switchbottwo", "yY:yY:yY:yY:yY:yY" }
};

/* Switchbot Curtain Settings */
static const int curtainClosedPosition = 10; // When 2 curtains are controlled (left -> right and right -> left) it's possible one of the curtains pushes one of the switchbots more open. Change this value to set a position where a curtain is still considered closed
static std::map<std::string, std::string> allCurtains = {
{ "curtainone", "xX:xX:xX:xX:xX:xX" },
{ "curtaintwo", "yY:yY:yY:yY:yY:yY" }
};

/* Switchbot Meter Settings */
static std::map<std::string, std::string> allMeters = {
{ "meterone", "xX:xX:xX:xX:xX:xX" },
{ "metertwo", "yY:yY:yY:yY:yY:yY" }
};

/* Switchbot Contact Sensor Settings */
static std::map<std::string, std::string> allContactSensors = {
{ "contactone", "xX:xX:xX:xX:xX:xX" },
{ "contacttwo", "yY:yY:yY:yY:yY:yY" }
};

/* Switchbot Motion Sensor Settings */
static std::map<std::string, std::string> allMotionSensors = {
{ "motionone", "xX:xX:xX:xX:xX:xX" },
{ "motiontwo", "yY:yY:yY:yY:yY:yY" }
};

/* Switchbot Plug Mini Settings */
static std::map<std::string, std::string> allPlugs = {
{ "plugone", "xX:xX:xX:xX:xX:xX" },
{ "plugtwo", "yY:yY:yY:yY:yY:yY" }
};

/* Switchbot Bot Passwords */
static std::map<std::string, std::string> allPasswords = { // Set all the bot passwords (setup in app first). Ignore if passwords are not used
{ "switchbotone", "switchbotonePassword" },
{ "switchbottwo", "switchbottwoPassword" }
};


static const int rescanTime = 10800; // Automatically perform a full active scan for device info of all devices every X seconds (default 3 hours). XXXXActiveScanSecs will also active scan on schedule

static const int defaultBotScanAfterControlSecs = 10; // Default How many seconds to wait for state/status update call after set/control command. *override with botScanTime list
static const int defaultCurtainScanAfterControlSecs = 30; // Default How many seconds to wait for state/status update call after set/control command. *override with botScanTime list. Also used by scanWhileCurtainIsMoving
static const int defaultBotMQTTUpdateSecs = 600; // Used only when alwaysMQTTUpdate = false. Default MQTT Update for bot every X seconds. Note: a change in state will be always be published either way during active scanning
static const int defaultCurtainMQTTUpdateSecs = 600; // Used only when alwaysMQTTUpdate = false. Default MQTT Update for curtain every X seconds. Note: a change in state will be always be published either way during active scanning
static const int defaultMeterMQTTUpdateSecs = 600; // Used only when alwaysMQTTUpdate = false. Default MQTT Update for meter temp sensors every X seconds. Note: a change in state will be always be published either way at all times (active or passive)
static const int defaultMotionMQTTUpdateSecs = 600; // Used only when alwaysMQTTUpdate = false. Default MQTT Update for motion sensors every X seconds. Note: a change in state will be always be published either way at all times (active or passive)
static const int defaultContactMQTTUpdateSecs = 600; // Used only when alwaysMQTTUpdate = false. Default MQTT Update for contact temp sensors every X seconds. Note: a change in state will be always be published either way at all times (active or passive)
static const int defaultPlugMQTTUpdateSecs = 600; // Used only when alwaysMQTTUpdate = false. Default MQTT Update for motion sensors every X seconds. Note: a change in state will be always be published either way at all times (active or passive)
static const int defaultBotActiveScanSecs = 1800; // Default Active Scan for bot every X seconds if not active scanned since X seconds. *override with botScanTime list.
static const int defaultCurtainActiveScanSecs = 1800; // Default Active Scan for curtain every X seconds if not active scanned since X seconds. *override with botScanTime list.
static const int defaultMeterActiveScanSecs = 3600; // Default Active Scan for meter temp sensors every X seconds if not active scanned since X seconds. *override with botScanTime list.
static const int defaultMotionActiveScanSecs = 3600; // Default Active Scan for motion sensors every X seconds if not active scanned since X seconds. *override with botScanTime list.
static const int defaultContactActiveScanSecs = 3600; // Default Active Scan for contact temp sensors every X seconds if not active scanned since X seconds. *override with botScanTime list.
static const int defaultPlugActiveScanSecs = 3600; // Default Active Scan for motion sensors every X seconds if not active scanned since X seconds. *override with botScanTime list.

static const bool autoRescan = true; // perform automatic rescan (uses rescanTime and initialScan).
static const bool activeScanOnSchedule = true; // perform an active scan on decice types based on the scheduled seconds values for XXXXActiveScanSecs

static const bool alwaysMQTTUpdate = false; // If the ESP32 is scanning, always publish MQTT data instead of using set times. ***Note: This creates a lot of MQTT traffic
static const bool onlyActiveScan = false; // Active scanning requires more battery from the BLE switchbot devices. If false, passive scanning is used when possible for contact/motion
static const bool onlyPassiveScan = false; // If this ESP32 is a mesh ESP32 or you only have motion/contact/meter sensors. Passive scanning uses less battery from BLE switchbot devices. Passive scanning provides less data then active scanning, but uses less battery
static const bool alwaysActiveScan = false; // No battery optimizations. If you are using the switchbot hub or app to control devices also and you want immediate state updates for bot and curtains in MQTT set to true
88 changes: 88 additions & 0 deletions Examples/Config-Example1/Mesh-SecondaryESP32-Config-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@

/*
*
* THIS EXAMPLE IS FOR THE SECONDARY ESP32 IN A MESH
*
* Choose a balance of settings between active scanning and passive scanning. Active scanning uses more battery of switchbot devices but it required for bot, curtain meter
*
*
* THIS IS NOT THE FULL CODE. These are the settings that apply to a mesh setup
*/


/* Wifi Settings */
static const char* host = "esp32mesh1"; // Unique name for ESP32. The name detected by your router and MQTT. If you are using more then 1 ESPs to control different switchbots be sure to use unique hostnames. Host is the MQTT Client name and is used in MQTT topics

/* Mesh Settings */
/* Ignore if only one ESP32 is used */
static const bool enableMesh = true; // Ignore if only one ESP32 is used. Set to false
static const char* meshHost = "esp32"; // Ignore if only one ESP32 is used. Ignore if you don't have either meter/contact/motion. Enter the host value of the primary ESP32 if you are using multiple esp32s and you want to mesh them together for better contact/motion
static const bool meshMeters = true; // Mesh meters together if meshHost is set. The meter values will use the meshHost MQTT topics
static const bool meshContactSensors = true; // Mesh contact sensors together if meshHost is set. The contact values will use the meshHost MQTT topics.
static const bool meshMotionSensors = true; // Mesh motion sensors together if meshHost is set. The motion values will use the meshHost MQTT topics
static const bool onlyAllowRootESPToPublishContact = true; // All meshed messages for contact and motions sensors will pass through the root mesh host ESP32. Only the root host will send contact and motion messages
static const bool onlyAllowRootESPToPublishMotion = false; // All meshed messages for motion will pass through the root mesh host ESP32. Only the root host will send motion messages
static const bool onlyAllowRootESPToPublishLight = false; // All meshed messages for illuminance will pass through the root mesh host ESP32. Only the root host will send illuminance messages
static const bool countContactToAvoidDuplicates = true; // count the number of open/close/timeout over all esp32s so that none are duplicated
static const bool countMotionToAvoidDuplicates = false; // count the number of motion/no motion over all esp32s so that none are duplicated
static const bool countLightToAvoidDuplicates = false; // count the number of dark/bright over all esp32s so that none are duplicated
static const int timeToIgnoreDuplicates = 30; // if a duplicate is determined, ignore it within X seconds



/* Switchbot Bot Settings */
static std::map<std::string, std::string> allBots = {
};

/* Switchbot Curtain Settings */
static std::map<std::string, std::string> allCurtains = {
};

/* Switchbot Meter Settings */
static std::map<std::string, std::string> allMeters = {
{ "meterone", "xX:xX:xX:xX:xX:xX" },
{ "metertwo", "yY:yY:yY:yY:yY:yY" }
};

/* Switchbot Contact Sensor Settings */
static std::map<std::string, std::string> allContactSensors = {
{ "contactone", "xX:xX:xX:xX:xX:xX" },
{ "contacttwo", "yY:yY:yY:yY:yY:yY" }
};

/* Switchbot Motion Sensor Settings */
static std::map<std::string, std::string> allMotionSensors = {
{ "motionone", "xX:xX:xX:xX:xX:xX" },
{ "motiontwo", "yY:yY:yY:yY:yY:yY" }
};

/* Switchbot Plug Mini Settings */
static std::map<std::string, std::string> allPlugs = {
};



static const int rescanTime = 21600; // Automatically perform a full active scan for device info of all devices every X seconds (default 3 hours). XXXXActiveScanSecs will also active scan on schedule

static const int defaultBotScanAfterControlSecs = 10; // Default How many seconds to wait for state/status update call after set/control command. *override with botScanTime list
static const int defaultCurtainScanAfterControlSecs = 30; // Default How many seconds to wait for state/status update call after set/control command. *override with botScanTime list. Also used by scanWhileCurtainIsMoving
static const int defaultBotMQTTUpdateSecs = 600; // Used only when alwaysMQTTUpdate = false. Default MQTT Update for bot every X seconds. Note: a change in state will be always be published either way during active scanning
static const int defaultCurtainMQTTUpdateSecs = 600; // Used only when alwaysMQTTUpdate = false. Default MQTT Update for curtain every X seconds. Note: a change in state will be always be published either way during active scanning
static const int defaultMeterMQTTUpdateSecs = 600; // Used only when alwaysMQTTUpdate = false. Default MQTT Update for meter temp sensors every X seconds. Note: a change in state will be always be published either way at all times (active or passive)
static const int defaultMotionMQTTUpdateSecs = 600; // Used only when alwaysMQTTUpdate = false. Default MQTT Update for motion sensors every X seconds. Note: a change in state will be always be published either way at all times (active or passive)
static const int defaultContactMQTTUpdateSecs = 600; // Used only when alwaysMQTTUpdate = false. Default MQTT Update for contact temp sensors every X seconds. Note: a change in state will be always be published either way at all times (active or passive)
static const int defaultPlugMQTTUpdateSecs = 600; // Used only when alwaysMQTTUpdate = false. Default MQTT Update for motion sensors every X seconds. Note: a change in state will be always be published either way at all times (active or passive)
static const int defaultBotActiveScanSecs = 1800; // Default Active Scan for bot every X seconds if not active scanned since X seconds. *override with botScanTime list.
static const int defaultCurtainActiveScanSecs = 1800; // Default Active Scan for curtain every X seconds if not active scanned since X seconds. *override with botScanTime list.
static const int defaultMeterActiveScanSecs = 3600; // Default Active Scan for meter temp sensors every X seconds if not active scanned since X seconds. *override with botScanTime list.
static const int defaultMotionActiveScanSecs = 3600; // Default Active Scan for motion sensors every X seconds if not active scanned since X seconds. *override with botScanTime list.
static const int defaultContactActiveScanSecs = 3600; // Default Active Scan for contact temp sensors every X seconds if not active scanned since X seconds. *override with botScanTime list.
static const int defaultPlugActiveScanSecs = 3600; // Default Active Scan for motion sensors every X seconds if not active scanned since X seconds. *override with botScanTime list.

static const bool autoRescan = true; // perform automatic rescan (uses rescanTime and initialScan).
static const bool activeScanOnSchedule = false; // perform an active scan on decice types based on the scheduled seconds values for XXXXActiveScanSecs

static const bool alwaysMQTTUpdate = false; // If the ESP32 is scanning, always publish MQTT data instead of using set times. ***Note: This creates a lot of MQTT traffic
static const bool onlyActiveScan = false; // Active scanning requires more battery from the BLE switchbot devices. If false, passive scanning is used when possible for contact/motion
static const bool onlyPassiveScan = false; // If this ESP32 is a mesh ESP32 or you only have motion/contact/meter sensors. Passive scanning uses less battery from BLE switchbot devices. Passive scanning provides less data then active scanning, but uses less battery
static const bool alwaysActiveScan = false; // No battery optimizations. If you are using the switchbot hub or app to control devices also and you want immediate state updates for bot and curtains in MQTT set to true
Loading

0 comments on commit 84ecc50

Please sign in to comment.