logo elektroda
logo elektroda
X
logo elektroda

Zemismart SPM01-D2TW Energy Counter: Data Issues with Tuya MCU and ESP02S Swap

Pete0815 5475 35
Best answers

How can I decode the Zemismart SPM01-D2TW Tuya MCU data after swapping the CB2S module, and what is the correct way to do the two-step UART capture on a CB2S-powered board?

Put the original CB2S back in and capture the UART traffic from the MCU with the board powered safely from 5V into the input of the 3.3V regulator, not from mains; start the capture before applying power so you catch the early boot messages [#21072618] On your board, the 5V source should go to the LDO input and you should see 3.3V at the module output; if the board uses a step-down converter instead, feed that converter’s input in the same way [#21072162][#21072618] If the chip is already desoldered, a 2 MB backup is still useful, and you can also flash OpenBeken to the module and then solder it back [#21072162][#21080800] For TuyaMCU devices, some data only appears when the MCU believes Wi‑Fi/cloud is present, so use either a real MQTT connection or force WiFi state 0x04, then run `tuyaMcu_sendQueryState` or even `addRepeatingEvent 5 -1 tuyaMcu_sendQueryState` if needed [#21072162][#21080877][#21126038] In this meter, dpID 6 carries the raw voltage/current/power packet and dpID 1 is total energy; the thread eventually got stable reporting with either `waitFor MQTTState 1` + one-shot query or `tuyaMcu_defWiFiState 4` + repeating query [#21080877][#21126038]
ADVERTISEMENT
  • #31 21099682
    Pete0815
    Level 7  
    p.kaczmarek2 wrote:
    Wait @Pete0815, so what exactly helped?


    Mmmh, need some more testing. For a quick answer, I just changed the state query back to a one-shot from repeating every 5 sec and expecting this is it.
    BUT, the energy counter is still working fine after this.

    So very strange because of no function before. Will test more carefully to find out.

    Edit: Tested again and again but no result why.

    The autoexec.bat is now:
    // This script provides extra REST page for hosting in LittleFS
    // Please see: https://www.elektroda.com/rtvforum/topic4040354.html
    // Script taken from: https://www.elektroda.com/rtvforum/viewtopic.php?p=20990058#20990058
    // Download HTML there.
    
    // Clear all previous definitions
    clearIO
    
    // clear flags
    // flags 0
    
    // set flag 46
    SetFlag 46 1
    
    // start TuyaMCU driver
    startDriver TuyaMCU
    tuyaMcu_setBaudRate 9600
    
    // This one is better than tuyaMcu_defWiFiState 4;  MQTTState 1 = WiFiState 4
    // issuing of tuyaMcu_defWiFiState 4 continues the script,
    // but doesnt report to MQTT since there is still no connection.
    // if you didn't setup MQTT connection then issue tuyaMcu_defWiFiState 4
    // and comment waitFor MQTTState 1
    
    waitFor MQTTState 1
    //tuyaMcu_defWiFiState 4
    
    // Total energy - Dpid 1 "total_forward_energy" -> channel 4
    linkTuyaMCUOutputToChannel 1 val 4
    setChannelType 4 EnergyTotal_kWh_div100
    setChannelLabel 4 "Total Energy"
    
    // Measurements - Dpid 6 "phase_a" - channel RAW_TAC2121C_VCP -> 5,6,7
    // TAC2121C VoltageCurrentPower Packet
    // This will automatically set voltage, power and current
    linkTuyaMCUOutputToChannel 6 RAW_TAC2121C_VCP
    setChannelType 5 Voltage_div10
    setChannelLabel 5 "Voltage"
    setChannelType 6 Power
    setChannelLabel 6 "Power"
    setChannelType 7 Current_div1000
    setChannelLabel 7 "Current"
    
    
    // Fault - Dpid 9 "fault" -> channel 9
    linkTuyaMCUOutputToChannel 9 raw 9
    setChannelType 9 ReadOnly
    setChannelLabel 9 "Fault"
    
    // Prepayment on-off - Dpid 11 "switch_prepayment" -> channel 2
    linkTuyaMCUOutputToChannel 11 bool 2
    setChannelType 2 toggle
    setChannelLabel 2 "Prepayment"
    
    // Clear Energy Counters - Dpid 12 "clear_energy" -> channel 3
    linkTuyaMCUOutputToChannel 12 bool 3
    setChannelType 3 toggle
    setChannelLabel 3 "Clear Energy Counters"
    
    // Prepaid energy - Dpid 13 "balance_energy" -> channel 11
    linkTuyaMCUOutputToChannel 13 val 11
    setChannelType 11 EnergyTotal_kWh_div100
    setChannelLabel 11 "Total Prepaid Energy"
    
    // Charge Prepayment - Dpid 14 "charge_energy" - channel 12
    linkTuyaMCUOutputToChannel 14 val 12
    setChannelType 12 TextField
    setChannelLabel 12 "Purchased Energy [kWh*100], i.e. 1kWh = 100"
    
    // Settings 1 - Dpid 17 "alarm_set_1" - channel 13
    linkTuyaMCUOutputToChannel 17 val 13
    setChannelType 13 TextField
    setChannelLabel 13 "Leakage Current Protection Settings"
    
    // Settings 2 - Dpid 18 "alarm_set_2" -> channel 10
    linkTuyaMCUOutputToChannel 18 raw 10
    setChannelType 10 ReadOnly
    setChannelLabel 10 "UV, OV, Max. Current Protections Settings"
    
    // Breaker Id - Dpid 19 "breaker_id" -> channel 0
    linkTuyaMCUOutputToChannel 19 str 0
    setChannelType 0 ReadOnly
    setChannelLabel 0 "Breaker ID"
    
    // The above are read only channels. Except Settings 2 (mapped on channel 10), but since we cannot set it due to wrong parse of TuyaMCU driver - for now read only
    
    
    // NOTE: addRepeatingEvent [RepeatTime] [RepeatCount]
    // code below will forever Send query state command every 5 seconds
    //addRepeatingEvent 5 -1 tuyaMcu_sendQueryState 
    
    // AngelOfDeath - We don't need it forever, since TuyaMCU sends everything when necessary
    // we need it just first time to obtain initial status. Some dpIDs not reported without asking
     tuyaMcu_sendQueryState


    So beside Alarm Set 1 & 2 and Leakage Protection everything seems to be ok.
    Because OV, UV and Max current protection are read only I´m not sure how to set this alarm values.


    Edit2:
    Still a guess what has happened but identified a new problem/reason.

    Maybe during I struggeld getting some data my MQTT connecting was not connected/configured.
    So the autoexec.bat entry:
    waitFor MQTTState 1

    will be a problem.

    So for now, I deleted the MQTT configuration for testing and the current, power and energy counter values are missing/not send anymore.

    Then I activated
    tuyaMcu_defWiFiState 4
    in the autoexec.bat and the energy counter started to send a value but current and power still missing.

    After changing back the
    waitFor MQTTState 1
    in the autoexec.bat and configured a mqtt connection everything is fine again.

    So to achieve this status I need a working MQTT connection.
  • ADVERTISEMENT
  • #32 21100203
    p.kaczmarek2
    Moderator Smart Home
    do you have enabled TuyaMCU queue in device flags?
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #33 21100333
    Pete0815
    Level 7  

    p.kaczmarek2 wrote:
    do you have enabled TuyaMCU queue in device flags?


    No it was disabled and now enabled and tested again .... very illogical results....

    After enabling TuyaMCU queue I disabled the MQTT configuration and switch from waitFor MQTTState 1 to tuyaMcu_defWiFiState 4

    Result: No reported values anymore

    Then switch from tuyaMcu_sendQueryState to addRepeatingEvent 5 -1 tuyaMcu_sendQueryState

    Result: everything fine again

    Then switch back from addRepeatingEvent 5 -1 tuyaMcu_sendQueryState to tuyaMcu_sendQueryState

    Result: everything still ok. This I do not understand.

    Edit:

    Ok, figured out that there is a difference in using the Button "Save, Reset SVM and rund file as script thread" + Restart instead of disconnecting the device from power.

    After the last test scenario, where everything seems to work (active: tuyaMcu_defWiFiState 4 & tuyaMcu_sendQueryState) I disconnected the device from power.

    Now after repowering the situation is faulty again and values like dpIx 6 (voltage, current, power) are missing / not received from MCU.

    Guess I really have to repower fully for every test scenario to get true results.
  • ADVERTISEMENT
  • #34 21126038
    Pete0815
    Level 7  
    Ok, took some time for some new motivation to test.

    I figured out 2 working configurations:

    A) Mqtt Connection + One shot query send by autoexec
    
    waitFor MQTTState 1
    tuyaMcu_sendQueryState
    


    B) Wifi state + repeating query send by autoexec
    
    tuyaMcu_defWiFiState 4
    addRepeatingEvent 5 -1 tuyaMcu_sendQueryState
    


    Because maybe I'm not using Mqtt in every configuration, I'm preferring configuration B at the moment because it should always work.

    Still have not found a way to set alarm1 and alarm2 and therefore of course also not how to activate or check if it's working.
  • ADVERTISEMENT
  • #35 21126060
    p.kaczmarek2
    Moderator Smart Home
    It seems you've found one of the causes - doing OBK reset does not reset the TuyaMCU. If you want to have a fresh start, you need to power off the whole setup, wait a moment, and power it again.

    It's not possible to reset TuyaMCU from software.
    Helpful post? Buy me a coffee.
  • #36 21126492
    vincenzoernst1
    Level 8  
    what i do is: remove vcc jumper cable, start flashing and reconnect vcc. needs some timing but works in general.

