# 6. Write IoT Applications
# 6.1 Summary
This software allows users to create IoT applications to run non-real-time tasks, such as reading the value of an IEC address variable, writing a value to an IEC address, saving an IEC address variable to a local log, setting a threshold alarm, or calculating an average value.
Currently, IoT programs support Python3.
After you create a project, right-click on Other Logic, select an application, and enter the application name in the Create Non-Real-Time Application dialog box to add an IoT application. This is shown in the figure below.
After the IoT application is created, the project interface is shown in the figure in which myIoT is the application name typed by the user, and the following two items, startup configuration and Python, are the configuration items and Python source code of the IoT application, respectively.
# 6.2 Startup Configuration
There are two ways for users to choose from the execution mode of an IoT application:
- Single execution
- Loop execution
Click on the Startup Configuration option to set the execution mode of the Python program. A single execution means that the IoT application is executed only once, and a loop execution means that the application will be executed once at intervals.
Click on the loop, and the loop interval edit box will appear, which is used to set the time interval for the loop to be executed. The unit is milliseconds, as shown in the following figure:
Note: The loop execution here refers to the execution of the application at a specified interval, so the value of the variable in the corresponding python code will be initialized every time it is executed, if the user wants to be able to maintain the value of the variable in the python code, for example, the user wants to calculate the sum of IEC data within 10 seconds, set up loop execution, execute the application once a second, get the value of the variable in the python code and sum it by suming the temporary variable, this will not work. Because the sum variable will be initialized every time this application is executed.
To achieve this, the user can choose to execute it once, and then write the corresponding loop in the python code logic to get the variable. It is also recommended that users choose a single execution as much as possible, and then control the execution logic of the program themselves in the code. Loop execution is mainly used to execute some applications with the same logic every time, such as threshold alarms, saving variable values to log files, etc., of course, you can also choose a single execution in these scenarios, and then implement the corresponding logic in the python code.
# 6.3 Python Programming Instructions
Click the Python option in Other Logic, and the Python code edit box will appear, as shown in the figure below, in the edit box, you can edit the corresponding python code that needs to be executed. The default code will appear as well as some instructions for using Python programming.
Instructions for using Python code:
- Python code uses the Python3 specification;
- This software provides two python system libraries, namely smec_logging and get_iec_data, which are used to record python code running logs and obtain the value of IEC address variables, and the library files are stored in the /opt/js_runtime/pylib directory;
- If you need to use it, you need to import the corresponding library by yourself, and the code is as follows
smec_logging is a standard logstore, which contains three methods: debug, info, and error, and can print logs, and the logs are in the /var/log/smec directory:
get_iec_data library to obtain IEC address variables, you can write data to the IEC address or read the data in the IEC address, only the addresses of Q and M can be written, and use them as follows:
Users can create their own Python libraries and import them by putting them in /opt/js_runtime/pylib
Users can install python packages by themselves, note: python packages must be installed by the root user;
The code can print the variables and return their contents to the IDE at runtime, which requires print to set real-time refresh to True. For example: print("Test", flush = True)
# 6.4 Download Execution
After writing the IoT application, the user can choose to download and execute, as shown in the following figure, select the current device and the IoT application, then connect the device, and then click the download button to download the program, and the application can be executed after the download is completed.
Note: Once the device is connected, the program cannot be changed, and if you want to change the program, you need to disconnect it first. If you have a print, you will see the output in the corresponding output bar: for example:
# 6.5 IoT Programming Examples
In the following example, get the value of %QW0 address every 1s, print the sum of the maximum value and the last ten values obtained in the last ten times, and end after 100 seconds, that is, get 100 numbers, the code is as follows (the IP address of the machine is 192.168.10.238, and the user needs to modify the IP address according to the IP where the runtime is located):