Make Your Own Control Driver

Using the Data Logger Application provided by Observator you can easily integrate your own custom code driver. Please study the manual on how to use the OMC-048 Data Logger Application before you start integrating your own control driver.

Creating a .py driver file

Create a new .py file and place this in the scriptmodules folder of your OMC-048 data logger. In this example we create the file ‘control_driver.py’ but you are free to choose any name you like, as long as there are no duplicate names.

Now paste the following code in your driver file:

control_driver.py
 1from control_drv import CtrlDrv
 2
 3class control_driver(CtrlDrv):
 4
 5    def __init__(self, conf):
 6        CtrlDrv.__init__(self, conf)
 7        print("[CONTROL DRIVER] Paste your Initialize code here")
 8
 9    async def execute(self):
10        print("[CONTROL DRIVER] Paste your CRON execute code here")
11        return True

Important

The class name (line 3) must be identical to the .py file name!

Add to the config

The base for our driver is now provided, we can already test it by adding our driver to the config.txt The entire config is displayed below, note the ‘control_driver’ for our example.

# ----System---- #
Omc048:
  system_id: PROTO_1
  application: PROTO_1
  file_log_level: info
  repl_log_level: info
  utc_time_offset_hours: +0
  utc_time_offset_minutes: +0
  sensor_data_print: True
  usb_mode: debug
  self_test: False

control_driver:
- id: control_1
  sample_interval: "* * * * *"

# ----Data-log-settings---- #
Data_file:
- id: data
  create_interval: "0 0,5,10,15,20,25,30,35,40,45,50,55 * * *"

Using the REPL to start our application shows our driver is working:

2024-09-02 12:53:10 [APPLICATION] INFO config.txt detected
2024-09-02 12:53:10 [APPHELPER] INFO DEBUG mode is active
2024-09-02 12:53:10 [APPLICATION] INFO UTC time device offset: 0(hrs):0(min)
[CONTROL DRIVER] Paste your Initialize code here
2024-09-02 12:53:10 [LOGGER_MANAGER] INFO Driver appended: control_1
2024-09-02 12:53:10 [APPLICATION] INFO Application started: Stage_1
2024-09-02 12:53:10 [APPLICATION] INFO File Syslog level: info
2024-09-02 12:53:10 [APPLICATION] INFO Repl Syslog level: info
2024-09-02 12:53:10 [TEST] INFO Initiating self-test procedure
2024-09-02 12:53:10 [TEST] INFO Self-test result: PASSED
2024-09-02 12:53:10 [DATALOG045] INFO Started logging in 'data/OMC-045_Stage_1_048000202_240902_125310.txt'
2024-09-02 12:53:10 [SCHEDULER] INFO Enabled sleeping in between tasks
[CONTROL DRIVER] Paste your CRON execute code here

Note

The base of our sensor is operational now, from this point we can start adding our own code.

Another example

This examples shows a custom made control driver which toggles the relay, its called clicky.py.

clicky.py
 1from control_drv import CtrlDrv
 2import omc048
 3
 4class clicky(CtrlDrv):
 5
 6    def __init__(self, conf):
 7        CtrlDrv.__init__(self, conf)
 8        print("CLICKY INIT")
 9        self.clicks = 0
10
11    async def execute(self):
12        self.clicks +=1
13        omc048.relay(1).toggle()
14        print("CLICKY COUNT: {}".format(self.clicks))
15        return True
# ----System---- #
Omc048:
  system_id: PROTO_1
  application: PROTO_1
  file_log_level: info
  repl_log_level: info
  utc_time_offset_hours: +0
  utc_time_offset_minutes: +0
  sensor_data_print: True
  usb_mode: debug
  self_test: False

clicky:
- id: clicky_1
  sample_interval: "* * * * *"
  supply_port: 1
  supply_port_always_on: False
  response_timeout: 120
  relay_port: 2
  relay_port_always_on: False
  startup_time: 5
  cooldown_time: 1

# ----Data-log-settings---- #
Data_file:
- id: data
  create_interval: "0 0,5,10,15,20,25,30,35,40,45,50,55 * * *"

Using the REPL to start our application shows our driver is working:

MPY: sync filesystems
MPY: soft reboot
2024-09-02 13:11:23 [APPLICATION] INFO config.txt detected
2024-09-02 13:11:23 [APPHELPER] INFO DEBUG mode is active
2024-09-02 13:11:23 [APPLICATION] INFO UTC time device offset: 0(hrs):0(min)
2024-09-02 13:11:23 [DRIVER_MANAGER] INFO Enabled power supply port 1 for sensor: clicky_1
2024-09-02 13:11:23 [DRIVER_MANAGER] INFO Enabled relay supply port 2 for sensor: clicky_1
CLICKY INIT
2024-09-02 13:11:23 [LOGGER_MANAGER] INFO Log parameters appended: clicky_1
2024-09-02 13:11:23 [APPLICATION] INFO Application started: Stage_1
2024-09-02 13:11:23 [APPLICATION] INFO File Syslog level: info
2024-09-02 13:11:23 [APPLICATION] INFO Repl Syslog level: info
2024-09-02 13:11:24 [TEST] INFO Initiating self-test procedure
2024-09-02 13:11:24 [TEST] INFO Self-test result: PASSED
2024-09-02 13:11:24 [DATALOG045] INFO Started logging in 'data/OMC-045_Stage_1_048000202_240902_131124.txt'
2024-09-02 13:11:24 [SCHEDULER] INFO Enabled sleeping in between tasks
2024-09-02 13:11:25 [DRIVER_MANAGER] INFO Sensor [clicky_1] power on
2024-09-02 13:11:25 [DRIVER_MANAGER] INFO Sensor [clicky_1] relay on
2024-09-02 13:11:25 [DRIVER_MANAGER] INFO Sensor [clicky_1] sec startup time: 5
2024-09-02 13:11:26 [SCHEDULER] INFO Sleep for 3.822 seconds
CLICKY COUNT: 1
2024-09-02 13:11:30 [DRIVER_MANAGER] INFO Sensor [clicky_1] sec cooldown time: 1
2024-09-02 13:11:31 [DRIVER_MANAGER] INFO Sensor [clicky_1] power off
2024-09-02 13:11:31 [DRIVER_MANAGER] INFO Sensor [clicky_1] relay off
2024-09-02 13:11:31 [DRIVER_MANAGER] INFO Sensor [clicky_1] power on
2024-09-02 13:11:31 [DRIVER_MANAGER] INFO Sensor [clicky_1] relay on
2024-09-02 13:11:31 [DRIVER_MANAGER] INFO Sensor [clicky_1] sec startup time: 5
2024-09-02 13:11:32 [SCHEDULER] INFO Sleep for 2.798 seconds

Important

  • The code in def execute must always return a True or False. When False is returned the driver loops back until response_timeout was expired.

  • You can add sensor specific settings to the config.txt file by using the config module!

  • Adding delays and loops e.t.c within the custom code may comprimise the operation of the data logger, use at your own risk.

Tip

Can’t get it figured out? Feel free to contact us!