Changes for page 2 Script

Last modified by Devin Chen on 2025/06/06 14:03

From version 19.1
edited by Hunter
on 2022/07/29 15:56
Change comment: There is no comment for this version
To version 22.1
edited by Hunter
on 2022/09/08 18:57
Change comment: There is no comment for this version

Summary

Details

Page properties
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,8 +154,132 @@
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:
... ... @@ -303,7 +303,7 @@
303 303  end
304 304  {{/code}}
305 305  
306 -== **2.2 V-Box connect with Azure platform** ==
508 +== **2.3 V-Box connect with Azure platform** ==
307 307  
308 308  In this demo,V-Box connects with Azure by SSL certification.
309 309  
... ... @@ -434,7 +434,7 @@
434 434  end
435 435  )))
436 436  
437 -== **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)** ==
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)** ==
438 438  
439 439  1.Register a account: [[https:~~/~~/www.huaweicloud.com/intl/en-us/s/JUlPVERNJQ>>https://www.huaweicloud.com/intl/en-us/s/JUlPVERNJQ]]
440 440  
... ... @@ -583,7 +583,7 @@
583 583  (% style="text-align:center" %)
584 584  [[image:1624441186851-536.png||height="434" width="700" class="img-thumbnail"]]
585 585  
586 -== **2.4 V-Box connect with Huawei platform** ==
788 +== **2.5 V-Box connect with Huawei platform** ==
587 587  
588 588  In this demo,V-Box connects with Huawei by SSL certification.
589 589  
... ... @@ -704,7 +704,7 @@
704 704  (% style="text-align:center" %)
705 705  [[image:1624506666650-161.png||height="547" width="1000" class="img-thumbnail"]]
706 706  
707 -== **2.5 V-Box connect with AWS platform** ==
909 +== **2.6 V-Box connect with AWS platform** ==
708 708  
709 709  === **Log in AWS** ===
710 710