Multiprotocol Access Product Introduction QuickStart User Guide Development Guide
EDP TCP transparent transmission MQTT Modbus HTTP
FAQ

Development Process

Step 1: Create a product, and select TCP transparent transmission.

First, you need to complete the account registration in the platform and create a product of TCP transparent transmission.

Step 2: Create a device and record the device ID and other information.

Click “Add Device” in the page, enter the device name and authentication information (i.e. device number), and record the device number.

Step 3: Upload the Lua script.

Upload the analysis script into the product in the page, as shown below: pic

pic

Script example:

The script has the following features:

  • “Hello” is issued at intervals of 5 seconds after the device is connected.

  • After the user uploads data, the platform responds “received”.

  • Convert the data uploaded by the user into a HEX string, and save it as a data point in the ds_test data stream.

-----------------------------------------------------------------------
-- Instructions for use::                                                        --
-- V1.3                                                              --
-- Users need to implement 2 functions.                                 --
-- 1. Issue the data task initialization function regularly: device_timer_init(dev) [optional].     --
-- 2. Analyze the data (e.g. heartbeat) uploaded by the device: device_data_analyze(dev)            --
-----------------------------------------------------------------------

-------------------------------------------------------------------------------
-- Register the C function.                                                                          --
-- u2f converts the 32-bit integer memory data to float type (different from value conversion).      --
-- Similar to C code*(float*)(&u)                                            --
-- function u2f(u)                                                           --
-- @param   u   number   integer                                             --
-- @return  Return a float in case of success and nil otherwise.             --
-- @example local u = 123                                                    --
--          local f = u2f( 123 )                                             --
--                                                                           --
-- time Get the timestamp, the number of milliseconds from (00:00:00 UTC, January 1, 1970)   --
-- function time()                                                           --
-- @return  Return the current timestamp.                                    --
-- @example local t = time()                                                 --
--                                                                           --
-- year, get the year (year-1900)                                            --
-- function year(t)                                                          --
-- @param t number timestamp, the number of seconds from (00:00:00 UTC, January 1, 1970) --
-- @return  Return the year                                                  --
-- @example local y = year( t )                                              --
--                                                                           --
-- month Get the month (0-11)                                                --
-- function month(t)                                                         --
-- @param t number time stamp, the number of seconds from (00:00:00 UTC, January 1, 1970) --
-- @return  Return the month                                                 --
-- @example local m = month( t )                                             --
--                                                                           --
-- day Get the day (1-31)                                                    --
-- function day(t)                                                           --
-- @param t number time stamp, the number of seconds from (00:00:00 UTC, January 1, 1970) --
-- @return  Return the day                                                   --
-- @example local d = day( t )                                               --
--                                                                           --
-- hour Get the hour (0-23)                                                  --
-- function hour(t)                                                          --
-- @param t number time stamp, the number of seconds from (00:00:00 UTC, January 1, 1970) --
-- @return  Return the hour                                                  --
-- @example local h = hour( t )                                              --
--                                                                           --
-- minute Get the minute (0-59)                                              --
-- function minute(t)                                                        --
-- @param t number time stamp, the number of seconds from (00:00:00 UTC, January 1, 1970) --
-- @return  Return the minute                                                --
-- @example local m = minute( t )                                            --
--                                                                           --
-- second Get the second (0-59)                                              --
-- function second(t)                                                        --
-- @param t number time stamp, the number of seconds from (00:00:00 UTC, January 1, 1970) --
-- @return  Return the second                                                --
-- @example local m = second( t )                                            --
-------------------------------------------------------------------------------

--------------------------------------------------------
-- Convert the bytes string to hex string                       --
-- @param   s   string   bytes string                 --
-- @return Return the hex string, similar to “0A0B0C0D...”         --
-- @example local hex = to_hex("\2\2\0\150\0\37\206") --
--------------------------------------------------------
function to_hex(s)
        local i
        local t

        t={s:byte(1,s:len())}
        for i=1,#t do
                t[i]=string.format('%02X',t[i])
        end

        return table.concat(t)
end

-----------------------------------------------
-- Serialize object into string                    --
-- @param   o   boolean|number|string|table  --
-- @return Return the serialized string                 --
-- @example local str = to_str({x=100})      --
-----------------------------------------------
function to_str(o)
        local i=1
        local t={}
        local f

        f=function(x)
                local y=type(x)
                if y=="number" then
                        t[i]=x
                        i=i+1
                elseif y=="boolean" then
                        t[i]=tostring(x)
                        i=i+1
                elseif y=="string" then
                        t[i]="\""
                        t[i+1]=x
                        t[i+2]="\""
                        i=i+3
                elseif y=="table" then
                        t[i]="{"
                        i=i+1

                        local z=true
                        for k,v in pairs(x) do
                                if z then
                                        z=false
                                        t[i]="\""
                                        t[i+1]=k
                                        t[i+2]="\""
                                        t[i+3]=":"
                                        i=i+4
                                        f(v)
                                else
                                        t[i]=","
                                        t[i+1]="\""
                                        t[i+2]=k
                                        t[i+3]="\""
                                        t[i+4]=":"
                                        i=i+5
                                        f(v)
                                end
                        end

                        t[i]="}"
                        i=i+1
                else
                        t[i]="nil"
                        i=i+1
                end
        end
        f(o)

        return table.concat(t)
