In the previous tutorial,FTP protocol和file transfer over it between a Client and Server was discussed. The FTP protocol can really useful in many IoT applications. Many物联网devices are installed in places like nuclear plant, electrical grids and other industrial setups where these types of devices can get some bugs and need application software updates to fix them. On standard IoT protocols like MQTT, CoAP, etc, it is hard to update and reinstall application software because most of the IoT protocols are designed for IoT communication between devices and network but not for tasks like application updates. The application update on an IoT device is only possible overTCP/IPstack. The FTP protocol is the specifically designed protocol over TCP/IP stack for file transfer between a client and a server. So, it can be used by an IoT device to download updated application software and reinstall it once downloaded.
在此项目中,Raspberry Pi用万博app怎么更新作IoT设备,该设备正在运行简单的Python应用程序以闪烁LED。因此,该项目设计的IoT设备是与LED连接的Raspberry Pi。万博app怎么更新彼此之间的python应用程序还保持在女儿板上的运行,该应用程序不断寻找FTP服务器的LED闪烁代码的更新。更新寻求应用程序与FTP服务器连接,并跟踪FTP服务器上可用的LED闪烁代码的版本号。如果更改了LED闪烁代码的版本编号,则更新寻求代码下载更新的代码并将其重新安装在Raspberry Pi上。万博app怎么更新更新过程会自动进行,不需要任何人类干预。因此,从FTP上传和下载文件是可靠而快速的,许多应用程序在更新devers中使用FTP。
需要组件 -
1) Raspberry Pi 3
2)LED
3) 1K Ω Resistor
4)跳线
需要软件 -
1) Raspbian OS
2)LeafPad/gnu nano/python 3闲置(用于编写Python脚本)
Block Diagram –
Fig. 1: Block Diagram of Raspberry Pi 3 based IoT FTP Client
Circuit Connections –
在此项目中设计了一个简单的IoT设备。它的LED与Raspberry Pi 3相连。该设备是通过将以下组件万博app怎么更新组装在一起设计的。
万博app怎么更新覆盆子Pi 3 -Raspberry Pi 3 is the third generation Raspberry Pi. It is a miniature marvel, packing considerable computing power into a footprint no larger than a credit card. The processor at the heart of the Raspberry Pi system is a Broadcom BCM2837 system-on-chip (SoC) which houses a 1.2 GHz Quad Core ARM Cortex-A53 processor. The vast majority of the system’s components, including its central and graphics processing units along with the audio and communications hardware, are built onto that single component along with 1 GB LPDDR2 memory chip at the centre of the board. It is not just this SoC design that makes the BCM2837 different to the processor found in a typical desktop or laptop, however, it also uses a different instruction set architecture (ISA), known as ARM.
The Pi comes equipped with on-board 10/100 BaseT Ethernet Socket, HDMI and Composite RCA port for video, 3.5 mm audio output jack, 15-pin MIPI Camera Serial Interface (CSI-2), Display Serial Interface, Bluetooth 4.1, 802.11 b/g/n Wireless LAN, Micro SDIO for Micro SD Card, 4 USB 2.0 Connectors, 40 pin header containing 27 GPIO pins and Micro USB socket for power supply.
The Raspberry Pi is a single board computer and is designed to run an operating system called GNU/Linux Raspbian. Hereafter referred to simply as Linux. Unlike Windows or OS X, Linux is open source, so it is possible to download the source code for the entire operating system and make whatever changes desired. The Raspberry Pi 3 can also run Windows 10 IoT and many other embedded operating systems most of which are Linux derivatives. The operating system should be loaded in a MicroSD card and boot from it. With powerful computing resources, large number of multimedia interfaces and GPIO pins, Raspberry Pi 3 is a suitable choice to run a software oriented complex IoT or Embedded project that requires sufficient computing power as well as large scale sensor connectivity. With on-board Bluetooth and Wi-Fi, this 3rd generation Pi can be easily deployed in an IoT network. The key specifications of the Raspberry Pi 3 are summarized in the following table –

图2:Raspberry Pi的表列表的表列表3万博app怎么更新
The 40-pin header on Raspberry Pi 3 has the following pin configuration –

图3:Raspberry Pi的40针标头的表列表销型配置万博app怎么更新

