logo elektroda
logo elektroda
X
logo elektroda

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

p.kaczmarek2 
Screenshot of the obk_config.h file from the OpenBK7231T_App project on GitHub, showing preprocessor definitions.
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
OpenBK7231T_App repository page on GitHub.
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.
Screenshot showing the Github Desktop interface with the OpenBK7231T_App repository open.
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:
Screenshot of the obk_config.h file from the OpenBeken repository on GitHub.
Our diff so far:
Screenshot of a code editor showing a change in the obk_config.h file


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.
Screenshot of a code editor showing modifications in the `drv_main.c` file, where an entry for the new driver `SampleNTPSwitch` has been added.
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++
Log in, to see the code

We can also add some display to know whether our driver really works:
Code: C / C++
Log in, to see the code

Let's build and flash firmware for our chosen platform and try starting the driver:

startDriver SampleNTPSwitch

You should get "OK" reply:
Command tool panel with startDriver SampleNTPSwitch command and OK message
You should also get text on main panel:
A webpage displaying My driver is running! at the top with several buttons at the bottom.

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++
Log in, to see the code

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:
Screenshot of Visual Studio with OpenBeken driver source code
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++
Log in, to see the code

This way you cam get a console/script command with a full arguments testing.
Screenshot of a command entry tool with an error
Command Tool interface displaying Bad argument error message.
Screenshot of command tool WinTest_3145CAFF showing the result of the SQRT 16 command.
Of course, channel value can be also used as an argument.
Screenshot showing the WinTest command tool with a sample command.
Command tool interface for WinTest_3145CAFF

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++
Log in, to see the code

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++
Log in, to see the code

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++
Log in, to see the code


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++
Log in, to see the code

Clicking the button toggles channel 5 value:
Screenshot of the OpenBeken user interface with a sample driver.
Custom driver SampleNTPSwitch running on WinTest_3145CAFF

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.

About Author
p.kaczmarek2
p.kaczmarek2 wrote 11836 posts with rating 9933 , helped 566 times. Been with us since 2014 year.

Comments

divadiow 10 Jul 2024 23:56

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]

p.kaczmarek2 11 Jul 2024 09:29

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]

divadiow 11 Jul 2024 09:38

eek. OK, thanks. will play [Read more]

p.kaczmarek2 11 Jul 2024 09:49

This also will have to be ported and compiled: https://github.com/PaulStoffregen/OneWire/blob/master/OneWire.cpp [Read more]

insmod 11 Jul 2024 15:37

DS18B20 library for esp-idf without separate onewire library https://github.com/feelfreelinux/ds18b20/blob/master/ds18b20.c [Read more]

p.kaczmarek2 11 Jul 2024 21:54

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]

divadiow 11 Jul 2024 22:29

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]

p.kaczmarek2 11 Jul 2024 22:57

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]

divadiow 11 Jul 2024 23:25

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]

p.kaczmarek2 14 Jul 2024 19:16

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]

divadiow 14 Jul 2024 19:58

Cool. Nice work. Thanks! 👍 [Read more]

max4elektroda 14 Jul 2024 22:36

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]

p.kaczmarek2 14 Jul 2024 23:04

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]

ElektrodaBot 14 Jul 2024 23:05

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]

p.kaczmarek2 14 Jul 2024 23:32

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]

max4elektroda 15 Jul 2024 06:42

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]

p.kaczmarek2 15 Jul 2024 09:04

@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]

divadiow 15 Jul 2024 19:10

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]

max4elektroda 16 Jul 2024 17:26

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]