rtkv - Remote KV Database#

Adapted Air780E Air780EP

Note

This page document is automatically generated by this file. If there is any error, please submit issue or help modify pr, thank you!

Example

-- Are you still worrying about reporting several data values??
-- Are you still having a headache about storing data in the database??
-- There is no external network server, and internal network penetration is very troublesome.?
-- I don't understand mqtt, and I haven't issued any requirements. I just want to report some values.?

-- The API is perfect for you.
-- It can:
--   Save data to the server, such as temperature and humidity, GPS coordinates, GPIO status
--   Read server data, such as OTA information
--   The server saves history and also supports charting.
-- It can't be:
--   Real-time data delivery to the device
--   Upload huge amounts of data

-- On the homepage of the website, you can see the data by entering the device identification number. https://rtkv.air32.cn
-- Example device http://rtkv.air32.cn/d/6055F9779010

-- Scenario 1: Report temperature and humidity data to the server, and then the website view address is XXX
rtkv.setup()
sys.taskInit(function()
    sys.waitUntil("IP_READY")
    while 1 do
        local val,result = sensor.ds18b20(17, true) 
        if result then
            rtkv.set("ds18b20_temp", val)
        end
        sys.wait(60*1000) -- Report once a minute
    end
end)

-- Scenario Example 2, Simple Edition OTA
rtkv.setup()
sys.taskInit(function()
    sys.waitUntil("IP_READY")
    sys.wait(1000)
    while 1 do
        local ota_version = rtkv.get("ota_version")
        if ota_version and ota_version ~= _G.VERSION then
            local ota_url = rtkv.get("ota_url")
            if ota_url then
                -- Execute OTA, take esp32c3 as an example
                local code = http.request("GET", ota_url, nil, nil, {dst="/update.bin"}).wait()
                if code and code == 200 then
                    log.info("ota", "ota Package download complete, restart after 5 seconds")
                    sys.wait(5000)
                    rtos.reboot()
                end
            end
        end
        sys.wait(4*3600*1000) -- 4 Check once every hour.
    end
end)

-- Scenario 3, non-real-time delivery control
rtkv.setup()
sys.taskInit(function()
    local LED = gpio.setup(27, 0, nil, gpio.PULLUP)
    local INPUT = gpio.setup(22, nil)
    sys.waitUntil("IP_READY")
    sys.wait(1000)
    while 1 do
        local gpio27 = rtkv.get("gpio27")
        if gpio27 then
            LED(gpio27 == "1" and 1 or 0)
        end
        rtkv.set("gpio22", INPUT()) -- Report status of GPIO22
        sys.wait(15*1000) -- 15 Query once per second
    end
end)

rtkv.setup(conf)#

rtkv Initialization

Parameters

Incoming Value Type

Explanation

table

Configuration information, detailed instructions see the following example

Return Value

return value type

explanation

nil

No return value

Examples

-- This function only needs to be called once, usually in main.lua

-- Default initialization with debug log turned on
rtkv.setup()
-- Initialize and turn off the debug log
rtkv.setup({nodebug=true})
-- Detailed initialization, you can fill in only the items that need to be configured.
rtkv.setup({
    apiurl = "http://rtkv.air32.cn", -- Server address, can be self-deployed https://gitee.com/openLuat/luatos-service-rtkv
    device = "abc", -- Device ID, which can only be English character values, case-sensitive
    token = "123456", -- the device key, which is the unique id of the device by default.mcu.unique_id()
    nodebug = false,  -- Turn off the debug log, the default false
    timeout = 3000, -- Request timeout, in milliseconds, 3000 milliseconds by default
})

-- About the default value of the device value
-- If 4G is supported, it will be taken IMEI
-- If wifi is supported, it will be taken MAC
-- In other cases, mcu.unique_id() is the only one of the devices.id

rtkv.set(key, value)#

Sets the value corresponding to the specified key

Parameters

Incoming Value Type

Explanation

string

Key, cannot be nil, it is recommended to use only English letters/numbers.

string

The value cannot be nil, generally no more than 512 bytes are recommended.

Return Value

return value type

explanation

bool

Returns true on success, otherwise nil

Examples

-- If you care about the execution result, you need to execute it in the task.
-- Non-task context, will return nil, and then background execution
rtkv.set("age", "18")
rtkv.set("version", _G.VERSION)
rtkv.set("project", _G.PROJECT)

-- About the description of the type of value
-- Supports incoming strings, Boolean values, integers, and floating-point numbers, which will eventually be converted to string uploads.
-- When obtaining a value through rtkv.get, the type of the returned value will also be a string.

rtkv.sets(datas)#

Batch Set Key Values

Parameters

Incoming Value Type

Explanation

table

Key-value pairs to be set

Return Value

return value type

explanation

bool

Returns true on success, otherwise nil

Examples

-- If you care about the execution result, you need to execute it in the task.
-- Non-task context, will return nil, and then background execution
rtkv.sets({
    age = "18",
    vbat = 4193,
    temp = 23423
})

rtkv.get(key)#

Gets the value corresponding to the specified key

Parameters

Incoming Value Type

Explanation

string

Key, cannot be nil, length needs to be more than 2 bytes

Return Value

return value type

explanation

string

Returns characters on success, otherwise nil

Examples

-- Note that it must be executed in task, otherwise it must be returned.nil
local age = rtkv.get("age")