logo elektroda
logo elektroda
X
logo elektroda

How to send measurements from TuyaMCU battery-powered thermometer to HTTP server?

p.kaczmarek2 3300 10

TL;DR

  • Sends temperature and humidity from a TuyaMCU battery-powered CB3S/BK7231N sensor flashed with OpenBK7231 to an arbitrary HTTP server using GET requests.
  • Uses `addEventHandler OnChannelChange` to trigger `SendGet` on channel 1 and channel 2, plus a `waitFor WiFiState 4` startup request and PHP logging script.
  • The setup links TuyaMCU DPIDs 1, 2, 3, 17, and 18, sets the UART baud rate to 115200, and divides temperature by 10.
  • Collected logs show repeated timestamps with temperature and humidity values, proving the logger works independently of Home Assistant.
  • TuyaMCU sends readings only when values change, so temperature and humidity do not always arrive together.
Generated by the language model.
ADVERTISEMENT
📢 Listen (AI):
  • PHP script snippet for logging data from the TuyaMCU sensor.
    Here I will show you how to send measurements (temperature and humidity) from TuyaMCU battery-powered sensor to arbitrary HTTP server via GET request. Following implementation will work independently from Home Assistant and will allow you to create fully customizable data logger. For the sake of presentation, I will use the device described here:
    [CB3S/BK7231N] Temperature/Humidity Sensor with TuyaMCU - Diagram, Reverse Enginering
    I am assuming here that device is already flashed with OBK.

    Following topic was created with @DeDaMrAz. All testing was done on his side. Thanks for cooperation!

    Requirements for this topic
    To replicate our demo from here, you basically need two things:
    - TuyaMCU sensor device that will send data
    - a HTTP server with a PHP script that will receive data
    No Home Assistant is needed.

    Sending measurements from TuyaMCU

    According to OBK docs:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/commands.md
    There is a SendGET command, but we need to know when to use it.
    TuyaMCU reports measurements with a delay.
    So we need to setup a change handler for measurement channel:
    
    addEventHandler OnChannelChange 1 SendGet http://192.168.0.75/log.php?t=$CH1
    

    The following script will run SendGet with channel 1 value, which in this particular case is used for temperature value.
    For the sake of an example, I can also show you how to send a GET once the device connects to WiFi. It is important to wait for the WiFi connection first, see script belowq:
    
    waitFor WiFiState 4
    SendGet http://192.168.0.75/log.php?t=HEEELLLO
    

    Here is full script (modified script from here)
    
    startDriver TuyaMCU
    startDriver tmSensor
    
    // may be needed, depends on device, some also use 9600
    tuyaMCU_setBaudRate 115200
    
    // dpID 1 is tempererature div 10
    setChannelType 1 temperature_div10
    linkTuyaMCUOutputToChannel 1 val 1
    // dpID 2 is % humidity
    setChannelType 2 Humidity
    linkTuyaMCUOutputToChannel 2 val 2
    // dpID 3 is battery state - low(0), mid(1) and high(2)
    linkTuyaMCUOutputToChannel 3 enum 3
    setChannelType 3 ReadOnlyLowMidHigh
    setChannelLabel 3 Battery
    
    // on tempererature change
    addEventHandler OnChannelChange 1 SendGet http://192.168.0.75/log.php?t=$CH1
    
    
    //
    // setup dpCache - temperature interval
    //
    // Show textfield for that
    setChannelType 5 TextField
    // setup display name
    setChannelLabel 5 Temperature Interval
    // Make value persistant (stored between reboots), 
    // start value -1 means "remember last"
    SetStartValue 5 -1
    // set default value if not set
    if $CH5==0 then "setChannel 5 1"
    // link dpID 17 to channel 5, the type is val, extra '1' means that its dpCache variable
    linkTuyaMCUOutputToChannel 17 val 5 1
    
    setChannelType 6 TextField
    setChannelLabel 6 Humidity Interval
    SetStartValue 6 -1
    if $CH6==0 then "setChannel 6 1"
    linkTuyaMCUOutputToChannel 18 val 6 1
    
    
    
    waitFor WiFiState 4
    SendGet http://192.168.0.75/log.php?t=HEEELLLO
    


    HTTP server side
    HTTP server should be reachable from the network of your device. On your HTTP server, setup just a simple PHP file that prints received GET argument to file along with the current timestamp. You can also insert it to the MySQL database, but that's not the scope of this tutorial. Here is a sample, simplest script:
    PHP script snippet for logging data from the TuyaMCU sensor.
    You can test the script just by accessing it's URL in your web browser:
    Screenshot of a webpage showing a message about data appended to a file.


    Now let's run our setup for some time. Here are sample results:
    Screenshot of a log file showing timestamps and measurement results.
    Everything seems to be working!

    Adding second measurement
    Everything seems to be working, but the device in question is able to measure both temperature and humidity. So let's add another change handler to send humidity data as well:
    
    startDriver TuyaMCU
    startDriver tmSensor
    
    // may be needed, depends on device, some also use 9600
    tuyaMCU_setBaudRate 115200
    
    // dpID 1 is tempererature div 10
    setChannelType 1 temperature_div10
    linkTuyaMCUOutputToChannel 1 val 1
    // dpID 2 is % humidity
    setChannelType 2 Humidity
    linkTuyaMCUOutputToChannel 2 val 2
    // dpID 3 is battery state - low(0), mid(1) and high(2)
    linkTuyaMCUOutputToChannel 3 enum 3
    setChannelType 3 ReadOnlyLowMidHigh
    setChannelLabel 3 Battery
    
    // on tempererature change
    addEventHandler OnChannelChange 1 SendGet http://192.168.0.75/log.php?t=$CH1
    addEventHandler OnChannelChange 2 SendGet http://192.168.0.75/log.php?h=$CH2
    
    
    //
    // setup dpCache - temperature interval
    //
    // Show textfield for that
    setChannelType 5 TextField
    // setup display name
    setChannelLabel 5 Temperature Interval
    // Make value persistant (stored between reboots), 
    // start value -1 means "remember last"
    SetStartValue 5 -1
    // set default value if not set
    if $CH5==0 then "setChannel 5 1"
    // link dpID 17 to channel 5, the type is val, extra '1' means that its dpCache variable
    linkTuyaMCUOutputToChannel 17 val 5 1
    
    setChannelType 6 TextField
    setChannelLabel 6 Humidity Interval
    SetStartValue 6 -1
    if $CH6==0 then "setChannel 6 1"
    linkTuyaMCUOutputToChannel 18 val 6 1
    
    
    
    waitFor WiFiState 4
    SendGet http://192.168.0.75/log.php?t=HEEELLLO
    

    PHP script also needs to be updates:
    PHP code for logging temperature and humidity measurements
    Now it looks like this:
    Screenshot showing temperature and humidity measurements sent by TuyaMCU.
    Here are results collected over time:
    
    Timestamp: 2023-10-12 19:40:13 | t: HEEELLLO
    Timestamp: 2023-10-12 19:40:18 | t: 279
    Timestamp: 2023-10-12 19:41:22 | t: HEEELLLO
    Timestamp: 2023-10-12 19:41:27 | t: 312
    Timestamp: 2023-10-12 19:41:27 | h: 40
    Timestamp: 2023-10-12 19:42:31 | t: HEEELLLO
    Timestamp: 2023-10-12 19:42:36 | h: 37
    Timestamp: 2023-10-12 19:43:40 | t: HEEELLLO
    Timestamp: 2023-10-12 19:43:45 | h: 35
    Timestamp: 2023-10-12 19:44:49 | t: HEEELLLO
    Timestamp: 2023-10-12 19:44:55 | t: 349
    Timestamp: 2023-10-12 19:45:59 | t: HEEELLLO
    Timestamp: 2023-10-12 19:46:03 | h: 34
    Timestamp: 2023-10-12 19:47:08 | t: HEEELLLO
    Timestamp: 2023-10-12 19:47:13 | t: 357
    Timestamp: 2023-10-12 19:48:17 | t: HEEELLLO
    Timestamp: 2023-10-12 19:48:22 | t: 362
    Timestamp: 2023-10-12 19:49:26 | t: HEEELLLO
    Timestamp: 2023-10-12 19:49:31 | t: 364
    Timestamp: 2023-10-12 19:50:35 | t: HEEELLLO
    Timestamp: 2023-10-12 19:50:40 | t: 362
    Timestamp: 2023-10-12 19:51:44 | t: HEEELLLO
    Timestamp: 2023-10-12 19:51:49 | t: 359
    Timestamp: 2023-10-12 19:51:50 | h: 33
    Timestamp: 2023-10-12 19:52:53 | t: HEEELLLO
    Timestamp: 2023-10-12 19:52:58 | t: 357
    Timestamp: 2023-10-12 19:54:03 | t: HEEELLLO
    Timestamp: 2023-10-12 19:54:08 | h: 33
    Timestamp: 2023-10-12 19:55:12 | t: HEEELLLO
    Timestamp: 2023-10-12 19:55:17 | t: 359
    Timestamp: 2023-10-12 19:55:17 | h: 33
    Timestamp: 2023-10-12 19:56:21 | t: HEEELLLO
    Timestamp: 2023-10-12 19:56:26 | t: 359
    Timestamp: 2023-10-12 19:57:30 | t: HEEELLLO
    Timestamp: 2023-10-12 19:57:35 | h: 33
    Timestamp: 2023-10-12 19:58:39 | t: HEEELLLO
    Timestamp: 2023-10-12 19:58:44 | h: 32
    Timestamp: 2023-10-12 19:59:48 | t: HEEELLLO
    Timestamp: 2023-10-12 19:59:53 | h: 32
    Timestamp: 2023-10-12 20:00:58 | t: HEEELLLO
    Timestamp: 2023-10-12 20:01:03 | h: 32
    Timestamp: 2023-10-12 20:02:07 | t: HEEELLLO
    Timestamp: 2023-10-12 20:02:12 | t: 362
    Timestamp: 2023-10-12 20:03:16 | t: HEEELLLO
    Timestamp: 2023-10-12 20:03:21 | t: 360
    Timestamp: 2023-10-12 20:03:21 | h: 33
    Timestamp: 2023-10-12 20:04:25 | t: HEEELLLO
    Timestamp: 2023-10-12 20:04:30 | h: 32
    Timestamp: 2023-10-12 20:05:34 | t: HEEELLLO
    Timestamp: 2023-10-12 20:05:39 | t: 363
    Timestamp: 2023-10-12 20:06:43 | t: HEEELLLO
    Timestamp: 2023-10-12 20:06:48 | t: 363
    Timestamp: 2023-10-12 20:07:53 | t: HEEELLLO
    Timestamp: 2023-10-12 20:07:58 | t: 363
    Timestamp: 2023-10-12 20:09:02 | t: HEEELLLO
    Timestamp: 2023-10-12 20:09:07 | h: 33
    Timestamp: 2023-10-12 20:10:11 | t: HEEELLLO
    Timestamp: 2023-10-12 20:10:16 | t: 312
    Timestamp: 2023-10-12 20:11:20 | t: HEEELLLO
    Timestamp: 2023-10-12 20:11:25 | t: 285
    Timestamp: 2023-10-12 20:12:29 | t: HEEELLLO
    Timestamp: 2023-10-12 20:12:34 | t: 268
    Timestamp: 2023-10-12 20:13:39 | t: HEEELLLO
    Timestamp: 2023-10-12 20:13:44 | t: 257
    Timestamp: 2023-10-12 20:13:44 | h: 48
    Timestamp: 2023-10-12 20:14:48 | t: HEEELLLO
    Timestamp: 2023-10-12 20:14:53 | h: 50
    Timestamp: 2023-10-12 20:15:57 | t: HEEELLLO
    Timestamp: 2023-10-12 20:16:02 | h: 52
    Timestamp: 2023-10-12 20:17:06 | t: HEEELLLO
    Timestamp: 2023-10-12 20:17:11 | t: 242
    Timestamp: 2023-10-12 20:18:16 | t: HEEELLLO
    Timestamp: 2023-10-12 20:18:21 | t: 239
    Timestamp: 2023-10-12 20:18:21 | h: 53
    Timestamp: 2023-10-12 20:19:25 | t: HEEELLLO
    Timestamp: 2023-10-12 20:19:30 | t: 237
    Timestamp: 2023-10-12 20:20:34 | t: HEEELLLO
    Timestamp: 2023-10-12 20:20:39 | t: 235
    Timestamp: 2023-10-12 20:21:43 | t: HEEELLLO
    Timestamp: 2023-10-12 20:21:48 | t: 234
    Timestamp: 2023-10-12 20:22:53 | t: HEEELLLO
    Timestamp: 2023-10-12 20:22:58 | t: 234
    Timestamp: 2023-10-12 20:22:58 | h: 55
    Timestamp: 2023-10-12 20:24:02 | t: HEEELLLO
    Timestamp: 2023-10-12 20:24:07 | t: 233
    Timestamp: 2023-10-12 20:25:11 | t: HEEELLLO
    Timestamp: 2023-10-12 20:25:16 | t: 232
    Timestamp: 2023-10-12 20:26:21 | t: HEEELLLO
    Timestamp: 2023-10-12 20:26:25 | t: 232
    Timestamp: 2023-10-12 20:27:30 | t: HEEELLLO
    Timestamp: 2023-10-12 20:27:35 | t: 231
    Timestamp: 2023-10-12 20:28:39 | t: HEEELLLO
    Timestamp: 2023-10-12 20:28:44 | t: 230
    Timestamp: 2023-10-12 20:28:44 | h: 55
    Timestamp: 2023-10-12 20:29:48 | t: HEEELLLO
    Timestamp: 2023-10-12 20:29:53 | t: 230
    Timestamp: 2023-10-12 20:30:58 | t: HEEELLLO
    Timestamp: 2023-10-12 20:31:03 | t: 231
    Timestamp: 2023-10-12 20:32:07 | t: HEEELLLO
    Timestamp: 2023-10-12 20:32:12 | t: 230
    Timestamp: 2023-10-12 20:33:16 | t: HEEELLLO
    Timestamp: 2023-10-12 20:33:21 | t: 229
    Timestamp: 2023-10-12 20:34:25 | t: HEEELLLO
    Timestamp: 2023-10-12 20:34:30 | t: 229
    Timestamp: 2023-10-12 20:35:35 | t: HEEELLLO
    Timestamp: 2023-10-12 20:35:40 | t: 229
    Timestamp: 2023-10-12 20:36:44 | t: HEEELLLO
    Timestamp: 2023-10-12 20:36:49 | t: 229
    Timestamp: 2023-10-12 20:37:53 | t: HEEELLLO
    Timestamp: 2023-10-12 20:37:58 | t: 229
    Timestamp: 2023-10-12 20:39:03 | t: HEEELLLO
    Timestamp: 2023-10-12 20:39:08 | h: 56
    Timestamp: 2023-10-12 20:40:12 | t: HEEELLLO
    Timestamp: 2023-10-12 20:40:17 | t: 229
    Timestamp: 2023-10-12 20:41:21 | t: HEEELLLO
    Timestamp: 2023-10-12 20:41:26 | t: 229
    Timestamp: 2023-10-12 20:42:30 | t: HEEELLLO
    Timestamp: 2023-10-12 20:42:36 | t: 229
    Timestamp: 2023-10-12 20:43:40 | t: HEEELLLO
    Timestamp: 2023-10-12 20:43:45 | t: 229
    Timestamp: 2023-10-12 20:44:49 | t: HEEELLLO
    Timestamp: 2023-10-12 20:44:54 | t: 229
    Timestamp: 2023-10-12 20:45:58 | t: HEEELLLO
    Timestamp: 2023-10-12 20:46:03 | t: 231
    Timestamp: 2023-10-12 20:47:08 | t: HEEELLLO
    Timestamp: 2023-10-12 20:47:13 | h: 55
    Timestamp: 2023-10-12 20:48:17 | t: HEEELLLO
    Timestamp: 2023-10-12 20:48:22 | t: 232
    Timestamp: 2023-10-12 20:49:26 | t: HEEELLLO
    Timestamp: 2023-10-12 20:49:31 | t: 234
    Timestamp: 2023-10-12 20:50:35 | t: HEEELLLO
    Timestamp: 2023-10-12 20:50:40 | t: 233
    Timestamp: 2023-10-12 20:51:45 | t: HEEELLLO
    Timestamp: 2023-10-12 20:51:50 | t: 234
    Timestamp: 2023-10-12 20:51:50 | h: 55
    Timestamp: 2023-10-12 20:52:54 | t: HEEELLLO
    Timestamp: 2023-10-12 20:52:59 | h: 55
    Timestamp: 2023-10-12 20:52:59 | t: 234
    

    Some things to note:
    - TuyaMCU sends temperature as an integer, you need to divide it by 10 to get correct result in this case
    - TuyaMCU seems to report only data when there is a change, so not always all measurements are sent
    You can also report the battery state in the same way.


    Summary

    This way you can report data directly to HTTP server and then process it in PHP (or any other) script any way you like. All required files are available on Github:
    https://github.com/openshwprojects/BK7231-TuyaMCU-PHP-logger/tree/main
    Soon I will publish another tutorial on the topic, this time maybe with MySQL or with chart creation, so stay tuned!

    Cool? Ranking DIY
    Helpful post? Buy me a coffee.
    About Author
    p.kaczmarek2
    Moderator Smart Home
    Offline 
    p.kaczmarek2 wrote 14570 posts with rating 12584, helped 654 times. Been with us since 2014 year.
  • ADVERTISEMENT
  • #2 20769314
    Tommy82
    Level 41  
    Posts: 12139
    Help: 455
    Rate: 1072
    And why not using the POST method?
    Posting this explicitly as parameters has the downside that it can unnecessarily litter the http server log.
  • ADVERTISEMENT
  • #3 20769358
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14570
    Help: 654
    Rate: 12584
    Basically, there is nothing preventing you from using POST. In OpenBeken there is a SendPost function for this. You can also create JSON in its body, quote escaping is supported.

    It just so happened that we used GET because it has this tiny advantage that for beginners it is easier to test (you can call it from the URL conveniently and a beginner can see what it sends).
    Helpful post? Buy me a coffee.
  • #4 20769703
    krzbor
    Level 29  
    Posts: 1755
    Help: 41
    Rate: 1063
    It seems to me that it is more convenient to use MQTT.
    In the topic Link I presented the combination of MQTT and PHP (both automation and reading by web server).
  • #5 20769813
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14570
    Help: 654
    Rate: 12584
    @krzbor can your solution be fired on free/budget HTTP/PHP hosting? There used to be a lot of such even free services with one subdomain like mojasub dot hosting dot pl
    In the module you are using I see:
    Code: PHP
    Log in, to see the code
    It seems to me that these free or budget hosting have blocked access to other ports and sockets at all, so in their case your way would not work.
    Helpful post? Buy me a coffee.
  • #6 20770175
    krzbor
    Level 29  
    Posts: 1755
    Help: 41
    Rate: 1063
    p.kaczmarek2 wrote:
    can your solution be fired up on free/budget HTTP/PHP hosting? There used to be a lot of such even free services with one subdomain like mojasub dot hosting dot p

    There should be no problem stream_socket_client is in PHP a connection opening - in other words a low-level access that is used by any higher layer. If there is no certificate then a connection is opened without encryption. If there is a certificate then encryption is attached. The problem may be the MQTT port, but this is the connection to MQTT (from the point of view of PHP is after the outgoing connection and does not require opening external ports). In general, socket in PHP has nothing in common with websocket known from javascript.
    I don't quite understand your concept, though. Well, we have 3 components:
    1. IOT devices with MQTT
    2. MQTT broker (e.g. Mosquitto)
    3. Web page.
    Whether we have the 1st or the 3rd, the connection is always established to the MQTT broker. This is a huge advantage, because the 1st and 3rd can work completely asynchronously.
    Now let's go to the deployment of the various components:
    - IOT - of course, on the home LAN
    - broker - we are unlikely to put on free hosting, because it is a separate program, but there are sites offering free MQTT brokers
    - WWW site with PHP - if we put it on free hosting, it should rather be possible to connect to the home MQTT (you need to open the appropriate port at home). The problem, however, can be a variable home IP - so the address of our broker can change (unless we have a fixed IP). Of course, you can get around this by using DDNS. However, if we already have DDNS then isn't it better to run the site on the internal network as well?

    To set the whole thing up at home all we need is a Pi3 - we install Apache, PHP, Mosquitto on it and in addition we can add zigbee2mqtt with a coordinator and we can integrate both devices on WiFi, cable ethernet and Zigbee - the integrating platform is, of course, MQTT.

    The possibility of taking MQTT and the site (PHP) outside looks interesting. I've never tested this, but in theory it should work. We have an MQTT server that our IOT connects to, and since the MQTT address is known there should be no problem establishing a connection to PHP (unless the free servers block outbound traffic from PHP).

    I found a site like this, for example: https://www.emqx.com/en/mqtt/public-mqtt5-broker
    However, I have never tested something like this. For me, the main disadvantage of this solution is the dependence on 2 providers. If one fails the whole thing will stop working. The advantage, of course, is that we do not have a hardware/software layer in the home network.
  • ADVERTISEMENT
  • #7 20776221
    sq3evp
    Level 39  
    Posts: 6494
    Help: 217
    Rate: 861
    Do I understand correctly that, in general, any sensor not necessarily Tuya can so transmit data to the HTTP server?

    Does it have to be implemented something specific (some dedicated library) or anything, as long as it sends data understandable to the HTTP server?

    Server writes data to a file - a fairly typical implementation using php, should work with older versions also?

    I understand that we can have the HTTP server on the local network only for ourselves?
  • #8 20776280
    Tommy82
    Level 41  
    Posts: 12139
    Help: 455
    Rate: 1072
    @sq3evp
    Everything from which you are able to generate a request (request) to the http server will work, that is, in the case of the GET method is the analogy of clicking on a link with the appropriate parameters.
    Here in this case is a ready-made solution send Get function.
    In other devices it may be different. From a certain "level of complexity" of devices you can use
    https://pl.wikipedia.org/wiki/CURL

    Paradoxically, the data does not have to be understood by the http server he does not do anything specific with them. This request must be correct.
    You can have the server where you want it is just a matter of how you refer to it by external IP by internal IP or by domain.
  • #9 20776318
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14570
    Help: 654
    Rate: 12584
    sq3evp wrote:
    Do I understand correctly that, in general, any sensor not necessarily Tuya can transmit data to the HTTP server this way? Does it have to be implemented something specifically (some dedicated library) or anything, as long as it sends data understandable to the HTTP server?

    Any Tuya IoT device and any other IoT device, as long as it is supported by the batch used OpenBeken and will have this batch uploaded to it according to the materials on our channel Elektroda.com can work in a 100% local way without the cloud and can send any data to the HTTP server in the presented way. You can also plug in your DHT11 and script that, it will work too. You can connect a potentiometer, OpenBeken will handle it too. You can connect, for example, 5 buttons per ADC pin and script them to send HTTP GET, OpenBeken will handle that too.


    sq3evp wrote:

    Server writes data to a file - a fairly typical implementation using php, should work with old versions too?

    What I used is a pure classic, any old PHP will handle, you can write to a file, you can write to a database, you can write where you want.

    sq3evp wrote:

    I understand that we can have the HTTP server on the local network just for ourselves?


    You can put the HTTP server wherever you want, you can put the Xampp package on Windows, you can put any other server either with PHP backend or with any other. It doesn't matter. It can be on a local network, it can be on the Internet. If you want you can even put a simple server on Raspberry PI and it will work too.
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #10 20776393
    sq3evp
    Level 39  
    Posts: 6494
    Help: 217
    Rate: 861
    Other sensors via bluetooth will probably need gateways?
    Anybody got to Xiaomi Mijia thermometer 2 Bluetooth? Will it be possible to read data without a gateway from Xiaomi?
  • #11 20776858
    krzbor
    Level 29  
    Posts: 1755
    Help: 41
    Rate: 1063
    sq3evp wrote:
    Other sensors via bluetooth will probably need gateways?
    sq3evp wrote:
    Anybody got into the Xiaomi Mijia thermometer 2 Bluetooth? Can you manage to read the data without a gateway from Xiaomi?
    I once posted something like this: Link But now I would choose Zigbee.