图4:Raspberry Pi的40针标头的表列表销型配置万博app怎么更新
在这个项目中,LED连接到Raspberry Pi的GPIO18针。万博app怎么更新董事会由USB适配器提供动力。
引领 -An LED is connected at GPIO18 of the Raspberry Pi. Its anode is connected to Raspberry Pi pin and cathode is cathode is connected to ground. The LED is connected to Pi via a 1K ohm pull-up resistor in series.
电源 -电源连接到Raspberry Pi。万博app怎么更新PI应由2.5 A电流输出的5V适配器供电。适配器可以在Micro USB插座上连接。
How the circuit works –
The IoT device designed here is simply a Raspberry Pi 3 controlling an LED. For controlling the LED and keeping the Pi connected to a FTP server, two separate python scripts need to be run on the daughter board. One python script controls the LED blinking and another python script manages to automatically update the LED blinking script. First of all, the Raspberry Pi needs to loaded with an operating system. Here the official operating system for the Raspberry Pi – Linux Raspbian is installed on it. While installing the operating system, the Raspberry Pi should be connected to a display monitor using HDMI cable and a keyboard and mouse through USB ports.
For installing the Raspbian Operating System on MicroSD card, first download the latest image of Raspbian OS from Raspberry Pi website from the following link –
Copy the image of the latest Raspbian OS in the MicroSD card. If the MicroSD card used is 32 GB or below, it must be formatted to FAT32 (file system) before copying the image or if the MicroSD card is more than 32 GB, it should be formatted to exFAT before copying the image. Extract the OS Zip and copy it to the MicroSD card. The image can be written to the card by connecting the card to a laptop or PC using a MicroSD card reader. After copying the extracted image, insert the card in the MicroSD slot as shown below –

