logo elektroda
logo elektroda
X
logo elektroda

Motion sensor with BK7231N and TuyaMCU battery powered devices protocol

caubergjef57 14076 193
ADVERTISEMENT
  • #121 20545084
    p.kaczmarek2
    Moderator Smart Home
    Excuse me, there hasn't been any work done recently on the TuyaMCU. It is still pending and waiting for my update, but I have all the necessary data.

    Yesterday, I improved the connection speed. The WiFi connection is now very fast, and I have added support for static IP. MQTT connection will also occur faster, and it will retry if necessary in the next loop.
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #122 20545105
    spin55
    Level 17  
    p.kaczmarek2 wrote:
    Excuse me, there was no work done yet on the TuyaMCU, at least not in the last days. It is still pending and waiting for my update, but I have all the data I need.


    Don't worry. I am aware of the great work you do and how busy you are. I publish these data in case they can be of help.

    p.kaczmarek2 wrote:
    Yesterday I improved connection speed. WiFi connection is now very fast, I also added static IP support. MQTT connect will also happen faster. It will retry if needed in the next loop.


    Good!!

    Thank you so much
  • ADVERTISEMENT
  • #123 20545129
    p.kaczmarek2
    Moderator Smart Home
    My current plan is to write a simple "battery powered device simulator" for Windows OpenBeken simulator and then implement the battery powered devices protocol correctly. I will start working on that in a few days, I hope.
    Helpful post? Buy me a coffee.
  • #124 20545135
    spin55
    Level 17  
    Very good idea. I don't know how you manage to deal with so many tasks. Great job.

    Added after 21 [minutes]:

    Just a doubt that bothers me:

    When you send a notification to TuyaMCU that the WiFi module is connected (55 AA 00 02 00 01 04 06), do you verify if it is really connected? Or is it just a simulated communication?
  • #125 20548488
    spin55
    Level 17  
    OK.
    With Build on Apr 20 2023 09:18:13 version 1.17.12 and Flag 37 enabled.

    Added after 30 [minutes]:

    There are fringes but this seems the way.

    Info:MAIN:Main_Init_Before_Delay
    Info:CFG:####### Boot Count 3665 #######
    Warn:CFG:CFG_InitAndLoad: Correct config has been loaded with 7 changes count.
    Error:CMD:no file early.bat err -2
    Info:GEN:PIN_SetupPins pins have been set up.
    Info:MAIN:Main_Init_Before_Delay done
    Info:MAIN:Main_Init_Delay
    Info:MAIN:Main_Init_Delay done
    Info:MAIN:Main_Init_After_Delay
    Info:MAIN:ssid: key:
    Info:MAIN:Using SSID []
    Info:MAIN:Using Pass []
    Info:MQTT:MQTT_RegisterCallback called for bT obk1F8D1236/ subT obk1F8D1236/+/set
    Info:MQTT:MQTT_RegisterCallback called for bT bekens/ subT bekens/+/set
    Info:MQTT:MQTT_RegisterCallback called for bT cmnd/obk1F8D1236/ subT cmnd/obk1F8D1236/+
    Info:MQTT:MQTT_RegisterCallback called for bT cmnd/bekens/ subT cmnd/bekens/+
    Info:MQTT:MQTT_RegisterCallback called for bT obk1F8D1236/ subT obk1F8D1236/+/get
    Info:CMD:CMD_StartScript: started autoexec.bat at the beginning
    Info:MAIN:Main_Init_After_Delay done
    Info:MAIN:Started TuyaMCU.
    Info:MAIN:Started tmSensor.
    Info:GEN:Channel 1 type changed to readonly
    Info:GEN:Channel 4 type changed to readonly
    Info:MAIN:Time 1, idle 287196/s, free 73184, MQTT 0(0), bWifi 0, secondsWithNoPing -1, socks 2/38 
    Info:TuyaMCU:TUYAMCU received: 55 AA 00 00 00 01 00 00 
    Info:TuyaMCU:TuyaMCU_ProcessIncoming[ver=0]: processing command 0 (Hearbeat) with 8 bytes
    Info:MAIN:Time 2, idle 184804/s, free 72760, MQTT 0(0), bWifi 0, secondsWithNoPing -1, socks 2/38 
    Info:MAIN:Time 3, idle 67744/s, free 73200, MQTT 0(0), bWifi 0, secondsWithNoPing -1, socks 2/38 
    Info:TuyaMCU:TUYAMCU received: 55 AA 00 01 00 24 7B 22 70 22 3A 22 6C 79 64 77 6F 62 77 79 71 78 69 79 6F 62 39 68 22 2C 22 76 22 3A 22 31 2E 30 2E 35 22 7D 62 
    Info:TuyaMCU:TuyaMCU_ProcessIncoming[ver=0]: processing command 1 (QueryProductInformation) with 43 bytes
    Info:TuyaMCU:TuyaMCU_ParseQueryProductInformation: received {"p":"lydwobwyqxiyob9h","v":"1.0.5"}
    Info:MAIN:Time 4, idle 0/s, free 73200, MQTT 0(0), bWifi 0, secondsWithNoPing -1, socks 2/38 
    Info:MAIN:Time 5, idle 0/s, free 73200, MQTT 0(0), bWifi 0, secondsWithNoPing -1, socks 2/38 
    Info:MAIN:Main_OnWiFiStatusChange - WIFI_STA_CONNECTING - 1
    Info:MAIN:Main_OnWiFiStatusChange - WIFI_STA_CONNECTED - 4
    Info:MQTT:mqtt_userName 
    mqtt_pass 
    mqtt_clientID obk1F8D1236
    mqtt_host 192.168.1.45:1883
    Info:MQTT:Connect error in mqtt_client_connect - code: -4 (ERR_RTE)
    Info:TuyaMCU:TUYAMCU received: 55 AA 00 00 00 01 00 00 
    Info:TuyaMCU:TuyaMCU_ProcessIncoming[ver=0]: processing command 0 (Hearbeat) with 8 bytes
    Info:MAIN:Time 6, idle 102685/s, free 73296, MQTT 0(1), bWifi 1, secondsWithNoPing -1, socks 2/38 
    Info:MAIN:Boot complete time reached (5 seconds)
    Info:CFG:####### Set Boot Complete #######
    Info:MAIN:Main_OnWiFiStatusChange - WIFI_STA_CONNECTED - 4
    Info:MQTT:mqtt_userName 
    mqtt_pass 
    mqtt_clientID obk1F8D1236
    mqtt_host 192.168.1.45:1883
    Info:TuyaMCU:TUYAMCU received: 55 AA 00 02 00 00 01 
    Info:TuyaMCU:TuyaMCU_ProcessIncoming[ver=0]: processing command 2 (MCUconf) with 7 bytes
    Info:TuyaMCU:TuyaMCU_ProcessIncoming: TUYA_CMD_MCU_CONF, TODO!
    Info:TuyaMCU:TUYAMCU received: 55 AA 00 05 00 05 01 04 00 01 00 0F 
    Info:TuyaMCU:TuyaMCU_ProcessIncoming[ver=0]: processing command 5 (WiFiSelect) with 12 bytes
    Info:TuyaMCU:TuyaMCU_V0_ParseRealTimeWithRecordStorage: processing dpId 1, dataType 4-DP_TYPE_ENUM and 1 data bytes
    Info:TuyaMCU:TuyaMCU_V0_ParseRealTimeWithRecordStorage: raw data 1 byte: 
    Info:GEN:No change in channel 1 (still set to 0) - ignoring
    
    Info:MAIN:Time 7, idle 173928/s, free 71024, MQTT 0(2), bWifi 1, secondsWithNoPing -1, socks 2/38 
    Info:MAIN:Time 8, idle 178741/s, free 70832, MQTT 0(2), bWifi 1, secondsWithNoPing -1, socks 2/38 
    Info:MAIN:Time 9, idle 193307/s, free 73248, MQTT 0(2), bWifi 1, secondsWithNoPing -1, socks 2/38 
    Info:MQTT:mqtt_connection_cb: Successfully connected
    Info:MQTT:mqtt_subscribed to obk1F8D1236/+/set
    Info:MQTT:mqtt_subscribed to bekens/+/set
    Info:MQTT:mqtt_subscribed to cmnd/obk1F8D1236/+
    Info:MQTT:mqtt_subscribed to cmnd/bekens/+
    Info:MQTT:mqtt_subscribed to obk1F8D1236/+/get
    Info:MQTT:Publishing val obk1F8D1236 to obk1F8D1236/host retain=0
    Info:TuyaMCU:TUYAMCU received: 55 AA 00 00 00 01 00 00 
    Info:TuyaMCU:TuyaMCU_ProcessIncoming[ver=0]: processing command 0 (Hearbeat) with 8 bytes
    Info:MAIN:Time 10, idle 187380/s, free 73248, MQTT 1(2), bWifi 1, secondsWithNoPing -1, socks 2/38 
    Info:GEN:dhcp=0 ip=192.168.1.57 gate=192.168.1.1 mask=255.255.255.0 mac=38:1f:8d:12:36:69 
    Info:GEN:sta: 1, softap: 0, b/g/n
    Info:GEN:sta:rssi=-67,ssid=,bssid=00:1a:2b:48:20:34 ,channel=13,cipher_type:CCMP
    Info:MQTT:Publishing val Build on Apr 20 2023 09:18:13 version 1.17.12 to obk1F8D1236/build retain=0
    Info:MAIN:Time 11, idle 182046/s, free 72976, MQTT 1(2), bWifi 1, secondsWithNoPing -1, socks 2/38
  • ADVERTISEMENT
  • #126 20560809
    p.kaczmarek2
    Moderator Smart Home
    OpenBeken update for better battery-powered devices protocol is very soon. @spin55, are you still here?
    @DeDaMrAz will also help testing, hopefully.
    Helpful post? Buy me a coffee.
  • #127 20560983
    spin55
    Level 17  
    Hello,

    I'm still here, but today I won't be able to do anything. Family meeting.

    Regards
  • #128 20560988
    p.kaczmarek2
    Moderator Smart Home
    Thanks, no problem, just let me know when you're ready to test.

    I will be committing changes to official releases tonight. I hope @DeDaMrAz will also help with initial testing.

    I have also entered the gathered data to the SelfTest system so OBK now basically tests itself on Windows simulator start:
    Motion sensor with BK7231N and TuyaMCU battery powered devices protocol
    Helpful post? Buy me a coffee.
  • #129 20563703
    spin55
    Level 17  
    Hello,
    Although I can't spend as much time as I'd like, I'll be around if you need me to do any testing.

    Regards
  • #130 20563733
    p.kaczmarek2
    Moderator Smart Home
    The firmware is ready for testing. Please update your device.
    Helpful post? Buy me a coffee.
  • #132 20563748
    p.kaczmarek2
    Moderator Smart Home
    Yes, it should work. It has been tested by me on door sensors and by @DeDaMrAz on a humidity/temperature sensor.

    How it works for you? What is your autoexec.bat? Why do you have no drivers running?
    Helpful post? Buy me a coffee.
  • #133 20563759
    spin55
    Level 17  
    p.kaczmarek2 wrote:
    What is your autoexec.bat?


    startDriver TuyaMCU
    startDriver tmSensor
    setChannelType 1 readonly
    // vincular dpid al canal 1
    linkTuyaMCUOutputToChannel 1 val 1
    setChannelType 4 readonly
    // vincular dpid al canal 4
    linkTuyaMCUOutputToChannel 4 val 4


    There is no data because the web does not start.

    Added after 1 [minutes]:

    With Build on Apr 20 2023 09:18:13 version 1.17.12 and Flag 37 enabled.

    Sometimes it starts and sometimes it doesn't.

    Added after 2 [minutes]:

    With version 599 the communication between chips is stable but it does not start.
  • #134 20563770
    p.kaczmarek2
    Moderator Smart Home
    How does UART log (from TX2) looks like?
    Helpful post? Buy me a coffee.
  • #135 20563771
    DeDaMrAz
    Level 19  
    spin55 wrote:
    p.kaczmarek2 wrote:
    What is your autoexec.bat?


    startDriver TuyaMCU
    startDriver tmSensor
    setChannelType 1 readonly
    // vincular dpid al canal 1
    linkTuyaMCUOutputToChannel 1 val 1
    setChannelType 4 readonly
    // vincular dpid al canal 4
    linkTuyaMCUOutputToChannel 4 val 4


    There is no data because the web does not start.

    Added after 1 [minutes]:

    With Build on Apr 20 2023 09:18:13 version 1.17.12 and Flag 37 enabled.

    Sometimes it starts and sometimes it doesn't.

    Added after 2 [minutes]:

    With version 599 the communication between chips is stable but it does not start.


    @spin55 Can you post the cold boot log from that device? Also very important device must be powered via TuyaMCU ( so battery terminals ie) not directly on the WiFi module!!

    Also add this line to your autoexec.bat file

    // may be needed, depends on device change to 9600 and test
    tuyaMCU_setBaudRate 115200
  • #136 20563774
    spin55
    Level 17  
    DeDaMrAz wrote:
    @spin55 Can you post the cold boot logo from that device?


    For that I will have to force AP mode, we'll see if it enters.

    Also very important device must be powered via TuyaMCU ( so battery terminals ie) not directly!!

    Yes, I am supplying a 3.3V source at the battery terminals.
  • #137 20563779
    p.kaczmarek2
    Moderator Smart Home
    I don't think that @spin55 device requires baud 115200. I am not sure, but I think it was reported in this topic that he captured packets at 9600 (default baud).
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #138 20563786
    spin55
    Level 17  
    p.kaczmarek2 wrote:
    I don't think that @spin55 device requires baud 115200. I am not sure, but I think it was reported in this topic that he captured packets at 9600 (default baud).


    Indeed. I have a lot of Logs of communication between chips and the speed is 9600 baud.

    Added after 55 [seconds]:

    But I can't change autoexec either because I can't force it to AP mode, it doesn't respond.
  • #139 20563788
    DeDaMrAz
    Level 19  
    spin55 wrote:
    p.kaczmarek2 wrote:
    I don't think that @spin55 device requires baud 115200. I am not sure, but I think it was reported in this topic that he captured packets at 9600 (default baud).


    Indeed. I have a lot of Logs of communication between chips and the speed is 9600 baud.


    Fair enough can you post the log from TX2 line then?
  • #140 20563793
    spin55
    Level 17  
    DeDaMrAz wrote:
    Fair enough can you post the log from TX2 line then?


    This chip is on the board, I'm going to look to see if I can access Tx2.
    A simultaneous Log Tx/Rx with RealTerm between chips doesn't work for you?

    Added after 52 [seconds]:

    Motion sensor with BK7231N and TuyaMCU battery powered devices protocol
  • #141 20563798
    p.kaczmarek2
    Moderator Smart Home
    Maybe try using the pair mode? Press button on the device for a long time (6 seconds). Wait 5 seconds, press it for 6 seconds again. Etc. Device should stay online enough time to connect.

    It works on my door sensors.
    Helpful post? Buy me a coffee.
  • #143 20563805
    spin55
    Level 17  
    p.kaczmarek2 wrote:
    Maybe try using the pair mode? Press button on the device for a long time (6 seconds). Wait 5 seconds, press it for 6 seconds again. Etc. Device should stay online enough time to connect.


    That's what I mean when I talk about passing it to AP mode. It also works well for me with version 599, but with these, sometimes it does and most of it doesn't

    Added after 1 [minutes]:

    Quote:
    @spin55 ¿puede publicar eso en formato de texto tal vez o en un archivo txt?


    A moment.
  • #144 20563807
    p.kaczmarek2
    Moderator Smart Home
    spin55 wrote:

    That's what I mean when I talk about passing it to AP mode. It also works well for me with version 599, but with these, sometimes it does and most of it doesn't

    I am afraid that you are misunderstanding me. I am not talking about OpenBeken AP mode. I am saying that, with Tuya devices with TuyaMCU, if you press button for a long time several times, they will not shut down WiFi module. WiFi module will stay powered for over a minute or so. At least in case of my door sensors.
    Helpful post? Buy me a coffee.
  • #145 20563820
    spin55
    Level 17  
    p.kaczmarek2 wrote:
    I am afraid that you are misunderstanding me. I am not talking about OpenBeken AP mode. I am saying that, with Tuya devices with TuyaMCU, if you press button for a long time several times, they will not shut down WiFi module. WiFi module will stay powered for over a minute or so. At least in case of my door sensors.


    I follow the instructions in the manual to enter AP mode. Long press until the leds turn off and repeat the sequence again. I don't know if there is another way to do it but it has worked well for me so far.
    capture...txt Download (1.47 kB) capture...txt Download (1.7 kB)
  • #146 20563831
    DeDaMrAz
    Level 19  
    I apologize for jumping in the middle of this conversation :) I forgot to mention that.

    Those captures to me are a sign that your device is in AP mode (which you can trigger by power cycling the device 5 times - the button on the device may do that as well, not sure).

    OBK outputs the log on the second TX2 line that is what I would be interested in seeing if possible.

    Also you can power the Wi-Fi module directly and setup all you need to do (IP, MQTT, autoexec...) but again if possible.
  • #147 20563837
    p.kaczmarek2
    Moderator Smart Home
    The log you have attached contains two perfectly valid TuyaMCU transactions. There seems to be nothing wrong with it.

    The only strange part is here:
    Motion sensor with BK7231N and TuyaMCU battery powered devices protocol
    Let me quote this:
    
    55 AA	00	02		00 01	03	05	
    HEADER	VER=00	McuConf		LEN	03	CHK	
    
    55 AA	00	02		00 01	04	06	
    HEADER	VER=00	McuConf		LEN	04	CHK	
    
    

    first MCUconf is with WiFi state 0x03 (WiFi connected) and second is with 0x04 (MQTT connected).

    Let's look at the code:
    Motion sensor with BK7231N and TuyaMCU battery powered devices protocol
    It is not possible to get 0x04 WiFi state packet (MQTT connected) unless you have MQTT connection (or are on windows, or force default state) !!

    You must have MQTT connection working.

    @spin55, can you check/confirm, do you get MQTT connection working?

    The following log may mean that only HTTP is not working for you.

    @spin55, does device show up in DHCP?

    @spin55 , get device IP and run:
    
    ping 192.168.0.111 -t
    

    (change IP to yours), does it respond?

    The only problem possibility is that you have somehow WWW panel broken, but that would be very strange. Maybe try to Restore RF partition in BK flasher on in our Web App?
    Helpful post? Buy me a coffee.
  • #148 20563865
    spin55
    Level 17  
    @spin55, can you check/confirm, do you get MQTT connection working?

    1683048619: New connection from 192.168.1.57 on port 1883.
    1683048619: New client connected from 192.168.1.57 as obk1F8D1236 (p2, c1, k100, u'homeassistant').
    1683048641: New connection from 192.168.1.57 on port 1883.
    1683048641: Client obk1F8D1236 already connected, closing old connection.
    1683048641: New client connected from 192.168.1.57 as obk1F8D1236 (p2, c1, k100, u'homeassistant').
    1683048799: Client obk1F8D1236 has exceeded timeout, disconnecting.
    1683048874: New connection from 192.168.1.57 on port 1883.
    [INFO] found homeassistant on local database
    1683048875: New client connected from 192.168.1.57 as obk1F8D1236 (p2, c1, k100, u'homeassistant').
    1683048920: New connection from 192.168.1.57 on port 1883.
    1683048920: Client obk1F8D1236 already connected, closing old connection.
    1683048920: New client connected from 192.168.1.57 as obk1F8D1236 (p2, c1, k100, u'homeassistant').
    1683049076: Client obk1F8D1236 has exceeded timeout, disconnecting.
    1683049172: Saving in-memory database to /data/mosquitto.db.

    Added after 3 [minutes]:

    DeDaMrAz wrote:
    I apologize for jumping in the middle of this conversation I forgot to mention that.


    You do not need to apologize. All collaboration is appreciated.

    Those captures to me are a sign that your device is in AP mode (which you can trigger by power cycling the device 5 times - the button on the device may do that as well, not sure).

    If it was in AP mode it would see the SSID, but no. I can take it to AP OpenBeken mode, connecting and disconnecting several times in a row, but it doesn't solve anything.

    DeDaMrAz wrote:
    OBK outputs the log on the second TX2 line that is what I would be interested in seeing if possible.


    To access I have to unsolder the board and flip it over because Tx2 is not accessible from this side. I'll look more closely.

    DeDaMrAz wrote:
    Also you can power the Wi-Fi module directly and setup all you need to do (IP, MQTT, autoexec...) but again if possible.


    All the configuration of iP, MQTT, autoexec is already done. On the first boot it can be done because the drivers are not loaded yet.

    There is no wifi module. the BK7321N is on the board and if I tried to feed it independently I don't know if I would succeed. In addition TuyaMCU would continue to have control.

    Regards

    Added after 1 [minutes]:

    Sorry for not being more explicit but the language is a big barrier for me.

    Added after 3 [minutes]:

    p.kaczmarek2 wrote:
    @spin55, does device show up in DHCP?


    I have assigned a fixed IP as you will have seen.
  • #149 20563897
    p.kaczmarek2
    Moderator Smart Home
    Please do the ping check. Tell me if device is pingable from your PC (when it's on the same LAN) when it's waken up.
    Helpful post? Buy me a coffee.
  • #150 20563933
    spin55
    Level 17  
    p.kaczmarek2 wrote:
    Please do the ping check. Tell me if device is pingable from your PC (when it's on the same LAN) when it's waken up.


    If the ping test is to find out if there is MQTT communication with HA, I already say yes. But I know from this, because the boot is random and there is no way to catch ping.

    Motion sensor with BK7231N and TuyaMCU battery powered devices protocol

Topic summary

The discussion revolves around configuring a motion sensor device powered by the BK7231N chip and utilizing the TuyaMCU protocol. Users share experiences and troubleshooting steps related to flashing the device, setting up the autoexec.bat file, and ensuring proper communication between the TuyaMCU and the Wi-Fi module. Key issues include the device not waking from sleep, MQTT communication problems, and the need for specific commands to maintain connectivity. Solutions involve using RealTerm for monitoring, adjusting baud rates, and ensuring the correct setup of drivers and commands in the autoexec.bat file. Users also discuss the importance of maintaining a stable power supply and the potential for firmware updates to improve functionality.
Summary generated by the language model.
ADVERTISEMENT