Skip to main content

NatureMiniPro Service Extension

Provide device control and display capabilities on NatureMiniPro.

Requirements

This function is used for page device configuration on NatureMiniPro, and the supported pages include home page/scene page/device page.

There can be up to 4 devices or io objects on the home page.

There can be up to 6 devices or io objects on the scene page.

A device page can only correspond to one device object.

The developed application interface can be replaced to the home page or scene page, and then query and control the device objects configured on the original page through the service interface, and use other information services through the service interface.

Device Access

The device is accessed through the path id, and the path format is $PAGENAME.IDX, PAGENAME is the page name, and IDX is the device number under this page.

List of current page names:

  • NM_HOME : home page, IDX is 1..4
  • NM_SCENE : scene page, IDX is 1..6
  • NM_SCENE2 : extended scene page, IDX is 1..6

Device Information Query svcget(path)

The following example obtains the information of the first device on the home page:

local obj=svcget('$NM_HOME.1')
if type(obj)=='table' then
printh(obj.cgy)
printh(obj.cls)
printh(obj.name)
if cgy=='io' then
printh(obj.type&1)
elseif cgy=='ep' then
elseif cgy=='ai' then
printh(obj.stat)
end
end

Device Control svcreq("epctl",string.format("json:%s:%s",path,cmd))

The 'epctl' service is used to control the device, the command is represented by a json string, the format is json:path:cmd.

Where path is the object path, cmd is the operation instruction.