📢 Listen (AI):

Topic summary

✨ The discussion focuses on sending temperature and humidity measurements from a TuyaMCU battery-powered sensor to an HTTP server using GET requests. The implementation is independent of Home Assistant and allows for a customizable data logger. The requirements include a TuyaMCU sensor and an HTTP server with a PHP script. While GET requests are highlighted for their ease of testing, POST requests can also be utilized, as supported by OpenBeken. Alternatives like MQTT are suggested for convenience, though they may require additional components like an MQTT broker. The conversation also addresses the compatibility of other sensors and the flexibility of server deployment, emphasizing that any device capable of generating HTTP requests can send data to the server.
Generated by the language model.

FAQ

TL;DR: With 3 mapped data points and WiFi ready state 4, you can send TuyaMCU thermometer readings from OpenBeken straight to a PHP logger. As one expert note says, "Everything seems to be working!" This solves simple local logging for users who want temperature and humidity on any HTTP server without Home Assistant. [#20768717]

Why it matters: This method gives you cloud-free logging, simple testing, and flexible hosting on LAN, Raspberry Pi, Windows XAMPP, or basic PHP servers.

Method Setup complexity Best use Main trade-off
GET Low Fast beginner testing and simple PHP logging Parameters appear in URL and server logs
POST Medium Cleaner payloads, including JSON Slightly harder to test manually
MQTT Higher Asynchronous multi-component systems Needs broker infrastructure

Key insight: The most important detail is not the HTTP method. You must trigger requests on measurement change and wait for WiFiState 4, or the device may send before networking is ready. [#20768717]

Quick Facts

  • The example BK7231N script maps dpID 1 to temperature_div10, dpID 2 to humidity, and dpID 3 to battery state with enum values 0, 1, 2 for low, mid, and high. [#20768717]
  • The TuyaMCU UART speed may need 115200 baud; some devices instead use 9600 baud. [#20768717]
  • Temperature values arrive as scaled integers such as 279, 312, or 362; divide by 10 to read 27.9°C, 31.2°C, or 36.2°C. [#20768717]
  • Reporting intervals are exposed through dpCache-linked channels: dpID 17 for temperature interval and dpID 18 for humidity interval, both stored persistently with SetStartValue -1. [#20768717]
  • The receiver can run on a local LAN IP, Raspberry Pi, Windows XAMPP, or external hosting, as long as the device can reach the HTTP endpoint. [#20776318]

How do I send temperature and humidity measurements from a TuyaMCU battery-powered thermometer running OpenBeken to an HTTP server with a GET request?

Use OpenBeken event handlers to fire SendGet when channel values change. 1. Map TuyaMCU temperature and humidity dpIDs to channels. 2. Add OnChannelChange handlers such as t=$CH1 and h=$CH2. 3. Point both requests to a reachable PHP endpoint like http://192.168.0.75/log.php. In the shown setup, channel 1 carries temperature and channel 2 carries humidity, so each change creates a separate HTTP GET request. [#20768717]

What is the SendGet command in OpenBeken, and how is it used with addEventHandler OnChannelChange?

SendGet is an OpenBeken command that sends an HTTP GET request to a chosen URL. With addEventHandler OnChannelChange, it runs automatically when a mapped channel changes value. The example uses addEventHandler OnChannelChange 1 SendGet ...t=$CH1, so temperature is sent only after channel 1 updates. That makes it a simple event-driven logger instead of a fixed-interval polling loop. [#20768717]

Why should I wait for WiFiState 4 before sending an HTTP request from an OpenBeken device?

You should wait for WiFiState 4 because that state indicates the device is connected and ready to use the network. The example script explicitly runs waitFor WiFiState 4 before a test SendGet, which prevents sending too early after boot. If you skip that step, the first request can fail because Wi-Fi is not ready yet. [#20768717]

How do I map TuyaMCU dpIDs for temperature, humidity, and battery state to OpenBeken channels in a BK7231N sensor?

Map each dpID with linkTuyaMCUOutputToChannel and set a matching channel type. The shown BK7231N setup uses dpID 1 as val on channel 1 with temperature_div10, dpID 2 as val on channel 2 with Humidity, and dpID 3 as enum on channel 3 with ReadOnlyLowMidHigh. Battery states are encoded as 0 for low, 1 for mid, and 2 for high. [#20768717]

What is dpCache in TuyaMCU/OpenBeken, and how do channels 17 and 18 relate to temperature and humidity reporting intervals?

"dpCache" is a TuyaMCU/OpenBeken storage mechanism that keeps cached device parameters, including persistent settings that survive reboots. In the example, dpID 17 links to channel 5 for temperature interval and dpID 18 links to channel 6 for humidity interval, both with the extra 1 flag marking them as dpCache values. SetStartValue -1 keeps the previous value after restart. [#20768717]

Why does a TuyaMCU thermometer report temperature like 279 or 362, and how do I convert temperature_div10 values to real degrees?

It reports scaled integers because this device sends temperature in tenths of a degree. Divide the raw value by 10: 279 becomes 27.9°C, 362 becomes 36.2°C, and 229 becomes 22.9°C. The script labels dpID 1 as temperature_div10, which confirms the conversion rule for this sensor. [#20768717]

What's the simplest PHP script for logging OpenBeken GET parameters with timestamps to a file on an HTTP server?

The simplest PHP logger reads a GET parameter and appends it with the current timestamp to a text file. The thread describes a minimal script that prints the received argument to a file and shows sample output like Timestamp: 2023-10-12 19:40:18 | t: 279. That approach is intentionally basic, and the author states that writing to MySQL is possible but outside this tutorial. [#20768717]

How should I modify the OpenBeken script and PHP logger to send and store both temperature and humidity instead of temperature alone?

Add a second event handler for humidity and extend the PHP script to accept h as well as t. The OpenBeken side uses one GET for channel 1 temperature and another for channel 2 humidity. The resulting log then stores separate lines such as t: 312 and h: 40, both timestamped. That keeps the logger simple while capturing both measurements from the same device. [#20768717]

GET vs POST vs MQTT for OpenBeken sensor logging — which approach is better for a simple local data logger and why?

GET is best for the simplest local logger because it is easiest to test and understand. One participant notes that GET lets beginners call the URL directly and see what the device sends, while POST avoids cluttering server logs with parameters, and MQTT suits more complex asynchronous systems with a broker. Expert guidance from the thread is clear: “GET… is easier to test” for beginners. [#20769358]

How do I use SendPost in OpenBeken to send JSON data instead of exposing measurements in the URL with GET?

Use OpenBeken SendPost instead of SendGet and place the measurement data in the request body as JSON. The thread states that OpenBeken supports SendPost, supports quote escaping, and lets you build JSON in the body. That avoids putting t=279 or h=40 directly in the URL, which can reduce log clutter on the HTTP server. [#20769358]

What is MQTT, and how would MQTT plus PHP differ from direct HTTP logging for IoT measurements?

"MQTT" is a lightweight messaging protocol that moves device data through a broker, enabling asynchronous communication between IoT nodes and applications. In the thread, the MQTT design has 3 parts: IoT device, MQTT broker, and PHP web page. Unlike direct HTTP logging, both device and site connect to the broker, so they can work independently in time and do not need direct request-response coupling. [#20770175]

How can I host the receiver for OpenBeken measurements on local LAN, Raspberry Pi, XAMPP on Windows, or budget PHP hosting?

You can host it anywhere the device can reach over HTTP. The thread explicitly mentions local LAN, Raspberry Pi, Windows with XAMPP, and external Internet hosting as valid targets. For MQTT-based variants, budget hosting may block outbound traffic or non-HTTP ports, but the plain HTTP/PHP logger does not require Home Assistant and works with a simple reachable web server. [#20776318]

Why does TuyaMCU seem to send data only when the measurement changes, and how does that affect HTTP logging intervals?

TuyaMCU appears to report only on change, so the logger does not receive every metric on every cycle. The thread states that “not always all measurements are sent,” and the sample log shows temperature and humidity arriving on different timestamps. That means your HTTP file will contain uneven intervals, with new lines only when temperature or humidity actually changes. [#20768717]

What tools or libraries do non-Tuya sensors need to send data to an HTTP server if they don't have a built-in SendGet-style function?

They need any mechanism that can generate a valid HTTP request. One reply says everything that can create a request to an HTTP server will work, and mentions cURL as an option once device complexity rises. The server does not need to understand a special sensor protocol first; the HTTP request itself must be correct, and the backend can then store the data in a file or database. [#20776280]

How can I read data from Bluetooth sensors like the Xiaomi Mijia Thermometer 2 without the Xiaomi gateway, and when would a BLE gateway or Zigbee alternative make more sense?

The thread does not provide a direct no-gateway method for Xiaomi Mijia Thermometer 2. It only suggests that Bluetooth sensors may need a gateway and points to an older related example, while one participant says they would now choose Zigbee instead. A BLE route fits if your sensor already uses Bluetooth; Zigbee makes more sense when you want a cleaner multi-device ecosystem going forward. [#20776858]
Generated by the language model.
ADVERTISEMENT