How to create a custom driver for OpenBeken with online builds (no toolchain required)

Here I will show you how you can create a custom OpenBeken driver. Custom OpenBeken driver can implement almost any logic you want and runs directly on OBK device. You don't need to setup toolchain for that, builds can be done online. Writing a custom OBK driver does not require much programming knowledge, it can be done with the really basic, basic skillset. Writing OBK driver directly is also much more flexible than scripting and gives you a better control over device. Futhermore, I can always help with that, so let's check out how it can be done.
This short guide was created for users asking how to add custom features to OpenBeken. I hope it will give you a basic overview of the procedure.
1. Preparing workflow
Ok, so first go to our repository on Github:
https://github.com/openshwprojects/OpenBK7231T_App

Make a fork and open a pull request like you would usually do. It's easiest to do it with Github gui. If you are not sure how, check the Git documentation and tutorials.

This will allow you to get online builds in PRs:
https://github.com/openshwprojects/OpenBK7231T_App/pulls
Once your PR is accepted, you will be able to get binaries for all platforms as described in:
OpenBeken online building system - compiling firmware for all platforms (BK7231, BL602, W800, etc)
2. Preprocessor define for a driver
All drivers should have a #define in obk_config.h:
https://github.com/openshwprojects/OpenBK7231T_App/blob/main/src/obk_config.h
This allows us to turn them on and off for each platform.
Let's create a define for our driver. Let's say that I will be developing it on Windows Simulator for now. So, in the #ifdef WINDOWS section, let's add our own new define:

Our diff so far:

3. Main drivers table entry
Now let's modify drv_main.c:
https://github.com/openshwprojects/OpenBK7231T_App/blob/main/src/driver/drv_main.c
We need to add an entry to drivers array. Whole entry should be in preprocessor block. We also need to fill the documentation entries.

A driver has:
- name
- init function
- every second update function
- quick tick update function
- http index display function
- and some other less often used functions, like shutdown function or channel change callback.
So next we can create those function in separate new file and add their declarations to drv_local.h
4. New driver file
So let's create a drv_sample_ntpSwitch.c stub:
Code: C / C++
We can also add some display to know whether our driver really works:
Code: C / C++
Let's build and flash firmware for our chosen platform and try starting the driver:
startDriver SampleNTPSwitch
You should get "OK" reply:

You should also get text on main panel:

5. What can be done in drivers?
Basically everything can be done in drivers, but some more specific operations may require code injection outside your driver file. Still, for the start, let's do something simple. Like the user request we had recently:
Quote:
Switch the NTP server IP based on your current network IP
So, we will do checking every second. Simple string comparison is not time-consuming so it's acceptable:
Code: C / C++
This will set the NTP server IP depending on your current IP. Don't worry about multiple calls to CFG_SetNTPServer. The flash config will be saved only if there is a change in the settings.
The similiar behaviour was requested by user here:
https://www.elektroda.com/rtvforum/topic4054813.html
6. How to debug drivers (on Windows)?
OpenBeken can run on Windows, along with device simulator.
So if you run our MSVC projects, you can put breakpoints in drivers:

This way you can:
- see variable values
- see current call stack
- pause or resume execution, also manually step in/out the calls
This can substantially reduce your testing and development time.
7. How to make drivers scriptable?
The best way is to use console commands. Console commands can be invoked via autoexec.bat, HTTP interface or even by MQTT. Creating a new command is very simple:
Code: C / C++
This way you cam get a console/script command with a full arguments testing.



Of course, channel value can be also used as an argument.


