logo elektroda
logo elektroda
X
logo elektroda

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

p.kaczmarek2 2946 10
ADVERTISEMENT
  • 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 11805 posts with rating 9921, helped 563 times. Been with us since 2014 year.
  • ADVERTISEMENT
  • #2 20769314
    Tommy82
    Level 41  
    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
    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 27  
    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).
  • ADVERTISEMENT
  • #5 20769813
    p.kaczmarek2
    Moderator Smart Home
    @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.
  • ADVERTISEMENT
  • #6 20770175
    krzbor
    Level 27  
    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.
  • #7 20776221
    sq3evp
    Level 37  
    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  
    @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
    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.
  • #10 20776393
    sq3evp
    Level 37  
    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 27  
    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.

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.
Summary generated by the language model.
ADVERTISEMENT