2 Script
1 General Script Demo
1.1 Address Operation:Write/Read data from address A to B
For example:transfer D2 to D0
Depend on diffferent format of data.V-Box use different script functions.
for example. addr_setshort(addr,num) Function: Write 16-bit signed decimal address
addr_getshort(addr) Function:Read 16-bit signed decimal address
addr_getword(string addr)Function: Read 16-bit unsigned decimal address
More script function are in the second section of “V-BOX Script Interface Manual”
1.2 Arithmetic
1.3 Set 100 to D0--D19
1.4 Short message
When the alarm condition is reached: temp1 > 5 & temp2 >10 & temp3 < 20(lasts more than 5 seconds) , then send an "alarm trigger" sms.
When the alarm condition is released,then send an "alarm release" sms.
Script is as below:
1.5 Telegram notification
This example shows how to use the Bot API to send message into Telegram group or channel. When monitoring bit "@HDX" changes, it will trigger and send the message. Please replace with your own Token and chat id.
local tempWord = 0
local botToken = "5504549693:AAEy6a5G-sOF3CINONxMNABeYnoS4ABVlfg"
local chatID = "-641959124"--The chat id from Channel or Group
local https = require("https")
local json = require("json")
-- Send http.get request and return response result
function getHttpsUrl(url)
local body = {}
local bodyJson = json.encode(body)
local header = {}
header["content-type"] = "application/json"
local result_table, code, headers, status = https.request(url, bodyJson)
print("code:"..code)
if code~= 200 then
return
else
return body
end
end
function sendAlarm(telegramBotToken, message, telegramChatID)
local url = "https://api.telegram.org/bot"..telegramBotToken.."/sendMessage?text="..message.."&chat_id="..telegramChatID
--local url = 'http://v-box.net'
--local url = 'https://www.google.com/'
print("Get the link:"..url)
getHttpsUrl(url)
end
function AlarmNotificate.main()
local bitValue = addr_getbit("@HDX");
local message = ''
print("b=="..bitValue)
if bitValue == 1 and bitValue ~= tempBit then
message = 'Alarm triggered, the monitoring point test value is '.. bitValue
sendAlarm(botToken, message, chatID)
print("Notification pushed of triggering alarm,"..bitValue)
elseif bitValue == 0 and bitValue ~= tempBit then
message = 'Alarm dismissed, the monitoring point test value is '.. bitValue
sendAlarm(botToken, message, chatID)
print("Notification pushed of dismissing alarm,"..bitValue)
end
tempBit = bitValue----Prevent monitoring values from continuous being sent to the platform
local wordValue = addr_getword("@HDW10")
print("w=="..wordValue)
--dosomething
if wordValue >= 100 and wordValue ~= tempWord and tempWord <= 100 then
message = 'Word alarm triggered, the word value is '.. wordValue
sendAlarm(botToken, message, chatID)
print("Notification pushed of triggering alarm,"..wordValue)
elseif wordValue < 100 and wordValue ~= tempWord and tempWord >= 100 then
message = 'Word alarm dismissed, the word value is '.. wordValue
sendAlarm(botToken, message, chatID)
print("Notification pushed of dismissing alarm,"..wordValue)
end
tempWord = wordValue----Prevent monitoring values from continuous being sent to the platform
end
2 V-Box connect with third part server
V-Box have two mode.One is for V-Net,User need to use WECON server to store data.We call this V-NET platform.
1.Europe server:eu.v-box.net
2.Asean server:asean.v-box.net
Second is for OpenCloud mode.It means V-Box data will transfer to third part server and do not want to store data to WECON server.We call OpenCloud paltform
OpenCloud platform is used in configuring the script.Then V-Box can connect with third part server.
Both mode can support script to connect with third part server.the difference:
1.If you want to store in WECON server ,please use V-NET.
2.If your server requires SSL certificate to log in,please use OpenCloud.Because only OpenCloud platform can support to upload certificate
2.1 V-Box connect with customer server:grouprobotinfo.com
This demo does not use SSL certification. Script is as below
Demo1:
2.2 V-Box connect with Azure platform
In this demo,V-Box connects with Azure by SSL certification.
Video link: https://youtu.be/cdI6rIQcpMY?list=PL_Bpnb2RgaksCic9HCcVAZhU9sYwCRKzW
Tool Download link: https://wecon-disk.oss-ap-southeast-1.aliyuncs.com/Download/WIKI/V-BOX/Demo/Azure/Azure%20tool.zip
Script is as below
2.3 How to configure the Huawei platform?(✎Note: Huawei IOT DA function is only in China area.If you want this function,you need to use chinese mobile to register)
1.Register a account: https://www.huaweicloud.com/intl/en-us/s/JUlPVERNJQ
2.log in the Huawei IOTDA
https://console.huaweicloud.com/iotdm/?region=cn-north-4&locale=en-us#/dm-portal/home
3.Create product
4.Product name,manufacturer,device type and industry,set according to your own needs.
Protocol: MQTT
Data Type: JSON
After finishing configuration,please click "OK"
5.Device
After product register,continue to configure "individual register".Click "Device"-->"individual register"
Notes for registering device:
Product: Previous product registration.
Node ID, Device Name: set according to your own needs.
Secret: need to be configured, will be used when connecting later
After configuration, click OK to generate a device ID and password, which will be used for device access later.
6. Connection authentication (use MQTT.fx tool to access the IoT platform)
(1)Open mqttClientIdGenerator tool Java(TM) Platform SE binary
(2)Fill in the device ID and secret (deviceid and secret generated when registering the device) to generate connection message
Client ID, user name, password
(3) Download certificate file"North-Beijing4"
https://support.huaweicloud.com/en-us/devg-iothub/iot_02_1004.html
7.Run MQTTfx tool to connect with Huawei
Download link: http://mqttfx.jensd.de/index.php/download
(1)Click on the setting ICON
(2)Fill in IIOT MQTT device access address, and configure authentication parameters.
First: It is the server and port connected to Huawei IOT, which can be viewed through the overview of the interface.
Domain name:iot-mqtts.cn-north-4.myhuaweicloud.com
Port: 8883
Client ID: check step 6
(3)Upload SSL certificate file,check step 6
Select folder java-->DigiCertGlobalRootCA.crt.pem and click OK or apply button
(4)Connect and test publish and subscribe
Huawei publish topic format: $oc/devices/{device_id}/sys/properties/report
Huawei subscribe topic format: $oc/devices/{device_id}/sys/commands/#
(5).How to configure to view the received data format intuitively, multiple products (Huawei) are required to configure the model. The specific steps are as follows:
①Select the corresponding product from Huawei products to view
②Custom model: used to display the service ID name of the configuration report.
③Add property, ID of monitoring point, and data format:
④After the configuration is complete, check the received data on the device
2.4 V-Box connect with Huawei platform
In this demo,V-Box connects with Huawei by SSL certification.
1.Create a project access for Huawei IOT
2.configure MQTT configuration
3.Create a script with the demo as below.
Script is as below
4.Download project access into V-Box to test in debug page
2.5 V-Box connect with AWS platform
Log in AWS
Login aws account and click“Connect an IoT device”
Create policy
Click “Secure”--->“Policies”--->“Create policy”--->Click “Create”
Name the policy--->Click “JSON”--->Copy the following content--->Click “Create”
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect",
"iot:Publish",
"iot:Subscribe",
"iot:Receive",
"greengrass:Discover"
],
"Resource": "*"
}
]
}
- Create things
Click “Manage”--->“Things”--->“Create things”--->“Create single thing”
![]() |
![]() |
![]() |
Name the thing--->Click “Next”
Select the way to create certificate
![]() |
Select policy
![]() |
![]() |
- Test with MQTT.fx tool
Click “View Setting” to get the “Broker Adress”
![]() |
![]() |
![]() |
Create one connection in MQTT.fx tool, set broker port as 8883.
Upload the CA File, Client Certificate File, Client Key File
![]() |
Publish message to topic “TEST”
![]() |
![]() |
Click”Test”--->”MQTT test client”--->”Subscrible to a topic”, to get message publish from MQTT.fx tool.
And we can also send message form AWS platform to MQTT.fx tool.
![]() |
- Configurate in CloudTool
Copy the same setting in MQTT.fx to MQTT configuration
![]() |
Add a lua script and copy the lua demo into it.
![]() |
sprint = print
--Cloud mode interface to obtain the MQTT information configured by the cloud platform: (5 returns, namely the server address, client ID, connection table, last word table, certificate table)
local MQTT_URL, MQTT_CLIENTID, MQTT_CFG, MQTT_LWT, MQTT_CART = mqtt.setup_cfg()
--publish to topics
local pub_RE_TOPIC = string.format('TEST')
--Subscribe topics
local Subscribe_RE_TOPIC1 = string.format('TEST')
--variable
local last_time = 0
--Timing main function
function aws.main()
sprint(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " aws.main start")
if g_mq then
if g_mq:isconnected() then
send_Data()
else
if os.time() - last_time > 5 then
last_time = os.time()
mymqtt_connect()
end
end
else
mymqtt_init()
end
sprint(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " aws.main end")
end
-- Initialize MQTT
function mymqtt_init()
sprint(string.format("mqtt init mqtt_url:%s mqtt_clientid:%s", MQTT_URL, MQTT_CLIENTID))
g_mq, err = mqtt.create(MQTT_URL, MQTT_CLIENTID) -- Create the object and declare it as a global variable
if g_mq then
g_mq:on("message", mymqtt_msg_callback) -- Register to receive message callbacks
sprint("mqtt init success")
else
sprint("mqtt init failed:", err)
end
end
-- Connect to MQTT server
function mymqtt_connect()
sprint("mqtt connecting...")
local stat, err = g_mq:connect(MQTT_CFG,MQTT_LWT, MQTT_CART)
if stat == nil then
sprint("mqtt connect failed:", err)
return
else
sprint("mqtt connected")
end
g_mq:subscribe(TEST, 0)
end
-- Receive MQTT message callback function
function mymqtt_msg_callback(topic, msg)
print("topic:",topic)
print("revdata:",msg)
local revData = json.decode(msg)
print (revData)
if topic == Subscribe_RE_TOPIC1 then --Process topic information subscribed from the cloud
if string.match(topic,Subscribe_RE_TOPIC1) then
--if revData ~= nil then
for k,v in pairs (revData) do
print("printing revdata after kv here")
print (k,v)
end
print ("current state is",fanstate)
--end
end
end
end
--Get real-time data
function getData()
local jdata = {}
local addr = bns_get_alldata()
print(json.encode(addr))
for i,v in pairs(addr) do
if v[2] == 1 then
jdata[v[3]] = v[4]
end
end
return jdata
end
--send data
function send_Data()
local pub_data =
{
123
}
sprint(json.encode(pub_data))
print("..........",pub_RE_TOPIC)
return g_mq:publish(pub_RE_TOPIC, json.encode(pub_data), 0, 0)
end
Get message in AWS
![]() |