Changes for page 2 Script
Last modified by Devin Chen on 2025/06/06 14:03
Summary
-
Page properties (2 modified, 0 added, 0 removed)
Details
- Page properties
-
- Author
-
... ... @@ -1,1 +1,1 @@ 1 -XWiki. Stone1 +XWiki.Hunter - Content
-
... ... @@ -13,7 +13,7 @@ 13 13 for example. addr_setshort(addr,num) Function: Write 16-bit signed decimal address 14 14 addr_getshort(addr) Function:Read 16-bit signed decimal address 15 15 addr_getword(string addr)Function: Read 16-bit unsigned decimal address 16 -More script function are in the second section of [[“V-BOX Script Interface Manual”>>doc:V-BOX.V-Net.04 Lua Script.01 Lua Functions.WebHome]] 16 +More script function are in the second section of [[“V-BOX Script Interface Manual”>>doc:V-BOX.V-Net.Manual.04 Lua Script.01 Lua Functions.WebHome]] 17 17 18 18 == **1.2 Arithmetic** == 19 19 ... ... @@ -135,7 +135,85 @@ 135 135 end 136 136 {{/code}} 137 137 138 +== **1.6 LINE Notify** == 138 138 140 +This example shows how to use the LINE Notify to send message into LINE group. When monitoring bit "@test" changes, it will trigger and send the message. Please replace with your own Token. 141 + 142 +{{code}} 143 +local tempBit = 0 144 +local tempWord = 0 145 + 146 +local LineToken = "08XCpubkOdwGdGgRTXF0x8umiyrALtoM0v6lBFUV6PC" 147 + 148 +local https = require("https") 149 +local json = require("json") 150 +local ltn12 = require("ltn12") 151 + 152 +-- Send http.get request and return response result 153 +function getHttpsUrl(url,header,reqbody) 154 + local body = {} 155 + local bodyJson = json.encode(body) 156 + local result_table, code, headers, status = https.request{ 157 + method = "POST", 158 + url = url, 159 + source = ltn12.source.string(reqbody), 160 + headers = header, 161 + sink = ltn12.sink.table(body) 162 + } 163 + print("code:"..code) 164 + if code~= 200 then 165 + return 166 + else 167 + return body 168 + end 169 +end 170 + 171 +function getMessageUrl(lineMessage) 172 + local url = "https://notify-api.line.me/api/notify" 173 + local reqMess = "message="..lineMessage 174 + local headers = 175 + { 176 + ["Authorization"] = "Bearer "..LineToken, 177 + ["Content-Type"] = "application/x-www-form-urlencoded", 178 + ["Content-Length"] = #reqMess 179 + } 180 + 181 + print("Get the link:"..url) 182 + getHttpsUrl(url, headers, reqMess) 183 +end 184 + 185 + 186 +function linenotify.main() 187 + local bitValue = addr_getbit("@test"); 188 + local message = '' 189 + print("b=="..bitValue) 190 + if bitValue == 1 and bitValue ~= tempBit then 191 + message = 'Alarm V-Box triggered, the output is '.. bitValue 192 + getMessageUrl(message) 193 + print("Notification pushed of triggering alarm,"..bitValue) 194 + elseif bitValue == 0 and bitValue ~= tempBit then 195 + message = 'Alarm V-Box dismissed, the output is '.. bitValue 196 + getMessageUrl(message) 197 + print("Notification pushed of dismissing alarm,"..bitValue) 198 + end 199 + tempBit = bitValue----Prevent monitoring values from continuous being sent to the platform 200 + 201 + local wordValue = addr_getword("@t2") 202 + print("w=="..wordValue) 203 + --dosomething 204 + if wordValue >= 100 and wordValue ~= tempWord and tempWord <= 100 then 205 + message = 'Alarm V-Box triggered, the temperature is '.. wordValue 206 + getMessageUrl(message) 207 + print("Notification pushed of triggering alarm,"..wordValue) 208 + elseif wordValue < 100 and wordValue ~= tempWord and tempWord >= 100 then 209 + message = 'Alarm V-Box dismissed, the temperature is '.. wordValue 210 + getMessageUrl(message) 211 + print("Notification pushed of dismissing alarm,"..wordValue) 212 + end 213 + tempWord = wordValue----Prevent monitoring values from continuous being sent to the platform 214 +end 215 +{{/code}} 216 + 139 139 = **2 V-Box connect with third part server** = 140 140 141 141 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. ... ... @@ -154,149 +154,280 @@ 154 154 155 155 (% class="mark" %)2.If your server requires SSL certificate to log in,please use OpenCloud.Because only OpenCloud platform can support to upload certificate 156 156 157 -== **2.1 V-Box connect with customer server:grouprobotinfo.com** == 235 +(% class="wikigeneratedid" %) 236 +**✎Note: **Before program the script of MQTT, please make sure the server(MQTT broker) can be connected through MQTT Client tool. 158 158 238 +(% class="wikigeneratedid" %) 239 +Tool link: **[[MQTT.fx>>http://mqttfx.jensd.de/index.php/download]]** 240 + 241 +== **2.1 V-Box connect with test server(General Example)** == 242 + 243 +{{code language="lua"}} 244 +--MQTT configuration table 245 +local MQTT_CFG={} 246 +MQTT_CFG.username = "weconsupport" 247 +MQTT_CFG.password = "123456" 248 +MQTT_CFG.netway = 0 249 +MQTT_CFG.keepalive = 60 250 +MQTT_CFG.cleansession = 1 251 +--TCP URL 252 +MQTT_URL = "tcp://mq.tongxinmao.com:1883" 253 +--Client ID 254 +MQTT_CLIENT_ID = "V-BOXH-AG" 255 + 256 +--Generate UUID 257 +function uuid() 258 + local seed = {'e','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'} 259 + local tb = {} 260 + for i=1, 32 do 261 + table.insert(tb, seed[math.random(1,16)]) 262 + end 263 + local sid=table.concat(tb) 264 + return string.format('%s', 265 + string.sub(sid,1,32) 266 + ) 267 +end 268 + 269 + 270 +--Topic name to subscribed 271 +local SUBSCRIBE_TOPIC = 'testtopic/test/no1/123456' 272 + 273 +--Topic name to be published 274 +local PUBLISH_TOPIC = 'testtopic/test/no1/7890' 275 + 276 + 277 +--real time 278 +local LAST_TIME = 0 279 + 280 + 281 +--initialize mqtt 282 +function mqtt_init() 283 + print(string.format("mqtt init mqtt_url:%s mqtt_clientid:%s", MQTT_URL, MQTT_CLIENT_ID)) 284 + g_mq, err = mqtt.create(MQTT_URL, MQTT_CLIENT_ID) -- create mqtt object,and declare it as a global variable 285 + if g_mq then 286 + g_mq:on("message", mqtt_msg_callback) -- Register a callback for receiving messages 287 + print("mqtt init success") 288 + else 289 + print("mqtt init failed:", err) 290 + end 291 +end 292 + 293 +-- connect to mqtt 294 +function mqtt_connect() 295 + print("mqtt connecting...") 296 + local stat, err = g_mq:connect(MQTT_CFG) 297 + if stat == nil then 298 + print("mqtt connect failed:", err) 299 + return 300 + else 301 + print("mqtt connected") 302 + end 303 + g_mq:subscribe(SUBSCRIBE_TOPIC, 0) 304 +end 305 + 306 +-- Received message callback function 307 +function mqtt_msg_callback(topic, msg) 308 + print("topic:", topic) 309 + print("msg:", msg) 310 + local objMsg = json.decode(msg) 311 + local water = objMsg.data.waterlevel 312 + local temp = objMsg.data.temperature 313 + addr_setword("@HDW20",water) 314 + addr_setword("@HDW10",temp) 315 +end 316 + 317 +--Send data (data upload to platform and encapsulate it with custom functions) 318 +function send_data() 319 + local pub_data = { 320 + timestamp = os.time(), 321 + messageId = 1, 322 + event = 'test_data', 323 + mfrs = 'V-Box', 324 + data = { 325 + id = uuid(), 326 + waterlevel = addr_getword("@HDW10"), 327 + temperature = addr_getword("@HDW20") 328 + } 329 + } 330 + return g_mq:publish(PUBLISH_TOPIC, json.encode(pub_data), 0, 0) 331 +end 332 + 333 + 334 +--main function fixed timed execution 335 +function MQTT.main() 336 + --dosomething 337 + print(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " main start") 338 + --determine the mqtt object whether exist 339 + if g_mq then 340 + --determine the mqtt object whether has been connected or not 341 + if g_mq:isconnected() then 342 + send_data() 343 + else 344 + --if exceed 20 sec not connect, reconnect once 345 + if os.time() - LAST_TIME > 20 then 346 + LAST_TIME = os.time() 347 + --connect to mqtt or reconnect 348 + mqtt_connect() 349 + end 350 + end 351 + else 352 + --mqtt object does not exist so create new one 353 + mqtt_init() 354 + end 355 + print(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " main end") 356 +end 357 +{{/code}} 358 + 359 +== **2.2 V-Box connect with customer server:grouprobotinfo.com** == 360 + 159 159 This demo does not use SSL certification. Script is as below 160 160 161 161 Demo1: 162 162 163 -(% class="box infomessage" %) 164 -((( 165 -~-~- Meta class 166 -~-~-main 365 +{{code language="lua"}} 366 +-- Meta class 367 +--main 167 167 function mq.main() 168 - if not mq.m then 169 - local err = "" 170 -\\ mq.m, err = mqtt.create("tcp:~/~/grouprobotinfo.com:1883", "ClienID") ~-~- create connection 171 - if mq.m then 172 - mq.config = { 173 - username = "",~-~- ID 174 - password = "",~-~- password 175 - netway = 1, ~-~- Ethernet connection, WIFI=1 176 - ~-~- keepalive = 100, ~-~- Optional, set the connection heartbeat interval for 100 seconds. 177 - ~-~- cleansession = 0, ~-~- Optional, keep session 178 - } 179 - mq.m:on("message", function(topic, msg) ~-~- Register for receiving message callbacks 180 - local str = string.format("%s:%s", topic, msg) 181 - ~-~- print("mqtt msg:", str) ~-~- Print out the received topics and content 182 - end 183 - ) 184 - mq.m:on("offline", function (cause) ~-~- Register for lost connection callbacks 185 - ~-~- addr_setstring("@xxx", "cause"..(cause or " got nil")) 186 - end) 187 - mq.m:on("arrived", function() ~-~- Registration for sending messages to callbacks 188 - print("msg arrived") 189 - end) 190 - else 191 - print("mqtt create failed:", err) ~-~- Create object failed 192 - end 193 - else 194 - if mq.m:isconnected() then ~-~- If online, post a message 195 - local phaseStatus ="unknow" 196 - if addr_getbit("@Standby")== 1 then 197 - phaseStatus = "Standby" 198 - elseif addr_getbit("@Pre-Freeze")==1 then 199 - phaseStatus= "Pre-Freeze" 200 - elseif addr_getbit("@Prepare")==1 then 201 - phaseStatus ="Prepare" 202 - elseif addr_getbit("@Primary Dry")==1 then 203 - phaseStatus = "Primary dry" 204 - elseif addr_getbit("@Secondary Dry")==1 then 205 - phaseStatus = "Secondary Dry" 206 - end 207 -~-~- print(addr_getbit("@Primary Dry")) 208 -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-- 209 - local activating ="unknow" 210 - if addr_getbit("@Compressor")==1 then 211 - activating = ",".."Compressor" 212 - end 213 - if addr_getbit("@Silicone Pump")==1 then 214 - activating = activating..",".."Silicone Pump" 215 - end 216 - if addr_getbit("@Vacuum Pump")==1 then 217 - activating = activating..",".."Vacuum Pump" 218 - end 219 - if addr_getbit("@Root Pump")==1 then 220 - activating = activating..",".."Root Pump" 221 - end 222 - if addr_getbit("@Heater")==1 then 223 - activating = activating..",".."Heater" 224 - end 225 - if addr_getbit("@Valve Silicone")==1 then 226 - activating = activating..",".."Valve Silicone" 227 - end 228 - if addr_getbit("@Valve Ice Condenser")==1 then 229 - activating = activating..",".."Valve Ice Condenser" 230 - end 231 - if addr_getbit("@Valve Vacuum Pump")==1 then 232 - activating = activating..",".."Valve Vacuum Pump" 233 - end 234 - local pr_activating =string.sub(activating,2) 235 - ~-~- print(pr_activating) 369 + if not mq.m then 370 + local err = "" 236 236 372 + mq.m, err = mqtt.create("tcp://grouprobotinfo.com:1883", "ClienID") -- create connection 373 + if mq.m then 374 + mq.config = { 375 + username = "",-- ID 376 + password = "",-- password 377 + netway = 1, -- Ethernet connection, WIFI=1 378 + -- keepalive = 100, -- Optional, set the connection heartbeat interval for 100 seconds. 379 + -- cleansession = 0, -- Optional, keep session 380 + } 381 + mq.m:on("message", function(topic, msg) -- Register for receiving message callbacks 382 + local str = string.format("%s:%s", topic, msg) 383 + -- print("mqtt msg:", str) -- Print out the received topics and content 384 + end 385 + ) 386 + mq.m:on("offline", function (cause) -- Register for lost connection callbacks 387 + -- addr_setstring("@xxx", "cause"..(cause or " got nil")) 388 + end) 389 + mq.m:on("arrived", function() -- Registration for sending messages to callbacks 390 + print("msg arrived") 391 + end) 392 + else 393 + print("mqtt create failed:", err) -- Create object failed 394 + end 395 + else 396 + if mq.m:isconnected() then -- If online, post a message 397 + local phaseStatus ="unknow" 398 + if addr_getbit("@Standby")== 1 then 399 + phaseStatus = "Standby" 400 + elseif addr_getbit("@Pre-Freeze")==1 then 401 + phaseStatus= "Pre-Freeze" 402 + elseif addr_getbit("@Prepare")==1 then 403 + phaseStatus ="Prepare" 404 + elseif addr_getbit("@Primary Dry")==1 then 405 + phaseStatus = "Primary dry" 406 + elseif addr_getbit("@Secondary Dry")==1 then 407 + phaseStatus = "Secondary Dry" 408 + end 409 +-- print(addr_getbit("@Primary Dry")) 410 +------------------------------------------------------------------------------------------------------------------------- 411 + local activating ="unknow" 412 + if addr_getbit("@Compressor")==1 then 413 + activating = ",".."Compressor" 414 + end 415 + if addr_getbit("@Silicone Pump")==1 then 416 + activating = activating..",".."Silicone Pump" 417 + end 418 + if addr_getbit("@Vacuum Pump")==1 then 419 + activating = activating..",".."Vacuum Pump" 420 + end 421 + if addr_getbit("@Root Pump")==1 then 422 + activating = activating..",".."Root Pump" 423 + end 424 + if addr_getbit("@Heater")==1 then 425 + activating = activating..",".."Heater" 426 + end 427 + if addr_getbit("@Valve Silicone")==1 then 428 + activating = activating..",".."Valve Silicone" 429 + end 430 + if addr_getbit("@Valve Ice Condenser")==1 then 431 + activating = activating..",".."Valve Ice Condenser" 432 + end 433 + if addr_getbit("@Valve Vacuum Pump")==1 then 434 + activating = activating..",".."Valve Vacuum Pump" 435 + end 436 + local pr_activating =string.sub(activating,2) 437 + -- print(pr_activating) 237 237 238 - local status_text ="unknow" 239 - if addr_getbit("@Status Run")==1 then 240 - status_text = "RUNNING" 241 - else 242 - status_text = "STOP" 243 - end 244 -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-- 245 -\\ local js = {type="status", 246 - mc_name ="FD300", 247 - status=status_text, 248 - elapsed_time={ 249 - hour=addr_getword("@Elapsed Time (Hour)"), 250 - min=addr_getword("@Elapsed Time (Minute)"), 251 - sec=addr_getword("@Elapsed Time (Second)") 252 - }, 253 - phase = phaseStatus, 254 - step = addr_getword("@Step"), 255 - activating_output = pr_activating, 256 - sv=addr_getshort("@SV Silicone")/10, 257 - pv=addr_getshort("@PV Silicone")/10, 258 - product1=addr_getshort("@Product 1")/10, 259 -\\ product2=addr_getshort("@Product 2")/10, 260 - product3=addr_getshort("@Product 3")/10, 261 - product4=addr_getshort("@Product 4")/10, 262 - ice1=addr_getshort("@Ice condenser 1")/10, 263 - ice2=addr_getshort("@Ice condenser 2")/10, 264 - vacuum=addr_getfloat("@Vacuum") 265 -\\ } 266 - local jsAlarm = { HPC = addr_getbit("@B_25395#W0.00"), 267 - ODPC = addr_getbit("@B_25395#W0.01"), 268 - MTPC=addr_getbit("@B_25395#W0.02"), 269 - HTT = addr_getbit("@B_25395#W1.03"), 270 - CPC = addr_getbit("@B_25395#W0.08"), 271 - CPSP =addr_getbit("@B_25395#W1.00"), 272 - CPVP =addr_getbit("@B_25395#W0.10"), 273 - CPRP =addr_getbit("@B_25395#W0.11"), 274 - HP =addr_getbit("@B_25395#W1.01"), 275 - PP= addr_getbit("@B_25395#W1.02"), 276 - PO=addr_getbit("@B_25395#W0.07"), 277 - FSE=addr_getbit("@B_25395#W2.04"), 278 - AVVSVV=addr_getbit("@B_25395#W1.12"), 279 - ICHT=addr_getbit("@B_25395#W3.06") 280 -\\ } 281 -\\ ~-~- ("@B_25395#CIO1.02") 282 - mq.m:publish("mqtt-v-box-epsilon-fd300", json.encode(js) , 0, 0) 283 - mq.m:publish("mqtt-v-box-epsilon-alarm-fd300", json.encode(jsAlarm) , 0, 0) 284 - else 285 - local stat, err = mq.m:connect(mq.config) ~-~- connection 286 - if stat == nil then ~-~-Determine whether to connect 287 - print("mqtt connect failed:", err) 288 - return ~-~- Connection failed, return directly 289 - end 290 - mq.m:subscribe("mqtt-v-box-epsilon", 0)~-~- Subscribe to topics 291 -\\ end 292 - ~-~- mq.m:unsubscribe("stc/test") 293 - ~-~- mq.m:disconnect() ~-~- close matt 294 - ~-~- mq.m:close() ~-~- close clase 295 - end 439 + 440 + 441 + local status_text ="unknow" 442 + if addr_getbit("@Status Run")==1 then 443 + status_text = "RUNNING" 444 + else 445 + status_text = "STOP" 446 + end 447 +------------------------------------------------------------------------------------------------------------------------- 448 + 449 + local js = {type="status", 450 + mc_name ="FD300", 451 + status=status_text, 452 + elapsed_time={ 453 + hour=addr_getword("@Elapsed Time (Hour)"), 454 + min=addr_getword("@Elapsed Time (Minute)"), 455 + sec=addr_getword("@Elapsed Time (Second)") 456 + }, 457 + phase = phaseStatus, 458 + step = addr_getword("@Step"), 459 + activating_output = pr_activating, 460 + sv=addr_getshort("@SV Silicone")/10, 461 + pv=addr_getshort("@PV Silicone")/10, 462 + product1=addr_getshort("@Product 1")/10, 463 + 464 + product2=addr_getshort("@Product 2")/10, 465 + product3=addr_getshort("@Product 3")/10, 466 + product4=addr_getshort("@Product 4")/10, 467 + ice1=addr_getshort("@Ice condenser 1")/10, 468 + ice2=addr_getshort("@Ice condenser 2")/10, 469 + vacuum=addr_getfloat("@Vacuum") 470 + 471 + } 472 + local jsAlarm = { HPC = addr_getbit("@B_25395#W0.00"), 473 + ODPC = addr_getbit("@B_25395#W0.01"), 474 + MTPC=addr_getbit("@B_25395#W0.02"), 475 + HTT = addr_getbit("@B_25395#W1.03"), 476 + CPC = addr_getbit("@B_25395#W0.08"), 477 + CPSP =addr_getbit("@B_25395#W1.00"), 478 + CPVP =addr_getbit("@B_25395#W0.10"), 479 + CPRP =addr_getbit("@B_25395#W0.11"), 480 + HP =addr_getbit("@B_25395#W1.01"), 481 + PP= addr_getbit("@B_25395#W1.02"), 482 + PO=addr_getbit("@B_25395#W0.07"), 483 + FSE=addr_getbit("@B_25395#W2.04"), 484 + AVVSVV=addr_getbit("@B_25395#W1.12"), 485 + ICHT=addr_getbit("@B_25395#W3.06") 486 + 487 + } 488 + 489 + -- ("@B_25395#CIO1.02") 490 + mq.m:publish("mqtt-v-box-epsilon-fd300", json.encode(js) , 0, 0) 491 + mq.m:publish("mqtt-v-box-epsilon-alarm-fd300", json.encode(jsAlarm) , 0, 0) 492 + else 493 + local stat, err = mq.m:connect(mq.config) -- connection 494 + if stat == nil then --Determine whether to connect 495 + print("mqtt connect failed:", err) 496 + return -- Connection failed, return directly 497 + end 498 + mq.m:subscribe("mqtt-v-box-epsilon", 0)-- Subscribe to topics 499 + 500 + end 501 + -- mq.m:unsubscribe("stc/test") 502 + -- mq.m:disconnect() -- close matt 503 + -- mq.m:close() -- close clase 504 + end 296 296 end 297 - )))506 +{{/code}} 298 298 299 -== **2. 2V-Box connect with Azure platform** ==508 +== **2.3 V-Box connect with Azure platform** == 300 300 301 301 In this demo,V-Box connects with Azure by SSL certification. 302 302 ... ... @@ -427,7 +427,7 @@ 427 427 end 428 428 ))) 429 429 430 -== **2. 3How 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)** ==639 +== **2.4 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)** == 431 431 432 432 1.Register a account: [[https:~~/~~/www.huaweicloud.com/intl/en-us/s/JUlPVERNJQ>>https://www.huaweicloud.com/intl/en-us/s/JUlPVERNJQ]] 433 433 ... ... @@ -576,7 +576,7 @@ 576 576 (% style="text-align:center" %) 577 577 [[image:1624441186851-536.png||height="434" width="700" class="img-thumbnail"]] 578 578 579 -== **2. 4V-Box connect with Huawei platform** ==788 +== **2.5 V-Box connect with Huawei platform** == 580 580 581 581 In this demo,V-Box connects with Huawei by SSL certification. 582 582 ... ... @@ -697,7 +697,7 @@ 697 697 (% style="text-align:center" %) 698 698 [[image:1624506666650-161.png||height="547" width="1000" class="img-thumbnail"]] 699 699 700 -== **2. 5V-Box connect with AWS platform** ==909 +== **2.6 V-Box connect with AWS platform** == 701 701 702 702 === **Log in AWS** === 703 703