logo elektroda
logo elektroda
X
logo elektroda

Door/window sensor without TuyaMCU - deep sleep and energy saving, OpenBeken

p.kaczmarek2 13545 69
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #31 20671552
    taggbricka
    Level 7  
    Quote:
    Do you have battery relay pin set?

    I don't understand this. This is my pin setup.
    Screenshot showing pin configuration settings of an electronic device, with functions assigned to each pin.

    EDIT:
    I copied the pin settings from the Tuya PIR sensor, this had a battery relay pin.
    Now battery voltage measurement work as as expected.
  • ADVERTISEMENT
  • #32 20671570
    p.kaczmarek2
    Moderator Smart Home
    I am not an expert when it comes to battery driver, but you've posted this JSON in your previous post:
    
    	"samp_sw_pin":"17",
    	"max_V":"3000",
    	"min_V":"2200",
    	"samp_pin":"23",
    

    This means that sampling pin (ADC) is P23 and the sampling switch (Battery Relay) pin is P17.

    See the related battery driver article:
    https://www.elektroda.com/rtvforum/topic3959103.html
    Helpful post? Buy me a coffee.
  • #33 20951862
    Mad_Maxs
    Level 11  
    p.kaczmarek2 wrote:
    Sending HTTP GET also allows you to control other devices (e.g. Tasmoty or OBK, via cm?cmnd=COMMAND), but I don`t know if it is needed, because OBK also supports the Tasmoty Device Groups protocol anyway:
    https://www.elektroda.pl/rtvforum/topic3925472.html

    Could I ask for an example of how to set the sensor in OBK so that when it wakes up, it performs a GET request to an external HTTP server?
  • ADVERTISEMENT
  • #34 20951901
    p.kaczmarek2
    Moderator Smart Home
    All examples are here:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/autoexecExamples.md
    If sending data via HTTP GET, search for "SendGET" on the website, for example:
    Screenshot of an example script using the SHT30 sensor and sending data via HTTP GET.
    Related forum post:
    https://www.elektroda.com/rtvforum/topic3945688-180.html#20693161
    The script was run by @@GUTEK@
    Helpful post? Buy me a coffee.
  • #35 20953340
    Mad_Maxs
    Level 11  
    Is it possible to reduce the waiting time for mqtt connection? Or should I disable it completely so that deepSleep follows the defined DSTime?
  • ADVERTISEMENT
  • #36 20953480
    p.kaczmarek2
    Moderator Smart Home
    Hello, from your description it looks like it uses the DoorSensor driver. DoorSensor assumes that the sensor is used with MQTT. However, then you write that you want to completely turn off waiting for MQTT... maybe it would be better to script the sensor "on foot" using the command PinDeepSleep ? I posted a similar example in the previous post, you just replace DeepSleep with PinDeepSleep to wake up with a button.
    Helpful post? Buy me a coffee.
  • #37 20953574
    Mad_Maxs
    Level 11  
    @p.kaczmarek2 admits that it crossed my mind, although I wasn`t sure which command I should use to send him to sleep :)
    So in my case, the reed switch energizes the device and:
    waitFor WiFiState 4
    SendGET http:/ip?doorState=$var1
    delay_s 10 (poczekam na zamknięcie drzwi)
    PinDeepSleep (żeby poszedł spać aż znowu obudzę go kontaktronem) 


    The question is how to extract the variable from the reed switch? In terms of whether the door is open or closed.
    How to extract state from BatteryADC to a variable? publishFloat "voltage" $CH1/1000 ?? (from your examples) always gives me 0
    How to code a trigger that will send SendGET when the sensor state changes during wake-up?
  • #38 20953662
    p.kaczmarek2
    Moderator Smart Home
    Mad_Maxs wrote:

    
    SendGET http:/ip?doorState=$var1


    This is not correct. We store variables in channels, for example:
    
    SendGET http:/ip?doorState=$CH1

    Mad_Maxs wrote:

    The question is how to extract the variable from the reed switch?

    You set the pin with the sensor to the appropriate role (e.g. dInput - digital input, or its version with pull up) and assign it a channel, it will read the value 1 or 0 to this channel, and you will use $CH1 in the script (change the index accordingly) to read the channel.

    Even ADC is handled in the same way - you set the ADC pin, assign a channel to it, and the script has access to the values from the ADC. But that`s just incidentally, I`m giving an example.

    Mad_Maxs wrote:

    How to extract state from BatteryADC to a variable? publishFloat "voltage" $CH1/1000 ?? (from your examples) always gives me 0

    Search for "battery" in the list here:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/constants.md
    The above assumes that the battery controller is configured.

    Mad_Maxs wrote:

    How to code a trigger that will send SendGET when the sensor state changes during wake-up?

    This is a strange question. You currently have a script that is executed immediately after waking up, so in fact you have already created the trigger.

    I will just add a small tip - depending on what you want to achieve, it is worth considering the situation in which someone opens the door for one second and then closes it immediately.

    PS: You chose the challenge with this door sensor a bit difficult. This can be problematic, because if you create a script like "wait 1 second and sleep", you will block the possibility of saving the sensor without UART. I recommend caution. Maybe it would be worth practicing with longer delays to get to know the environment and not make any mistakes in the script due to ignorance.

    Added after 46 [seconds]:

    You can try it yourself and maybe tomorrow I will write (but without testing) a sketch/example of autoexec for what you want to achieve

    Added after 14 [minutes]:

    p.kaczmarek2 wrote:

    Mad_Maxs wrote:

    How to code a trigger that will send SendGET when the sensor state changes during wake-up?

    This is a strange question. You currently have a script that is executed immediately after waking up, so in fact you have already created the trigger.


    Now I think so, maybe this question is about what if someone opens and closes the door before the WiFi is connected?
    If so, script writing the sensor value to another channel at startup and publish both values via HTTP after connecting to WiFi.
    Something like:
    
    // save door state at boot time - it assumes that channel 5 is a temporary variable, and channel 1 is door sensor
    setChannel 5 $CH1
    // wait for WiFi
    waitFor WiFistate 4
    // send both states
    SendGET http:/ip?doorState=$CH5
    delay_s 2
    SendGET http:/ip?doorState=$CH1
    
    Helpful post? Buy me a coffee.
  • #39 20953734
    Mad_Maxs
    Level 11  
    @p.kaczmarek2 so putting everything together, I get:
    
    setChannel 5 $CH0
    waitFor WiFiState 4
    SendGET http://IP/sensor.php?doorSensor=1&sensor=$CH5&bat=$batteryLevel
    delay_s 2
    SendGET http://IP/sensor.php?doorSensor=1&sensor=$CH0&bat=$batteryLevel
    delay_s 2
    PinDeepSleep

    Unfortunately, there is always a chance that someone will shoot perfectly at the moment when the sensor falls asleep, although tests show that you must be lucky. This delay_2 before PinDeepSleep is important. Without it, it was impossible to send the second GET

    I saw it with the channels, but somehow I convinced myself that it should be a value, e.g. $CH00 instead of $CH0.
    Sometimes it`s hard to get off such a wrong path on your own.
    I already made Delay_s 1 by adding DeepSleep 120 to autoexec.bat :) apparently a person once programmed ESP8266, where the command was similar, but when I made the connection, it was too late. An additional question arises: how to remove such a whip from the programmer level? Because I uploaded the firmware again, but the configuration was saved ;) only when I uploaded ori and it went obk

    Added after 6 [minutes]:

    I think that it would be worth adding an if here that will check whether $ch5 == $ch0, and if so, it will not send a second SendGET
  • #40 20953786
    p.kaczmarek2
    Moderator Smart Home
    Mad_Maxs wrote:
    An additional question arises: how to remove such a whip from the programmer level?

    I would suggest scripting better in bat so that an additional button (e.g. with a Btn_ScriptOnly pin, or even as DigitalInput) checks whether it is pressed and if it is pressed, exits the script.
    Maybe something like:
    
    // script assumes there is a digitalInput on channel 2, which is low by default and high if user presses the button
    if $CH2==1 then goto nothing
    // here remaining script 
    (...)
    // at the end of everything:
    nothing:
    

    The above code will make it so that when the configuration button is pressed, "goto" will skip the rest of the script and leave the device turned on permanently (until the user reboots)


    Mad_Maxs wrote:

    I think that it would be worth adding an if here that will check whether $ch5 == $ch0, and if so, it will not send a second SendGET

    You`re right, I thought about it, but I didn`t want to complicate it anymore
    Helpful post? Buy me a coffee.
  • #41 20954087
    Mad_Maxs
    Level 11  
    p.kaczmarek2 wrote:
    I would suggest scripting better in bat so that an additional button (e.g. with a Btn_ScriptOnly pin, or even as DigitalInput) checks whether it is pressed and if it is pressed, exits the script.
    Maybe something like:
    Code: text Expand Select all Copy to clipboard
    // script assumes there is a digitalInput on channel 2, which is low by default and high if user presses the button
    if $CH2==1 then goto nothing
    // here remaining script
    (...)
    // at the end of everything:
    nothing:

    The above code will make it so that when the configuration button is pressed, "goto" will skip the rest of the script and leave the device turned on permanently (until the user reboots)

    Great idea :) added to the code
    p.kaczmarek2 wrote:
    Mad_Maxs wrote:
    I think that it would be worth adding an if here that will check whether $ch5 == $ch0, and if so, it will not send a second SendGET

    You`re right, I thought about it, but I didn`t want to complicate it anymore

    So, summing up all the additional changes, we get:
    
    setChannel 5 $CH0
    waitFor WiFiState 4
    SendGET http://ip/sensor.php?doorSensor=1&sensor=$CH5&bat=$batteryLevel
    delay_s 5
    if $CH5!=$CH0 then SendGET http://ip/sensor.php?doorSensor=1&sensor=$CH0&bat=$batteryLevel; delay_s 2
    if $CH1==1 then goto nothing
    PinDeepSleep
    
    nothing:
    

    I added this IF with a comparison of channels 5 and 0, just check if I wrote down the execution of two commands correctly (separated by a semicolon), are any additional brackets necessary?

    If I replaced PinDeepSleep with the DeepSleep 43 200 command, is wake-up from the pin still available, but I have a control signal every 12 hours? in ESP, excitation from the pin was independent of the code, but I want to make sure :)
  • #42 20954195
    p.kaczmarek2
    Moderator Smart Home
    This won`t work. At the beginning with @btsimonh we decided to follow the basic Tasmota syntax, so if you want to enter several commands with a semicolon you have to use backlog . backlog something; something; something
    
    if $CH5!=$CH0 then SendGET http://ip/sensor.php?doorSensor=1&sensor=$CH0&bat=$batteryLevel; delay_s 2
    


    Quote:

    I added this IF with a comparison of channels 5 and 0, just check if I wrote down the execution of two commands correctly (separated by a semicolon), are any additional brackets necessary?

    You could divide these delay_s 5 into 5 delay_s 1 (with a loop or manually) and check this safe mode pin every second

    Quote:

    If I replaced PinDeepSleep with the DeepSleep 43 200 command, is wake-up from the pin still available, but I have a control signal every 12 hours? in ESP, excitation from the pin was independent of the code, but I want to make sure :)

    Currently, DeepSleep will not be woken up from the pin because there was no such option in the SDK. Only recently a PR for the SDK appeared:
    https://github.com/openshwprojects/OpenBK7231N/pull/18
    And only in the future will I integrate it, then it will work as you write.
    Helpful post? Buy me a coffee.
  • #43 20955046
    Mad_Maxs
    Level 11  
    @p.kaczmarek2 , so it will be correct:
    
    if $CH5!=$CH0 then backlog SendGET http://ip/sensor.php?doorSensor=1&sensor=$CH0&bat=$batteryLevel; delay_s 2
    

    p.kaczmarek2 wrote:
    You could divide these delay_s 5 into 5 delay_s 1 (with a loop or manually) and check this safe mode pin every second

    You`re right, but in its current form, you`d have to wait those few seconds while keeping it pressed. Alternatively, in nothing, you could blink the WiFi LED 3 times as confirmation of exiting the script to be sure that it is finished.
    p.kaczmarek2 wrote:
    Currently, DeepSleep will not be woken up from the pin because there was no such option in the SDK. Only recently a PR for the SDK appeared:
    https://github.com/openshwprojects/OpenBK7231N/pull/18

    It remains to follow the release change ;)
  • #44 20971864
    Mad_Maxs
    Level 11  
    Has anyone tried programming such a board? BK7231N looks good, but is it possible to solder the programmer somewhere other than the chip itself?
    Close-up of a PCB with a BK7231N chip. PCB board with BK7231N chip on a world map background. Close-up of a PCB featuring the BK7231N chip and electronic components.
  • #45 20971881
    p.kaczmarek2
    Moderator Smart Home
    And on the other hand? Are there no pads on the other side of the PCB? You will need to unsolder the battery connector.
    Helpful post? Buy me a coffee.
  • #46 20972260
    Mad_Maxs
    Level 11  
    Indeed, after desoldering, there are pads on the other side
  • ADVERTISEMENT
  • #47 20972263
    p.kaczmarek2
    Moderator Smart Home
    Can you post a photo?
    Helpful post? Buy me a coffee.
  • #48 20972327
    Mad_Maxs
    Level 11  
    Bright:
    Close-up of an electronic module with connected wires and a battery compartment.

    However, I have a strange problem: I loaded the pinout into it:
    Code: JSON
    Log in, to see the code

    I have a reading of the battery status, on channel 0 I see a change when the magnet changes position, and yet after switching to PinDeepSleep mode, the change in the magnet position does not wake him up

    //EDIT
    I found that the btn is pin 14, now when it sleeps, I can wake it up with the button, but it still doesn`t respond to the magnet.

    By the way, how can you calibrate the battery reading, because it`s a bit off with the meter ;)
  • #49 20972420
    p.kaczmarek2
    Moderator Smart Home
    Doesn`t changing the position of the magnet wake up the equipment only when, for example, "opening the door" or only when "closing" it, or does it not wake up at all?

    You must correctly select the pin role, i.e. DoorSnsrWSleep without pull up/down or DoorSnsrWSleep with pull up or pull down.
    Additionally, you also need to choose one of the three DSEdge options.

    Added after 31 [seconds]:

    https://www.elektroda.com/rtvforum/find.php?q=DSEdge
    Helpful post? Buy me a coffee.
  • #50 20972455
    Mad_Maxs
    Level 11  
    Bingo! DSEdge 1 solved the problem :) I read the description for DoorSensor, typed it, was surprised and saw your comment :)

    
    if $CH2==1 then goto nothing
    PinDeepSleep
    
    nothing:
    

    It does the job, although btn works more like a switch. By default, $CH2 is 0, I press (and release) and the state changes to 1, I press and release again and the state returns to 0. Is it supposed to be like that? Because this is another device that reacts this way to the use of BTN
    
    if $CH5!=$CH0 then backlog SendGET http://ip/sensor.php?doorSensor=3&sensor=$CH0&bat=$batteryLevel; delay_s 2
    

    Something about this piece of code doesn`t work for me, it seems to completely ignore the line (I`m assuming there`s some syntax error)

    //EDIT: I fixed the alias
    
    alias check_again backlog SendGET http://ip/sensor.php?doorSensor=3&sensor=$CH0&bat=$batteryLevel; delay_s 2
    
    if $CH5!=$CH0 then check_again
    
  • #51 20972517
    p.kaczmarek2
    Moderator Smart Home
    Congratulations, you found an edge case in the if that was handled incorrectly by the parser. I posted an update, can you first disable deep sleep and then do an OTA to the version that will appear on Github in about 10 minutes (when it compiles)?

    EDIT: yes, an alias can help, but I also posted a fix for it on the repo so this problem won`t happen again, the automatic testing system will take care of it:
    Source code in a text editor showing various test commands and conditional statements in a programming language.
    Helpful post? Buy me a coffee.
  • #52 20972578
    Mad_Maxs
    Level 11  
    p.kaczmarek2 wrote:
    I posted an update, can you first disable deep sleep and then do an OTA to the version that will appear on Github in about 10 minutes (when it compiles)?

    I can check it tomorrow since I`ve already sorted out my mess :) but there are still 7 pieces left to work on, so I`ll check for sure :)
  • #53 20972947
    p.kaczmarek2
    Moderator Smart Home
    A lot of these sensors for one WiFi network. How far will they be from the router?
    Helpful post? Buy me a coffee.
  • #54 20973013
    Mad_Maxs
    Level 11  
    p.kaczmarek2 wrote:
    A lot of these sensors for one WiFi network. How far will they be from the router?

    Difficult question, because I have a mesh :) max 10 meters to the nearest antenna. I have a dedicated vWlan for these gadgets :)

    MyFriend from China played a trick on me and out of 7 sensors, one was different :) the little one was sitting there so much that it was dying because of the battery, I destroyed it, but it worked:
    Electronic board of a Tuya door sensor with visible circuits and connectors on a brown background. Printed circuit board of a sensor with visible markings and a connector. Tuya Smart Door Sensor circuit board with BK7231N chip. Tuya Smart Door Sensor with parts spread out on a table. Close-up view of a circuit board with components, featuring a lit blue LED.
    
    {
      "vendor": "Tuya",
      "bDetailed": "0",
      "name": "Tuya Smart Door Sensor Wifi",
      "model": "enter short model name here",
      "chip": "BK7231N",
      "board": "TODO",
      "flags": "0",
      "keywords": [
        "TODO",
        "TODO",
        "TODO"
      ],
      "pins": {
        "8": "DoorSnsrWSleep;0",
        "17": "BAT_Relay;1",
        "20": "Btn;2",
        "23": "BAT_ADC;1",
        "26": "WifiLED;0"
      }
      "image": "https://obrazki.elektroda.pl/8866914500_1708590926.jpg",
      "wiki": "https://www.elektroda.pl/rtvforum/topic3960149.html"
    }
    
  • #55 20973078
    p.kaczmarek2
    Moderator Smart Home
    Please complete the link to the image and forum ("wiki") and I will add both templates to https://openbekeniot.github.io/webapp/devicesList.html

    Does the device read batteries well? Maybe you need to change the constants when starting the driver?
    Helpful post? Buy me a coffee.
  • #56 20973122
    Mad_Maxs
    Level 11  
    p.kaczmarek2 wrote:
    Does the device read batteries well? Maybe you need to change the constants when starting the driver?

    Well, I was just about to ask about the calibration of this battery voltage reading, because it`s a bit inconsistent

    p.kaczmarek2 wrote:
    Please complete the link to the image and forum ("wiki") and I will add both templates to https://openbekeniot.github.io/webapp/devicesList.html

    How to add execute startup to the template? Because you need to enable the Battery and DoorSensor drivers, I also set DSEdge 1 in autoexec and turn off Flag 42 (but this is for consistency of configuration with previous sensors)

    Added after 5 [hours] 1 [minutes]:

    p.kaczmarek2 wrote:
    I posted an update, can you first disable deep sleep and then do an OTA to the version that will appear on Github in about 10 minutes (when it compiles)?

    My code:
    
    setChannel 5 $CH0
    DSEdge 1
    alias check_again backlog SendGET http://ip/sensor.php?doorSensor=5&sensor=$CH0&bat=$batteryLevel; delay_s 2
    
    waitFor WiFiState 4
    SendGET http://ip/sensor.php?doorSensor=5&sensor=$CH5&bat=$batteryLevel
    delay_s 5
    
    if $CH5!=$CH0 then backlog SendGET http://ip/sensor.php?doorSensor=5&sensor=$CH0&bat=$batteryLevel; delay_s 2
    
    if $CH2==1 then goto nothing
    
    PinDeepSleep
    
    nothing:
    

    and unfortunately only the first GET goes to the server. Tested on:
    
    Build on Feb 22 2024 07:58:31 version 1.17.477
    

    When instead of a command, I post check_again , works correctly
  • #57 20974794
    p.kaczmarek2
    Moderator Smart Home
    From what I see, it didn`t get into 477, but into 478:
    OpenBK7231T/OpenBeken version 1.17.478 release information.
    Additionally, all automatic tests for this phrase pass correctly:
    Code snippet in a programming language with CMD_ExecuteCommand and SELFTEST_ASSERT_CHANNEL instructions.
    Helpful post? Buy me a coffee.
  • #58 20975036
    Mad_Maxs
    Level 11  
    p.kaczmarek2 wrote:
    From what I see, it didn`t get into 477, but into 478:

    I upgraded to the latest version 478 and looking at the logs (because I have the server that is supposed to process this GET at home), I see that the code is implemented correctly :) Good job ;)
  • #59 20987616
    bchris326
    Level 1  
    Hi,

    I have successfully flashed the device and everything appears to be operating as expected. However, altering the DSTime setting does not result in any change. The countdown always starts from 60, regardless of the setting. I've attempted to modify this in the startup code and tried initiating the Driver beforehand, but to no avail. Does anyone know how to change the DSTime setting or is it a bug?

    Thanks!
  • #60 20996494
    Mad_Maxs
    Level 11  
    p.kaczmarek2 wrote:
    Does the device read batteries well? Maybe you need to change the constants when starting the driver?

    Can you tell me how to calibrate it correctly? I have 100% for a very long time, but at 36% the battery is already discharged so much that the sensor does not wake up

Topic summary

The discussion focuses on configuring a door/window sensor using OpenBeken without TuyaMCU, emphasizing deep sleep functionality for energy efficiency. The sensor connects to WiFi only to send state changes to an MQTT server, minimizing power consumption. Users inquire about HTTP event notifications, firmware updates, and the impact of static IP on battery life. Various responses address configuration issues, connection times, and the importance of using the correct firmware version. The conversation also covers the calibration of battery readings, the implementation of GET requests upon waking, and troubleshooting connection delays. Key solutions include using the DSEdge command for wake-up configuration and ensuring proper pin assignments for functionality.
Summary generated by the language model.
ADVERTISEMENT