Changes for page 2 Script

Last modified by Devin Chen on 2026/03/10 10:53

From version 20.1
edited by Devin Chen
on 2026/03/05 16:07
Change comment: There is no comment for this version
To version 5.1
edited by Devin Chen
on 2025/09/09 11:17
Change comment: There is no comment for this version

Summary

Details

Page properties
Parent
... ... @@ -1,1 +1,1 @@
1 -V-Net 2\.0.Demo.WebHome
1 +V-BOX.V-Net.Training.WebHome
Content
... ... @@ -27,12 +27,12 @@
27 27  
28 28  == **1.4 Short message** ==
29 29  
30 -The following demo shows that when the alarm condition is reached: temp1 > 50 (lasts more than 5 seconds) , then send an "alarm trigger" sms.
30 +The following demo shows that when the alarm condition is reached: temp1 > 5 & temp2 >10 & temp3 < 20(lasts more than 5 seconds) , then send an "alarm trigger" sms.
31 31  
32 32  When the alarm condition is released,then send an  "alarm release" sms.
33 33  
34 -Real-time tags setting
35 -[[image:1757401730077-284.png]]
34 +Registers setting
35 +\\[[image:1757387490907-390.png]]
36 36  
37 37  Script is as below:
38 38  
... ... @@ -39,51 +39,35 @@
39 39  {{code language="lua"}}
40 40  function sms.main()
41 41  ------send condition------
42 - local temp1 = addr_getword("@Temperature1")
43 - local timer = addr_getword("@Timer")
44 - local tag = addr_getbit("@Tag")
45 - local tag2 = addr_getbit("@Y0")
46 - local sms_timer = addr_getword("@SMSTimer")
47 - local sms_id = addr_getword("@id") -- SMS ID
48 -
49 - ------lasting time------
50 - if temp1 > 50 then
51 - timer = timer + 1
52 - addr_setword("@Timer", timer)
53 - else
54 - timer = 0
55 - addr_setword("@Timer", timer)
42 +local temp1 = addr_getword("@Temperature1")
43 +local temp2 = addr_getword("@Temperature2")
44 +local temp3 = addr_getword("@Temperature3")
45 +local timer = addr_getword("@Timer")
46 +local tag = addr_getbit("@Tag")
47 +------lasting time------
48 +if temp1 > 5 and temp2 > 10 and temp3 < 20 then
49 + timer = timer + 1
50 + addr_setword("@Timer",timer)
51 +else
52 + timer = 0
53 + addr_setword("@Timer",timer)
54 +end
55 +------send sms & output Y0------
56 +if timer > 5 then
57 + if tag == 0 then
58 + --send SMS to 2 recipients
59 + send_sms_ira("198****4700","alarm trigger")
60 + local id = send_sms_ira("187****3130","alarm trigger")
61 + --get the SMS state from ID
62 + local state = sms_get_state(id)
63 + addr_setword("@state",state)
64 + addr_setbit("@Tag",1)
56 56   end
57 -
58 - ------send sms & output Y0------
59 - if timer > 5 then
60 - if tag == 0 then
61 - --send SMS to 2 number
62 - local id = send_sms_ira("198****4800", "alarm trigger")
63 - local id = send_sms_ira("187****3130", "alarm trigger")
64 - addr_setword("@id", id) -- Store SMS ID to dedicated variable
65 - addr_setbit("@Tag", 1)
66 - addr_setword("@SMSTimer", 0) -- Reset SMS status check timer
67 - else
68 - -- If SMS already sent, increment timer
69 - sms_timer = sms_timer + 1
70 - addr_setword("@SMSTimer", sms_timer)
71 -
72 - -- Check SMS status after 20 seconds
73 - if sms_timer >= 20 then
74 - local state = sms_get_state(sms_id)
75 - addr_setword("@state", state)
76 - addr_setword("@SMSTimer", 0) -- Reset timer
77 - end
78 - end
79 - elseif tag == 1 then
80 - -- Send alarm release SMS
81 - send_sms_ira("198****4800","alarm release")
82 - send_sms_ira("187****3130", "alarm release")
83 - addr_setbit("@Tag", 0)
84 - addr_setword("@SMSTimer", 0) -- Reset timer
85 - end
66 +elseif tag == 1 then
67 +send_sms_ira("198****4700","alarm release")
68 +addr_setbit("@Tag",0)
86 86  end
70 +end
87 87  {{/code}}
88 88  
89 89  == **1.5 Telegram notification** ==
... ... @@ -590,7 +590,7 @@
590 590  
591 591  This demo shows how to obtain UTC standard time from the V-Box and get the local time based on the device's time zone.
592 592  
593 -**Real-time tags configuration**
577 +**Register configuration**
594 594  
595 595  [[image:1757043722849-910.png]]
596 596  
... ... @@ -599,8 +599,11 @@
599 599  (% style="text-align:center" %)
600 600  [[image:1757043900216-610.png||height="412" width="439"]]
601 601  
586 +
602 602  **Script**
603 603  
589 +
590 +
604 604  {{code language="lua"}}
605 605  function time.main()
606 606  
... ... @@ -635,77 +635,6 @@
635 635  (% style="text-align:center" %)
636 636  [[image:1757044137457-543.png||height="468" width="708"]]
637 637  
638 -== **1.13 JSON encoding and decoding** ==
639 -
640 -This demo shows how to encode tag values into JSON format and decode the JSON to assign data to new tags.
641 -
642 -**Real-time tags configuration**
643 -
644 -(% style="text-align:center" %)
645 -[[image:PixPin_2025-11-05_09-21-33.png]]
646 -
647 -(% class="wikigeneratedid" %)
648 -**Script configuration**
649 -
650 -(% style="text-align:center" %)
651 -[[image:PixPin_2025-11-05_09-23-03.png||height="376" width="400"]]
652 -
653 -(% class="wikigeneratedid" %)
654 -**Script**
655 -
656 -{{code language="lua"}}
657 -function json_test.main()
658 -
659 --- Load JSON module for encoding and decoding JSON data
660 -local json = require("json")
661 -
662 --- Read values from different types of address tags
663 -local a = addr_getbit("@bit") -- Read a bit (boolean) value
664 -local b = addr_getword("@word") -- Read a word (integer) value
665 -local c = addr_getfloat("@floating number") -- Read a floating point value
666 -local d = addr_getstring("@string",10) -- Read a string value with max length 10
667 -local e = json.null -- JSON null value constant
668 -
669 --- Encode the data into JSON format
670 -local jsondata = json.encode({
671 - bit = a or 0, -- Bit value with default 0
672 - word = b or 0, -- Word value with default 0
673 - float = c or 0, -- Float value with default 0
674 - str = d or 0, -- String value with default 0
675 - {none= e} -- Nested table with null value
676 - })
677 -print("json encode:", jsondata) -- Print the encoded JSON data
678 -print(".......................")
679 -
680 --- Decode the JSON string back to Lua table
681 -local data = json.decode(jsondata)
682 -
683 --- Check if decoding was successful and print the results
684 -if type(data) == 'table' then
685 - print("json decode:")
686 - -- Iterate through all key-value pairs in the decoded table
687 - for k, v in pairs(data) do
688 - print(k, v)
689 - end
690 -end
691 --- Write the decoded values back to corresponding address tags
692 -addr_setbit("@bit_copy", data["bit"] or 0) -- Write bit value
693 -addr_setword("@word_copy", data["word"] or 0) -- Write word value
694 -addr_setfloat("@floating number_copy", data["float"] or 0) -- Write float value
695 -addr_setstring("@string_copy", data["str"], 10) -- Write string value
696 -
697 -print("-----------------------")
698 -
699 -end
700 -{{/code}}
701 -
702 -(% class="wikigeneratedid" %)
703 -**Result**
704 -
705 -(% style="text-align:center" %)
706 -[[image:1762306008662-362.png||height="485" width="1228"]]
707 -
708 -
709 709  = **2 Third part server** =
710 710  
711 711  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.
... ... @@ -1921,214 +1921,3 @@
1921 1921  {{info}}
1922 1922  ✎Note: If you want to use CMD to access Mysql, you need to add the bin file to the environment variable, please check online for details of the operation.
1923 1923  {{/info}}
1924 -
1925 -== **2.8 Google sheet** ==
1926 -
1927 - In this demo you can upload the real-time tags data to google sheet
1928 -
1929 -**1.Google sheets setting**
1930 -
1931 -1.1 Create a table file based on actual needs and fill in the headers.
1932 -
1933 -(% style="text-align:center" %)
1934 -[[image:WeCom Screenshot_20260305114836.png||height="618" width="977"]]
1935 -
1936 -1.2 Modify the access permissions of the table to 'Anyone on the internet with the link can edit'.
1937 -
1938 -(% style="text-align:center" %)
1939 -[[image:z6rgXmeOMz1.png||height="621" width="982"]]
1940 -
1941 -1.3 Enable the Apps Script extension feature for the sheet.
1942 -
1943 -(% style="text-align:center" %)
1944 -[[image:u8QbgKcOgA.png||height="621" width="982"]]
1945 -
1946 -1.4 On the Apps Script editing page, program according to actual needs.
1947 -
1948 -Google Apps Script Demo
1949 -
1950 -{{code language="JavaScript"}}
1951 -/**
1952 - * Handles POST requests: parses JSON data, writes it to the sheet with timestamp
1953 - * Expected JSON format: {
1954 - * sheetName: "Sheet1" (optional, defaults to "Sheet1"),
1955 - * integerNumber: 123,
1956 - * floatingNumber: 45.67,
1957 - * stringData: "example text"
1958 - * }
1959 - */
1960 -function doPost(e) {
1961 - // 1. Parse the JSON data from the request body
1962 - var data;
1963 - try {
1964 - data = JSON.parse(e.postData.contents);
1965 - } catch (error) {
1966 - return ContentService.createTextOutput('Error: Invalid JSON data');
1967 - }
1968 -
1969 - // 2. Get the target sheet (defaults to "Sheet1")
1970 - var sheetName = data.sheetName || 'Sheet1';
1971 - var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
1972 - if (!sheet) {
1973 - return ContentService.createTextOutput('Error: Specified sheet not found');
1974 - }
1975 -
1976 - // 3. Verify required fields are present
1977 - if (data.integerNumber === undefined ||
1978 - data.floatingNumber === undefined ||
1979 - data.stringData === undefined) {
1980 - return ContentService.createTextOutput('Error: Missing required fields (integerNumber, floatingNumber, stringData)');
1981 - }
1982 -
1983 - // 4. Generate formatted current timestamp (first column)
1984 - var now = new Date();
1985 - var timeZone = SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone();
1986 - var formattedTimestamp = Utilities.formatDate(now, timeZone, "yyyy-MM-dd HH:mm:ss");
1987 -
1988 - // 5. Extract data from JSON
1989 - var integerNumber = data.integerNumber;
1990 - var floatingNumber = data.floatingNumber;
1991 - var stringData = data.stringData;
1992 -
1993 - // 6. Append the data to the sheet with proper column mapping:
1994 - // Column A: Time (timestamp)
1995 - // Column B: Integer number
1996 - // Column C: Floating number
1997 - // Column D: String data
1998 - sheet.appendRow([formattedTimestamp, integerNumber, floatingNumber, stringData]);
1999 -
2000 - // 7. Return success response
2001 - return ContentService.createTextOutput('Success: Data written to Google Sheets');
2002 -}
2003 -{{/code}}
2004 -
2005 -(% style="text-align:center" %)
2006 -[[image:PixPin_2026-03-05_14-40-27.png||height="623" width="985"]]
2007 -
2008 -1.5 Click "Deploy" to make a new deployment. Select the 'Web APP' type, set the user permissions, and complete the deployment of the script.
2009 -
2010 -(% style="text-align:center" %)
2011 -[[image:3W00uSwkVQ1.png||height="641" width="981"]]
2012 -
2013 -1.6 In the 'Manage deployments' interface, obtain the URL of the web app.
2014 -
2015 -(% style="text-align:center" %)
2016 -[[image:HoLYu2nzvq.png||height="631" width="966"]]
2017 -
2018 -2. Test the Web APP URL
2019 -
2020 -With the Web APP URL, we can test it via API testing tool (Postman, Hoppscotch)
2021 -
2022 -Web APP Handles POST requests: parses JSON data, writes it to the sheet with timestamp
2023 -
2024 -Header: [Content-Type: application/json]
2025 -
2026 -Expected JSON format: {
2027 -
2028 - sheetName: "Sheet1" (optional, defaults to "Sheet1"),
2029 -
2030 - integerNumber: 123,
2031 -
2032 - floatingNumber: 45.67,
2033 -
2034 - stringData: "example text"
2035 -
2036 - }
2037 -
2038 -(% style="text-align:center" %)
2039 -[[image:8a8vG9Z7IT.png||height="597" width="913"]]
2040 -
2041 -POST request execution effect
2042 -
2043 -(% style="text-align:center" %)
2044 -[[image:WeCom Screenshot_20260305153954.png||height="602" width="921"]]
2045 -
2046 -{{info}}
2047 -After successfully testing with the API tool, it is recommended to change the General access of the Google Sheet to 'Restricted.' This will help improve data security without affecting the use of the Web App.
2048 -
2049 -(% style="text-align:center" %)
2050 -[[image:RW7OUCVVp1.png||height="617" width="943"]]
2051 -{{/info}}
2052 -
2053 -**3. V-box settings**
2054 -
2055 -3.1 Real-time tags setting
2056 -
2057 -(% style="text-align:center" %)
2058 -[[image:PixPin_2026-03-05_16-00-15.png||height="291" width="982"]]
2059 -
2060 -3.2 Lua Script:
2061 -
2062 -3.2.1 Basic Configuration Information
2063 -
2064 -(% style="text-align:center" %)
2065 -[[image:PixPin_2026-03-05_16-02-35.png||height="451" width="480"]]
2066 -
2067 -3.2.2 Lua Script Demo
2068 -
2069 -{{code language="Lua"}}
2070 --- Google Apps Script deployment URL (required)
2071 -local req_url = "https://script.google.com/macros/s/AKfycbxbAxKSysisJKdXeL5k1IuH4mYhnN2qOuq9ZbTtYJRQMSBgYi67eaqZRrS4JmhsA2dL/exec"
2072 -
2073 -function sheet.main()
2074 - -- Entry point: trigger the data sending process
2075 - prepareAndSendSheetData()
2076 -end
2077 -
2078 --- Prepares data from PLC addresses and sends it to Google Sheets
2079 --- Fetches: name (string) and score (word) from specified memory addresses
2080 --- Constructs JSON payload and triggers HTTP POST request
2081 -
2082 -function prepareAndSendSheetData()
2083 -
2084 - float_num = addr_getfloat("@floating number")
2085 - -- Build the data payload to be sent to Google Sheets
2086 - local SheetData = {
2087 - sheetName = "Sheet1", -- Target sheet name
2088 - stringData = addr_getstring("@string", 20), -- Fetch string from address (max 20 chars)
2089 - integerNumber = addr_getword("@word"), -- Fetch 16-bit integer from address
2090 - floatingNumber = string.format("%0.4f", float_num) -- Fetch 32-bit floating number from address
2091 - }
2092 -
2093 - -- Directly use the single dataset as the message
2094 - local message = SheetData
2095 -
2096 - -- Debug output (optional, can be removed in production)
2097 - print("URL: " .. tostring(req_url))
2098 - print("Request Body: " .. json.encode(message))
2099 -
2100 - -- Send the request via HTTP POST
2101 - executeHttpPostRequest(req_url, message)
2102 -end
2103 -
2104 -
2105 --- Executes HTTP POST request to Google Apps Script endpoint
2106 --- @param req_url: target URL for the POST request
2107 --- @param message: Lua table containing data to be sent (will be JSON-encoded)
2108 -
2109 -
2110 -function executeHttpPostRequest(req_url, message)
2111 - -- Required libraries
2112 - local json = require("json")
2113 - local ltn12 = require("ltn12")
2114 - local https = require("https")
2115 -
2116 - -- Encode the message table into a JSON string
2117 - local request_body = json.encode(message)
2118 - local max_redirects = 0 -- Number of redirects to follow (set to 0 for none)
2119 - local current_url = req_url
2120 -
2121 - local response_body = {}
2122 - local res, code, response_headers = https.request{
2123 - url = current_url,
2124 - method = "POST",
2125 - headers = {
2126 - ["Content-Type"] = "application/json", -- Must be text/plain to bypass CORS preflight
2127 - ["Content-Length"] = #request_body
2128 - },
2129 - source = ltn12.source.string(request_body),
2130 - sink = ltn12.sink.table(response_body)
2131 - }
2132 -
2133 -end
2134 -{{/code}}
1757401730077-284.png
Author
... ... @@ -1,1 +1,0 @@
1 -XWiki.DevinChen
Size
... ... @@ -1,1 +1,0 @@
1 -92.2 KB
Content
1762306008662-362.png
Author
... ... @@ -1,1 +1,0 @@
1 -XWiki.DevinChen
Size
... ... @@ -1,1 +1,0 @@
1 -66.9 KB
Content
3W00uSwkVQ1.png
Author
... ... @@ -1,1 +1,0 @@
1 -XWiki.DevinChen
Size
... ... @@ -1,1 +1,0 @@
1 -140.1 KB
Content
8a8vG9Z7IT.png
Author
... ... @@ -1,1 +1,0 @@
1 -XWiki.DevinChen
Size
... ... @@ -1,1 +1,0 @@
1 -132.2 KB
Content
Doa6Qhp4bS.png
Author
... ... @@ -1,1 +1,0 @@
1 -XWiki.DevinChen
Size
... ... @@ -1,1 +1,0 @@
1 -139.2 KB
Content
HoLYu2nzvq.png
Author
... ... @@ -1,1 +1,0 @@
1 -XWiki.DevinChen
Size
... ... @@ -1,1 +1,0 @@
1 -136.6 KB
Content
PixPin_2025-11-05_09-21-33.png
Author
... ... @@ -1,1 +1,0 @@
1 -XWiki.DevinChen
Size
... ... @@ -1,1 +1,0 @@
1 -65.8 KB
Content
PixPin_2025-11-05_09-23-03.png
Author
... ... @@ -1,1 +1,0 @@
1 -XWiki.DevinChen
Size
... ... @@ -1,1 +1,0 @@
1 -60.9 KB
Content
PixPin_2026-03-05_14-40-27.png
Author
... ... @@ -1,1 +1,0 @@
1 -XWiki.DevinChen
Size
... ... @@ -1,1 +1,0 @@
1 -154.8 KB
Content
PixPin_2026-03-05_16-00-15.png
Author
... ... @@ -1,1 +1,0 @@
1 -XWiki.DevinChen
Size
... ... @@ -1,1 +1,0 @@
1 -187.2 KB
Content
PixPin_2026-03-05_16-02-35.png
Author
... ... @@ -1,1 +1,0 @@
1 -XWiki.DevinChen
Size
... ... @@ -1,1 +1,0 @@
1 -60.0 KB
Content
RW7OUCVVp1.png
Author
... ... @@ -1,1 +1,0 @@
1 -XWiki.DevinChen
Size
... ... @@ -1,1 +1,0 @@
1 -100.0 KB
Content
WeCom Screenshot_20260305114836.png
Author
... ... @@ -1,1 +1,0 @@
1 -XWiki.DevinChen
Size
... ... @@ -1,1 +1,0 @@
1 -53.5 KB
Content
WeCom Screenshot_20260305153954.png
Author
... ... @@ -1,1 +1,0 @@
1 -XWiki.DevinChen
Size
... ... @@ -1,1 +1,0 @@
1 -71.7 KB
Content
u8QbgKcOgA.png
Author
... ... @@ -1,1 +1,0 @@
1 -XWiki.DevinChen
Size
... ... @@ -1,1 +1,0 @@
1 -61.3 KB
Content
z6rgXmeOMz.png
Author
... ... @@ -1,1 +1,0 @@
1 -XWiki.DevinChen
Size
... ... @@ -1,1 +1,0 @@
1 -80.7 KB
Content
z6rgXmeOMz1.png
Author
... ... @@ -1,1 +1,0 @@
1 -XWiki.DevinChen
Size
... ... @@ -1,1 +1,0 @@
1 -80.7 KB
Content