8. How to access channels and measurements?
Any driver can have full access to OBK API, so you can easily read and write channels, read measurements, etc:
Code: C / C++
The following code will log the channel 1 value and voltage (from BL0942/BL0937/etc sensor).
This way you can also, for example, measure how long voltage is above given value:
Code: C / C++
If you want, you can change the hardcoded 230 value to something set via console command, just look at the console command sample and save the value to the static global variable.
Then you can set channels and even publish data via MQTT:
Code: C / C++
9. How to make custom Web page interface?
Per-driver main HTTP page callback can be used also to get user input. You can easily create HTML forms that way. For this example, I will create a simple button that will just toggle the channel 5:
Code: C / C++
Clicking the button toggles channel 5 value:


And that's it! That's how you can create a custom driver in OBK. Remember to download our code to check the full API that is available for your development. All details related to arguments of MQTT/channel/etc calls are in our repository.
Comments
Hi. If anyone has a moment can they please cast their eyes over this https://github.com/openshwprojects/OpenBK7231T_App/pull/1288 Failing on missing files. Have I got paths wrong in file includes or... [Read more]
1. See how drv_ir.cpp includes IRremote: https://github.com/openshwprojects/OpenBK7231T_App/blob/main/src/driver/drv_ir.cpp If not, put OneWire dir into driver dir just to see what happens. 2. This... [Read more]
eek. OK, thanks. will play [Read more]
This also will have to be ported and compiled: https://github.com/PaulStoffregen/OneWire/blob/master/OneWire.cpp [Read more]
DS18B20 library for esp-idf without separate onewire library https://github.com/feelfreelinux/ds18b20/blob/master/ds18b20.c [Read more]
That's cool, wanna port it, @divadiow? Here are some things to change: void ds18b20_write(char bit){ if (bit & 1) { gpio_set_direction(DS_GPIO, GPIO_MODE_OUTPUT); noInterrupts(); gpio_set_level(DS_GPIO,0); ets_delay_us(6); gpio_set_direction(DS_GPIO,... [Read more]
I do I do. I need to get my head round it. Also, I'm distracted by BK7238 today Added after 15 [minutes]: so am I going with this or the separate libraries I started with? [Read more]
To be honest, I think you can put everything in one file for now. Or just put converted library into ds18b20_internal.c , make header ds18b20_internal.h and include it from drv_ds18b20.c [Read more]
hmm. im just making a GPT hash of it right now. feel free to do it or I'll poke around when not tired [Read more]
I have ported @insmod 's recommendation to OBK, but I haven't plugged the "disable interrupts" code yet. It is not linked anywhere, but it compiles: https://github.com/openshwprojects/OpenBK7231T_App/commit/8bec03a85cb9e22414be93caff254d3e6a30891c Of... [Read more]
Cool. Nice work. Thanks! 👍 [Read more]
Since I was working on this some month ago (and didn't get it to work) I also played with this. So I am quite a big distance behind this full approach, but also a tiny bit ahead: I have a (very very... [Read more]
Great job, can you open a PR? @dedamraz has some sensors to test it btw: Is DS1820 the same as DS18B20? @ElektrodaBot is DS1820 the same as DS18B20? What are the protocol differences? [Read more]
Direct Answer to the User's Question The DS1820 and DS18B20 are not the same, although they share many similarities. Both are digital temperature sensors produced by Dallas Semiconductor (now part of... [Read more]
Ah ok, so it's different, but since @max4elektroda has proper timings already, maybe I could just move his nops to my ported code... [Read more]
I'll do the PR later, just didn't want to much confusion about different code for similar purposes... Regarding DS1820 to DS18B20: I just took the shorter name, but I don't think there are many "non... [Read more]
@DeDaMrAz have a scope and can help, but don't forget that on those ESP32-like platforms timings are not always precise: https://github.com/openshwprojects/OpenBK7231T_App/issues/497 [Read more]
I guess I'm no help with the driver code, but I do have a TYTE-D1 with DS18B20 for when testing is required. [Read more]
I opened a PR (https://github.com/openshwprojects/OpenBK7231T_App/pull/1289) with a code working for me. Beken should work even from the artifacts, for W800 and LN882H additions to makefiles are needed. usleep... [Read more]