end

----------------------------------------------------------------------------------------------------------
-- Add the data point into the table                                                                     --
-- @param   t   table                                                                                    --
--          i   string                      data stream or data stream template name                     --
--          a   number                      millisecond-level timestamp, from (00:00:00 UTC, January 1)  --
--                                          If the value is 0, the current time is used                  --
--          v   boolean|number|string|table Boolean, number, string, json                                --
--          c   string                      identify the data point attribution (device AuthCode, optional)  --
--                                          If the value is “” or nil, it means that the data point is to establish a TCP connection     --
-- @return Return true in case of success and false otherwise                                                                --
-- @example local ok = add_val(t,"dsname",0,100)                                                        --
----------------------------------------------------------------------------------------------------------
function add_val(t, i, a, v, c)
        if type(t)~="table" then
                return false
        elseif type(i)~="string" then
                return false
        elseif type(a)~="number" then
                return false
        else
                local o = type(v)
                if o~="boolean" and o~="number" and o~="string" and o~="table" then
                        return false
                end

                local n = {i=i,v=to_hex(v)}
--                n.v = n.v .. "("..o..")"
                if a~=0 and a~=nil then
                        n["a"]=a
                end
                if c~=nil then
                        n["c"]=c
                end

                -- list push_back --
                if t.h==nil then
                        t.h={nil,n}
                        t.t=t.h
                else
                        t.t[1]={nil,n}
                        t.t=t.t[1]
                end
        end

        return true
end

--------------------------------------------------------------
-- Serialize the table into json string                     --
-- @param t table,table constructed by add_val and add_bin  --
-- @return Return the serialized json string                --
-- @example local json = to_json(t)                         --
--------------------------------------------------------------
function to_json(t)
        local i=1
        local o={}
        local n

        o[i]="["
        i=i+1
        n=t.h
        while n~=nil do
                if n[2]~=nil then
                        o[i]=to_str(n[2])
                        i=i+1
                end

                n=n[1]
                if n~=nil then
                        o[i]=","
                        i=i+1
                end
        end
        o[i]="]"

        return table.concat(o)
end

------------------------------------
-- begin- add the user-defined value or function, etc. --

-- end- add the user-defined value or function, etc.--
------------------------------------

------------------------------------------------------------------------------------------
-- Set the data to be issued regularly (optional)                                       --
-- @param  dev    user_data   Device manager                                            --
-- @return None                                                                         --
-- @notice This is a callback function and cannot be called in the script.              --
-- @readme dev Provide the following functions::                                       --
--         dev:add(interval,name,data) Add the data to be issued regularly.             --
--           @param   interval   number   Time interval (in seconds) of data issuing    --
--                    name       string   Name (unique)                                 --
--                    data       string   Data (binary), converted into a string via lua--
--           @return  Return true in case of success and false otherwise                --
--           @notice  The average frequency of regular data issuing does not exceed 1, and 1/interval_1+...+1/interval_n<=1 --
--           @example local ok = dev:add(10,"test","\1\1\0\150\0\37\253\29")            --
--         dev:timeout(sec) Set the device response timeout (seconds) for data issuing  --
--           @param   sec        int      Response timeout (seconds)                    --
--                                        If the value is 0, it means that the device response timeout is not detected.             --
--           @return  None                                                              --
--           @example dev:timeout(3)                                                    --
--         dev:response() The device responds successfully.                             --
--           @param   None                                                              --
--           @return  None                                                              --
--           @example dev:response()                                                    --
--         dev:send(data) Send data to the device                                       --
--           @param   data   string   Data (binary), converted into a string via lua    --
--           @return  None                                                              --
--           @example dev:send("\2\2\0\150\0\37\206\89")                                --
------------------------------------------------------------------------------------------
function device_timer_init(dev)
        -- Add user-defined code --
        -- Example: --
        dev:timeout(3)
        dev:add(10,"dev1","hello")    --Send a packet of data (hello) at intervals of 10 seconds.
end

