logo elektroda
logo elektroda
X
logo elektroda

OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST

p.kaczmarek2 8796 12
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    I will show here how to create a website for any IoT device on multi-platform firmware OpenBeken , although the same approach will also offer compatibility with Tasmota (compatibility of sent commands and returned JSON format). We will write the device control page in HTML and Javascript, we will host it on the IoT device itself in the LittleFS file system, and the communication of this page with the device support itself will take place via Ajax and the REST interface, which means that the page will also refresh without reloading.

    The basis of the OpenBeken/Tasmota REST interface
    OpenBeken has a REST interface compatible with the Tasmota base, which means that the submission of commands and returned data is largely the same.
    We send the command itself via HTTP GET, for example:
    
    http://192.168.0.201/cm?cmnd=POWER%20ON
    

    It is a POWER command with an ON argument, i.e. switching on a relay or lighting a lamp.
    Then the device returns JSON:
    Code: JSON
    Log in, to see the code

    The commands and returned JSON are in the Tasmota documentation. It is also worth taking a look at the OBK documentation, which is constantly being developed.
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/README.md
    Information about the device can be retrieved by sending a command without an argument - eg POWER itself.

    Basic HTML, Javascript, JSON
    This topic assumes that the reader has a basic understanding of the topics mentioned in this paragraph. HTML (along with CSS) allows you to create the appearance of the page, its controls, and so on, Javascript allows you to properly script them - intercept events from the user and then send and receive data accordingly. Javascript here processes data in conjunction with Ajax, i.e. the page is not reloaded entirely, only Javascript itself sends data in the background, receives and updates only selected controls. JSON, on the other hand, is the format in which data is received, it allows for the textual representation of objects (with key/value pairs), arrays, etc.
    In this topic, I recommend, among others. W3 course:
    https://www.w3schools.com/html/
    https://www.w3schools.com/js/
    https://www.w3schools.com/js/js_ajax_intro.asp
    https://www.w3schools.com/js/js_json_intro.asp

    LittleFS file system on OpenBeken
    The LittleFS file system is available from the Web App panel, i.e. a panel that is itself downloaded from Github and communicates with the IoT device itself via the REST interface. Of course, it doesn't mean that you won't be able to access this panel without the Internet - if you want, you can host its sources in your network and simply change their address in the OBK configuration.
    We open the Web Panel:
    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    We open the LittleFS tab:
    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    Here is the file system interface:
    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    Create creates a new file, List refreshes their list.
    Here we can create autoexec.bat (which is executed at the beginning), any other scripts for the startScript command, and just HTML files and scripts.
    We can also drag existing files onto the field

    Hello World OBK
    We will create a basic document - Create File, hello.html:
    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    We can paste anything there - even a basic HTML document:
    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    Code: HTML, XML
    Log in, to see the code

    This way (after saving) it will be available under the path:
    
    http://192.168.0.201/api/lfs/hello.html
    

    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    Here is our first HTML document hosted (in this case) on a 12W LED lamp!

    REST interface - the simplest button
    OBK's REST interface is largely Tasmota compatible. All these POWER, STATUS commands are similarly implemented.
    So let's try to check the POWER (on/off) of the lamp.
    For ease of use, I've attached the bootstrap style here.
    For starters - ready example and result:
    Code: HTML, XML
    Log in, to see the code

    Code copy: https://github.com/openshwprojects/OpenBekenRESTDemo/blob/main/examples/Generic_Power.html
    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    The above page is a SPA (Single Page Application), the simplest possible on OBK, demonstrating the use of Tasmota's POWER command to control the status of the LED.
    This page basically does two things:
    - after loading the window, it sends an empty POWER (no argument) to the device to retrieve the current LED power state
    - after clicking the button, it tries to switch the LED state to the opposite one
    The color of the button reflects the status of the LED:
    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    This excerpt:
    
    			let baseURL = "http://192.168.0.210";
    

    It allows me to run it on Windows, which is basically anywhere, because it sets the IP of the device. When uploading a device to LittleFS, this variable should be an empty string.
    The REST query after the HTTP GET executes this snippet:
    Code: Javascript
    Log in, to see the code

    According to Tasmota's documentation, POWER without an argument will only return the current state, and with an argument (ON, OFF or Toggle) it will change it.
    You can test this in a browser:
    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    This snippet refreshes the state of the LED when the page loads:
    Code: Javascript
    Log in, to see the code

    The following snippet is a helper function to change the color/style of the button:
    Code: Javascript
    Log in, to see the code

    And below we have the button itself - along with the API call with the TOGGLE argument (here with an additional space):
    Code: HTML, XML
    Log in, to see the code


    Brightness control bar
    Following this lead, we can add support for a dimmer. First you will need a slider:
    Code: HTML, XML
    Log in, to see the code

    Then you still have to somehow receive the initial state and handle its change.
    The change event is added like this:
    Code: Javascript
    Log in, to see the code

    There is no sendCmnd function yet, but sendPower is reworked to accept any command. This is because you can use the Dimmer command to control the brightness of the LED.
    Here is the whole code:
    https://github.com/openshwprojects/OpenBekenR...mo/blob/main/examples/LED_PowerAndDimmer.html
    Result:
    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    Both the button and bar work. And even with Tasmota! Only then you have to save the HTML on the disk, and OBK has its own mini-hosting in LittleFS.


    Color change
    There is also a special HTML5 control for color selection:
    Code: HTML, XML
    Log in, to see the code

    It's just as easy to catch the event of its change:
    Code: Javascript
    Log in, to see the code

    In this case, I used the command from the OBK to make my work easier, led_basecolor_rgb returns the color in the form compatible with HTML.
    This is what the updated 'change' event looks like:
    Code: Javascript
    Log in, to see the code

    In addition, I modified the function sending the command so that it checks whether they exist before using the fields from the response:
    Code: Javascript
    Log in, to see the code

    Result:
    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    Visually - the script is here:
    https://github.com/openshwprojects/OpenBekenR.../blob/main/examples/LED_PowerDimmerColor.html

    Light temperature
    The white temperature can be handled similarly. It is available in the CW and RGBCW products. Sometimes it is also emulated with RGB diodes (+ warm white diode), but about that another time.
    Just a command CT , which is also consistent with Tasmota.
    Here is the page after adding the second bar:
    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    Added HTML:
    Code: HTML, XML
    Log in, to see the code

    Javascript (sending change):
    Code: Javascript
    Log in, to see the code

    Javascript (receiving the state, fragment to be pasted into the then block):
    Code: Javascript
    Log in, to see the code

    It is worth noting that thanks to the JSON structure of Tasmota, one block is enough to receive various values such as CT or POWER.
    Visually - the script is here:
    https://github.com/openshwprojects/OpenBekenR...examples/LED_PowerDimmerColorTemperature.html

    OpenBeken Channels

    OpenBeken introduces a slightly different concept from Tasmota, the so-called channels, which are really general purpose variables and allow you to chain different peripherals and scripts together. The channel can contain anything, from the state of the relay (1 or 0), through PWM (from 0 to 100), to e.g. a value read from the ADC or even just a simple event counter (e.g. if we want the system to take some action after N events action).
    Access to OpenBeken channels is available e.g. by the command:
    
    http://127.0.0.1/cm?cmnd=Ch1%20100
    

    Basic submission is Ch , then without spaces the channel index and . possibly its value if we want to set it and not just read it. Similar to Tasmota.
    Ch+4 style notation is also supported, where + means that we want to add something to the channel value.
    OBK then returns JSON like this:
    Code: JSON
    Log in, to see the code

    The presence of channels in a packet determines whether they are used - eg mapping a channel to a relay or ADC (or setting its type) causes the channel to appear in those used.
    Compatibility is also very important TuyaMCU . In OpenBeken, you can map any TuyaMCU variable to a channel, even one whose type was not provided during software development (for example, I don't know, pollen level in the air or gas concentration) and then pass it on for processing via the REST interface.
    In addition, there are slightly older commands that return a text value:
    - SetChannel [index] [value] - sets the channel to a given value and returns the current value
    - AddChannel [index] [value] - adds a value to the channel and returns the current value
    - GetChannel [index] - just returns the current value
    But in general it's better to use the common Ch* command I already mentioned.

    Based on duct access, I made a fryer controller just for fun:
    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    source on Github: https://github.com/openshwprojects/OpenBekenRESTDemo/blob/main/examples/TuyaMCU_AirFryer.html


    Downloading information about the device, sensors, electricity measurements
    OpenBeken also supports Tasmota-style STATUS commands, i.e. you can download a JSON description of the entire device status through them. For example, for a device with BL0942 (electricity measurement) we can obtain:
    Code: JSON
    Log in, to see the code

    Similarly for many other sensors, both in Tasmot and OpenBeken, eg the entire DHT family (DHT11 and similar), etc. also publishes its results in JSON.

    Can you go the other way?
    The entire presentation was based on the assumption that this IoT device is a mini server and responds to a GET request, but can it be done the other way around?
    It so happens that in OpenBeken there is a SendGET command, which also supports substituting variables (channels), so nothing stands in the way of reporting measurements from the device to an external server in the timer or in the onChange event (even one on HTTP hosting in the network). But about that another time.

    Summary
    The following conclusions can be drawn from this short game:
    - for both Tasmota and OpenBeken, you can easily write a simple page to control a given device
    - it can be hosted e.g. on OpenBeken itself in the LittleFS file system, but it can also be left as an HTML document on our computer, or on any HTTP hosting (e.g. on Raspberry)
    - such a page can have any style and any interface, you can do anything really with CSS
    - the REST interface basically gives access to everything, so it's possible to create really arbitrary controllers for any use
    - OpenBeken also offers a simple scripting language, so you can even place fields for creating automation on the website, e.g. you can configure the sprinkler switch-on time and then operate them on the OBK itself (firmware supports downloading time from NTP and supports events set for a given hour of a given day)
    The possibilities are really great. In addition to controlling a single device, you could basically also make one collective page for many devices, which is basically your own DIY Home Assistant.
    So are there any downsides?
    One of the downsides may be the nature of Javascript itself and the fact that such a site only works if we have it open in the browser, i.e. we will not, for example, record a nice record of power consumption measurements from BL0942 to the graph, unless we only want to have a time graph when we have a website open. By the way, I would like to add that information between sessions could be kept on the browser's side in cookies, but that's another thing...
    Still, I think the possibilities are still great. Soon I will try to present a more practical example of such a page, but that's for another topic.

    Cool? Ranking DIY
    Helpful post? Buy me a coffee.
    Do you have a problem with Raspberry? Ask question. Visit our forum Raspberry.
    About Author
    p.kaczmarek2
    Moderator Smart Home
    Offline 
    p.kaczmarek2 wrote 11798 posts with rating 9918, helped 563 times. Been with us since 2014 year.
  • ADVERTISEMENT
  • #2 20537714
    kassans
    Level 32  
    As always a great elaboration. I unfortunately encountered a problem with WB2S after winning your soft configuring wifi mqtt everything works until disconnected from the power supply for a few hours then to work I have to flash WB2S again then wifi appears and I can reconfigure.
  • #3 20537738
    p.kaczmarek2
    Moderator Smart Home
    I'll be happy to try to help you, but start a separate topic. The description of the problem sounds strange to say the least. Disconnecting the power should not break anything, the data is stored in flash memory. In addition, reloading the software should not do anything to fix , because by uploading the batch you program only the offsets from 0x11000 to the end of the compiled firmware itself, and the device configuration is in memory a bit further. Simply reloading the software does not overwrite the configuration. So I don't know how uploading would help... well, unless you do erase all , i.e. you clear the entire 2MB of flash memory, then you also clear the configuration.

    Anyway - start a separate topic and we'll see.

    To make it a bit more on topic, I will add that OpenBeken compatibility with Tasmota's JSON format, e.g. with this:
    Code: JSON
    Log in, to see the code

    It also ensures compatibility with e.g. an application Tasmota Control with Google Play, as well as with ioBroker.

    I also used to add support for this JSON to a project on PIC:
    https://www.elektroda.pl/rtvforum/topic3789324.html
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #4 20626417
    p.kaczmarek2
    Moderator Smart Home
    UPDATE:
    If you want to make a button link to LittleFS on latest OBK version, do:
    
    startDriver httpButtons
    setButtonEnabled 0 1
    setButtonLabel 0 "Open Config"
    setButtonCommand 0 "*/api/lfs/cfg.html"
    setButtonColor 0 "#FF0000"
    

    The * marks that command is not OBK script command, but an URL and it will treated as such. Without *, it is treated as command to execute on OBK console:
    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    It will make a button on main page opening given link:
    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #6 20627609
    p.kaczmarek2
    Moderator Smart Home
    Sample pages added to https://github.com/openshwprojects/OpenBekenRESTDemo/tree/main/examples , especially those two ones:
    
    TuyaMCU_RadarSensorConfig.html
    TuyaMCU_RadarSensorConfigAndResults.html
    
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #7 20628668
    Jomsha
    Level 3  
    The "window.onload" function doesn't work with Tasmota.
    Only the "onclick" button event is working with Tasmota.
    They both are working with OpenBeken totally fine.

    I can't figure out why, the JSON Output is the same...

    Code: Javascript
    Log in, to see the code
  • #8 20628688
    p.kaczmarek2
    Moderator Smart Home
    I have tried my best to keep my examples compatible with Tasmota, although OpenBeken features some more advanced API which is useful for handling TuyaMCU devices. @DeDaMrAz knows some more about it, as I have written an interface for his radar sensor. The code for his sensor will not work with Tasmota, as Tasmota doesn't even provide direct access to TuyaMCU dpIDs, as far as I know.

    The window.onload function is Javascript function, it doesn't have anything to do with Tasmota or OBK. You are most likely referring to REST API call that is done in window.onload.

    Regarding the sendPower code, best way to test it is to use web browser. First, without argument:
    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    Then, with argument:
    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    To be clear, it's a Tasmota device:
    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    If it works with browser, then it must work in code, unless you have some kind of mistake in the script. The POWER sending sample I think is in the first post of this topic.

    Let me know if you need any REST javascript code examples for either OpenBeken or Tasmota. If you give me details, I can write a simple Javascript sample for you.
    Helpful post? Buy me a coffee.
  • #9 20628701
    Jomsha
    Level 3  
    p.kaczmarek2 wrote:
    I have tried my best to keep my examples compatible with Tasmota, although OpenBeken features some more advanced API which is useful for handling TuyaMCU devices. @DeDaMrAz knows some more about it, as I have written an interface for his radar sensor. The code for his sensor will not work with Tasmota, as Tasmota doesn't even provide direct access to TuyaMCU dpIDs, as far as I know.

    The window.onload function is Javascript function, it doesn't have anything to do with Tasmota or OBK. You are most likely referring to REST API call that is done in window.onload.

    Regarding the sendPower code, best way to test it is to use web browser. First, without argument:
    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    Then, with argument:
    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    To be clear, it's a Tasmota device:
    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    If it works with browser, then it must work in code, unless you have some kind of mistake in the script. The POWER sending sample I think is in the first post of this topic.

    Let me know if you need any REST javascript code examples for either OpenBeken or Tasmota. If you give me details, I can write a simple Javascript sample for you.


    Thanks , I already modified the code found on the git...
    This is my modified code:
    First is OpenBeken
    Second is Tasmota.
    "Onload" state of the Tasmota device doesn't get updated (2nd button stay blue) .
    user and pass are correct. tried even without user and pass. still no luck.
    I dont know what to do next.. tried everything

    Code: Javascript
    Log in, to see the code


    When I change the 2nd device to OpenBeken its all working fine.
  • #10 20628733
    p.kaczmarek2
    Moderator Smart Home
    It looks like Tasmota has changed something recently. They are enforcing CORS policy now. I've found a related Reddit thread:
    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    You can see that in the connections of Chrome inspector:
    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    I've checked some Github threads for a SetOption setting for that, but I've found none so far. Of course it could be fixed with recompiling Tasmota, but it's not a perfect solution.

    I have tested it with latest Tasmota device and I can confirm there is a problem. I have currently fixed it by using Chrome extension:
    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    called "Moesif Origin & CORS Changer" but I don't like using Chrome extensions, they may be a vulnerability thread. I will try to find another solution if possible.
    Helpful post? Buy me a coffee.
  • #11 20628756
    Jomsha
    Level 3  
    Thanks for your testings.
    I Hope you'll find the best solution...
    I Don't know how to check those stuff...
  • #12 20628773
    p.kaczmarek2
    Moderator Smart Home
    Btw, if you want simple on/off access, why not try this approach?


    Helpful post? Buy me a coffee.
  • #13 20629078
    Jomsha
    Level 3  
    Actually I think I Will pass the option of HTML .
    I need to port forward all my devices to make it work for my purposes...

    Is anybody know a free cloud (mqtt) ?
    Tried hivemq but it can't connect...

Topic summary

The discussion focuses on utilizing OpenBeken firmware for mini HTTP hosting on IoT devices, allowing users to create web pages in HTML and JavaScript. The communication between the web page and the device is facilitated through Ajax and a REST interface, which is compatible with Tasmota. Users share experiences with device configurations, issues with Tasmota's CORS policy, and provide code snippets for creating buttons linked to the LittleFS file system. There are mentions of specific functionalities that work with OpenBeken but not with Tasmota, highlighting differences in API behavior. Additionally, users discuss challenges related to device power loss and configuration retention, as well as seeking alternatives for cloud MQTT services.
Summary generated by the language model.
ADVERTISEMENT