cmd can choose the following values:

  • click : click the operation, and automatically select the real operation according to the device object. If it is a switch, it will be reversed, and if it is a device or a sensor, it will open the device page.
  • on : turn on
  • off : turn off
  • set[sw=1 type=0x81 val=50] : set to On, and change the analog value at the same time, type can be omitted, val can also be omitted, and the original value of the device will be used after omission. sw means switch, 0 is off, 1 is On, it can easily replace type (if you don't know how to set type)
  • selbind : Open the interface for resetting the device object

Example:

svcreq("epctl","json:$NM_SCENE.1:set[sw=1 val=127]") -- Operate the first device (dimming light) on the scene, turn it on and set the brightness to 50%
svcreq("epctl","json:$NM_HOME.1:click") --Operate the first device on the homepage, switch reverse

Object Information Description

When svcget obtains the object data successfully, it returns the table, which contains detailed information.

  • cgy: object type (ep,io,ai)
  • cls: detailed specification
  • name: name, may be empty

ep: device object, can use all io data under the device.

io: io object, corresponding to a function point on a device. At this time, vidx, type, val, rv data can be used in the table.

ai: intelligent/scene object. At this time, stat data can be used in the table, and stat is the state of ai.

System Information

Get Device Type

svcget('$sys.model') get device type, NatureMiniPro's model='LSNAMIV3'

Get Language Settings

svcget('$sys.lang') get the language setting, return the language serial number, 1 means English, 2 means Chinese, and those that are not supported temporarily will return English.

Query Device Temperature

svcget('$sc.T') query device temperature data

Get Weather Forecast

svcget('$weather.cur') query weather forecast

The content format of the weather forecast data example is as follows:

+"wind" ["355.08,15.96"]
+"pressure" [101348.367]
+"pm25" [9]
+"nextHours"(table: 0x0b4d0680)
| +1(table: 0x0b4d06a8)
| +"wind" ["355.35,15.39"]
| +"pressure" [101303.365]
| +"pm25" [8]
| +"skycon" ["CLOUDY"]
| +"hourFullStr" ["2023-04-25T16:00+08:00"]
| +"hourTs" [1682409600]
| +"temperature" [19.67]
| +"humidity" [0.68]
| +"aqi" [33]
+"queryCity"(table: 0x0b4cf290)
| +"distance" [12779.155401337]
| +"lat" [23.021548]
| +"city" ["广东省佛山市"]
| +"lng" [113.121416]
+"skycon" ["CLOUDY"]
+"queryHourTs" [1682406000]
+"queryHourFullStr" ["2023-04-25T15:00+08:00"]
+"temperature" [19.5]
+"humidity" [0.71]
+"aqi" [30]

Possible values of skycon:

local skycon_namemap={
CLEAR_DAY='clear day';
CLEAR_NIGHT='clear Night';
PARTLY_CLOUDY_DAY='partly cloudy day';
PARTLY_CLOUDY_NIGHT='partly cloudy night';
CLOUDY='cloudy';
LIGHT_HAZE='light';
MODERATE_HAZE='moderate haze';
HEAVY_HAZE='heavy haze';
RAIN='rain';
LIGHT_RAIN='light rain';
MODERATE_RAIN='moderate rain';
HEAVY_RAIN='heavy rain';
STORM_RAIN='storm rain';
FOG='fog';
LIGHT_SNOW='light snow';
MODERATE_SNOW='moderate snow';
HEAVY_SNOW='heavy snow';
STORM_SNOW='storm snow';
DUST='dust';
SAND='sand';
WIND='wind';
}

Tool Script

In order to facilitate programming, we provide some written scripts to complete specific functions.

Tool package(click to download)

Utility Tool colox_mini.lua

colox_mini.lua - Provides convenient LifeSmart scene control

Define Key Area

mini:add_touch(path,cmd,idx,flag,x,y,x2,y2,kv) Define a key area

  • path: Corresponding scene (for example: $NM_HOME.1)

  • cmd: The command corresponding to the key

    • click : Click on the action to automatically select the real action based on the device object. If it is a switch, it will be reversed, if it is a device or sensor, it will open the device page
    • on : turn on
    • off : turn off
    • set[sw=1 type=0x81 val=50] : The setting is turned on, and the analog value is changed at the same time. The type can be omitted, and the val can also be omitted. After omitting, the original value of the device will be used. sw means switch, 0 is off, 1 is on, it can easily replace type (if you don't know how to set type)
    • selbind : Open the interface for resetting the device object
  • idx: Integer, key sequence number, starting from 1

  • flag: integer (0: default button, allow the page to be slid; 1: the touch area does not allow the page to swipe)

  • x,y,x2,y2: Button hot zone (If it is a polygonal area, then x is an array, and the contents of the array are polygon points)

  • kv: table, storing custom data

#include colox_mini.lua
local mini=cx_mini_init()
mini:add_touch('$HOME.1',nil,1,0,309,180,480,480)
mini:add_touch('$HOME.2',nil,2,0,0,0,480,232)
mini:add_touch('$HOME.3',nil,3,0,0,230,306,480)

Handle Key Operation

hotidx,trigger=mini:scantouch(dotrigger) handle key operation

  • dotrigger: Whether to execute the action
  • return:
    • hotidx: Touch the serial number of the button
    • trigger: Whether to trigger the action (if dotrigger is true and trigger is also true, then the defined cmd will be executed internally)
local hotidx,trigger=mini:scantouch(true)

Draw Key State

  • Users need to draw the keys themselves
  • Process all defined keys by scanning for drawing

Example:

function _draw()
cls()
local hotidx,trigger=mini:scantouch(true)
for k,v in pairs(mini.mini_objs) do
local info=mini:get_info(k)
if info then
--Judging switch state
if info.sw then
--Draw the pressed state, simply fill the hotspot rectangle
rectfill(v[3],v[4],v[5],v[6],red)
else
--Draw the idle state, simply fill the hotspot rectangle
rectfill(v[3],v[4],v[5],v[6],blue)
end
else
--Invalid state (the configured path is not a switch scene device)
rectfill(v[3],v[4],v[5],v[6],orange)
end
end
end

Utility Tool colox_layer.lua

colox_layer.lua - Provides convenient multi-layer image drawing

  • By defining tree-shaped drawing nodes (levels), it is convenient to draw backgrounds/buttons/retouched pictures
  • Objects in each layer use relative coordinates
  • Draw according to the layer level when drawing
  • You can control whether each layer is drawn
  • You can dynamically modify the drawing data in the layer

Steps for usage

  • Define top-level objects
  • Add sublayer objects, and sublayer objects of sublayer objects
  • Reference objects that need to be dynamically modified for later operations
  • In the main loop, modify the object according to the state, and draw

Create LAYER, currently supports the following types:

  • CXLAYER(id,z,x,y) logic layer, no actual drawing
  • CXLAYER_LABEL(id,z,txt,x,y,col,opt,size,fname,outlinecol) string drawing
  • CXLAYER_IMG(id,z,x,y,img,anch,sx,sy,sw,sh) image drawing

General parameter description (most parameters can be omitted):

  • id: tag name, can be used to find
  • z: the drawing order of the same level (small priority drawing)
  • x,y: the coordinates of the object relative to the upper layer
  • txt: the string to draw
  • outlinecol: the color to draw the outlinecol
  • img: the image to draw
  • anch: image drawing parameters
    • bit0 1 means alpha blending drawing (only 32-bit color mode is valid)
    • bit1-2 indicates the anchor point in the x direction 0: left, 1: right, 2: middle
    • bit3-4 indicates the anchor point in the y direction 0: up, 1: down, 2: middle