多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
Label: DTU Openwrt --- ## Basic preparation Install python 2.7+ Install python pip (not necessary, but this can make it easier to install the PY module) ## Get the Python code It is actually a simple tcp2serial code. [python DTU github code][1] Pay attention to the following changes according to the actual situation. ``` According to the actual userkey & device ID in your account on DeviceBit gatewayNo = 'xxx'#gateway No. in lewei50 userKey = 'your_userkey_in_lewei50' ``` According to the actual serial port parameters, the first one is used in Linux, the second one in windows. Please choose one according to the actual situation. serial_port=0 stands for com1 in windows. ``` #serial_port="/dev/ttyUSB0" serial_port=0#serial_port = 8 means serial id 9 in windows ``` ## Connect the device and monitor the data The easiest way to demonstrate how Python is used as DTU is to use PC as hardware, and then connect the device and run the Python code directly. Possible errors include: 1. For Python version 3 or more,parentheses are needed for the command print. This code is for version 2.7, so find a 2.7 is a relatively simple method. 2. For the python module is not installed totally, there is no module for the code. You should install the corresponding modules by yourself. Under normal circumstances, you can first communicate with the Modbus RTU device through the serial port software. Look at the following picture. ![串口通信正常][2] Ensure that the communication function of the data serial port is normal, and then run python. The normal operation of python is shown in the screenshot as follows. ![openwrt 路由器][6] Then the communication can be done with the Modbus device in your account, as shown below. ![openwrt 路由器][7] OK, so a python DTU is finished. Is it hard to use the computer? Then let's run python with Openwrt and use it as DTU. ## Use openwrt as DTU If using python as DTU with PC is for a demonstration, running python with Openwrt as DTU definitely has more significances. First look at the appearance of the Openwrt router. On the right hand is a Xiaomi bracelet, used to compare the volume. ![openwrt 路由器][3] ### Why do you use the Openwrt router? Advantages of using OP (short name for openwrt router) 1. There is no need to customize your own hardware, and there are many sold online.Support Python, which is best for validating prototypes. 2. The router system supports multiple uplink modes, and one OP can be used as WiFi, Ethernet, 4G (need USB hub) DTU. ### Type to choose 1. 16M flash, with external USB interfaces; 2. Recommend 703 or GL-net series Recommend one on TB: [Remember to ask for GL-net dual ports and acrylic shell, FLASH 16M, OPpure system][4] ### Be familiar with OP & install Python Be familiar with OP: study by yourself Method for installing python: [OP install python][5] Note that different versions of OP have different installation commands, and if it is the latest version of CC, the commands should be as follows. ``` opkg update opkg install python-light ``` ### Install python pip Still for make python module installation easier. opkg install python-pip ### Install USB serial port bridge device driver Because the serial port needs USB bridging, and according to the bridges used (CP2102, 340, FTDI ) to select different drivers. Mine is 2102, so the the command for driver installation is: opkg install kmod-usb-serial-cp210x ### Download python code into Openwrt Find a scp client and download the python code, Then install modules, solve errors, and congratulations! ---------- The following content is optional. ### Add a daemon Daemon.sh to OP, or py will exit if there is an error while running. `echo "hello shell" ps |grep lewei|grep -v grep if [ $? -ne 0 ] then echo "process is not exist" python lewei_tcp2serial.py& else echo "process is on" fi` OP adds a timed task to perform a daemon every minute. chmod 777 Daemon.sh ./Daemon.sh Add a timed task. */1 * * * * ./Daemon.sh [1]: https://github.com/lewei50/DTU/blob/master/python/lewei_tcp2serial.py [2]: http://doc-resources.lewei50.com/lewei50/img/DTU-laoliu-20170509-1.jpg [3]: http://doc-resources.lewei50.com/lewei50/img/DTU-laoliu-20170509-2.jpg [4]: https://item.taobao.com/item.htm?spm=a1z09.2.0.0.azdWz8&id=45413712684&_u=91jno1fi6c83 [5]: https://wiki.openwrt.org/doc/software/python [6]: http://doc-resources.lewei50.com/lewei50/img/DTU-laoliu-20170509-3.jpg [7]: http://doc-resources.lewei50.com/lewei50/img/DTU-laoliu-20170509-4.jpg