图5:覆盆子PI的典型图像3 microSD卡插槽万博app怎么更新
使用HDMI电缆,键盘和万博app怎么更新鼠标将Raspberry Pi与显示监视器连接。通过连接到电源适配器来在板上电源。板上的红色LED将开始眨眼,操作系统将开始从microSD卡启动。引导过程将显示在显示器上,一旦启动完成,绿色LED将在Raspberry Pi上点亮。万博app怎么更新在Raspberry Pi上成功安装Raspbian OS后,建议执行软件更新。万博app怎么更新可以通过在Linux终端中运行以下Linux命令来完成。
$ sudo apt-get更新
$ sudo apt-get升级
安装Raspbian后,是时候在Raspbian上编写和运行Python脚本了。Python脚本可以使用LeafPad或Gnu Nano等文本编辑器在Raspbian上编写。Python脚本也可以使用默认的Python IDE编写,例如python 2闲置或python 3空闲。通过菜单 - >编程 - > python 3空闲,打开python 3闲置。一个称为Python 3.4.2外壳的窗口将打开。编写Python脚本并将其保存到目录中。
The python script written for this project should run at the startup as the Pi 3 is powered on. The script runs an infinite loop so it never ends. There are some methods by which the Raspberry Pi can be configured to run a python script on start up. Any of the following methods can be used –
1) Editing rc.local –
The commands can be added to the file /etc/rc.local to run a program or command when the raspberry Pi boots up. This is especially useful if the Pi has to plug in to power headless, and have it run a program without configuration or a manual start. The file should be edited with root by running the following commands in the Linux Terminal –
sudo nano /etc/rc.local
现在,添加命令使用完整的文件路径执行Python脚本,并在命令的末尾添加一个and和anmpersand,以便脚本在单独的过程中运行并可以继续进行启动。应在将Python脚本保存为ftp.py和code.py-的情况下添加以下命令。
sudo python /home/pi/FTP.py &
exit 0
应在RC.LOCAL文件中的线退出0之前添加命令。
2)编辑.bashrc -
The .bashrc is a hidden file in the home folder that contains user configuration options. Open the .bshrc file by running the following commands in the Linux terminal –
sudo nano/home/pi/.bashrc
Add the following lines after the last line in the file –
回声在启动时运行
sudo python /home/pi/FTP.py
3) Adding script to init.d directory –
The init.d directory contains the scripts which are started during the boot process (in addition, all programs here are executed when Pi is shutdown or rebooted). Add the script to be run at startup to the init.d directory using the following commands –
sudo cp /home/pi/securitysystem.py /etc/init.d/
Move to the init directory and open the python script by running the following commands –
cd /etc/init.d
sudo nano FTP.py
将以下行添加到Python脚本中,以使其成为Linux标准基础(LSB) -
# /etc/init.d/sample.py
###BEGIN INIT INFO
# Provides: sample.py
#必需启动:$ exement_fs $ syslog
#必需停留:$ remote_fs $ syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
#简短描述:启动时开始守护程序
# Description: Enable service provided by daemon.
###结束初始信息
Make the python script in the init directory executable by changing its permission by running the following command –
sudo chmod +x FTP.py
Then run the following command –
sudo update-rc.d ftp.py默认
Next reboot the Pi by running the following command –
sudo重启
Any of the above methods can be used to make the python script run on startup. Now the Pi 3 can be disconnected with the display monitor, keyboard and mouse. Interface the LED with the Raspberry Pi and power it up.
Now when the Pi will power up, it will run both the scripts on start up. The main script (CODE.py) controls the blinking of LED. It let the LED blink over a period of few seconds. The other script (FTP.py) keeps checking for file on FTP server for version of the main file. The file checked by the script and containing version number is named as ‘Readme.txt’ and simply contains version number of the LED Blinking script as plain text. If update script (FTP.py) finds any changes in the version indication file (Readme.txt) on FTP server, which means the application file is changed, (in which case the period of blinking led could have been changed in CODE.py uploaded to FTP server), it indicates that new version of the main file is available. Now the updated version of the main file is downloaded from FTP server. After downloading, the main script is restarted so the changes can take effect and thus the led blink duration is then changed.
The FTP.py runs on the startup and checks for the application code (CODE.py) from the FTP server. The FTP.py itself manages to download, install and run the main code i.e. CODE.py.
Programming Guide –
There are two python scripts written which run on the startup. One of the python script simply blinks LED connected to the Raspberry Pi and it is saved as CODE.py. This script begins with importing required libraries – RPi.GPIO library for handing digital input and output and time library for time delays. RPi.GPIO is a module to control Raspberry Pi GPIO channels. There may be more than one script/circuit on the GPIO of the Raspberry Pi. As a result of this, if RPi.GPIO detects that a pin has been configured to something other than the default (input), it may give a warning when the Pi tries to configure the script. To disable these warnings, false argument is passed to setwarning() method. A variable is initialized which is set to 24. The GPIO numbering system is set to BCM which refers to the channel numbers on Broadcom SoC. The BCM channel number 24 is configured as GPIO output using the setup() method and it is set to LOW using output() method. An infinite loop is run in which the GPIO output is set to HIGH and LOW alternatively using output() method and time delay is provided using sleep() method of time module which accepts delay interval as parameter in milliseconds.
第二个文件(ftp.py)是更新脚本。应用程序文件可以是任何脚本文件,在这种情况下为code.py。更新脚本分为四个部分 -
1. Setting FTP server
2. Connecting to FTP server
3.下载版本文件并比较
4. Downloading the Application File and restarting the Application
1.Setting Server:The FTP server must contain two files, readme.txt (Which contains the version information) and code.py (Containing Main application code file). Any open FTP server like www.biz.nf can be used for web hosting. Create an account there and just upload those two files.
2.Connecting to FTP server:To connect to FTP server a python library that supports FTP commands “ftplib” is imported. It should be imported into top of the update script.
导入FTPLIB作为FTP
To connect to ftp server, the connection is started on a session variable. The server address and its username and password are provided to the variable.
session = ftp.ftp('192.168.1.100',‘iot_user’,’123123123')
3.下载版本文件并比较:The readme file on server contains information about the current version of the Application code. Also in the update script, current version is defined with a variable.
c_version =“ 1”
The readme file from FTP server is downloaded and stored into a variable “version”. The file is opened and read to detect version number.
with open(‘README.txt’, ‘r’) as myfile:
版本= myfile.read()。替换('n',”,)
存储后,将比较版本变量,如果发现更改,则会启动来自FTP服务器的应用程序代码的下载过程。
如果(版本!= c_version):
全局C_version
c_version =版本
4.Downloading the Application File and restarting the Application:After the comparison between version and detection of version change, the download of updated Code.py from FTP server is initiated.
session.trbinary('trev code.py',open('code.py','wb')。写)
The file is retrieved and stored as the same name as the main Application file. First the running application is killed.
check_kill_process(“ code.py”)
Then, a separate thread is started to restart the application.
start ='sudo python code.py’
proc = subprocess.popen(start,shell = true,stdout = subprocess.pipe,stderr = subprocess.pipe,stdin = subprocess.pipe)
因此,这就是可以在IoT应用程序中使用FTP协议以进行自动应用程序更新。从“代码”部分查看Python脚本,然后尝试一下。在下一个教程中,了解SFTP协议。SFTP是FTP协议的安全变体。
项目源代码
### //程序以将ftplib导入为ftp导入时间导入OS,信号导入subprocess版本=“” c_version =“ 1” session = ftp.ftp('192.168.1.1.100','iot_user','iot_user','123123123')='sudo kill $(ps aux | grep code.py | awk'{{print $ 2}}')'start ='sudo python code.py'def Update():#ftp.cwd('debian') #session.retrlines('LIST') session.retrbinary('RETR README.txt', open('README.txt', 'wb').write) with open('README.txt', 'r') as myfile: version=myfile.read().replace('n', '') if(version != c_version): global c_version session.retrbinary('RETR CODE.py', open('CODE.py', 'wb').write) c_version = version time.sleep(1) check_kill_process("CODE.py") print "STOP pass" time.sleep(1) proc = subprocess.Popen(start, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) print "START Pass" time.sleep(3) print "UPDATED!" else: time.sleep(5) print "NO UPDATES" def check_kill_process(pstring): for line in os.popen("ps ax | grep " + pstring + " | grep -v grep"): fields = line.split() pid = fields[0] os.kill(int(pid), signal.SIGKILL) while True: try: update() except Exception as e: session.quit() print e###
Circuit Diagrams
提交以下:物联网,354manbetx