0.前言
webiopi是一个可控制树莓派GPIO的web框架,该框架面向物联网IoT开发。该项目托管于google,并可在sourceforge上下载到源代码。现在webiopi已经发展到0.7版本,webiopi支持REST Server,CoAP server,并提供python库和javascript库,毫无疑问的说webiopi是一个优秀的框架。从sourceforge上的信息来看,下载量前三位为美国,德国和英国,天朝的下载量排在了26位(2014年3月8日),所以我觉得应该写点什么普及一下webiopi,并顺便激励一下仍在玩“单片机”的中国嵌入式工程师们,往前走“一步”海阔天空也。
要熟练使用webiopi,先要对webiopi的配置有所了解,webiopi通过某一个config文件实现。
1.GPIO初始化
[GPIO]
# Initialize following GPIOs with given function and optional value
# This is used during WebIOPi start process
#21 = IN
#23 = OUT 0
#24 = OUT 0
#25 = OUT 1
【作用】
webiopi运行之后立即设置GPIO端口的状态,例如输入还是输出,若是输出的话输出高电平还是低电平
2.GPIO重置
[~GPIO]
# Reset following GPIOs with given function and optional value
# This is used at the end of WebIOPi stop process
#21 = IN
#23 = IN
#24 = IN
#25 = OUT 0
【作用】
webiopi结束运行之前,设置GPIO状态。该设置最好和GPIO初始化相对应,更确切的说恢复所有的GPIO为输入。
3.载入脚本
[SCRIPTS]
# Load custom scripts syntax :
# name = sourcefile
# each sourcefile may have setup, loop and destroy functions and macros
#myscript = /home/pi/webiopi/examples/scripts/macros/script.py
【作用】
script.py可理解为一个主文件,主文件中包含3大块内容,setup loop和destroy。webiopi运行之后的顺序依次是:setup运行一次,webiopi运行时loop连续运行,webiopi运行结束之前destroy运行一次。script.py文件的主要功能存在于loop中,在没有网页操作时,loop中的相关操作也会运行。
4.HTTP服务
# HTTP Server configuration
enabled = true
port = 8000
# File containing sha256(base64("user:password"))
# Use webiopi-passwd command to generate it
passwd-file = /etc/webiopi/passwd
# Change login prompt message
prompt = "WebIOPi"
# Use doc-root to change default HTML and resource files location
#doc-root = /home/pi/webiopi/examples/scripts/macros
# Use welcome-file to change the default "Welcome" file
#welcome-file = index.html
【作用】
1.使能或者禁止HTTP服务
2.设置HTTP服务的端口号,默认为8000,注意由于存在web各种web服务器(apache或者lighttpd),所有请勿使用80端口。
3.设置登录用户名和密码,若是新手建议不用修改。
4.设置html文件目录(请注意是目录而不是文件)。该设置经常会被修改。
5.CoAP服务
# CoAP Server configuration
enabled = true
port = 5683
# Enable CoAP multicast
multicast = true
【作用】
1.使能CoAP服务
2.CoAP的默认端口为5683,不建议修改。
6.载入设备
[DEVICES]
# Device configuration syntax:
# name = device [args...]
# name : used in the URL mapping
# device : device name
# args : (optional) see device driver doc
# If enabled, devices configured here are mapped on REST API /device/name
# Devices are also accessible in custom scripts using deviceInstance(name)
# See device driver doc for methods and URI scheme available
# Raspberry native UART on GPIO, uncomment to enable
# Don‘t forget to remove console on ttyAMA0 in /boot/cmdline.txt
# And also disable getty on ttyAMA0 in /etc/inittab
#serial0 = Serial device:ttyAMA0 baudrate:9600
# USB serial adapters
#usb0 = Serial device:ttyUSB0 baudrate:9600
#usb1 = Serial device:ttyACM0 baudrate:9600
#temp0 = TMP102
#temp1 = TMP102 slave:0x49
#temp2 = DS18B20
#temp3 = DS18B20 slave:28-0000049bc218
#bmp = BMP085
#gpio0 = PCF8574
#gpio1 = PCF8574 slave:0x21
#light0 = TSL2561T
#light1 = TSL2561T slave:0b0101001
#gpio0 = MCP23017
#gpio1 = MCP23017 slave:0x21
#gpio2 = MCP23017 slave:0x22
#pwm0 = PCA9685
#pwm1 = PCA9685 slave:0x41
#adc0 = MCP3008
#adc1 = MCP3008 chip:1 vref:5
#dac1 = MCP4922 chip:1
【作用】
载入各种设备,由于设备暂缺未详细测试。
7.REST设置
# By default, REST API allows to GET/POST on all GPIOs
# Use gpio-export to limit GPIO available through REST API
#gpio-export = 21, 23, 24, 25
# Uncomment to forbid changing GPIO values
#gpio-post-value = false
# Uncomment to forbid changing GPIO functions
#gpio-post-function = false
# Uncomment to disable automatic device mapping
#device-mapping = false
【作用】
默认的情况下,通过REST API可以控制所有的GPIO端口。这些危险操作会和loop中的功能冲突,为了防止该情况可通过配置限制REST的功能,例如设置GPIO的方法和输出电平。
8.URL重路由
# Custom REST API route syntax :
# source = destination
# source : URL to route
# destination : Resulting URL
# Adding routes allows to simplify access with Human comprehensive URLs
# In the next example with have the bedroom light connected to GPIO 25
# and a temperature sensor named temp2, defined in [DEVICES] section
# - GET /bedroom/light => GET /GPIO/25/value, returns the light state
# - POST /bedroom/light/0 => POST /GPIO/25/value/0, turn off the light
# - POST /bedroom/light/1 => POST /GPIO/25/value/1, turn on the light
# - GET /bedroom/temperature => GET /devices/temp2/temperature/c, returns the temperature in celsius
#/bedroom/light = /GPIO/25/value
#/bedroom/temperature = /devices/temp2/temperature/c
#/livingroom/light = /devices/expander0/0
#/livingroom/brightness = /devices/adc/0/float
#/livingroom/temperature = /devices/temp0/temperature/c
#/weather/temperature = /devices/bmp/temperature/c
#/weather/pressure = /devices/bmp/pressure/hpa
【作用】
使得REST API看起来更尤优美一些,暂时未详细测试。
树莓派学习笔记——webiopi配置文件说明,古老的榕树,5-wow.com