Topic summary

✨ The discussion revolves around issues encountered while using the Zemismart SPM01-D2TW energy counter, specifically after replacing the Tuya CB2S chip with an ESP02S. The user reports receiving limited and inconsistent data points from the device, primarily only one data point changing despite attempts to modify the load. Various solutions are proposed, including using the TuyaMCU guide for setup and configuration, capturing communication data, and utilizing commands like `tuyaMcu_sendQueryState` to request data points. The conversation also touches on the importance of ensuring proper power supply and the need for a stable connection to retrieve accurate readings. Users share insights on configuring the autoexec.bat file for optimal performance and troubleshooting steps to resolve data reporting issues.

FAQ

TL;DR: Polling the Tuya-MCU every 5 s restored 100 % of the SPM01-D2TW’s data points; “OBK reset does not reboot the MCU” [Elektroda, p.kaczmarek2, post #21126060] Use either MQTT or a forced Wi-Fi state to keep metrics flowing.

Why it matters: Correct polling avoids the "zero-value" trap and unlocks full voltage, current, power and energy data.

Quick Facts

• Tuya-MCU UART baud: 9 600 bps [Elektroda, Pete0815, post #21080872] • RAW packet 8 bytes → V (÷10), I (÷1 000), P (W) [Elektroda, divadiow, post #21074304] • Flash size (CB2S): 2 MB backup recommended before modding [Elektroda, divadiow, post #21072736] • Safe bench-power: 5 V → LDO → 3.3 V, never mains during UART [Elektroda, p.kaczmarek2, post #21072162] • Two stable configs: A = MQTT + one-shot query, B = Wi-Fi state 0x04 + 5 s polling [Elektroda, Pete0815, post #21126038]

What chips are inside the Zemismart SPM01-D2TW Wi-Fi version?

The board carries a Tuya CB2S (ESP8684) Wi-Fi module, a BL0942-equivalent metering MCU, an LP2178 buck converter to 5 V, and a MicrOne SOT-23 LDO supplying 3.3 V to the radio [Elektroda, Pete0815, post #21072224]

Which UART settings are required to talk to the Tuya-MCU?

Set the OpenBeken TuyaMCU driver to 9 600 bps, 8-N-1. The device refuses data above that rate [Elektroda, Pete0815, post #21080872]

Why do I only see dpID 6 but not voltage or energy totals?

If Wi-Fi state is not 0x04 or MQTT is disconnected, the MCU withholds most telemetry and you see zeros for dpIDs 1, 2, 9, 13 [Elektroda, Pete0815, post #21080872]

How do I make all dpIDs publish automatically?

Use one of two proven triggers:
  1. waitFor MQTTState 1 then tuyaMcu_sendQueryState (one-shot).
  2. tuyaMcu_defWiFiState 4 plus addRepeatingEvent 5 -1 tuyaMcu_sendQueryState (polling). Both restored full output, including energy totals [Elektroda, Pete0815, post #21126038]

How can I back-up the factory firmware before flashing OpenBeken?

Desolder the CB2S, connect a USB-TTL adapter, and read the full 2 MB flash with the OpenBK flasher. Keep the BIN file unpaired to avoid leaking Wi-Fi creds [Elektroda, divadiow, post #21072736]

What does dpID 1 report and how is it scaled?

dpID 1 returns total forward energy in Wh×100. OpenBeken channel type EnergyTotal_kWh_div100 converts the integer (e.g. 800 → 8 kWh) [Elektroda, Pete0815, post #21099598]

Why does the web dashboard show 0.07 kWh when the log shows value 8?

Value 8 divided by 100 equals 0.08 kWh; the 0.07 kWh display is a rounding artefact in the dashboard, not a measurement error [Elektroda, Pete0815, post #21099398]

Can I run the meter without MQTT at all?

Yes, but only if you force Wi-Fi state 0x04 and keep 5 s polling active; otherwise most dpIDs stay silent [Elektroda, Pete0815, post #21126038]

How do I charge or deduct prepaid energy?

Write the desired Wh×100 value to dpID 14. The meter subtracts consumption automatically, visible in channel 11 [Elektroda, Pete0815, post #21099598]

Edge-case: What fails if I power only from a weak USB-TTL adapter?

The adapter often cannot supply startup current, resulting in endless 55AA032B00002D heartbeat loops and no Wi-Fi AP appears [Elektroda, Pete0815, post #21072681]

How to flash OpenBeken safely on the table?

  1. Feed 5 V into the board’s buck input; verify 3.3 V at CB2S.
  2. Connect TX/RX/GND; leave mains disconnected.
  3. Flash, then power-cycle to test [Elektroda, p.kaczmarek2, post #21072162]

Can dpIDs 17 and 18 (alarm_set) be written yet?

Not reliably. Current OpenBeken parsing treats their RAW payloads as read-only; writes are ignored until driver update [Elektroda, Pete0815, post #21082232]
ADVERTISEMENT