-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request wled#992 from gegu/master
Usermods: PIR sensor switch, v2 unreachable net services
- Loading branch information
Showing
5 changed files
with
518 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Fix unreachable net services V2 | ||
|
||
This usermod-v2 modification performs a ping request to the local IP address every 60 seconds. By this procedure the net services of WLED remains accessible in some problematic WLAN environments. | ||
|
||
The modification works with static or DHCP IP address configuration. | ||
|
||
**Webinterface**: The number of pings and reconnects is displayed on the info page in the web interface. | ||
|
||
_Story:_ | ||
|
||
Unfortunately, with all ESP projects where a web server or other network services are running, I have the problem that after some time the web server is no longer accessible. Now I found out that the connection is at least reestablished when a ping request is executed by the device. | ||
|
||
With this modification, in the worst case, the network functions are not available for 60 seconds until the next ping request. | ||
|
||
## Installation | ||
|
||
1. Copy the file `usermod_Fix_unreachable_netservices.h` to the `wled00` directory. | ||
2. Register the usermod by adding `#include "usermod_Fix_unreachable_netservices.h"` in the top and `registerUsermod(new FixUnreachableNetServices());` in the bottom of `usermods_list.cpp`. | ||
|
||
Example **usermods_list.cpp**: | ||
|
||
```cpp | ||
#include "wled.h" | ||
/* | ||
* Register your v2 usermods here! | ||
* (for v1 usermods using just usermod.cpp, you can ignore this file) | ||
*/ | ||
|
||
/* | ||
* Add/uncomment your usermod filename here (and once more below) | ||
* || || || | ||
* \/ \/ \/ | ||
*/ | ||
//#include "usermod_v2_example.h" | ||
//#include "usermod_temperature.h" | ||
//#include "usermod_v2_empty.h" | ||
#include "usermod_Fix_unreachable_netservices.h" | ||
|
||
void registerUsermods() | ||
{ | ||
/* | ||
* Add your usermod class name here | ||
* || || || | ||
* \/ \/ \/ | ||
*/ | ||
//usermods.add(new MyExampleUsermod()); | ||
//usermods.add(new UsermodTemperature()); | ||
//usermods.add(new UsermodRenameMe()); | ||
usermods.add(new FixUnreachableNetServices()); | ||
|
||
} | ||
``` | ||
|
||
Hopefully I can help someone with that - @gegu |
138 changes: 138 additions & 0 deletions
138
usermods/Fix_unreachable_netservices_v2/usermod_Fix_unreachable_netservices.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
#pragma once | ||
|
||
#include "wled.h" | ||
#include <ping.h> | ||
|
||
/* | ||
* This usermod performs a ping request to the local IP address every 60 seconds. | ||
* By this procedure the net services of WLED remains accessible in some problematic WLAN environments. | ||
* | ||
* Usermods allow you to add own functionality to WLED more easily | ||
* See: https://github.com/Aircoookie/WLED/wiki/Add-own-functionality | ||
* | ||
* v2 usermods are class inheritance based and can (but don't have to) implement more functions, each of them is shown in this example. | ||
* Multiple v2 usermods can be added to one compilation easily. | ||
* | ||
* Creating a usermod: | ||
* This file serves as an example. If you want to create a usermod, it is recommended to use usermod_v2_empty.h from the usermods folder as a template. | ||
* Please remember to rename the class and file to a descriptive name. | ||
* You may also use multiple .h and .cpp files. | ||
* | ||
* Using a usermod: | ||
* 1. Copy the usermod into the sketch folder (same folder as wled00.ino) | ||
* 2. Register the usermod by adding #include "usermod_filename.h" in the top and registerUsermod(new MyUsermodClass()) in the bottom of usermods_list.cpp | ||
*/ | ||
|
||
class FixUnreachableNetServices : public Usermod { | ||
private: | ||
//Private class members. You can declare variables and functions only accessible to your usermod here | ||
unsigned long m_lastTime = 0; | ||
|
||
// desclare required variables | ||
const unsigned int PingDelayMs = 60000; | ||
unsigned long m_connectedWiFi = 0; | ||
ping_option m_pingOpt; | ||
unsigned int m_pingCount = 0; | ||
|
||
public: | ||
//Functions called by WLED | ||
|
||
/* | ||
* setup() is called once at boot. WiFi is not yet connected at this point. | ||
* You can use it to initialize variables, sensors or similar. | ||
*/ | ||
void setup() { | ||
//Serial.println("Hello from my usermod!"); | ||
} | ||
|
||
|
||
/* | ||
* connected() is called every time the WiFi is (re)connected | ||
* Use it to initialize network interfaces | ||
*/ | ||
void connected() { | ||
//Serial.println("Connected to WiFi!"); | ||
|
||
++m_connectedWiFi; | ||
|
||
// initialize ping_options structure | ||
memset(&m_pingOpt, 0, sizeof(struct ping_option)); | ||
m_pingOpt.count = 1; | ||
m_pingOpt.ip = WiFi.localIP(); | ||
|
||
} | ||
|
||
|
||
/* | ||
* loop() is called continuously. Here you can check for events, read sensors, etc. | ||
* | ||
* Tips: | ||
* 1. You can use "if (WLED_CONNECTED)" to check for a successful network connection. | ||
* Additionally, "if (WLED_MQTT_CONNECTED)" is available to check for a connection to an MQTT broker. | ||
* | ||
* 2. Try to avoid using the delay() function. NEVER use delays longer than 10 milliseconds. | ||
* Instead, use a timer check as shown here. | ||
*/ | ||
void loop() { | ||
if (m_connectedWiFi > 0 && millis()-m_lastTime > PingDelayMs) | ||
{ | ||
ping_start(&m_pingOpt); | ||
m_lastTime = millis(); | ||
++m_pingCount; | ||
} | ||
} | ||
|
||
|
||
/* | ||
* addToJsonInfo() can be used to add custom entries to the /json/info part of the JSON API. | ||
* Creating an "u" object allows you to add custom key/value pairs to the Info section of the WLED web UI. | ||
* Below it is shown how this could be used for e.g. a light sensor | ||
*/ | ||
void addToJsonInfo(JsonObject& root) | ||
{ | ||
//this code adds "u":{"⚡ Ping fix pings": m_pingCount} to the info object | ||
JsonObject user = root["u"]; | ||
if (user.isNull()) user = root.createNestedObject("u"); | ||
|
||
JsonArray infoArr = user.createNestedArray("⚡ Ping fix pings"); //name | ||
infoArr.add(m_pingCount); //value | ||
|
||
//this code adds "u":{"⚡ Reconnects": m_connectedWiFi - 1} to the info object | ||
infoArr = user.createNestedArray("⚡ Reconnects"); //name | ||
infoArr.add(m_connectedWiFi - 1); //value | ||
} | ||
|
||
|
||
/* | ||
* addToJsonState() can be used to add custom entries to the /json/state part of the JSON API (state object). | ||
* Values in the state object may be modified by connected clients | ||
*/ | ||
void addToJsonState(JsonObject& root) | ||
{ | ||
//root["user0"] = userVar0; | ||
} | ||
|
||
|
||
/* | ||
* readFromJsonState() can be used to receive data clients send to the /json/state part of the JSON API (state object). | ||
* Values in the state object may be modified by connected clients | ||
*/ | ||
void readFromJsonState(JsonObject& root) | ||
{ | ||
//userVar0 = root["user0"] | userVar0; //if "user0" key exists in JSON, update, else keep old value | ||
//if (root["bri"] == 255) Serial.println(F("Don't burn down your garage!")); | ||
} | ||
|
||
|
||
/* | ||
* getId() allows you to optionally give your V2 usermod an unique ID (please define it in const.h!). | ||
* This could be used in the future for the system to determine whether your usermod is installed. | ||
*/ | ||
uint16_t getId() | ||
{ | ||
return USERMOD_ID_FIXNETSERVICES; | ||
} | ||
|
||
//More methods can be added in the future, this example will then be extended. | ||
//Your usermod will remain compatible as it does not need to implement all methods from the Usermod base class! | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# PIR sensor switch | ||
|
||
This usermod-v2 modification allows the connection of a PIR sensor to switch on the LED strip when motion is detected. The switch-off occurs ten minutes after no more motion is detected. | ||
|
||
_Story:_ | ||
|
||
I use the PIR Sensor to automatically turn on the WLED analog clock in my home office room when I am there. | ||
The LED strip is switched [using a relay](https://github.com/Aircoookie/WLED/wiki/Control-a-relay-with-WLED) to keep the power consumption low when it is switched off. | ||
|
||
## Webinterface | ||
|
||
The info page in the web interface shows the items below | ||
|
||
- the state of the sensor. By clicking on the state the sensor can be deactivated/activated. | ||
**I recommend to deactivate the sensor before installing an OTA update**. | ||
- the remaining time of the off timer. | ||
|
||
## JSON API | ||
|
||
The usermod supports the following state changes: | ||
|
||
| JSON key | Value range | Description | | ||
|------------|-------------|---------------------------------| | ||
| PIRenabled | bool | Deactivdate/activate the sensor | | ||
| PIRoffSec | 60 to 43200 | Off timer seconds | | ||
|
||
## Sensor connection | ||
|
||
My setup uses an HC-SR501 sensor, a HC-SR505 should also work. | ||
|
||
The usermod uses GPIO13 (D1 mini pin D7) for the sensor signal. | ||
[This example page](http://www.esp8266learning.com/wemos-mini-pir-sensor-example.php) describes how to connect the sensor. | ||
|
||
Use the potentiometers on the sensor to set the time-delay to the minimum and the sensitivity to about half, or slightly above. | ||
|
||
## Usermod installation | ||
|
||
1. Copy the file `usermod_PIR_sensor_switch.h` to the `wled00` directory. | ||
2. Register the usermod by adding `#include "usermod_PIR_sensor_switch.h"` in the top and `registerUsermod(new PIRsensorSwitch());` in the bottom of `usermods_list.cpp`. | ||
|
||
Example **usermods_list.cpp**: | ||
|
||
```cpp | ||
#include "wled.h" | ||
/* | ||
* Register your v2 usermods here! | ||
* (for v1 usermods using just usermod.cpp, you can ignore this file) | ||
*/ | ||
|
||
/* | ||
* Add/uncomment your usermod filename here (and once more below) | ||
* || || || | ||
* \/ \/ \/ | ||
*/ | ||
//#include "usermod_v2_example.h" | ||
//#include "usermod_temperature.h" | ||
//#include "usermod_v2_empty.h" | ||
#include "usermod_PIR_sensor_switch.h" | ||
|
||
void registerUsermods() | ||
{ | ||
/* | ||
* Add your usermod class name here | ||
* || || || | ||
* \/ \/ \/ | ||
*/ | ||
//usermods.add(new MyExampleUsermod()); | ||
//usermods.add(new UsermodTemperature()); | ||
//usermods.add(new UsermodRenameMe()); | ||
usermods.add(new PIRsensorSwitch()); | ||
|
||
} | ||
``` | ||
|
||
Have fun - @gegu |
Oops, something went wrong.