logo elektroda
logo elektroda
X
logo elektroda

Refining autoexec.bat for Tuya SHT30 Sensor with Custom MQTT Payload and Deep Sleep

andresmorago 636 6
ADVERTISEMENT
  • #1 21139382
    andresmorago
    Level 2  
    Hello to all
    First timer around here and I am looking for assistance on my little project. (thanks to all who share your knowledge about this, specially @GUTEK@ )

    Here is where I am so far
    - I am running OpenBK7231N on the "Tuya Generic Temperature and Humidity Sensor Battery Powered SHT30" devices.
    - I have a MQTT broker already running on a server as I will need to capture that data later. This works OK.

    I require your asistance with the following.
    1- I need to refine an autoexec.bat so the device wakes only every 30 minutes, get the readings and sends them via MQTT
    2- I would like to have an specific data payload published via MQTT instead of all the information that the device pushes by default

    Here is my autoexec.bat file so far, which definitely needs some improvement on the 'publishChannels' section

    
    // activación del ahorro de energía
    PowerSave 1
    
    // activación del controlador para manejar SHT30
    startDriver SHT3X
    
    // mantener presionado el botón forzará el modo de servicio
    addEventHandler OnHold 20 SafeMode
    
    // activación del contador que pondrá el módulo en suspensión en caso de falta de conexión wifi
    addRepeatingEventID 60 -1 1337 DeepSleep 600
    
    // calibración y medición 18650 4.2V, min 2.5V
    //Battery_Setup 2500 4200 2.29 2400 4096
    Battery_Setup 1600 3200 2.29 2400 4096
    battery_measure
    
    SHT_cycle 15
    
    mqtt_broadcastInterval 1
    mqtt_broadcastItemsPerSec 5
    
    [inContentAd]
    
    // esperando la conexión wifi
    waitFor WiFiState 4
    
    cancelRepeatingEvent 1337
    
    // lo mismo que arriba pero ahora suspensión en caso de falta de conexión mqtt
    addRepeatingEventID 30 -1 1337 DeepSleep 600
    
    // esperando la conexión mqtt
    waitFor MQTTState 1
    
    cancelRepeatingEvent 1337
    
    // si SHT tiene errores, se pueden hacer correcciones
    SHT_Calibrate -0.8 2
    
    SHT_Measure
    delay_s 1
    
    publishChannels
    delay_s 5
    
    // suspensión profunda por 15 minutos
    DeepSleep 875
    



    the MQTT topic i want to use should include the MAC address of the device
    fcv/{MAC}/temperature
    fcv/{MAC}/humidity
    fcv/{MAC}/batteryLevel

    The payload of each variable should include the channel value and date and time of te reading
    {
      "value": 90,
      "timestamp": "2024-06-30T15:30:00Z"
    }
    


    will appreciate your help
  • ADVERTISEMENT
  • Helpful post
    #2 21141208
    p.kaczmarek2
    Moderator Smart Home
    Hello, sorry for the late reply.
    andresmorago wrote:

    I require your asistance with the following.
    1- I need to refine an autoexec.bat so the device wakes only every 30 minutes, get the readings and sends them via MQTT

    This is very simple, you can just change the DeepSleep time in autoexec.bat

    andresmorago wrote:

    2- I would like to have an specific data payload published via MQTT instead of all the information that the device pushes by default

    I'm afraid that we don't have a flag for that, but you can just comment out specific publishes in code and use online builds system to get binaries:
    https://www.elektroda.com/rtvforum/topic4033833.html#20946719

    andresmorago wrote:

    the MQTT topic i want to use should include the MAC address of the device
    fcv/{MAC}/temperature
    fcv/{MAC}/humidity
    fcv/{MAC}/batteryLevel

    Again, this is wildly specific, but it should be possible to name OBK device "fcv" and then use scripting command like:
    
    publishFloat fcv/YourMacHere/temperature $CH10 1
    



    andresmorago wrote:

    The payload of each variable should include the channel value and date and time of te reading
    {
      "value": 90,
      "timestamp": "2024-06-30T15:30:00Z"
    }
    


    Again, this is wildly specific, but you should be able to modify the MQTT_PublishMain_StringFloat easily.
    Currently the code is the following:
    Code: C / C++
    Log in, to see the code

    It could be changed to something like this (not tested):
    Code: C / C++
    Log in, to see the code

    You can use online OBK builds to compile it. Keep in mind that you will need to start NTP first in order to get valid timestamps.
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #3 21141567
    andresmorago
    Level 2  
    Hello and thanks for your reply and information.

    One Quick question while I check for your posted advises and sorry if I have missed it: how can I extract the device MAC address with a command?
    I am considering a REST method (POST) instead of using MQTT and I would like to include the MAC address inside the payload
  • ADVERTISEMENT
  • #4 21141571
    p.kaczmarek2
    Moderator Smart Home
    Device MAC address is always shown at the bottom side of HTTP page. It's also published via MQTT after reboot.
    Helpful post? Buy me a coffee.
  • #5 21141582
    andresmorago
    Level 2  
    Hello.
    Indeed, but is there a command to store the MAC address on a variable so I can send it inside the payload of an API rest?
  • ADVERTISEMENT
  • Helpful post
    #6 21141642
    p.kaczmarek2
    Moderator Smart Home
    We have some constants for scripts but MAC is not present:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/constants.md
    However, you can easily create a custom function to do that:
    Code: C / C++
    Log in, to see the code

    The code above is not tested but shoud work, more or less...

    Added after 1 [minutes]:

    and if you want REST API, you can also check Tasmota STATUS packet support, it can already return MAC address and some more data

    Added after 2 [minutes]:

    Rest API tutorial: https://www.elektroda.com/rtvforum/topic3971355.html

    Added after 14 [minutes]:

    Here is a sample of OBK Tasmota-compatible JSON command which can return MAC:
    
    http://192.168.0.159/cm?cmnd=STATUS%205
    

    See what is returned:
    Screenshot of C code in the json_interface.c file from the OpenBK7231T_App project on GitHub.
    Helpful post? Buy me a coffee.
  • #7 21141759
    andresmorago
    Level 2  
    Hello.
    Thanks so much for the information. I would like to go deeper on this function.

    I have decided to go with the REST API alternative as I need to send the device information to a main server.

    So far, my autoexec.bat has this

    
    //Start drivers
    startDriver Battery
    startDriver SHT3X
    startDriver TuyaMCU
    startDriver NTP
    startDriver SSDP
    
    /TimeZone
    ntp_timeZoneOfs -5
    
    //Wait for wifi to become WIFI_STA_CONNECTED
    waitFor WiFiState 4
    
    //Measure
    SHT_Measure
    
    //Battery_Setup 2500 4200 2.29 2400 4096
    battery_measure
    
    //Variable list
    publishFloat "Temp" $CH2/10
    publishFloat "Hum" $CH3
    
    //Extra wait
    delay_s 1
    
    //Send data
    SendPOST https://xxxxx.proxy.beeceptor.com/prueba/testapp 80 "application/json" "{\"Temperature\":$CH2, \"Humidity\":$CH3, \"Battery_Voltage\":$batteryVoltage, \"Battery_Level\":$batteryLevel}"
    
    // Wait for GET to be sent
    delay_s 3
    
    


    I would like to include the device MAC address inside the POST data.

    can you please guide me through the necessary steps to implement part of your proposed function? As I am new to this, I am unsure on where I should create that function


    My final objective is to include the MAC address on the post call that I have on the autoexec.bat


    void mySomething()
    {
        char topic[64]; 
       char tmp[16];
       sprintf(topic,"idk/%s/12345",HAL_GetMACStr(tmp));
        return MQTT_PublishMain(mqtt_client, topic, "Hello World", 0, true);
    }

Topic summary

The discussion revolves around refining the autoexec.bat file for the Tuya Generic Temperature and Humidity Sensor powered by the SHT30 chip, utilizing OpenBK7231N firmware. The user seeks assistance in configuring the device to wake every 30 minutes, take readings, and send specific MQTT payloads instead of the default data. Responses provide guidance on adjusting deep sleep settings, modifying MQTT publish commands, and extracting the device's MAC address for inclusion in a REST API payload. Suggestions include using custom functions for MQTT publishing and leveraging Tasmota's REST API capabilities to retrieve device information.
Summary generated by the language model.
ADVERTISEMENT