-----------------------------------------------------------------------------------------------------------
-- Analyze the data uploaded by the device                                                                                      --
-- @param  dev    user_data   Device manager                                                             --
-- @return size Indicate the bytes of data uploaded by the analysis device, and json means the set of analyzed data points, in the following format:                   --
--         [                                                                                             --
--           {                                                                                           --
--             "i" : "dsname1",          // Data stream or data stream template name 1                                      --
--             "a" : 1234567890,         // Millisecond-level timestamp, from (00:00:00 UTC, January 1   --
--                                       // If the value is 0, it means that the current time is used.                                  --
--             "v" : 123 | "123" | {...} // Boolean, number, string, json                                --
--             "b" : "0A0B0C0D..."       // Binary data (hexadecimal string), mutually exclusive with v, not saved at the same time              --
--             "d" : xxx | "xxx" | {...} // Used to describe b (optional); Boolean, number, string, json                --
--             "c" : "authcode1"         // Used to identify the data point attribution (device AuthCode, optional)   --
--                                       // If it is “” or does not exist, it means that a TCP connection is established for data point attribution.      --
--           }                                                                                           --
--           ...                                                                                         --
--           {                                                                                           --
--             "i" : "dsnamen",          // Data stream or data stream template name 1                                      --
--             "a" : 1234567890,         // Millisecond-level timestamp, from (00:00:00 UTC, January 1)  --
--                                       // If the value is 0, the current time is used.                                 --
--             "v" : 123 | "123" | {...} // Boolean, number, string, json                                --
--             "b" : "0A0B0C0D..."       // Binary data (hexadecimal string), mutually exclusive with v, not saved at the same time              --
--             "d" : xxx | "xxx" | {...} // Used to describe b (optional); Boolean, number, string, json                --
--             "c" : "authcoden"         // Used to identify the data point attribution (device AuthCode, optional)                        --
--                                       // If it is “” or does not exist, it means that a TCP connection is established for data point             --
--           }                                                                                           --
--         ]                                                                                             --
-- @notice This is a callback function and cannot be called in the script.                               --
-- @readme dev Provide the following functions::                                                        --
--         dev:add(interval,name,data) Provide the following functions:                                  --
--           @param   interval number   Time interval of data sending (seconds)                          --
--                    name     string   Name (unique)                                                    --
--                    data     string   Data (binary), converted into a string via lua                   --
--           @return  Return true in case of success and false otherwise.                                                        --
--           @notice  The average frequency of regular data issuing does not exceed 1, and 1/interval_1+...+1/interval_n<=1       --
--           @example local ok = dev:add(10,"test","\1\1\0\150\0\37\253\29")                             --
--         dev:timeout(sec)  Set the device response timeout (seconds) for data                          --
--           @param   sec      int      Response timeout (seconds)                                       --
--                                      If the value is 0, it means that the device response timeout is not detected.  --
--           @return  None                                                                               --
--           @example dev:timeout(3)                                                                     --
--         dev:response() The device responds successfully.                                              --
--           @param   None                                                                               --
--           @return  None                                                                               --
--           @example dev:response()                                                                     --
--         dev:send(data)Send data to the device                                                         --
--           @param   data   string   Data (binary), converted into a string via lua                     --
--           @return  None                                                                               --
--           @example dev:send("\2\2\0\150\0\37\206\89")                                                 --
--         dev:size() Get the device data size (bytes)                                                   --
--           @param   None                                                                               --
--           @return  Return the device data size (bytes)                                                --
--           @example local sz = dev:size()                                                              --
--         dev:byte(pos) Get the device data (bytes) corresponding to pos                                --
--           @param   pos   number   Specified position to get data, [1,dev:size()+1)                    --
--           @return  Return the device data (int) in case of success and nil otherwise.                 --
--           @example local data = dev:byte(1)                                                           --
--         dev:bytes(pos,count)Get the corresponding number of device data from pos, count the device data --   
--           @param   pos   number   Specified start position to get the data, [1,dev:size()+1)          --
--                    count number   Specified total number of data, [0,dev:size()+1-pos]                --
--           @return  Return the device data (string) in case of success and nil otherwise.              --
--           @example local datas = dev:bytes(1,dev:size())                                              --
-----------------------------------------------------------------------------------------------------------
function device_data_analyze(dev)
        local t = {}
        local a = 0

        -- 添加用户自定义代码 --
        -- 例如: --
        local s = dev:size()    --Get the uplink data length
        add_val(t,"ds_test",0,dev:bytes(1,s))

        dev:response()
        dev:send("received")  --Send response

        -- return $1,$2 --
        -- 例如: --
        return s,to_json(t)    --Save the data
end

Where: device_data_analyze(dev) is the uplink data analysis function of the terminal, and each packet of data will be processed; and device_timer_init(dev) is the timer initialization function, which is used to send data at specified intervals.

Step 4: Establish a connection.

Power on the device, establish a TCP connection with the OneNET access service (domain name: dtu.onenet.hk.chinamobile.com), and send a login message in the following format:

*$PID#$AUTH_INFO#$PARSER_NAME*

Example:

*12346#sn823mdn#testlua*

The meanings of parameters are as follows:

  • PID: Product ID, the unique digital identifier of the product generated by OneNET when the product is created;

  • AUTH_INFO: Device authentication information, the unique string identifier specified by the user for the device in registration;

  • PARSER_NAME: name of the user-defined parsing script, the unique string identifier specified by the user when the script is uploaded.

If the connection is established successfully, there will be a device online mark:

pic

Step 5: Display the data stream and view data points.

Click “Data Display” under “Device Management” of OneNET to enter the data display page. Click the drop-down menu to view the recently uploaded data points. You can also select the time interval to view the historical time.

个搜索结果,搜索内容 “

    0 个搜索结果,搜索内容 “